Shadows
Shadows add depth and realism to your scene. In Babylon.js, shadows are generated by a ShadowGenerator attached to a light source:
// Create a shadow generator attached to the directional light
const shadowGenerator = new BABYLON.ShadowGenerator(1024, dirLight);
// Add meshes that cast shadows
shadowGenerator.addShadowCaster(box);
shadowGenerator.addShadowCaster(sphere);
// Enable soft shadows
shadowGenerator.usePercentageCloserFiltering = true; // PCF soft shadows
// Or use: shadowGenerator.useBlurExponentialShadowMap = true;
// Enable shadow receiving on ground
ground.receiveShadows = true;
// Shadow quality settings
shadowGenerator.filteringQuality = BABYLON.ShadowGenerator.QUALITY_MEDIUM;
shadowGenerator.bias = 0.001; // Prevents shadow acne artifacts