| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
- export default QUnit.module( 'JSON4 Format', () => {
- QUnit.module( 'Instancing', () => {
- QUnit.test( 'InstancedMesh - count, instanceMatrix', ( 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: 'instanced-1',
- type: 'InstancedMesh',
- geometry: 'geom-1',
- material: 'mat-1',
- count: 3,
- instanceMatrix: {
- type: 'Float32Array',
- array: [
- 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
- 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1,
- 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 4, 0, 0, 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.isInstancedMesh, 'Is InstancedMesh' );
- assert.strictEqual( mesh.count, 3, 'Count is 3' );
- assert.ok( mesh.instanceMatrix, 'Has instanceMatrix' );
- assert.strictEqual( mesh.instanceMatrix.count, 3, 'instanceMatrix count is 3' );
- } );
- QUnit.test( 'InstancedMesh - instanceColor', ( 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: 16777215
- } ],
- object: {
- uuid: 'instanced-1',
- type: 'InstancedMesh',
- geometry: 'geom-1',
- material: 'mat-1',
- count: 3,
- instanceMatrix: {
- type: 'Float32Array',
- array: [
- 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
- 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1,
- 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 4, 0, 0, 1
- ]
- },
- instanceColor: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 1, 0, 0, 0, 1, 0, 0, 0, 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.instanceColor, 'Has instanceColor' );
- assert.strictEqual( mesh.instanceColor.count, 3, 'instanceColor count is 3' );
- assert.strictEqual( mesh.instanceColor.itemSize, 3, 'instanceColor itemSize is 3' );
- } );
- QUnit.test( 'InstancedBufferGeometry', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'InstancedBufferGeometry',
- isInstancedBufferGeometry: true,
- data: {
- attributes: {
- position: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 1, 0, 0, 0.5, 1, 0 ],
- normalized: false
- },
- instanceOffset: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 2, 0 ],
- normalized: false,
- isInstancedBufferAttribute: true,
- meshPerAttribute: 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.geometry.isInstancedBufferGeometry, 'Is InstancedBufferGeometry' );
- } );
- QUnit.test( 'InstancedBufferAttribute', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'InstancedBufferGeometry',
- isInstancedBufferGeometry: true,
- data: {
- attributes: {
- position: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 1, 0, 0, 0.5, 1, 0 ],
- normalized: false
- },
- instanceColor: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ],
- normalized: false,
- isInstancedBufferAttribute: true,
- meshPerAttribute: 1
- }
- }
- }
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'MeshBasicMaterial',
- color: 16777215,
- vertexColors: true
- } ],
- 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 );
- const attr = mesh.geometry.attributes.instanceColor;
- assert.ok( attr.isInstancedBufferAttribute, 'Is InstancedBufferAttribute' );
- assert.strictEqual( attr.meshPerAttribute, 1, 'meshPerAttribute is 1' );
- } );
- QUnit.test( 'InstancedInterleavedBuffer', ( assert ) => {
- // Create Float32Array data and convert to Uint32 representation for arrayBuffers
- const floatData = new Float32Array( [
- 0, 0, 0, 1, 0, 0,
- 2, 0, 0, 0, 1, 0,
- 4, 0, 0, 0, 0, 1
- ] );
- const arrayBufferData = Array.from( new Uint32Array( floatData.buffer ) );
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'InstancedBufferGeometry',
- isInstancedBufferGeometry: true,
- data: {
- attributes: {
- position: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 1, 0, 0, 0.5, 1, 0 ],
- normalized: false
- },
- instanceOffset: {
- isInterleavedBufferAttribute: true,
- itemSize: 3,
- data: 'buffer-1',
- offset: 0,
- normalized: false
- },
- instanceColor: {
- isInterleavedBufferAttribute: true,
- itemSize: 3,
- data: 'buffer-1',
- offset: 3,
- normalized: false
- }
- },
- arrayBuffers: {
- 'arraybuffer-1': arrayBufferData
- },
- interleavedBuffers: {
- 'buffer-1': {
- uuid: 'buffer-1',
- buffer: 'arraybuffer-1',
- type: 'Float32Array',
- stride: 6,
- isInstancedInterleavedBuffer: true,
- meshPerAttribute: 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.geometry.isInstancedBufferGeometry, 'Is InstancedBufferGeometry' );
- } );
- QUnit.test( 'InstancedMesh matrix verification', ( 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: 'instanced-1',
- type: 'InstancedMesh',
- geometry: 'geom-1',
- material: 'mat-1',
- count: 2,
- instanceMatrix: {
- type: 'Float32Array',
- array: [
- 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 1, 2, 3, 1,
- 0.5, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0.5, 0, 10, 20, 30, 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 );
- const matrix = mesh.instanceMatrix.array;
- // First instance: scale 2, translate (1, 2, 3)
- assert.strictEqual( matrix[ 0 ], 2, 'First instance scale x' );
- assert.strictEqual( matrix[ 12 ], 1, 'First instance translate x' );
- assert.strictEqual( matrix[ 13 ], 2, 'First instance translate y' );
- assert.strictEqual( matrix[ 14 ], 3, 'First instance translate z' );
- // Second instance: scale 0.5, translate (10, 20, 30)
- assert.strictEqual( matrix[ 16 ], 0.5, 'Second instance scale x' );
- assert.strictEqual( matrix[ 28 ], 10, 'Second instance translate x' );
- assert.strictEqual( matrix[ 29 ], 20, 'Second instance translate y' );
- assert.strictEqual( matrix[ 30 ], 30, 'Second instance translate z' );
- } );
- } );
- } );
|