Make objects cast/receive shadows

In my project I have functions to create objects and light, I don’t have direct access to objects. How can I make those objects cast shadows?

    createbox(args) {
      const box = BABYLON.MeshBuilder.CreateBox(args.name, { size: 1 }, scene);
      box.position.x = args.x;
      box.position.y = args.y;
      box.position.z = args.z;
      box.rotation.x = args.rx;
      box.rotation.y = args.ry;
      box.rotation.z = args.rz;
      box.scaling.x = args.sx;
      box.scaling.y = args.sy;
      box.scaling.z = args.sz;
      box.receiveShadows = true;
      box.castShadows = true;
      material.diffuseTexture = new BABYLON.Texture(textureDataURI, scene);
      if (args.staticdynamic == 1) {
        let boxAggregate = new BABYLON.PhysicsAggregate(box, BABYLON.PhysicsShapeType.BOX, { mass: 1, restitution:0}, scene);
      }
      else{
        var boxAggregate = new BABYLON.PhysicsAggregate(box, BABYLON.PhysicsShapeType.BOX, { mass: 0 }, scene);
      }
    }
    addpointlight(args){
      const pointLight = new BABYLON.PointLight(args.name, new BABYLON.Vector3(args.x,args.y, args.z), scene);}

I believe you have the list of args

const shadowGenerator = new BABYLON.ShadowGenerator(1024, scene.getLightByName(args.name));
shadowGenerator.getShadowMap().renderList.push(scene.getMeshByName(args.name));

(iterate the last part through the args list)

Args are not a list, they are just arguments. I don’t have any arrays here. Is there any way to just make it so that every object automatically casts shadows?

You need to create in shadowGenerator in your addpointlight function, then push meshes to renderList in your createbox function.

1 Like

Arrays will not do any harm, you may add each created mesh to some array for easy reference.

2 Likes

Great, I’m doing them now. Thanks!

1 Like

Actually, if I have an array for objects and a separate array for lights (shadowgenerators), how am I supposed to run in runtime. Do shadowgenerators check if they already made some object cast shadows?

Yes, there is the simple check

1 Like