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

LDrawLoader: Use WeakMap instead of userData for indexing materials. #26700

Merged
merged 1 commit into from
Sep 5, 2023
Merged
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
38 changes: 22 additions & 16 deletions examples/jsm/loaders/LDrawLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1520,19 +1520,19 @@ class LDrawPartsGeometryCache {
const group = info.group;
if ( info.faces.length > 0 ) {

group.add( createObject( info.faces, 3, false, info.totalFaces ) );
group.add( createObject( this.loader, info.faces, 3, false, info.totalFaces ) );

}

if ( info.lineSegments.length > 0 ) {

group.add( createObject( info.lineSegments, 2 ) );
group.add( createObject( this.loader, info.lineSegments, 2 ) );

}

if ( info.conditionalSegments.length > 0 ) {

group.add( createObject( info.conditionalSegments, 2, true ) );
group.add( createObject( this.loader, info.conditionalSegments, 2, true ) );

}

Expand Down Expand Up @@ -1640,7 +1640,7 @@ function sortByMaterial( a, b ) {

}

function createObject( elements, elementSize, isConditionalSegments = false, totalElements = null ) {
function createObject( loader, elements, elementSize, isConditionalSegments = false, totalElements = null ) {

// Creates a LineSegments (elementSize = 2) or a Mesh (elementSize = 3 )
// With per face / segment material, implemented with mesh groups and materials array
Expand Down Expand Up @@ -1759,11 +1759,13 @@ function createObject( elements, elementSize, isConditionalSegments = false, tot

if ( isConditionalSegments ) {

materials.push( material.userData.edgeMaterial.userData.conditionalEdgeMaterial );
const edgeMaterial = loader.edgeMaterialCache.get( material );

materials.push( loader.conditionalEdgeMaterialCache.get( edgeMaterial ) );

} else {

materials.push( material.userData.edgeMaterial );
materials.push( loader.edgeMaterialCache.get( material ) );

}

Expand Down Expand Up @@ -1886,6 +1888,8 @@ class LDrawLoader extends Loader {
// Array of THREE.Material
this.materials = [];
this.materialLibrary = {};
this.edgeMaterialCache = new WeakMap();
this.conditionalEdgeMaterialCache = new WeakMap();

// This also allows to handle the embedded text files ("0 FILE" lines)
this.partsCache = new LDrawPartsGeometryCache( this );
Expand All @@ -1906,8 +1910,8 @@ class LDrawLoader extends Loader {
this.missingColorMaterial = new MeshStandardMaterial( { name: Loader.DEFAULT_MATERIAL_NAME, color: 0xFF00FF, roughness: 0.3, metalness: 0 } );
this.missingEdgeColorMaterial = new LineBasicMaterial( { name: Loader.DEFAULT_MATERIAL_NAME, color: 0xFF00FF } );
this.missingConditionalEdgeColorMaterial = new LDrawConditionalLineMaterial( { name: Loader.DEFAULT_MATERIAL_NAME, fog: true, color: 0xFF00FF } );
this.missingColorMaterial.userData.edgeMaterial = this.missingEdgeColorMaterial;
this.missingEdgeColorMaterial.userData.conditionalEdgeMaterial = this.missingConditionalEdgeColorMaterial;
this.edgeMaterialCache.set( this.missingColorMaterial, this.missingEdgeColorMaterial );
this.conditionalEdgeMaterialCache.set( this.missingEdgeColorMaterial, this.missingConditionalEdgeColorMaterial );

}

Expand Down Expand Up @@ -2126,11 +2130,11 @@ class LDrawLoader extends Loader {

if ( c.isLineSegments ) {

material = material.userData.edgeMaterial;
material = loader.edgeMaterialCache.get( material );

if ( c.isConditionalLine ) {

material = material.userData.conditionalEdgeMaterial;
material = loader.conditionalEdgeMaterialCache.get( material );

}

Expand All @@ -2151,7 +2155,7 @@ class LDrawLoader extends Loader {
getMainEdgeMaterial() {

const mat = this.getMaterial( MAIN_EDGE_COLOUR_CODE );
return mat ? mat.userData.edgeMaterial : null;
return mat ? this.edgeMaterialCache.get( mat ) : null;

}

Expand Down Expand Up @@ -2236,7 +2240,7 @@ class LDrawLoader extends Loader {
}

// Get the edge material for this triangle material
edgeMaterial = edgeMaterial.userData.edgeMaterial;
edgeMaterial = this.edgeMaterialCache.get( edgeMaterial );

}

Expand Down Expand Up @@ -2380,7 +2384,7 @@ class LDrawLoader extends Loader {
edgeMaterial.name = name + ' - Edge';

// This is the material used for conditional edges
edgeMaterial.userData.conditionalEdgeMaterial = new LDrawConditionalLineMaterial( {
const conditionalEdgeMaterial = new LDrawConditionalLineMaterial( {

fog: true,
transparent: isTransparent,
Expand All @@ -2389,15 +2393,17 @@ class LDrawLoader extends Loader {
opacity: alpha,

} );
edgeMaterial.userData.conditionalEdgeMaterial.userData.code = code;
edgeMaterial.userData.conditionalEdgeMaterial.name = name + ' - Conditional Edge';
conditionalEdgeMaterial.userData.code = code;
conditionalEdgeMaterial.name = name + ' - Conditional Edge';

this.conditionalEdgeMaterialCache.set( edgeMaterial, conditionalEdgeMaterial );

}

material.userData.code = code;
material.name = name;

material.userData.edgeMaterial = edgeMaterial;
this.edgeMaterialCache.set( material, edgeMaterial );

this.addMaterial( material );

Expand Down