| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
- export default QUnit.module( 'JSON4 Format', () => {
- QUnit.module( 'Attributes', () => {
- QUnit.test( 'Float32BufferAttribute', ( 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 ],
- normalized: false
- }
- }
- }
- } ],
- 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 );
- const attr = mesh.geometry.attributes.position;
- assert.ok( attr.array instanceof Float32Array, 'Array is Float32Array' );
- assert.strictEqual( attr.itemSize, 3, 'ItemSize is 3' );
- assert.strictEqual( attr.count, 3, 'Count is 3' );
- } );
- QUnit.test( 'Int8BufferAttribute', ( 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 ],
- normalized: false
- },
- custom: {
- itemSize: 1,
- type: 'Int8Array',
- array: [ - 128, 0, 127 ],
- normalized: false
- }
- }
- }
- } ],
- 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 );
- const attr = mesh.geometry.attributes.custom;
- assert.ok( attr.array instanceof Int8Array, 'Array is Int8Array' );
- assert.strictEqual( attr.array[ 0 ], - 128, 'First value is -128' );
- assert.strictEqual( attr.array[ 2 ], 127, 'Last value is 127' );
- } );
- QUnit.test( 'Int16BufferAttribute', ( 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 ],
- normalized: false
- },
- custom: {
- itemSize: 1,
- type: 'Int16Array',
- array: [ - 32768, 0, 32767 ],
- normalized: false
- }
- }
- }
- } ],
- 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 );
- const attr = mesh.geometry.attributes.custom;
- assert.ok( attr.array instanceof Int16Array, 'Array is Int16Array' );
- assert.strictEqual( attr.array[ 0 ], - 32768, 'First value is -32768' );
- assert.strictEqual( attr.array[ 2 ], 32767, 'Last value is 32767' );
- } );
- QUnit.test( 'Int32BufferAttribute', ( 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 ],
- normalized: false
- },
- custom: {
- itemSize: 1,
- type: 'Int32Array',
- array: [ - 2147483648, 0, 2147483647 ],
- normalized: false
- }
- }
- }
- } ],
- 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 );
- const attr = mesh.geometry.attributes.custom;
- assert.ok( attr.array instanceof Int32Array, 'Array is Int32Array' );
- assert.strictEqual( attr.array[ 0 ], - 2147483648, 'First value' );
- assert.strictEqual( attr.array[ 2 ], 2147483647, 'Last value' );
- } );
- QUnit.test( 'Uint8BufferAttribute', ( 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 ],
- normalized: false
- },
- custom: {
- itemSize: 1,
- type: 'Uint8Array',
- array: [ 0, 128, 255 ],
- normalized: false
- }
- }
- }
- } ],
- 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 );
- const attr = mesh.geometry.attributes.custom;
- assert.ok( attr.array instanceof Uint8Array, 'Array is Uint8Array' );
- assert.strictEqual( attr.array[ 0 ], 0, 'First value is 0' );
- assert.strictEqual( attr.array[ 2 ], 255, 'Last value is 255' );
- } );
- QUnit.test( 'Uint16BufferAttribute', ( 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 ],
- normalized: false
- },
- custom: {
- itemSize: 1,
- type: 'Uint16Array',
- array: [ 0, 32768, 65535 ],
- normalized: false
- }
- }
- }
- } ],
- 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 );
- const attr = mesh.geometry.attributes.custom;
- assert.ok( attr.array instanceof Uint16Array, 'Array is Uint16Array' );
- assert.strictEqual( attr.array[ 0 ], 0, 'First value is 0' );
- assert.strictEqual( attr.array[ 2 ], 65535, 'Last value is 65535' );
- } );
- QUnit.test( 'Uint32BufferAttribute', ( 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 ],
- normalized: false
- },
- custom: {
- itemSize: 1,
- type: 'Uint32Array',
- array: [ 0, 2147483648, 4294967295 ],
- normalized: false
- }
- }
- }
- } ],
- 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 );
- const attr = mesh.geometry.attributes.custom;
- assert.ok( attr.array instanceof Uint32Array, 'Array is Uint32Array' );
- assert.strictEqual( attr.array[ 0 ], 0, 'First value is 0' );
- assert.strictEqual( attr.array[ 2 ], 4294967295, 'Last value is 4294967295' );
- } );
- QUnit.test( 'Normalized attribute', ( 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 ],
- normalized: false
- },
- color: {
- itemSize: 3,
- type: 'Uint8Array',
- array: [ 255, 0, 0, 0, 255, 0, 0, 0, 255 ],
- normalized: true
- }
- }
- }
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'MeshBasicMaterial',
- color: 16711680,
- 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.color;
- assert.ok( attr.array instanceof Uint8Array, 'Array is Uint8Array' );
- assert.strictEqual( attr.normalized, true, 'Attribute is normalized' );
- } );
- QUnit.test( 'InterleavedBuffer + InterleavedBufferAttribute', ( assert ) => {
- // Create Float32Array data and convert to Uint32 representation for arrayBuffers
- const floatData = new Float32Array( [
- 0, 0, 0, 0, 0,
- 1, 0, 0, 1, 0,
- 0, 1, 0, 0.5, 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: 'BufferGeometry',
- data: {
- arrayBuffers: {
- 'arraybuffer-1': arrayBufferData
- },
- interleavedBuffers: {
- 'buffer-1': {
- uuid: 'buffer-1',
- buffer: 'arraybuffer-1',
- type: 'Float32Array',
- stride: 5
- }
- },
- attributes: {
- position: {
- isInterleavedBufferAttribute: true,
- itemSize: 3,
- data: 'buffer-1',
- offset: 0,
- normalized: false
- },
- uv: {
- isInterleavedBufferAttribute: true,
- itemSize: 2,
- data: 'buffer-1',
- offset: 3,
- normalized: false
- }
- }
- }
- } ],
- 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 );
- const posAttr = mesh.geometry.attributes.position;
- const uvAttr = mesh.geometry.attributes.uv;
- assert.ok( posAttr.isInterleavedBufferAttribute, 'Position is InterleavedBufferAttribute' );
- assert.ok( uvAttr.isInterleavedBufferAttribute, 'UV is InterleavedBufferAttribute' );
- assert.strictEqual( posAttr.data, uvAttr.data, 'Share same InterleavedBuffer' );
- assert.strictEqual( posAttr.itemSize, 3, 'Position itemSize is 3' );
- assert.strictEqual( uvAttr.itemSize, 2, 'UV itemSize is 2' );
- assert.strictEqual( posAttr.offset, 0, 'Position offset is 0' );
- assert.strictEqual( uvAttr.offset, 3, 'UV offset is 3' );
- } );
- QUnit.test( 'Multiple attributes sharing InterleavedBuffer', ( assert ) => {
- // Create Float32Array data and convert to Uint32 representation for arrayBuffers
- const floatData = new Float32Array( [
- 0, 0, 0, 0, 0, 1, 0, 0,
- 1, 0, 0, 1, 0, 0, 0, 1,
- 0, 1, 0, 0.5, 1, 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: 'BufferGeometry',
- data: {
- arrayBuffers: {
- 'arraybuffer-1': arrayBufferData
- },
- interleavedBuffers: {
- 'buffer-1': {
- uuid: 'buffer-1',
- buffer: 'arraybuffer-1',
- type: 'Float32Array',
- stride: 8
- }
- },
- attributes: {
- position: {
- isInterleavedBufferAttribute: true,
- itemSize: 3,
- data: 'buffer-1',
- offset: 0,
- normalized: false
- },
- uv: {
- isInterleavedBufferAttribute: true,
- itemSize: 2,
- data: 'buffer-1',
- offset: 3,
- normalized: false
- },
- normal: {
- isInterleavedBufferAttribute: true,
- itemSize: 3,
- data: 'buffer-1',
- offset: 5,
- normalized: false
- }
- }
- }
- } ],
- 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 );
- const posAttr = mesh.geometry.attributes.position;
- const uvAttr = mesh.geometry.attributes.uv;
- const normalAttr = mesh.geometry.attributes.normal;
- assert.strictEqual( posAttr.data, uvAttr.data, 'Position and UV share same buffer' );
- assert.strictEqual( uvAttr.data, normalAttr.data, 'UV and Normal share same buffer' );
- assert.strictEqual( posAttr.data.stride, 8, 'Buffer stride is 8' );
- } );
- QUnit.test( 'InstancedBufferAttribute with meshPerAttribute', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'InstancedBufferGeometry',
- data: {
- attributes: {
- position: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
- normalized: false
- },
- instanceOffset: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 0, 2, 0, 0, 4, 0, 0 ],
- normalized: false,
- isInstancedBufferAttribute: true,
- meshPerAttribute: 1
- }
- }
- },
- instanceCount: 3
- } ],
- 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 );
- const attr = mesh.geometry.attributes.instanceOffset;
- assert.ok( attr.isInstancedBufferAttribute, 'Is InstancedBufferAttribute' );
- assert.strictEqual( attr.meshPerAttribute, 1, 'meshPerAttribute is 1' );
- } );
- } );
- } );
|