js/STLLoader.js

Thu, 06 Apr 2017 19:00:07 +0200

author
mdd
date
Thu, 06 Apr 2017 19:00:07 +0200
changeset 22
08f0118587d7
parent 16
800e8da193a7
permissions
-rw-r--r--

ht is not PVC....

16
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
1 /**
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
2 * @author aleeper / http://adamleeper.com/
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
3 * @author mrdoob / http://mrdoob.com/
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
4 * @author gero3 / https://github.com/gero3
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
5 * @author Mugen87 / https://github.com/Mugen87
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
6 *
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
7 * Description: A THREE loader for STL ASCII files, as created by Solidworks and other CAD programs.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
8 *
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
9 * Supports both binary and ASCII encoded files, with automatic detection of type.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
10 *
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
11 * The loader returns a non-indexed buffer geometry.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
12 *
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
13 * Limitations:
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
14 * Binary decoding supports "Magics" color format (http://en.wikipedia.org/wiki/STL_(file_format)#Color_in_binary_STL).
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
15 * There is perhaps some question as to how valid it is to always assume little-endian-ness.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
16 * ASCII decoding assumes file is UTF-8.
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
17 *
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
18 * Usage:
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
19 * var loader = new THREE.STLLoader();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
20 * loader.load( './models/stl/slotted_disk.stl', function ( geometry ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
21 * scene.add( new THREE.Mesh( geometry ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
22 * });
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
23 *
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
24 * For binary STLs geometry might contain colors for vertices. To use it:
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
25 * // use the same code to load STL as above
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
26 * if (geometry.hasColors) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
27 * material = new THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: THREE.VertexColors });
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
28 * } else { .... }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
29 * var mesh = new THREE.Mesh( geometry, material );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
30 */
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
31
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
32
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
33 THREE.STLLoader = function ( manager ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
34
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
35 this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
36
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
37 };
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
38
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
39 THREE.STLLoader.prototype = {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
40
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
41 constructor: THREE.STLLoader,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
42
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
43 load: function ( url, onLoad, onProgress, onError ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
44
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
45 var scope = this;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
46
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
47 var loader = new THREE.FileLoader( scope.manager );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
48 loader.setResponseType( 'arraybuffer' );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
49 loader.load( url, function ( text ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
50
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
51 onLoad( scope.parse( text ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
52
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
53 }, onProgress, onError );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
54
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
55 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
56
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
57 parse: function ( data ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
58
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
59 var isBinary = function () {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
60
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
61 var expect, face_size, n_faces, reader;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
62 reader = new DataView( binData );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
63 face_size = ( 32 / 8 * 3 ) + ( ( 32 / 8 * 3 ) * 3 ) + ( 16 / 8 );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
64 n_faces = reader.getUint32( 80, true );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
65 expect = 80 + ( 32 / 8 ) + ( n_faces * face_size );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
66
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
67 if ( expect === reader.byteLength ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
68
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
69 return true;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
70
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
71 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
72
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
73 // some binary files will have different size from expected,
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
74 // checking characters higher than ASCII to confirm is binary
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
75 var fileLength = reader.byteLength;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
76 for ( var index = 0; index < fileLength; index ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
77
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
78 if ( reader.getUint8( index, false ) > 127 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
79
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
80 return true;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
81
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
82 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
83
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
84 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
85
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
86 return false;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
87
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
88 };
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
89
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
90 var binData = this.ensureBinary( data );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
91
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
92 return isBinary() ? this.parseBinary( binData ) : this.parseASCII( this.ensureString( data ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
93
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
94 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
95
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
96 parseBinary: function ( data ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
97
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
98 var reader = new DataView( data );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
99 var faces = reader.getUint32( 80, true );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
100
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
101 var r, g, b, hasColors = false, colors;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
102 var defaultR, defaultG, defaultB, alpha;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
103
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
104 // process STL header
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
105 // check for default color in header ("COLOR=rgba" sequence).
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
106
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
107 for ( var index = 0; index < 80 - 10; index ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
108
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
109 if ( ( reader.getUint32( index, false ) == 0x434F4C4F /*COLO*/ ) &&
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
110 ( reader.getUint8( index + 4 ) == 0x52 /*'R'*/ ) &&
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
111 ( reader.getUint8( index + 5 ) == 0x3D /*'='*/ ) ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
112
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
113 hasColors = true;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
114 colors = [];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
115
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
116 defaultR = reader.getUint8( index + 6 ) / 255;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
117 defaultG = reader.getUint8( index + 7 ) / 255;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
118 defaultB = reader.getUint8( index + 8 ) / 255;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
119 alpha = reader.getUint8( index + 9 ) / 255;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
120
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
121 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
122
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
123 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
124
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
125 var dataOffset = 84;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
126 var faceLength = 12 * 4 + 2;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
127
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
128 var geometry = new THREE.BufferGeometry();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
129
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
130 var vertices = [];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
131 var normals = [];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
132
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
133 for ( var face = 0; face < faces; face ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
134
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
135 var start = dataOffset + face * faceLength;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
136 var normalX = reader.getFloat32( start, true );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
137 var normalY = reader.getFloat32( start + 4, true );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
138 var normalZ = reader.getFloat32( start + 8, true );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
139
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
140 if ( hasColors ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
141
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
142 var packedColor = reader.getUint16( start + 48, true );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
143
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
144 if ( ( packedColor & 0x8000 ) === 0 ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
145
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
146 // facet has its own unique color
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
147
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
148 r = ( packedColor & 0x1F ) / 31;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
149 g = ( ( packedColor >> 5 ) & 0x1F ) / 31;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
150 b = ( ( packedColor >> 10 ) & 0x1F ) / 31;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
151
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
152 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
153
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
154 r = defaultR;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
155 g = defaultG;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
156 b = defaultB;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
157
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
158 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
159
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
160 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
161
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
162 for ( var i = 1; i <= 3; i ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
163
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
164 var vertexstart = start + i * 12;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
165
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
166 vertices.push( reader.getFloat32( vertexstart, true ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
167 vertices.push( reader.getFloat32( vertexstart + 4, true ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
168 vertices.push( reader.getFloat32( vertexstart + 8, true ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
169
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
170 normals.push( normalX, normalY, normalZ );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
171
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
172 if ( hasColors ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
173
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
174 colors.push( r, g, b );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
175
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
176 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
177
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
178 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
179
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
180 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
181
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
182 geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
183 geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
184
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
185 if ( hasColors ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
186
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
187 geometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
188 geometry.hasColors = true;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
189 geometry.alpha = alpha;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
190
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
191 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
192
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
193 return geometry;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
194
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
195 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
196
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
197 parseASCII: function ( data ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
198
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
199 var geometry, length, patternFace, patternNormal, patternVertex, result, text;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
200 geometry = new THREE.BufferGeometry();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
201 patternFace = /facet([\s\S]*?)endfacet/g;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
202
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
203 var vertices = [];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
204 var normals = [];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
205
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
206 var normal = new THREE.Vector3();
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
207
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
208 while ( ( result = patternFace.exec( data ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
209
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
210 text = result[ 0 ];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
211 patternNormal = /normal[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
212
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
213 while ( ( result = patternNormal.exec( text ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
214
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
215 normal.x = parseFloat( result[ 1 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
216 normal.y = parseFloat( result[ 3 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
217 normal.z = parseFloat( result[ 5 ] );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
218
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
219 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
220
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
221 patternVertex = /vertex[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
222
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
223 while ( ( result = patternVertex.exec( text ) ) !== null ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
224
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
225 vertices.push( parseFloat( result[ 1 ] ), parseFloat( result[ 3 ] ), parseFloat( result[ 5 ] ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
226 normals.push( normal.x, normal.y, normal.z );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
227
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
228 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
229
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
230 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
231
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
232 geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
233 geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
234
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
235 return geometry;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
236
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
237 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
238
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
239 ensureString: function ( buf ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
240
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
241 if ( typeof buf !== "string" ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
242
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
243 var array_buffer = new Uint8Array( buf );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
244 var strArray = [];
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
245 for ( var i = 0; i < buf.byteLength; i ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
246
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
247 strArray.push(String.fromCharCode( array_buffer[ i ] )); // implicitly assumes little-endian
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
248
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
249 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
250 return strArray.join('');
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
251
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
252 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
253
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
254 return buf;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
255
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
256 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
257
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
258 },
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
259
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
260 ensureBinary: function ( buf ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
261
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
262 if ( typeof buf === "string" ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
263
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
264 var array_buffer = new Uint8Array( buf.length );
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
265 for ( var i = 0; i < buf.length; i ++ ) {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
266
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
267 array_buffer[ i ] = buf.charCodeAt( i ) & 0xff; // implicitly assumes little-endian
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
268
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
269 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
270 return array_buffer.buffer || array_buffer;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
271
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
272 } else {
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
273
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
274 return buf;
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
275
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
276 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
277
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
278 }
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
279
800e8da193a7 added dependencies to local repo
mdd
parents:
diff changeset
280 };

mercurial