Skip to content

InstancedMesh's setMatrixAt fails when another mesh in the scene has the same material #17701

@GuilhermeRossato

Description

@GuilhermeRossato
Description of the problem

I'm having a problem where the InstancedMesh method setMatrixAt seems to fail (the matrix positions doesn't change visually) whenever I add another object with the same material object (not a clone) in the scene. No log/warning/errors are emitted.

The following briefly illustrates:

    var geometry = new THREE.BoxBufferGeometry();
    var material = new THREE.MeshStandardMaterial( { roughness: 0, envMap: texture });
    var mesh1 = new THREE.Mesh( geometry, material );
    mesh1.position.set(0, 1, 0);
    scene.add( mesh1 ); // removing this line makes the instanced mesh work as intended
    mesh = new THREE.InstancedMesh( geometry, material, 9 ); // same material usage
    scene.add( mesh ); // This works and I can see both meshes in the scene.
    
    var dummy = new THREE.Object3D();
    dummy.position.set( 1, 5, 1 );
    dummy.updateMatrix();
    mesh.setMatrixAt( 0, dummy.matrix ); // Nope, doesn't work, it just stays where it's at.
    mesh.setMatrixAt( 1, dummy.matrix ); // Rotation seems to be applied, but position doesn't.
    // It looks like every instance is at the same place, but I don't know.

Note that if I use material.clone() at the instanced mesh declaration, the problem also goes away / is fixed, but now I have two materials, and I intend to have one for performance reasons.

A live example is available here:

  • jsfiddle (The problem is that you should see multiple boxes, but only 1 or 2 are visible, remove line 41 to fix)
Three.js version
  • Dev
  • r109
  • r108
Browser
  • All of them
  • Chrome
  • Firefox
  • Internet Explorer
OS
  • All of them
  • Windows
  • macOS
  • Linux
  • Android
  • iOS
Hardware Requirements (graphics card, VR Device, ...)

(Not applicable, as far as I know)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions