pmndrs / drei-vanilla

🍦 drei-inspired helpers for threejs
https://pmndrs.github.io/drei-vanilla/
MIT License
448 stars 17 forks source link

Disabling PCSS Shadows Throws a TypeError on Meshes with Multiple Materials #50

Closed PeteMatterfield closed 8 months ago

PeteMatterfield commented 8 months ago

Problem description:

I have a scene that contains a Mesh with multiple materials. The mesh.child.material value is an array in these cases. Otherwise, it's just a three material type. The reset code when disabling PCSS shadows doesn't support the material array.

Relevant code:

// node_modules/@pmndrs/vanilla/core/pcss.js

function reset(gl, scene, camera) {
  scene.traverse((object) => {
    if (object.material) {
      gl.properties.remove(object.material);
      object.material.dispose();
    }
  });

Suggested solution:

I'm not much of a programmer, but:

function reset(gl, scene, camera) {
  scene.traverse(object => {
    if(object.material instanceof Array)
    {
      object.material.forEach(material => {
        gl.properties.remove(material);
        material.dispose();
      });
    }
    else if (object.material) {
      gl.properties.remove(object.material);
      object.material.dispose();
    }
  });