Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTFLoader: Allow multiple associations #21737

Merged
merged 2 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2614,10 +2614,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 } );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I might change textures to texture and make the rest singular too, since we're only giving a single index.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the reason why they are plural is to easily access json data because they are plural in glTF json.

// The existing associations
const def = json[association.type][association.index];

// The new associations
for (const key in associations) {
  const def = json[key][associations[key]];
}


return texture;

Expand Down Expand Up @@ -2956,7 +2953,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 @@ -3169,6 +3166,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 @@ -3177,6 +3183,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 @@ -3584,7 +3592,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 } );

} );

} );

} );