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);}
labris
March 30, 2025, 7:10pm
2
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?
labris
March 30, 2025, 7:44pm
4
You need to create in shadowGenerator in your addpointlight function, then push meshes to renderList in your createbox function.
1 Like
labris
March 30, 2025, 7:46pm
5
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?
labris
March 31, 2025, 3:35pm
8
Yes, there is the simple check
* Helper function to add a mesh and its descendants to the list of shadow casters.
* @param mesh Mesh to add
* @param includeDescendants boolean indicating if the descendants should be added. Default to true
* @returns the Shadow Generator itself
*/
public addShadowCaster(mesh: AbstractMesh, includeDescendants = true): ShadowGenerator {
if (!this._shadowMap) {
return this;
}
if (!this._shadowMap.renderList) {
this._shadowMap.renderList = [];
}
if (this._shadowMap.renderList.indexOf(mesh) === -1) {
this._shadowMap.renderList.push(mesh);
}
if (includeDescendants) {
for (const childMesh of mesh.getChildMeshes()) {
if (this._shadowMap.renderList.indexOf(childMesh) === -1) {
1 Like