-
Notifications
You must be signed in to change notification settings - Fork 1
/
PLYLoader.js
501 lines (296 loc) · 10.4 KB
/
PLYLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
* @author Wei Meng / http://about.me/menway
*
* Description: A THREE loader for PLY ASCII files (known as the Polygon
* File Format or the Stanford Triangle Format).
*
* Limitations: ASCII decoding assumes file is UTF-8.
*
* Usage:
* var loader = new THREE.PLYLoader();
* loader.load('./models/ply/ascii/dolphins.ply', function (geometry) {
*
* scene.add( new THREE.Mesh( geometry ) );
*
* } );
*
* If the PLY file uses non standard property names, they can be mapped while
* loading. For example, the following maps the properties
* “diffuse_(red|green|blue)” in the file to standard color names.
*
* loader.setPropertyNameMapping( {
* diffuse_red: 'red',
* diffuse_green: 'green',
* diffuse_blue: 'blue'
* } );
*
*/
THREE.PLYLoader = function ( manager ) {
this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
this.propertyNameMapping = {};
};
THREE.PLYLoader.prototype = {
constructor: THREE.PLYLoader,
load: function ( url, onLoad, onProgress, onError ) {
var scope = this;
var loader = new THREE.FileLoader( this.manager );
loader.setResponseType( 'arraybuffer' );
loader.load( url, function ( text ) {
onLoad( scope.parse( text ) );
}, onProgress, onError );
},
setPropertyNameMapping: function ( mapping ) {
this.propertyNameMapping = mapping;
},
parse: function ( data ) {
function isASCII( data ) {
var header = parseHeader( bin2str( data ) );
return header.format === 'ascii';
}
function bin2str( buf ) {
var array_buffer = new Uint8Array( buf );
var str = '';
for ( var i = 0; i < buf.byteLength; i ++ ) {
str += String.fromCharCode( array_buffer[ i ] ); // implicitly assumes little-endian
}
return str;
}
function parseHeader( data ) {
var patternHeader = /ply([\s\S]*)end_header\s/;
var headerText = '';
var headerLength = 0;
var result = patternHeader.exec( data );
if ( result !== null ) {
headerText = result [ 1 ];
headerLength = result[ 0 ].length;
}
var header = {
comments: [],
elements: [],
headerLength: headerLength
};
var lines = headerText.split( '\n' );
var currentElement;
var lineType, lineValues;
function make_ply_element_property( propertValues, propertyNameMapping ) {
var property = { type: propertValues[ 0 ] };
if ( property.type === 'list' ) {
property.name = propertValues[ 3 ];
property.countType = propertValues[ 1 ];
property.itemType = propertValues[ 2 ];
} else {
property.name = propertValues[ 1 ];
}
if ( property.name in propertyNameMapping ) {
property.name = propertyNameMapping[ property.name ];
}
return property;
}
for ( var i = 0; i < lines.length; i ++ ) {
var line = lines[ i ];
line = line.trim();
if ( line === '' ) continue;
lineValues = line.split( /\s+/ );
lineType = lineValues.shift();
line = lineValues.join( ' ' );
switch ( lineType ) {
case 'format':
header.format = lineValues[ 0 ];
header.version = lineValues[ 1 ];
break;
case 'comment':
header.comments.push( line );
break;
case 'element':
if ( currentElement !== undefined ) {
header.elements.push( currentElement );
}
currentElement = {};
currentElement.name = lineValues[ 0 ];
currentElement.count = parseInt( lineValues[ 1 ] );
currentElement.properties = [];
break;
case 'property':
currentElement.properties.push( make_ply_element_property( lineValues, scope.propertyNameMapping ) );
break;
default:
console.log( 'unhandled', lineType, lineValues );
}
}
if ( currentElement !== undefined ) {
header.elements.push( currentElement );
}
return header;
}
function parseASCIINumber( n, type ) {
switch ( type ) {
case 'char': case 'uchar': case 'short': case 'ushort': case 'int': case 'uint':
case 'int8': case 'uint8': case 'int16': case 'uint16': case 'int32': case 'uint32':
return parseInt( n );
case 'float': case 'double': case 'float32': case 'float64':
return parseFloat( n );
}
}
function parseASCIIElement( properties, line ) {
var values = line.split( /\s+/ );
var element = {};
for ( var i = 0; i < properties.length; i ++ ) {
if ( properties[ i ].type === 'list' ) {
var list = [];
var n = parseASCIINumber( values.shift(), properties[ i ].countType );
for ( var j = 0; j < n; j ++ ) {
list.push( parseASCIINumber( values.shift(), properties[ i ].itemType ) );
}
element[ properties[ i ].name ] = list;
} else {
element[ properties[ i ].name ] = parseASCIINumber( values.shift(), properties[ i ].type );
}
}
return element;
}
function parseASCII( data ) {
// PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
var buffer = {
indices : [],
vertices : [],
normals : [],
uvs : [],
colors : []
};
var result;
var header = parseHeader( data );
var patternBody = /end_header\s([\s\S]*)$/;
var body = '';
if ( ( result = patternBody.exec( data ) ) !== null ) {
body = result [ 1 ];
}
var lines = body.split( '\n' );
var currentElement = 0;
var currentElementCount = 0;
for ( var i = 0; i < lines.length; i ++ ) {
var line = lines[ i ];
line = line.trim();
if ( line === '' ) {
continue;
}
if ( currentElementCount >= header.elements[ currentElement ].count ) {
currentElement ++;
currentElementCount = 0;
}
var element = parseASCIIElement( header.elements[ currentElement ].properties, line );
handleElement( buffer, header.elements[ currentElement ].name, element );
currentElementCount ++;
}
return postProcess( buffer );
}
function postProcess( buffer ) {
var geometry = new THREE.BufferGeometry();
// mandatory buffer data
if ( buffer.indices.length > 0 ) {
geometry.setIndex( buffer.indices );
}
geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( buffer.vertices, 3 ) );
// optional buffer data
if ( buffer.normals.length > 0 ) {
geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( buffer.normals, 3 ) );
}
if ( buffer.uvs.length > 0 ) {
geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( buffer.uvs, 2 ) );
}
if ( buffer.colors.length > 0 ) {
geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( buffer.colors, 3 ) );
}
geometry.computeBoundingSphere();
return geometry;
}
function handleElement( buffer, elementName, element ) {
if ( elementName === 'vertex' ) {
buffer.vertices.push( element.x, element.y, element.z );
if ( 'nx' in element && 'ny' in element && 'nz' in element ) {
buffer.normals.push( element.nx, element.ny, element.nz );
}
if ( 's' in element && 't' in element ) {
buffer.uvs.push( element.s, element.t );
}
if ( 'red' in element && 'green' in element && 'blue' in element ) {
buffer.colors.push( element.red / 255.0, element.green / 255.0, element.blue / 255.0 );
}
} else if ( elementName === 'face' ) {
var vertex_indices = element.vertex_indices || element.vertex_index; // issue #9338
if ( vertex_indices.length === 3 ) {
buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 2 ] );
} else if ( vertex_indices.length === 4 ) {
buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 3 ] );
buffer.indices.push( vertex_indices[ 1 ], vertex_indices[ 2 ], vertex_indices[ 3 ] );
}
}
}
function binaryRead( dataview, at, type, little_endian ) {
switch ( type ) {
// corespondences for non-specific length types here match rply:
case 'int8': case 'char': return [ dataview.getInt8( at ), 1 ];
case 'uint8': case 'uchar': return [ dataview.getUint8( at ), 1 ];
case 'int16': case 'short': return [ dataview.getInt16( at, little_endian ), 2 ];
case 'uint16': case 'ushort': return [ dataview.getUint16( at, little_endian ), 2 ];
case 'int32': case 'int': return [ dataview.getInt32( at, little_endian ), 4 ];
case 'uint32': case 'uint': return [ dataview.getUint32( at, little_endian ), 4 ];
case 'float32': case 'float': return [ dataview.getFloat32( at, little_endian ), 4 ];
case 'float64': case 'double': return [ dataview.getFloat64( at, little_endian ), 8 ];
}
}
function binaryReadElement( dataview, at, properties, little_endian ) {
var element = {};
var result, read = 0;
for ( var i = 0; i < properties.length; i ++ ) {
if ( properties[ i ].type === 'list' ) {
var list = [];
result = binaryRead( dataview, at + read, properties[ i ].countType, little_endian );
var n = result[ 0 ];
read += result[ 1 ];
for ( var j = 0; j < n; j ++ ) {
result = binaryRead( dataview, at + read, properties[ i ].itemType, little_endian );
list.push( result[ 0 ] );
read += result[ 1 ];
}
element[ properties[ i ].name ] = list;
} else {
result = binaryRead( dataview, at + read, properties[ i ].type, little_endian );
element[ properties[ i ].name ] = result[ 0 ];
read += result[ 1 ];
}
}
return [ element, read ];
}
function parseBinary( data ) {
var buffer = {
indices : [],
vertices : [],
normals : [],
uvs : [],
colors : []
};
var header = parseHeader( bin2str( data ) );
var little_endian = ( header.format === 'binary_little_endian' );
var body = new DataView( data, header.headerLength );
var result, loc = 0;
for ( var currentElement = 0; currentElement < header.elements.length; currentElement ++ ) {
for ( var currentElementCount = 0; currentElementCount < header.elements[ currentElement ].count; currentElementCount ++ ) {
result = binaryReadElement( body, loc, header.elements[ currentElement ].properties, little_endian );
loc += result[ 1 ];
var element = result[ 0 ];
handleElement( buffer, header.elements[ currentElement ].name, element );
}
}
return postProcess( buffer );
}
//
var geometry;
var scope = this;
if ( data instanceof ArrayBuffer ) {
geometry = isASCII( data ) ? parseASCII( bin2str( data ) ) : parseBinary( data );
} else {
geometry = parseASCII( data );
}
return geometry;
}
};