Skip to content

Commit

Permalink
GLTFLoader: Allow multiple associations (#21737)
Browse files Browse the repository at this point in the history
* GLTFLoader: Allow multiple associations

* Tests: Add GLTFLoader associations unit test
  • Loading branch information
takahirox authored Sep 21, 2021
1 parent c5bb543 commit aca1c4f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
26 changes: 20 additions & 6 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2826,10 +2826,7 @@ class GLTFParser {
texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || RepeatWrapping;
texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || RepeatWrapping;

parser.associations.set( texture, {
type: 'textures',
index: textureIndex
} );
parser.associations.set( texture, { textures: textureIndex } );

return texture;

Expand Down Expand Up @@ -3169,7 +3166,7 @@ class GLTFParser {

assignExtrasToUserData( material, materialDef );

parser.associations.set( material, { type: 'materials', index: materialIndex } );
parser.associations.set( material, { materials: materialIndex } );

if ( materialDef.extensions ) addUnknownExtensionsToUserData( extensions, material, materialDef );

Expand Down Expand Up @@ -3382,6 +3379,15 @@ class GLTFParser {

}

for ( let i = 0, il = meshes.length; i < il; i ++ ) {

parser.associations.set( meshes[ i ], {
meshes: meshIndex,
primitives: i
} );

}

if ( meshes.length === 1 ) {

return meshes[ 0 ];
Expand All @@ -3390,6 +3396,8 @@ class GLTFParser {

const group = new Group();

parser.associations.set( group, { meshes: meshIndex } );

for ( let i = 0, il = meshes.length; i < il; i ++ ) {

group.add( meshes[ i ] );
Expand Down Expand Up @@ -3799,7 +3807,13 @@ class GLTFParser {

}

parser.associations.set( node, { type: 'nodes', index: nodeIndex } );
if ( ! parser.associations.has( node ) ) {

parser.associations.set( node, {} );

}

parser.associations.get( node ).nodes = nodeIndex;

return node;

Expand Down
56 changes: 56 additions & 0 deletions test/unit/example/loaders/GLTFLoader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { GLTFLoader } from '../../../../examples/jsm/loaders/GLTFLoader';
import { AnimationClip } from '../../../../src/animation/AnimationClip';
import { BufferAttribute } from '../../../../src/core/BufferAttribute';
import { BufferGeometry } from '../../../../src/core/BufferGeometry';
import { BoxBufferGeometry } from '../../../../src/geometries/BoxGeometry';
import { Mesh } from '../../../../src/objects/Mesh';
import { MeshStandardMaterial } from '../../../../src/materials/MeshStandardMaterial';
import { Object3D } from '../../../../src/core/Object3D';
import { Scene } from '../../../../src/scenes/Scene';
import { DataTexture } from '../../../../src/textures/DataTexture';
import { VectorKeyframeTrack } from '../../../../src/animation/tracks/VectorKeyframeTrack';

export default QUnit.module( 'Loaders', () => {
Expand Down Expand Up @@ -113,6 +115,60 @@ export default QUnit.module( 'Loaders', () => {

} );

QUnit.test( 'parser - associations', ( assert ) => {

var done = assert.async();

var scene = new Scene();
scene.add( new Mesh(
new BoxBufferGeometry(),
new MeshStandardMaterial( { map: new DataTexture( new Uint8ClampedArray( [ 0, 0, 0, 0 ] ), 1, 1 ) } )
) );

var exporter = new GLTFExporter();
var loader = new GLTFLoader();

exporter.parse( scene, function ( binary ) {

loader.parse( binary, './', function ( gltf ) {

var parser = gltf.parser;
var associations = parser.associations;

gltf.scene.traverse( function ( object ) {

if ( object.isMesh ) {

assert.smartEqual( associations.get( object ), {
meshes: 0,
nodes: 0,
primitives: 0
}, 'Mesh has a proper association' );

assert.smartEqual( associations.get( object.material ), {
materials: 0
}, 'Material has a proper association' );

assert.smartEqual( associations.get( object.material.map ), {
textures: 0
}, 'Texture has a proper association' );

}

} );

done();

}, undefined, function ( e ) {

console.error( e );

} );

}, { binary: true } );

} );

} );

} );

0 comments on commit aca1c4f

Please sign in to comment.