On switching from InstancedMeshes to Cloned Meshes the 3D Model Disappears

I am working on optimizing my rendering. There are a lot of end-nodes in the scene that are exact clones of the master node with the only change being the position so I am using InstancedMeshes for those cases.

The issue I am having is when the user clicks on the end node which is identified from scene.pick(…), I dispose of the instancedmeshes and clone from the source so I can add it to the HighlightLayer.

When I do that the updated meshes do not appear on the scene. I am not sure why the meshes are not appearing. Here is the code section:

if (this.usingInstanceMeshes === true) {
  const scene = this.parent.getScene();
  const networkMesh = this.parent.parent;
  this.parent.dispose(); // childMeshes are InstancedMeshes
  this.parent = new Mesh(this.type, scene, networkMesh);
  this.meshes = RENDERER_ASSETS_SERVICE.createClonedMeshes(this, scene);
  this.usingInstanceMeshes = false;
}
if (this.highlightColor) {
  this.meshes.forEach(entry => {
    HIGHLIGHT_LAYER.removeMesh(entry);
  });
}
this.meshes.forEach(entry => {
  HIGHLIGHT_LAYER.addMesh(entry, SELECTED_COLOR);
});

where createClonedMeshes definition is

createClonedMeshes(rnode: RenderNode, scene: Scene): Array<Mesh> {
  const meshes = [];
  let sources: Array<Mesh> = this.nodeMeshesMap.get(rnode.type);
  if (!sources) {
    sources = this.nodeMeshesMap.get("?"); // unknown model
  }
  for (let ii = 0; ii < sources.length; ii += 1) {
    const source = sources[ii];
    const meshName = `${rnode.type}:${source.id}`;
    const mesh = new Mesh(meshName, scene, rnode.parent, source);
    mesh.material = source.material.clone(`mat-${rnode.label}-${ii}`);
  }
  let boundingInfo: BoundingInfo = null;
  let localCenter: Vector3 = null;
  if (!this.nodeBoundingInfo.has(rnode.type)) {
    boundingInfo = this.getBoundingInfoOfParentMeshInLocal(rnode.parent);
    this.nodeBoundingInfo.set(rnode.type, boundingInfo);
    localCenter = boundingInfo.boundingBox.center;
    this.nodeLocalCenter.set(rnode.type, localCenter);
  } else {
    boundingInfo = this.nodeBoundingInfo.get(rnode.type);
    localCenter = this.nodeLocalCenter.get(rnode.type);
  }
  rnode.parent.setBoundingInfo(boundingInfo);
  rnode.parent.getChildMeshes().forEach(mesh => {
   mesh.position.x = -localCenter.x; // translate to center of local space
   mesh.position.y = -localCenter.y;
   mesh.position.z = -localCenter.z;
   mesh.setPivotPoint(localCenter); // rotate about center
   mesh.rotation.x = Math.PI / 8;
   mesh.rotation.y = Math.PI;
  });
  return meshes;
}

Is there any insight someone can provide to look at why the object is not appearing? I am not seeing it.

Thanks,

Aaron

Do you mind providing a repro in the PG?

Thanks for looking at the question. The issue was staring me in the face. I forgot to push the new mesh on my mesh array.

2 Likes