0
votes

I want to visit all element in a scene. These elements are object of type: THREE.Object3D, THREE.Mesh, and so on. Some elements have a field called children, and this is an array of other elements. So we can conclude that the scene ( also it has the children field ) is a tree.

With this knowledge, I want to perform a depth first search visit, to set some attribute on, for each element present in the scene object. This is my code:

function setShadowRenderer( element ) {

   element.receiveShadow = true;
   element.castShadow = true;

   if ( element.children !== undefined ||
      element.children !== [] )
      for ( i = 0; i < element.children.length; i++ )
        setShadowRenderer( element.children[ i ] );

    console.log( C++ );
}

Now, this code lead to an infinite computation, it never ends. How is this possible?

1

1 Answers

0
votes

I think this will do the job you want:

if (object instanceof THREE.Object3D)
{
    object.traverse (function (mesh)
    {
        if (! (mesh instanceof THREE.Mesh)) return;

        mesh.castShadow = true;
        mesh.receiveShadow = true;

    });
}