Raycasting meshes with materialArray

Hi I noticed that if I have a mesh which can be intersected by raycaster and if I have an material array, which one of is null- while raycasting i receive Error: three.module.js:11410 Uncaught TypeError: Cannot read property ‘morphTargets’ of null. But If I have a mesh with material array with nulls, but this mesh is not a part of raycasting it works well. Should I understand this- if I have a mesh and it would be affected by raycasting- none of its material can be null?
I am asking this because sometimes I do not need to apply material to the bottom side of box- it would not be visible anyway for a user- and for better performacne I usually put null.
Example: Box that has no bottom material.

const materials = [new MeshBasicMaterial(),new MeshBasicMaterial(),,new MeshBasicMaterial(),null,new MeshBasicMaterial(),new MeshBasicMaterial()]
const mesh = new THREE.Mesh(g,materials);

Using null like in your use case actually not allowed. Please apply a material and then set its visible property to false. With this setup, you achieve the same result without affecting the performance. And the raycasting issue should be fixed.

1 Like

Thank you- I had no idea- once upon a time i stumbled across this technique as more performant. But i guess it was not as good as i expected it to be;).

Oh setting visible: false on material causes the same error;/

Can you please demonstrate the issue with a live example?

https://jsfiddle.net/f2Lommf5/

Issue appears when I am doing it via react-three-fiber with pure three js there is no error;(

@drcmda Any ideas what might cause this issue?

would also need a codesandbox or something to dig in. @celine3d you have one?