Hello, I have an error in my code and I can’t find it. Whenever I add objects, then add a point light, and then use shadow();
scene only gets a bit darker. Any help would be appreciated
createbox(args) {
const box = BABYLON.MeshBuilder.CreateBox(args.name, { size: 1 }, scene);
let material = new BABYLON.StandardMaterial("boxMaterial", 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;
const textureDataURI = args.texture;
material.diffuseTexture = new BABYLON.Texture(textureDataURI, scene);
box.material = material;
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);
}
objects.push(box);
}
addpointlight(args) {
const pointLight = new BABYLON.PointLight(args.name, new BABYLON.Vector3(args.x, args.y, args.z), scene);
const shadowGenerator = new BABYLON.ShadowGenerator(1024, pointLight);
shadowGenerator.useBlurExponentialShadowMap = true;
shadowGenerator.useKernelBlur = true;
shadowGenerator.blurKernel = 64;
shadowGenerator.bias = 0.01;
shadowGenerator.normalBias = 0.02;
lights.push(pointLight);
shadowGenerators.push(shadowGenerator);
}
shadow() {
objects.forEach(object => {
object.receiveShadows = true;
object.isPickable = false;
shadowGenerators.forEach(shadowGenerator => {
shadowGenerator.addShadowCaster(object);
shadowGenerator.getShadowMap().renderList.push(object);
});
});
}
Hello
It’s unlikely that you need to have mesh.receiveShadows = true;
on all your meshes, as well as adding all of them as shadow caster calling shadowGenerator.addShadowCaster(mesh);
Except for specific self shadowing structures, somes meshes cast shadows, some others receive it :
2 Likes
Thanks for the response!
Is there a way so that all objects receive and cast shadows at the same time?
There are some ways of having meshes being both a shadow caster and a shadow receiver, but setting up the params needs extra care.
I would advice you to have a close look to the DOC about Self Shadowing
1 Like
I can’t seem to fix my code…
addpointlight(args) {
const light = new BABYLON.PointLight(args.name, new BABYLON.Vector3(args.x, args.y, args.z), scene);
light.range = 100;
let shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
shadowGenerator.bias = 0.001;
shadowGenerator.normalBias = 0.02;
light.shadowMaxZ = 100;
light.shadowMinZ = 10;
shadowGenerator.useContactHardeningShadow = true;
shadowGenerator.contactHardeningLightSizeUVRatio = 0.05;
shadowGenerator.setDarkness(0.5);
lights.push(light);
shadowGenerators.push(shadowGenerator);
}
shadow() {
for(let i=0;i<shadowGenerators.length;i++){
const shadowGenerator = shadowGenerators[i];
for(let j=0;j<objects.length;j++){
const object = objects[j];
shadowGenerator.addShadowCaster(object);
}
}
console.log(shadowGenerators)
}
@Tricotou
here is my code now, wonder why it still doesn’t work…
Pryme8
April 1, 2025, 3:57pm
7
what’s your lights position? I see you have minZ and maxZ maybe try auto calculating the extends and see if that fixes it?
It is far enough from objects, but close enough to have shadows.
Edit: Changed minZ to 0.01, shadows are now being casted, but not sure if that’s how they are supposed to be casted.
Pryme8
April 1, 2025, 4:04pm
10
I’m assuming its the lights position then. Sounds like there is some depth stuff going on. Try using the autoCalcShadowZBounds and see if that will allow you to just move on.
So, thing is stuff works, but shadowGenerator.useBlurExponentialShadowMap = true;
, won’t work.
Pryme8
April 1, 2025, 4:19pm
12
Seems to be working fine. Id assume its your light setup.
Yeah, I’m losing it, everything looks fine, shadows are just not right. They are not smoothed.
addpointlight(args) {
const light = new BABYLON.PointLight(args.name, new BABYLON.Vector3(args.x, args.y, args.z), scene);
light.shadowMaxZ = 100;
light.shadowMinZ = 0.001;
const shadowGen = new BABYLON.ShadowGenerator(1024, light)
shadowGen.useBlurCloseExponentialShadowMap = true
shadowGen.bias = 0.001;
shadowGen.normalBias = 0.001
shadowGen.useKernelBlur = true;
shadowGen.blurKernel = 64;
shadowGen.setDarkness(0.5);
lights.push(light);
shadowGenerators.push(shadowGen);
}
shadow() {
for(let i=0;i<shadowGenerators.length;i++){
const shadowGenerator = shadowGenerators[i];
for(let j=0;j<objects.length;j++){
const object = objects[j];
shadowGenerator.addShadowCaster(object);
shadowGenerator.getShadowMap().renderList.push(object);
}
}
console.log(shadowGenerators)
}
Pryme8
April 2, 2025, 4:59pm
14
Looks like you don’t have aliasing going on in your scene. What rendering pipeline are you using? Did you disable aliasing when spinning up the engine?
Also maybe spin up the inspector and start making adjustments to the values through there till you find something that works?
const canvas = document.createElement("canvas");
canvas.id = "renderCanvas";
canvas.style.width = "100%";
canvas.style.height = "100%";
canvas.style.position = "absolute";
canvas.style.top = "0";
canvas.style.left = "0";
const engine = new BABYLON.Engine(canvas);
let scene = new BABYLON.Scene(engine);
let camera = new BABYLON.UniversalCamera("camera", new BABYLON.Vector3(0, 5, -10),
scene);
camera.attachControl(canvas, true);
no pipeline stuff
Point is (if this is reproduction of the bug), it doesn’t happen in the playground for some reason, but I’ll be happy to do it.
the issue you’re having. Can you repro it in the Playground? (unless I missed a link?)