@@ -86,6 +86,13 @@ THREE.BOMLoader.prototype = {
8686
8787 } ,
8888
89+ AssetDataAttribute = {
90+
91+ NONE : 1 << 0 ,
92+ NAME : 1 << 1
93+
94+ } ,
95+
8996 GroupDataAttribute = {
9097
9198 NONE : 1 << 0 ,
@@ -99,8 +106,7 @@ THREE.BOMLoader.prototype = {
99106 ObjectDataAttribute = {
100107
101108 NONE : 1 << 0 ,
102- NAME : 1 << 1 ,
103- GEOMETRY : 1 << 2
109+ GEOMETRY : 1 << 1
104110
105111 } ,
106112
@@ -146,7 +152,7 @@ THREE.BOMLoader.prototype = {
146152
147153 } ;
148154
149- var view = new DataView ( buffer ) , pos = 0 , isArrayContainer = ( this . responseType === 'array' ) , containers = ( isArrayContainer ? [ ] : new THREE . Group ( ) ) ;
155+ var view = new DataView ( buffer ) , pos = 0 , isAssetArray = ( this . responseType === 'array' ) , assets = ( isAssetArray ? [ ] : new THREE . Group ( ) ) ;
150156
151157 function readUint8 ( ) {
152158
@@ -499,168 +505,177 @@ THREE.BOMLoader.prototype = {
499505
500506 }
501507
502- // Object Count
503- var objectCount = readUint16 ( ) ;
504- if ( this . debug ) console . log ( 'Object Count' , objectCount ) ;
508+ // Asset Count
509+ var assetCount = readUint16 ( ) ;
510+ if ( this . debug ) console . log ( 'Asset Count' , assetCount ) ;
505511
506- for ( var i = 0 ; i < objectCount ; ++ i ) {
512+ for ( var a = 0 ; a < assetCount ; ++ a ) {
507513
508- // Object Data Attributes
509- var objectAttributes = readUint16 ( ) ;
514+ // Asset Data Attributes
515+ var assetAttributes = readUint16 ( ) ;
510516
511517 if ( this . debug ) {
512518
513519 console . log (
514520
515- 'ObjectAttributes:' , '\n' ,
516- 'Name' , ( objectAttributes & ObjectDataAttribute . NAME ) ? true : false , '\n' ,
517- 'Geometry' , ( objectAttributes & ObjectDataAttribute . GEOMETRY ) ? true : false
521+ 'AssetAttributes:' , '\n' ,
522+ 'Name' , ( assetAttributes & AssetDataAttribute . NAME ) ? true : false
518523
519524 ) ;
520525
521526 }
522527
523- var object = new THREE . Group ( ) ;
528+ var asset = assets ;
529+ if ( isAssetArray ) {
524530
525- // Container ID
526- var containerId = readUint32 ( ) ;
527- if ( isArrayContainer ) {
531+ asset = new THREE . Group ( ) ;
532+ assets . push ( asset ) ;
528533
529- if ( containerId >= containers . length ) containers . push ( new THREE . Group ( ) ) ;
530- containers [ containerId ] . add ( object ) ;
534+ }
531535
532- } else {
536+ // Asset Name
537+ if ( assetAttributes & AssetDataAttribute . NAME ) {
533538
534- if ( containerId >= containers . children . length ) containers . add ( new THREE . Group ( ) ) ;
535- containers . children [ containerId ] . add ( object ) ;
539+ var assetName = readString ( readUint16 ( ) ) ;
540+ if ( this . debug ) console . log ( 'Asset Name' , assetName ) ;
541+ if ( ! isAssetArray ) asset . name = assetName ;
536542
537543 }
538544
539- // Object Name
540- if ( objectAttributes & ObjectDataAttribute . NAME ) {
545+ // Object Count
546+ var objectCount = readUint16 ( ) ;
547+ if ( this . debug ) console . log ( 'Object Count' , objectCount ) ;
541548
542- var objectName = readString ( readUint16 ( ) ) ;
543- if ( this . debug ) console . log ( 'Object Name' , objectName ) ;
544- object . name = objectName ;
549+ for ( var i = 0 ; i < objectCount ; ++ i ) {
545550
546- }
547-
548- var vertices = { } ;
549- if ( objectAttributes & ObjectDataAttribute . GEOMETRY ) {
551+ // Object Data Attributes
552+ var objectAttributes = readUint16 ( ) ;
550553
551- // Geometry Data Attributes
552- var geometryAttributes = readUint16 ( ) ;
553554 if ( this . debug ) {
554555
555556 console . log (
556557
557- 'GeometryAttributes:' , '\n' ,
558- 'Normal' , ( geometryAttributes & GeometryDataAttribute . NORMAL ) ? true : false , '\n' ,
559- 'UV' , ( geometryAttributes & GeometryDataAttribute . UV ) ? true : false
558+ 'ObjectAttributes:' , '\n' ,
559+ 'Geometry' , ( objectAttributes & ObjectDataAttribute . GEOMETRY ) ? true : false
560560
561561 ) ;
562562
563563 }
564564
565- // Vertex Count
566- var vertexCount = readUint32 ( ) ;
567- if ( this . debug ) console . log ( 'Vertex Count' , vertexCount ) ;
568-
569- // Vertex Positions
570- vertices . positions = readFloat32Array ( vertexCount * 3 ) ;
565+ var vertices = { } ;
566+ if ( objectAttributes & ObjectDataAttribute . GEOMETRY ) {
571567
572- // Vertex Normals
573- if ( geometryAttributes & GeometryDataAttribute . NORMAL ) vertices . normals = readFloat32Array ( vertexCount * 3 ) ;
568+ // Geometry Data Attributes
569+ var geometryAttributes = readUint16 ( ) ;
570+ if ( this . debug ) {
574571
575- // Vertex UVs
576- if ( geometryAttributes & GeometryDataAttribute . UV ) vertices . uvs = readFloat32Array ( vertexCount * 2 ) ;
572+ console . log (
577573
578- }
574+ 'GeometryAttributes:' , '\n' ,
575+ 'Normal' , ( geometryAttributes & GeometryDataAttribute . NORMAL ) ? true : false , '\n' ,
576+ 'UV' , ( geometryAttributes & GeometryDataAttribute . UV ) ? true : false
579577
580- // Group Count
581- var groupCount = readUint16 ( ) ;
582- if ( this . debug ) console . log ( 'Group Count' , groupCount ) ;
578+ ) ;
583579
584- for ( var j = 0 ; j < groupCount ; ++ j ) {
580+ }
585581
586- // Group Data Attributes
587- var groupAttributes = readUint16 ( ) ;
588- if ( this . debug ) {
582+ // Vertex Count
583+ var vertexCount = readUint32 ( ) ;
584+ if ( this . debug ) console . log ( 'Vertex Count' , vertexCount ) ;
589585
590- console . log (
586+ // Vertex Positions
587+ vertices . positions = readFloat32Array ( vertexCount * 3 ) ;
591588
592- 'GroupAttributes:' , '\n' ,
593- 'Index' , ( groupAttributes & GroupDataAttribute . INDEX ) ? true : false , '\n' ,
594- 'Smoothing' , ( groupAttributes & GroupDataAttribute . SMOOTHING ) ? true : false , '\n' ,
595- 'Material' , ( groupAttributes & GroupDataAttribute . MATERIAL ) ? true : false
589+ // Vertex Normals
590+ if ( geometryAttributes & GeometryDataAttribute . NORMAL ) vertices . normals = readFloat32Array ( vertexCount * 3 ) ;
596591
597- ) ;
592+ // Vertex UVs
593+ if ( geometryAttributes & GeometryDataAttribute . UV ) vertices . uvs = readFloat32Array ( vertexCount * 2 ) ;
598594
599595 }
600596
601- // Group Name
602- var groupName ;
603- if ( groupAttributes & GroupDataAttribute . NAME ) {
597+ // Group Count
598+ var groupCount = readUint16 ( ) ;
599+ if ( this . debug ) console . log ( 'Group Count' , groupCount ) ;
604600
605- groupName = readString ( readUint16 ( ) ) ;
606- if ( this . debug ) console . log ( 'Group Name' , groupName ) ;
601+ for ( var j = 0 ; j < groupCount ; ++ j ) {
607602
608- }
603+ // Group Data Attributes
604+ var groupAttributes = readUint16 ( ) ;
605+ if ( this . debug ) {
609606
610- var group ;
607+ console . log (
611608
612- if ( objectAttributes & ObjectDataAttribute . GEOMETRY ) {
609+ 'GroupAttributes:' , '\n' ,
610+ 'Index' , ( groupAttributes & GroupDataAttribute . INDEX ) ? true : false , '\n' ,
611+ 'Smoothing' , ( groupAttributes & GroupDataAttribute . SMOOTHING ) ? true : false , '\n' ,
612+ 'Material' , ( groupAttributes & GroupDataAttribute . MATERIAL ) ? true : false
613+
614+ ) ;
613615
614- // Indices
615- var indices ;
616- if ( groupAttributes & GroupDataAttribute . INDEX ) {
616+ }
617617
618- // Index Count
619- var indexCount = readUint32 ( ) ;
620- if ( this . debug ) console . log ( 'Index Count' , indexCount ) ;
618+ // Group Name
619+ var groupName ;
620+ if ( groupAttributes & GroupDataAttribute . NAME ) {
621621
622- // Indices
623- indices = readUint16Array ( indexCount ) ;
622+ groupName = readString ( readUint16 ( ) ) ;
623+ if ( this . debug ) console . log ( 'Group Name' , groupName ) ;
624624
625625 }
626626
627- // Smoothing
628- var smoothing = ( groupAttributes & GroupDataAttribute . SMOOTHING ) ? readUint8 ( ) : 0 ;
629- if ( this . debug && ( groupAttributes & GroupDataAttribute . SMOOTHING ) ) console . log ( 'Smoothing' , smoothing ) ;
627+ if ( objectAttributes & ObjectDataAttribute . GEOMETRY ) {
630628
631- var geometry = new THREE . BufferGeometry ( ) ;
632- if ( vertices . positions ) geometry . addAttribute ( 'position' , new THREE . BufferAttribute ( vertices . positions , 3 ) ) ;
633- vertices . normals ? geometry . addAttribute ( 'normal' , new THREE . BufferAttribute ( vertices . normals , 3 ) ) : geometry . computeVertexNormals ( ) ;
634- if ( vertices . uvs ) geometry . addAttribute ( 'uv' , new THREE . BufferAttribute ( vertices . uvs , 2 ) ) ;
635- if ( indices ) geometry . setIndex ( new THREE . BufferAttribute ( indices , 1 ) ) ;
636- geometry . addGroup ( 0 , 1 , 0 ) ;
629+ // Indices
630+ var indices ;
631+ if ( groupAttributes & GroupDataAttribute . INDEX ) {
637632
638- var material ;
639- if ( groupAttributes & GroupDataAttribute . MATERIAL ) {
633+ // Index Count
634+ var indexCount = readUint32 ( ) ;
635+ if ( this . debug ) console . log ( 'Index Count' , indexCount ) ;
640636
641- // Material ID
642- var materialId = readUint16 ( ) ;
643- material = materials [ materialId ] . clone ( ) || new THREE . MeshPhongMaterial ( ) ;
644- material . shading = ( smoothing > 0 ) ? THREE . SmoothShading : THREE . FlatShading ;
645- if ( this . debug ) console . log ( 'Group Material' , materialId , material ) ;
637+ // Indices
638+ indices = readUint16Array ( indexCount ) ;
646639
647- }
640+ }
648641
649- group = new THREE . Mesh ( geometry , material ) ;
642+ // Smoothing
643+ var smoothing = ( groupAttributes & GroupDataAttribute . SMOOTHING ) ? readUint8 ( ) : 0 ;
644+ if ( this . debug && ( groupAttributes & GroupDataAttribute . SMOOTHING ) ) console . log ( 'Smoothing' , smoothing ) ;
650645
651- }
646+ var geometry = new THREE . BufferGeometry ( ) ;
647+ if ( vertices . positions ) geometry . addAttribute ( 'position' , new THREE . BufferAttribute ( vertices . positions , 3 ) ) ;
648+ vertices . normals ? geometry . addAttribute ( 'normal' , new THREE . BufferAttribute ( vertices . normals , 3 ) ) : geometry . computeVertexNormals ( ) ;
649+ if ( vertices . uvs ) geometry . addAttribute ( 'uv' , new THREE . BufferAttribute ( vertices . uvs , 2 ) ) ;
650+ if ( indices ) geometry . setIndex ( new THREE . BufferAttribute ( indices , 1 ) ) ;
651+ geometry . addGroup ( 0 , 1 , 0 ) ;
652+
653+ var material ;
654+ if ( groupAttributes & GroupDataAttribute . MATERIAL ) {
655+
656+ // Material ID
657+ var materialId = readUint16 ( ) ;
658+ material = materials [ materialId ] . clone ( ) || new THREE . MeshPhongMaterial ( ) ;
659+ material . shading = ( smoothing > 0 ) ? THREE . SmoothShading : THREE . FlatShading ;
660+ if ( this . debug ) console . log ( 'Group Material' , materialId , material ) ;
661+
662+ }
652663
653- if ( group === undefined ) group = new THREE . Group ( ) ;
654- if ( groupName ) group . name = groupName ;
655- object . add ( group ) ;
664+ var mesh = new THREE . Mesh ( geometry , material ) ;
665+ if ( groupName ) mesh . name = groupName ;
666+ asset . add ( mesh ) ;
667+
668+ }
669+
670+ }
656671
657672 }
658673
659674 }
660675
661676 if ( this . performanceTimer ) console . timeEnd ( 'BOMLoader' ) ;
662677
663- return containers ;
678+ return assets ;
664679
665680 }
666681
0 commit comments