| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- import { ObjectLoader } from '../../../src/loaders/ObjectLoader.js';
- export default QUnit.module( 'JSON4 Format', () => {
- QUnit.module( 'Geometries', () => {
- QUnit.test( 'BufferGeometry with position 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
- }
- }
- }
- } ],
- materials: [ {
- uuid: 'mat-1',
- type: 'MeshBasicMaterial',
- color: 16711680
- } ],
- object: {
- uuid: 'scene-1',
- type: 'Scene',
- 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 scene = loader.parse( json );
- assert.ok( scene.isScene, 'Parsed Scene' );
- const mesh = scene.children[ 0 ];
- assert.ok( mesh.isMesh, 'Child is Mesh' );
- assert.ok( mesh.geometry.isBufferGeometry, 'Has BufferGeometry' );
- assert.strictEqual(
- mesh.geometry.attributes.position.count,
- 3,
- 'Position has 3 vertices'
- );
- } );
- QUnit.test( 'BufferGeometry with position, normal, uv attributes', ( 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
- },
- normal: {
- itemSize: 3,
- type: 'Float32Array',
- array: [ 0, 0, 1, 0, 0, 1, 0, 0, 1 ],
- normalized: false
- },
- uv: {
- itemSize: 2,
- type: 'Float32Array',
- array: [ 0, 0, 1, 0, 0.5, 1 ],
- 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 );
- assert.ok( mesh.isMesh, 'Parsed Mesh' );
- assert.ok( mesh.geometry.attributes.position, 'Has position attribute' );
- assert.ok( mesh.geometry.attributes.normal, 'Has normal attribute' );
- assert.ok( mesh.geometry.attributes.uv, 'Has uv attribute' );
- assert.strictEqual( mesh.geometry.attributes.position.itemSize, 3, 'Position itemSize is 3' );
- assert.strictEqual( mesh.geometry.attributes.normal.itemSize, 3, 'Normal itemSize is 3' );
- assert.strictEqual( mesh.geometry.attributes.uv.itemSize, 2, 'UV itemSize is 2' );
- } );
- QUnit.test( 'Indexed BufferGeometry', ( 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, 1, 1, 0, 0, 1, 0 ],
- normalized: false
- }
- },
- index: {
- type: 'Uint16Array',
- array: [ 0, 1, 2, 0, 2, 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 );
- assert.ok( mesh.isMesh, 'Parsed Mesh' );
- assert.ok( mesh.geometry.index, 'Has index buffer' );
- assert.strictEqual( mesh.geometry.index.count, 6, 'Index count is 6' );
- assert.strictEqual( mesh.geometry.attributes.position.count, 4, 'Position has 4 vertices' );
- } );
- QUnit.test( 'Geometry groups (materialIndex)', ( 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( mesh.isMesh, 'Parsed Mesh' );
- assert.strictEqual( mesh.geometry.groups.length, 2, 'Has 2 groups' );
- assert.strictEqual( mesh.geometry.groups[ 0 ].materialIndex, 0, 'First group materialIndex is 0' );
- assert.strictEqual( mesh.geometry.groups[ 1 ].materialIndex, 1, 'Second group materialIndex is 1' );
- assert.ok( Array.isArray( mesh.material ), 'Material is an array' );
- assert.strictEqual( mesh.material.length, 2, 'Has 2 materials' );
- } );
- QUnit.test( 'BoundingSphere preservation', ( 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
- }
- },
- boundingSphere: {
- center: [ 0.5, 0.5, 0 ],
- radius: 0.7071067811865476
- }
- }
- } ],
- 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.boundingSphere, 'Has boundingSphere' );
- assert.strictEqual( mesh.geometry.boundingSphere.center.x, 0.5, 'BoundingSphere center.x' );
- assert.strictEqual( mesh.geometry.boundingSphere.center.y, 0.5, 'BoundingSphere center.y' );
- assert.ok( Math.abs( mesh.geometry.boundingSphere.radius - 0.7071067811865476 ) < 0.0001, 'BoundingSphere radius' );
- } );
- QUnit.test( 'BoxGeometry via parameters', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BoxGeometry',
- width: 2,
- height: 3,
- depth: 4,
- widthSegments: 2,
- heightSegments: 3,
- depthSegments: 4
- } ],
- 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, 'Parsed Mesh' );
- assert.strictEqual( mesh.geometry.type, 'BoxGeometry', 'Geometry type is BoxGeometry' );
- assert.strictEqual( mesh.geometry.parameters.width, 2, 'Width parameter' );
- assert.strictEqual( mesh.geometry.parameters.height, 3, 'Height parameter' );
- assert.strictEqual( mesh.geometry.parameters.depth, 4, 'Depth parameter' );
- } );
- QUnit.test( 'SphereGeometry via parameters', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'SphereGeometry',
- radius: 5,
- widthSegments: 32,
- heightSegments: 16,
- phiStart: 0,
- phiLength: Math.PI * 2,
- thetaStart: 0,
- thetaLength: Math.PI
- } ],
- 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, 'Parsed Mesh' );
- assert.strictEqual( mesh.geometry.type, 'SphereGeometry', 'Geometry type is SphereGeometry' );
- assert.strictEqual( mesh.geometry.parameters.radius, 5, 'Radius parameter' );
- assert.strictEqual( mesh.geometry.parameters.widthSegments, 32, 'widthSegments parameter' );
- assert.strictEqual( mesh.geometry.parameters.heightSegments, 16, 'heightSegments parameter' );
- } );
- QUnit.test( 'PlaneGeometry via parameters', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'PlaneGeometry',
- width: 10,
- height: 20,
- widthSegments: 5,
- heightSegments: 10
- } ],
- 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, 'Parsed Mesh' );
- assert.strictEqual( mesh.geometry.type, 'PlaneGeometry', 'Geometry type is PlaneGeometry' );
- assert.strictEqual( mesh.geometry.parameters.width, 10, 'Width parameter' );
- assert.strictEqual( mesh.geometry.parameters.height, 20, 'Height parameter' );
- } );
- QUnit.test( 'CylinderGeometry via parameters', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'CylinderGeometry',
- radiusTop: 1,
- radiusBottom: 2,
- height: 5,
- radialSegments: 16,
- heightSegments: 4,
- openEnded: false,
- thetaStart: 0,
- thetaLength: Math.PI * 2
- } ],
- 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, 'Parsed Mesh' );
- assert.strictEqual( mesh.geometry.type, 'CylinderGeometry', 'Geometry type is CylinderGeometry' );
- assert.strictEqual( mesh.geometry.parameters.radiusTop, 1, 'radiusTop parameter' );
- assert.strictEqual( mesh.geometry.parameters.radiusBottom, 2, 'radiusBottom parameter' );
- assert.strictEqual( mesh.geometry.parameters.height, 5, 'height parameter' );
- } );
- QUnit.test( 'TorusGeometry via parameters', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'TorusGeometry',
- radius: 10,
- tube: 3,
- radialSegments: 16,
- tubularSegments: 100,
- arc: Math.PI * 2
- } ],
- 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, 'Parsed Mesh' );
- assert.strictEqual( mesh.geometry.type, 'TorusGeometry', 'Geometry type is TorusGeometry' );
- assert.strictEqual( mesh.geometry.parameters.radius, 10, 'radius parameter' );
- assert.strictEqual( mesh.geometry.parameters.tube, 3, 'tube parameter' );
- } );
- QUnit.test( 'Geometry name and userData', ( assert ) => {
- const json = {
- metadata: { version: 4.7, type: 'Object', generator: 'Object3D.toJSON' },
- geometries: [ {
- uuid: 'geom-1',
- type: 'BufferGeometry',
- name: 'MyGeometry',
- userData: { customProp: 'customValue' },
- 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 );
- assert.strictEqual( mesh.geometry.name, 'MyGeometry', 'Geometry name' );
- assert.strictEqual( mesh.geometry.userData.customProp, 'customValue', 'Geometry userData' );
- } );
- } );
- } );
|