|
7 | 7 | * More information about the AMF format: http://amf.wikispaces.com |
8 | 8 | * |
9 | 9 | * Usage: |
10 | | - * var loader = new AMFLoader(); |
| 10 | + * const loader = new AMFLoader(); |
11 | 11 | * loader.load('/path/to/project.amf', function(objecttree) { |
12 | 12 | * scene.add(objecttree); |
13 | 13 | * }); |
|
18 | 18 | * |
19 | 19 | */ |
20 | 20 |
|
21 | | - var AMFLoader = function ( manager ) { |
| 21 | + class AMFLoader extends THREE.Loader { |
22 | 22 |
|
23 | | - THREE.Loader.call( this, manager ); |
| 23 | + constructor( manager ) { |
24 | 24 |
|
25 | | - }; |
| 25 | + super( manager ); |
26 | 26 |
|
27 | | - AMFLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), { |
28 | | - constructor: AMFLoader, |
29 | | - load: function ( url, onLoad, onProgress, onError ) { |
| 27 | + } |
| 28 | + |
| 29 | + load( url, onLoad, onProgress, onError ) { |
30 | 30 |
|
31 | | - var scope = this; |
32 | | - var loader = new THREE.FileLoader( scope.manager ); |
| 31 | + const scope = this; |
| 32 | + const loader = new THREE.FileLoader( scope.manager ); |
33 | 33 | loader.setPath( scope.path ); |
34 | 34 | loader.setResponseType( 'arraybuffer' ); |
35 | 35 | loader.setRequestHeader( scope.requestHeader ); |
|
58 | 58 |
|
59 | 59 | }, onProgress, onError ); |
60 | 60 |
|
61 | | - }, |
62 | | - parse: function ( data ) { |
| 61 | + } |
| 62 | + |
| 63 | + parse( data ) { |
63 | 64 |
|
64 | 65 | function loadDocument( data ) { |
65 | 66 |
|
66 | | - var view = new DataView( data ); |
67 | | - var magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) ); |
| 67 | + let view = new DataView( data ); |
| 68 | + const magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) ); |
68 | 69 |
|
69 | 70 | if ( magic === 'PK' ) { |
70 | 71 |
|
71 | | - var zip = null; |
72 | | - var file = null; |
| 72 | + let zip = null; |
| 73 | + let file = null; |
73 | 74 | console.log( 'THREE.AMFLoader: Loading Zip' ); |
74 | 75 |
|
75 | 76 | try { |
|
87 | 88 |
|
88 | 89 | } |
89 | 90 |
|
90 | | - for ( var file in zip ) { |
| 91 | + for ( file in zip ) { |
91 | 92 |
|
92 | 93 | if ( file.toLowerCase().substr( - 4 ) === '.amf' ) { |
93 | 94 |
|
|
102 | 103 |
|
103 | 104 | } |
104 | 105 |
|
105 | | - var fileText = THREE.LoaderUtils.decodeText( view ); |
106 | | - var xmlData = new DOMParser().parseFromString( fileText, 'application/xml' ); |
| 106 | + const fileText = THREE.LoaderUtils.decodeText( view ); |
| 107 | + const xmlData = new DOMParser().parseFromString( fileText, 'application/xml' ); |
107 | 108 |
|
108 | 109 | if ( xmlData.documentElement.nodeName.toLowerCase() !== 'amf' ) { |
109 | 110 |
|
|
118 | 119 |
|
119 | 120 | function loadDocumentScale( node ) { |
120 | 121 |
|
121 | | - var scale = 1.0; |
122 | | - var unit = 'millimeter'; |
| 122 | + let scale = 1.0; |
| 123 | + let unit = 'millimeter'; |
123 | 124 |
|
124 | 125 | if ( node.documentElement.attributes.unit !== undefined ) { |
125 | 126 |
|
126 | 127 | unit = node.documentElement.attributes.unit.value.toLowerCase(); |
127 | 128 |
|
128 | 129 | } |
129 | 130 |
|
130 | | - var scaleUnits = { |
| 131 | + const scaleUnits = { |
131 | 132 | millimeter: 1.0, |
132 | 133 | inch: 25.4, |
133 | 134 | feet: 304.8, |
|
148 | 149 |
|
149 | 150 | function loadMaterials( node ) { |
150 | 151 |
|
151 | | - var matName = 'AMF Material'; |
152 | | - var matId = node.attributes.id.textContent; |
153 | | - var color = { |
| 152 | + let matName = 'AMF Material'; |
| 153 | + const matId = node.attributes.id.textContent; |
| 154 | + let color = { |
154 | 155 | r: 1.0, |
155 | 156 | g: 1.0, |
156 | 157 | b: 1.0, |
157 | 158 | a: 1.0 |
158 | 159 | }; |
159 | | - var loadedMaterial = null; |
| 160 | + let loadedMaterial = null; |
160 | 161 |
|
161 | | - for ( var i = 0; i < node.childNodes.length; i ++ ) { |
| 162 | + for ( let i = 0; i < node.childNodes.length; i ++ ) { |
162 | 163 |
|
163 | | - var matChildEl = node.childNodes[ i ]; |
| 164 | + const matChildEl = node.childNodes[ i ]; |
164 | 165 |
|
165 | 166 | if ( matChildEl.nodeName === 'metadata' && matChildEl.attributes.type !== undefined ) { |
166 | 167 |
|
|
200 | 201 |
|
201 | 202 | function loadColor( node ) { |
202 | 203 |
|
203 | | - var color = { |
| 204 | + const color = { |
204 | 205 | r: 1.0, |
205 | 206 | g: 1.0, |
206 | 207 | b: 1.0, |
207 | 208 | a: 1.0 |
208 | 209 | }; |
209 | 210 |
|
210 | | - for ( var i = 0; i < node.childNodes.length; i ++ ) { |
| 211 | + for ( let i = 0; i < node.childNodes.length; i ++ ) { |
211 | 212 |
|
212 | | - var matColor = node.childNodes[ i ]; |
| 213 | + const matColor = node.childNodes[ i ]; |
213 | 214 |
|
214 | 215 | if ( matColor.nodeName === 'r' ) { |
215 | 216 |
|
|
237 | 238 |
|
238 | 239 | function loadMeshVolume( node ) { |
239 | 240 |
|
240 | | - var volume = { |
| 241 | + const volume = { |
241 | 242 | name: '', |
242 | 243 | triangles: [], |
243 | 244 | materialid: null |
244 | 245 | }; |
245 | | - var currVolumeNode = node.firstElementChild; |
| 246 | + let currVolumeNode = node.firstElementChild; |
246 | 247 |
|
247 | 248 | if ( node.attributes.materialid !== undefined ) { |
248 | 249 |
|
|
266 | 267 |
|
267 | 268 | } else if ( currVolumeNode.nodeName === 'triangle' ) { |
268 | 269 |
|
269 | | - var v1 = currVolumeNode.getElementsByTagName( 'v1' )[ 0 ].textContent; |
270 | | - var v2 = currVolumeNode.getElementsByTagName( 'v2' )[ 0 ].textContent; |
271 | | - var v3 = currVolumeNode.getElementsByTagName( 'v3' )[ 0 ].textContent; |
| 270 | + const v1 = currVolumeNode.getElementsByTagName( 'v1' )[ 0 ].textContent; |
| 271 | + const v2 = currVolumeNode.getElementsByTagName( 'v2' )[ 0 ].textContent; |
| 272 | + const v3 = currVolumeNode.getElementsByTagName( 'v3' )[ 0 ].textContent; |
272 | 273 | volume.triangles.push( v1, v2, v3 ); |
273 | 274 |
|
274 | 275 | } |
|
283 | 284 |
|
284 | 285 | function loadMeshVertices( node ) { |
285 | 286 |
|
286 | | - var vertArray = []; |
287 | | - var normalArray = []; |
288 | | - var currVerticesNode = node.firstElementChild; |
| 287 | + const vertArray = []; |
| 288 | + const normalArray = []; |
| 289 | + let currVerticesNode = node.firstElementChild; |
289 | 290 |
|
290 | 291 | while ( currVerticesNode ) { |
291 | 292 |
|
292 | 293 | if ( currVerticesNode.nodeName === 'vertex' ) { |
293 | 294 |
|
294 | | - var vNode = currVerticesNode.firstElementChild; |
| 295 | + let vNode = currVerticesNode.firstElementChild; |
295 | 296 |
|
296 | 297 | while ( vNode ) { |
297 | 298 |
|
298 | 299 | if ( vNode.nodeName === 'coordinates' ) { |
299 | 300 |
|
300 | | - var x = vNode.getElementsByTagName( 'x' )[ 0 ].textContent; |
301 | | - var y = vNode.getElementsByTagName( 'y' )[ 0 ].textContent; |
302 | | - var z = vNode.getElementsByTagName( 'z' )[ 0 ].textContent; |
| 301 | + const x = vNode.getElementsByTagName( 'x' )[ 0 ].textContent; |
| 302 | + const y = vNode.getElementsByTagName( 'y' )[ 0 ].textContent; |
| 303 | + const z = vNode.getElementsByTagName( 'z' )[ 0 ].textContent; |
303 | 304 | vertArray.push( x, y, z ); |
304 | 305 |
|
305 | 306 | } else if ( vNode.nodeName === 'normal' ) { |
306 | 307 |
|
307 | | - var nx = vNode.getElementsByTagName( 'nx' )[ 0 ].textContent; |
308 | | - var ny = vNode.getElementsByTagName( 'ny' )[ 0 ].textContent; |
309 | | - var nz = vNode.getElementsByTagName( 'nz' )[ 0 ].textContent; |
| 308 | + const nx = vNode.getElementsByTagName( 'nx' )[ 0 ].textContent; |
| 309 | + const ny = vNode.getElementsByTagName( 'ny' )[ 0 ].textContent; |
| 310 | + const nz = vNode.getElementsByTagName( 'nz' )[ 0 ].textContent; |
310 | 311 | normalArray.push( nx, ny, nz ); |
311 | 312 |
|
312 | 313 | } |
|
330 | 331 |
|
331 | 332 | function loadObject( node ) { |
332 | 333 |
|
333 | | - var objId = node.attributes.id.textContent; |
334 | | - var loadedObject = { |
| 334 | + const objId = node.attributes.id.textContent; |
| 335 | + const loadedObject = { |
335 | 336 | name: 'amfobject', |
336 | 337 | meshes: [] |
337 | 338 | }; |
338 | | - var currColor = null; |
339 | | - var currObjNode = node.firstElementChild; |
| 339 | + let currColor = null; |
| 340 | + let currObjNode = node.firstElementChild; |
340 | 341 |
|
341 | 342 | while ( currObjNode ) { |
342 | 343 |
|
|
358 | 359 |
|
359 | 360 | } else if ( currObjNode.nodeName === 'mesh' ) { |
360 | 361 |
|
361 | | - var currMeshNode = currObjNode.firstElementChild; |
362 | | - var mesh = { |
| 362 | + let currMeshNode = currObjNode.firstElementChild; |
| 363 | + const mesh = { |
363 | 364 | vertices: [], |
364 | 365 | normals: [], |
365 | 366 | volumes: [], |
|
370 | 371 |
|
371 | 372 | if ( currMeshNode.nodeName === 'vertices' ) { |
372 | 373 |
|
373 | | - var loadedVertices = loadMeshVertices( currMeshNode ); |
| 374 | + const loadedVertices = loadMeshVertices( currMeshNode ); |
374 | 375 | mesh.normals = mesh.normals.concat( loadedVertices.normals ); |
375 | 376 | mesh.vertices = mesh.vertices.concat( loadedVertices.vertices ); |
376 | 377 |
|
|
399 | 400 |
|
400 | 401 | } |
401 | 402 |
|
402 | | - var xmlData = loadDocument( data ); |
403 | | - var amfName = ''; |
404 | | - var amfAuthor = ''; |
405 | | - var amfScale = loadDocumentScale( xmlData ); |
406 | | - var amfMaterials = {}; |
407 | | - var amfObjects = {}; |
408 | | - var childNodes = xmlData.documentElement.childNodes; |
409 | | - var i, j; |
| 403 | + const xmlData = loadDocument( data ); |
| 404 | + let amfName = ''; |
| 405 | + let amfAuthor = ''; |
| 406 | + const amfScale = loadDocumentScale( xmlData ); |
| 407 | + const amfMaterials = {}; |
| 408 | + const amfObjects = {}; |
| 409 | + const childNodes = xmlData.documentElement.childNodes; |
| 410 | + let i, j; |
410 | 411 |
|
411 | 412 | for ( i = 0; i < childNodes.length; i ++ ) { |
412 | 413 |
|
413 | | - var child = childNodes[ i ]; |
| 414 | + const child = childNodes[ i ]; |
414 | 415 |
|
415 | 416 | if ( child.nodeName === 'metadata' ) { |
416 | 417 |
|
|
430 | 431 |
|
431 | 432 | } else if ( child.nodeName === 'material' ) { |
432 | 433 |
|
433 | | - var loadedMaterial = loadMaterials( child ); |
| 434 | + const loadedMaterial = loadMaterials( child ); |
434 | 435 | amfMaterials[ loadedMaterial.id ] = loadedMaterial.material; |
435 | 436 |
|
436 | 437 | } else if ( child.nodeName === 'object' ) { |
437 | 438 |
|
438 | | - var loadedObject = loadObject( child ); |
| 439 | + const loadedObject = loadObject( child ); |
439 | 440 | amfObjects[ loadedObject.id ] = loadedObject.obj; |
440 | 441 |
|
441 | 442 | } |
442 | 443 |
|
443 | 444 | } |
444 | 445 |
|
445 | | - var sceneObject = new THREE.Group(); |
446 | | - var defaultMaterial = new THREE.MeshPhongMaterial( { |
| 446 | + const sceneObject = new THREE.Group(); |
| 447 | + const defaultMaterial = new THREE.MeshPhongMaterial( { |
447 | 448 | color: 0xaaaaff, |
448 | 449 | flatShading: true |
449 | 450 | } ); |
450 | 451 | sceneObject.name = amfName; |
451 | 452 | sceneObject.userData.author = amfAuthor; |
452 | 453 | sceneObject.userData.loader = 'AMF'; |
453 | 454 |
|
454 | | - for ( var id in amfObjects ) { |
| 455 | + for ( const id in amfObjects ) { |
455 | 456 |
|
456 | | - var part = amfObjects[ id ]; |
457 | | - var meshes = part.meshes; |
458 | | - var newObject = new THREE.Group(); |
| 457 | + const part = amfObjects[ id ]; |
| 458 | + const meshes = part.meshes; |
| 459 | + const newObject = new THREE.Group(); |
459 | 460 | newObject.name = part.name || ''; |
460 | 461 |
|
461 | 462 | for ( i = 0; i < meshes.length; i ++ ) { |
462 | 463 |
|
463 | | - var objDefaultMaterial = defaultMaterial; |
464 | | - var mesh = meshes[ i ]; |
465 | | - var vertices = new THREE.Float32BufferAttribute( mesh.vertices, 3 ); |
466 | | - var normals = null; |
| 464 | + let objDefaultMaterial = defaultMaterial; |
| 465 | + const mesh = meshes[ i ]; |
| 466 | + const vertices = new THREE.Float32BufferAttribute( mesh.vertices, 3 ); |
| 467 | + let normals = null; |
467 | 468 |
|
468 | 469 | if ( mesh.normals.length ) { |
469 | 470 |
|
|
473 | 474 |
|
474 | 475 | if ( mesh.color ) { |
475 | 476 |
|
476 | | - var color = mesh.color; |
| 477 | + const color = mesh.color; |
477 | 478 | objDefaultMaterial = defaultMaterial.clone(); |
478 | 479 | objDefaultMaterial.color = new THREE.Color( color.r, color.g, color.b ); |
479 | 480 |
|
|
486 | 487 |
|
487 | 488 | } |
488 | 489 |
|
489 | | - var volumes = mesh.volumes; |
| 490 | + const volumes = mesh.volumes; |
490 | 491 |
|
491 | 492 | for ( j = 0; j < volumes.length; j ++ ) { |
492 | 493 |
|
493 | | - var volume = volumes[ j ]; |
494 | | - var newGeometry = new THREE.BufferGeometry(); |
495 | | - var material = objDefaultMaterial; |
| 494 | + const volume = volumes[ j ]; |
| 495 | + const newGeometry = new THREE.BufferGeometry(); |
| 496 | + let material = objDefaultMaterial; |
496 | 497 | newGeometry.setIndex( volume.triangles ); |
497 | 498 | newGeometry.setAttribute( 'position', vertices.clone() ); |
498 | 499 |
|
|
522 | 523 | return sceneObject; |
523 | 524 |
|
524 | 525 | } |
525 | | - } ); |
| 526 | + |
| 527 | + } |
526 | 528 |
|
527 | 529 | THREE.AMFLoader = AMFLoader; |
528 | 530 |
|
|
0 commit comments