I’m struggling to cast a shadow from my glTF model onto a ground. I have a Hemispheric light and some shadowGenerator code but a shadow isn’t casting. I’m also unsure of where the shadow code should sit, at the moment at the bottom. Any ideas appreciated.
Cheers
var canvas = document.getElementById("renderCanvas");
var createScene = function () {
var scene = new BABYLON.Scene(engine);
//scene.clearColor = new BABYLON.Color3(2, 1, 1);
scene.ambientColor = new BABYLON.Color3(1, 1, 1);
//Adding a light
var light = new BABYLON.HemisphericLight();
//Adding an Arc Rotate Camera
var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 10, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, false);
// The first parameter can be used to specify which mesh to import. Here we import all meshes
BABYLON.SceneLoader.Append("assets/", "red-car3.gltf", scene, function (newMeshes) {
var mesh = scene.meshes[0];
mesh.position.y = 0.35;
mesh.position.z = -0.3;
mesh.rotation = new BABYLON.Vector3(0, Math.PI, 0);
//mesh.scaling = new BABYLON.Vector3(0.05, 0.05, 0.05);
scene.activeCamera = null;
scene.createDefaultCameraOrLight(true);
scene.activeCamera.attachControl(canvas, false);
});
//Add ground
var myGround = BABYLON.MeshBuilder.CreateGround("myGround", {width: 4, height: 4, subdivisions: 4}, scene);
// Colour the ground
var material = new BABYLON.StandardMaterial(scene);
material.alpha = .5;
material.diffuseColor = new BABYLON.Color3(0,0,0);
myGround.material = material;
return scene;
}
var engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
var scene = createScene();
engine.runRenderLoop(function () {
if (scene) {
scene.render();
}
});
//Cast Shadow
var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
shadowGenerator.getShadowMap().renderList.push(scene);
myGround.receiveShadows = true;
shadowGenerator.useExponentialShadowMap = true;
// Resize
window.addEventListener("resize", function () {
engine.resize();
});