| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
- export default QUnit.module( 'JSON4 Format', () => {
- QUnit.module( 'Hierarchy', () => {
- QUnit.test( 'Parent-child relationships', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BoxGeometry',
- width: 1,
- height: 1,
- depth: 1
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'MeshBasicMaterial',
- color: 16711680
- } ],
- object: {
- uuid: 'scene-1',
- type: 'Scene',
- children: [
- {
- uuid: 'group-1',
- type: 'Group',
- name: 'Parent',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1,
- children: [
- {
- uuid: 'mesh-1',
- type: 'Mesh',
- name: 'Child',
- geometry: 'geom-1',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- }
- ]
- }
- ]
- }
- };
- const loader = new ObjectLoader();
- const scene = loader.parse( json );
- const parent = scene.children[ 0 ];
- const child = parent.children[ 0 ];
- assert.strictEqual( child.parent, parent, 'Child parent is correct' );
- assert.strictEqual( parent.children.length, 1, 'Parent has 1 child' );
- assert.strictEqual( child.name, 'Child', 'Child name' );
- } );
- QUnit.test( 'Nested hierarchy (3+ levels)', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BoxGeometry',
- width: 1,
- height: 1,
- depth: 1
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'MeshBasicMaterial',
- color: 16711680
- } ],
- object: {
- uuid: 'scene-1',
- type: 'Scene',
- children: [
- {
- uuid: 'level1',
- type: 'Group',
- name: 'Level1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1,
- children: [
- {
- uuid: 'level2',
- type: 'Group',
- name: 'Level2',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1 ],
- layers: 1,
- children: [
- {
- uuid: 'level3',
- type: 'Group',
- name: 'Level3',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1 ],
- layers: 1,
- children: [
- {
- uuid: 'mesh-1',
- type: 'Mesh',
- name: 'DeepMesh',
- geometry: 'geom-1',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1 ],
- layers: 1
- }
- ]
- }
- ]
- }
- ]
- }
- ]
- }
- };
- const loader = new ObjectLoader();
- const scene = loader.parse( json );
- const level1 = scene.children[ 0 ];
- const level2 = level1.children[ 0 ];
- const level3 = level2.children[ 0 ];
- const mesh = level3.children[ 0 ];
- assert.strictEqual( level1.name, 'Level1', 'Level 1 name' );
- assert.strictEqual( level2.name, 'Level2', 'Level 2 name' );
- assert.strictEqual( level3.name, 'Level3', 'Level 3 name' );
- assert.strictEqual( mesh.name, 'DeepMesh', 'Deep mesh name' );
- assert.strictEqual( mesh.parent.parent.parent.parent, scene, 'Deep parent chain' );
- } );
- QUnit.test( 'Multiple meshes sharing same geometry UUID', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'shared-geom',
- type: 'BoxGeometry',
- width: 1,
- height: 1,
- depth: 1
- } ],
- materials: [
- { uuid: 'mat-1', type: 'MeshBasicMaterial', color: 16711680 },
- { uuid: 'mat-2', type: 'MeshBasicMaterial', color: 65280 }
- ],
- object: {
- uuid: 'scene-1',
- type: 'Scene',
- children: [
- {
- uuid: 'mesh-1',
- type: 'Mesh',
- geometry: 'shared-geom',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'mesh-2',
- type: 'Mesh',
- geometry: 'shared-geom',
- material: 'mat-2',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'mesh-3',
- type: 'Mesh',
- geometry: 'shared-geom',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 4, 0, 0, 1 ],
- layers: 1
- }
- ]
- }
- };
- const loader = new ObjectLoader();
- const scene = loader.parse( json );
- const mesh1 = scene.children[ 0 ];
- const mesh2 = scene.children[ 1 ];
- const mesh3 = scene.children[ 2 ];
- assert.strictEqual( mesh1.geometry, mesh2.geometry, 'Mesh1 and Mesh2 share geometry' );
- assert.strictEqual( mesh2.geometry, mesh3.geometry, 'Mesh2 and Mesh3 share geometry' );
- } );
- QUnit.test( 'Multiple meshes sharing same material UUID', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [
- { uuid: 'geom-1', type: 'BoxGeometry', width: 1, height: 1, depth: 1 },
- { uuid: 'geom-2', type: 'SphereGeometry', radius: 0.5, widthSegments: 16, heightSegments: 8 }
- ],
- materials: [ {
- uuid: 'shared-mat',
- type: 'MeshBasicMaterial',
- color: 16711680
- } ],
- object: {
- uuid: 'scene-1',
- type: 'Scene',
- children: [
- {
- uuid: 'mesh-1',
- type: 'Mesh',
- geometry: 'geom-1',
- material: 'shared-mat',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'mesh-2',
- type: 'Mesh',
- geometry: 'geom-2',
- material: 'shared-mat',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1 ],
- layers: 1
- }
- ]
- }
- };
- const loader = new ObjectLoader();
- const scene = loader.parse( json );
- const mesh1 = scene.children[ 0 ];
- const mesh2 = scene.children[ 1 ];
- assert.strictEqual( mesh1.material, mesh2.material, 'Meshes share same material' );
- assert.notStrictEqual( mesh1.geometry, mesh2.geometry, 'Meshes have different geometries' );
- } );
- QUnit.test( 'LOD with levels', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [
- { uuid: 'geom-high', type: 'SphereGeometry', radius: 1, widthSegments: 32, heightSegments: 16 },
- { uuid: 'geom-med', type: 'SphereGeometry', radius: 1, widthSegments: 16, heightSegments: 8 },
- { uuid: 'geom-low', type: 'SphereGeometry', radius: 1, widthSegments: 8, heightSegments: 4 }
- ],
- materials: [ {
- uuid: 'mat-1',
- type: 'MeshBasicMaterial',
- color: 16711680
- } ],
- object: {
- uuid: 'lod-1',
- type: 'LOD',
- autoUpdate: true,
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1,
- levels: [
- { object: 'mesh-high', distance: 0, hysteresis: 0 },
- { object: 'mesh-med', distance: 50, hysteresis: 0 },
- { object: 'mesh-low', distance: 100, hysteresis: 0 }
- ],
- children: [
- {
- uuid: 'mesh-high',
- type: 'Mesh',
- name: 'High',
- geometry: 'geom-high',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'mesh-med',
- type: 'Mesh',
- name: 'Medium',
- geometry: 'geom-med',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'mesh-low',
- type: 'Mesh',
- name: 'Low',
- geometry: 'geom-low',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- }
- ]
- }
- };
- const loader = new ObjectLoader();
- const lod = loader.parse( json );
- assert.ok( lod.isLOD, 'Is LOD' );
- assert.strictEqual( lod.levels.length, 3, 'Has 3 levels' );
- assert.strictEqual( lod.levels[ 0 ].distance, 0, 'First level distance' );
- assert.strictEqual( lod.levels[ 1 ].distance, 50, 'Second level distance' );
- assert.strictEqual( lod.levels[ 2 ].distance, 100, 'Third level distance' );
- assert.strictEqual( lod.autoUpdate, true, 'autoUpdate is true' );
- } );
- QUnit.test( 'LOD with hysteresis', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [
- { uuid: 'geom-high', type: 'BoxGeometry', width: 1, height: 1, depth: 1 },
- { uuid: 'geom-low', type: 'BoxGeometry', width: 1, height: 1, depth: 1 }
- ],
- materials: [ {
- uuid: 'mat-1',
- type: 'MeshBasicMaterial',
- color: 16711680
- } ],
- object: {
- uuid: 'lod-1',
- type: 'LOD',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1,
- levels: [
- { object: 'mesh-high', distance: 0, hysteresis: 0.5 },
- { object: 'mesh-low', distance: 100, hysteresis: 0.5 }
- ],
- children: [
- {
- uuid: 'mesh-high',
- type: 'Mesh',
- geometry: 'geom-high',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'mesh-low',
- type: 'Mesh',
- geometry: 'geom-low',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- }
- ]
- }
- };
- const loader = new ObjectLoader();
- const lod = loader.parse( json );
- assert.strictEqual( lod.levels[ 0 ].hysteresis, 0.5, 'First level hysteresis' );
- assert.strictEqual( lod.levels[ 1 ].hysteresis, 0.5, 'Second level hysteresis' );
- } );
- QUnit.test( 'Multiple siblings', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BoxGeometry',
- width: 1,
- height: 1,
- depth: 1
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'MeshBasicMaterial',
- color: 16711680
- } ],
- object: {
- uuid: 'scene-1',
- type: 'Scene',
- children: [
- {
- uuid: 'mesh-1',
- type: 'Mesh',
- name: 'First',
- geometry: 'geom-1',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'mesh-2',
- type: 'Mesh',
- name: 'Second',
- geometry: 'geom-1',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'mesh-3',
- type: 'Mesh',
- name: 'Third',
- geometry: 'geom-1',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 4, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'mesh-4',
- type: 'Mesh',
- name: 'Fourth',
- geometry: 'geom-1',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 6, 0, 0, 1 ],
- layers: 1
- }
- ]
- }
- };
- const loader = new ObjectLoader();
- const scene = loader.parse( json );
- assert.strictEqual( scene.children.length, 4, 'Has 4 children' );
- assert.strictEqual( scene.children[ 0 ].name, 'First', 'First child name' );
- assert.strictEqual( scene.children[ 1 ].name, 'Second', 'Second child name' );
- assert.strictEqual( scene.children[ 2 ].name, 'Third', 'Third child name' );
- assert.strictEqual( scene.children[ 3 ].name, 'Fourth', 'Fourth child name' );
- } );
- QUnit.test( 'Mixed object types in hierarchy', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BoxGeometry',
- width: 1,
- height: 1,
- depth: 1
- } ],
- materials: [
- { uuid: 'mat-1', type: 'MeshBasicMaterial', color: 16711680 },
- { uuid: 'mat-2', type: 'SpriteMaterial', color: 65280 }
- ],
- object: {
- uuid: 'scene-1',
- type: 'Scene',
- children: [
- {
- uuid: 'group-1',
- type: 'Group',
- name: 'Objects',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1,
- children: [
- {
- uuid: 'mesh-1',
- type: 'Mesh',
- geometry: 'geom-1',
- material: 'mat-1',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'sprite-1',
- type: 'Sprite',
- material: 'mat-2',
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1 ],
- layers: 1
- }
- ]
- },
- {
- uuid: 'light-1',
- type: 'AmbientLight',
- color: 16777215,
- intensity: 0.5,
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- },
- {
- uuid: 'cam-1',
- type: 'PerspectiveCamera',
- fov: 75,
- aspect: 1,
- near: 0.1,
- far: 1000,
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 5, 1 ],
- layers: 1
- }
- ]
- }
- };
- const loader = new ObjectLoader();
- const scene = loader.parse( json );
- assert.strictEqual( scene.children.length, 3, 'Scene has 3 children' );
- assert.ok( scene.children[ 0 ].isGroup, 'First child is Group' );
- assert.ok( scene.children[ 1 ].isAmbientLight, 'Second child is AmbientLight' );
- assert.ok( scene.children[ 2 ].isPerspectiveCamera, 'Third child is PerspectiveCamera' );
- const group = scene.children[ 0 ];
- assert.ok( group.children[ 0 ].isMesh, 'Group first child is Mesh' );
- assert.ok( group.children[ 1 ].isSprite, 'Group second child is Sprite' );
- } );
- } );
- } );
|