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

Material Index not matched #662

Closed
dongho-shin opened this issue Jul 22, 2024 · 5 comments
Closed

Material Index not matched #662

dongho-shin opened this issue Jul 22, 2024 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@dongho-shin
Copy link
Contributor

dongho-shin commented Jul 22, 2024

Describe the bug

Untitled (16)

this model has all the same Materials (8 mesh but share 1 material, share 1 material(same uuid) is a key point of this bug)

  1. PathTrace Rendering first after import this model
  2. change only material of mesh
image
  1. PathTrace render again
image

Model

FYI) zip from mac

Blender_Smeg (2).glb.zip

Platform:

  • Device: Desktop
  • OS: MacOS
  • Browser: Chrome
  • Three.js version: r162
  • Library version: 0.0.23

This bug causes here

const materialIndex = allMaterials.indexOf( mat );

sorry for ugly notes

indexOf, if all meshes share 1 materials(in this notes a) geometry.materialIndex attributes are all 0
Screenshot_20240722_155500_Samsung Notes
and then material of mesh has changed(a -> b)
Screenshot_20240722_155534_Samsung Notes

StaticGeometryGenerator, bvhChanged => no changes(because in this scenario no geometry changes) so materialIndex not updated, geometry reuse materialIndex 0 so nothing has been updated

so my sugestion is
before

		const mat = Array.isArray( materials ) ? materials[ group.materialIndex ] : materials;
		const materialIndex = allMaterials.indexOf( mat );

after

	//	const mat = Array.isArray( materials ) ? materials[ group.materialIndex ] : materials; // not used
		const materialIndex = group.materialIndex;

always welcome any ideas & questions

@dongho-shin dongho-shin added the bug Something isn't working label Jul 22, 2024
@gkjohnson
Copy link
Owner

Thanks for reporting and digging in as always!

I suspect this is because the geometry has not changed, causing the static geometry generator to return "NO_CHANGE" meaning the material index buffer attribute is not changed - see here.

I think the easy fix would be the following:

// change this
if ( result.changeType !== NO_CHANGE ) {

	updateMaterialIndexAttribute( geometry, materials, materials );

}

// to this
updateMaterialIndexAttribute( geometry, materials, materials );

So the material index is always updated. Would you be able to test that?

A more optimal change would be to check if any of the materials have changed before regenerating. But that's a bit more complicated.

@gkjohnson gkjohnson added this to the v0.0.24 milestone Jul 22, 2024
@dongho-shin
Copy link
Contributor Author

dongho-shin commented Jul 23, 2024

first try that you suggest me, it fails so I tried workaround weird way(use group.materialIndex)
I found I miss this part

if ( bvhChanged ) {
material.bvh.updateFrom( bvh );
material.attributesArray.updateFrom(
geometry.attributes.normal,
geometry.attributes.tangent,
geometry.attributes.uv,
geometry.attributes.color,
);
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
}

your suggestion + change of code below fix this issue

//previous
 if ( bvhChanged ) { 
  
 	material.bvh.updateFrom( bvh ); 
 	material.attributesArray.updateFrom( 
 		geometry.attributes.normal, 
 		geometry.attributes.tangent, 
 		geometry.attributes.uv, 
 		geometry.attributes.color, 
 	); 
  
 	material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex ); 
  
 } 
 //after
  if ( bvhChanged ) { 
  
 	material.bvh.updateFrom( bvh ); 
 	material.attributesArray.updateFrom( 
 		geometry.attributes.normal, 
 		geometry.attributes.tangent, 
 		geometry.attributes.uv, 
 		geometry.attributes.color, 
 	); 
    
 }
 //No matter of bvhChanged
 material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );  

if you agree this fixed i'll open pr, always thanks for feedback @gkjohnson

@gkjohnson
Copy link
Owner

Good catch - a PR would be great.

It could be improved further if the material list information were saved as a hash to check if the list of materials had actually been updated or not - but I'l leave it up to you if you want to look into that.

@dongho-shin
Copy link
Contributor Author

I'll open a PR this week fixing this issue in this way. if hash improvement of material is not urgent I'll look into it

@gkjohnson
Copy link
Owner

Fixed in #664

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants