| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
- export default QUnit.module( 'JSON4 Format', () => {
- QUnit.module( 'Objects', () => {
- QUnit.test( 'Mesh with geometry + material', ( 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: '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
- }
- };
- const loader = new ObjectLoader();
- const mesh = loader.parse( json );
- assert.ok( mesh.isMesh, 'Is Mesh' );
- assert.ok( mesh.geometry.isBufferGeometry, 'Has geometry' );
- assert.ok( mesh.material.isMaterial, 'Has material' );
- } );
- QUnit.test( 'Mesh with material array (multi-material)', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BufferGeometry',
- data: {
- attributes: {
- position: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 ],
- normalized: false
- }
- },
- groups: [
- { start: 0, count: 3, materialIndex: 0 },
- { start: 3, count: 3, materialIndex: 1 }
- ]
- }
- } ],
- materials: [
- { uuid: 'mat-1', type: 'MeshBasicMaterial', color: 16711680 },
- { uuid: 'mat-2', type: 'MeshBasicMaterial', color: 65280 }
- ],
- object: {
- uuid: 'mesh-1',
- type: 'Mesh',
- geometry: 'geom-1',
- material: [ 'mat-1', 'mat-2' ],
- matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
- layers: 1
- }
- };
- const loader = new ObjectLoader();
- const mesh = loader.parse( json );
- assert.ok( Array.isArray( mesh.material ), 'Material is array' );
- assert.strictEqual( mesh.material.length, 2, 'Has 2 materials' );
- assert.strictEqual( mesh.material[ 0 ].color.getHex(), 0xff0000, 'First material is red' );
- assert.strictEqual( mesh.material[ 1 ].color.getHex(), 0x00ff00, 'Second material is green' );
- } );
- QUnit.test( 'Group container', ( 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: 'group-1',
- type: 'Group',
- 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
- }
- ]
- }
- };
- const loader = new ObjectLoader();
- const group = loader.parse( json );
- assert.ok( group.isGroup, 'Is Group' );
- assert.strictEqual( group.children.length, 1, 'Has 1 child' );
- assert.ok( group.children[ 0 ].isMesh, 'Child is Mesh' );
- } );
- QUnit.test( 'Object3D transforms (position, rotation, scale via matrix)', ( 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: 'mesh-1',
- type: 'Mesh',
- geometry: 'geom-1',
- material: 'mat-1',
- matrix: [ 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 5, 10, 15, 1 ],
- layers: 1
- }
- };
- const loader = new ObjectLoader();
- const mesh = loader.parse( json );
- assert.strictEqual( mesh.position.x, 5, 'Position.x is 5' );
- assert.strictEqual( mesh.position.y, 10, 'Position.y is 10' );
- assert.strictEqual( mesh.position.z, 15, 'Position.z is 15' );
- assert.strictEqual( mesh.scale.x, 2, 'Scale.x is 2' );
- assert.strictEqual( mesh.scale.y, 2, 'Scale.y is 2' );
- assert.strictEqual( mesh.scale.z, 2, 'Scale.z is 2' );
- } );
- QUnit.test( 'Visibility, frustumCulled, renderOrder, layers', ( 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: '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 ],
- visible: false,
- frustumCulled: false,
- renderOrder: 5,
- layers: 3
- }
- };
- const loader = new ObjectLoader();
- const mesh = loader.parse( json );
- assert.strictEqual( mesh.visible, false, 'Visible is false' );
- assert.strictEqual( mesh.frustumCulled, false, 'FrustumCulled is false' );
- assert.strictEqual( mesh.renderOrder, 5, 'RenderOrder is 5' );
- assert.strictEqual( mesh.layers.mask, 3, 'Layers mask is 3' );
- } );
- QUnit.test( 'castShadow, receiveShadow', ( 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: '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,
- castShadow: true,
- receiveShadow: true
- }
- };
- const loader = new ObjectLoader();
- const mesh = loader.parse( json );
- assert.strictEqual( mesh.castShadow, true, 'CastShadow is true' );
- assert.strictEqual( mesh.receiveShadow, true, 'ReceiveShadow is true' );
- } );
- QUnit.test( 'userData preservation', ( 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: '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,
- userData: {
- customString: 'hello',
- customNumber: 42,
- customArray: [ 1, 2, 3 ],
- customObject: { nested: true }
- }
- }
- };
- const loader = new ObjectLoader();
- const mesh = loader.parse( json );
- assert.strictEqual( mesh.userData.customString, 'hello', 'userData string' );
- assert.strictEqual( mesh.userData.customNumber, 42, 'userData number' );
- assert.deepEqual( mesh.userData.customArray, [ 1, 2, 3 ], 'userData array' );
- assert.deepEqual( mesh.userData.customObject, { nested: true }, 'userData object' );
- } );
- QUnit.test( 'Points with PointsMaterial', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BufferGeometry',
- data: {
- attributes: {
- position: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 1, 1, 1, 2, 2, 2 ],
- normalized: false
- }
- }
- }
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'PointsMaterial',
- color: 16711680,
- size: 5
- } ],
- object: {
- uuid: 'points-1',
- type: 'Points',
- 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 points = loader.parse( json );
- assert.ok( points.isPoints, 'Is Points' );
- assert.ok( points.material.isPointsMaterial, 'Has PointsMaterial' );
- } );
- QUnit.test( 'Line', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BufferGeometry',
- data: {
- attributes: {
- position: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 1, 1, 0, 2, 0, 0 ],
- normalized: false
- }
- }
- }
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'LineBasicMaterial',
- color: 65280
- } ],
- object: {
- uuid: 'line-1',
- type: 'Line',
- 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 line = loader.parse( json );
- assert.ok( line.isLine, 'Is Line' );
- assert.ok( line.material.isLineBasicMaterial, 'Has LineBasicMaterial' );
- } );
- QUnit.test( 'LineSegments', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BufferGeometry',
- data: {
- attributes: {
- position: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0 ],
- normalized: false
- }
- }
- }
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'LineBasicMaterial',
- color: 65280
- } ],
- object: {
- uuid: 'lines-1',
- type: 'LineSegments',
- 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 lines = loader.parse( json );
- assert.ok( lines.isLineSegments, 'Is LineSegments' );
- } );
- QUnit.test( 'LineLoop', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BufferGeometry',
- data: {
- attributes: {
- position: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 1, 0, 0, 0.5, 1, 0 ],
- normalized: false
- }
- }
- }
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'LineBasicMaterial',
- color: 65280
- } ],
- object: {
- uuid: 'loop-1',
- type: 'LineLoop',
- 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 loop = loader.parse( json );
- assert.ok( loop.isLineLoop, 'Is LineLoop' );
- } );
- QUnit.test( 'Sprite', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- materials: [ {
- uuid: 'mat-1',
- type: 'SpriteMaterial',
- color: 16711680
- } ],
- object: {
- uuid: 'sprite-1',
- type: 'Sprite',
- 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 sprite = loader.parse( json );
- assert.ok( sprite.isSprite, 'Is Sprite' );
- assert.ok( sprite.material.isSpriteMaterial, 'Has SpriteMaterial' );
- } );
- QUnit.test( 'Object name', ( 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: 'mesh-1',
- type: 'Mesh',
- name: 'MyMesh',
- 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 mesh = loader.parse( json );
- assert.strictEqual( mesh.name, 'MyMesh', 'Object name' );
- } );
- } );
- } );
|