Dinkelborg

Can imported meshes cast shadows?

Recommended Posts

Hi, has been a while ;)

 

however I started a new project using BabylonJS,

where I'm importing a model from Blender into a scene,

now for some reason I cannot get it to cast a shadow.

 

I added a cube next to it and it does cast a shadow, 
I'm also adding both the cube and the model to the renderList of the shadow generator..

Any ideas or suggestions what I could try or why it wouldn't work?

post-11235-0-64082200-1440583400.png

Share this post


Link to post
Share on other sites
var spot = new BABYLON.SpotLight("Lamp",BABYLON.Vector3.zero,BABYLON.Vector3.zero,2.5,8,scene);        spot.intensity = 1.75;        spot.diffuse = new BABYLON.Color3(0.9, 0.8, 0.8);        spot.angle = 5;        spot.exponent = 8;        spot.position = new BABYLON.Vector3(-17.6, 18.8, -49.9);        spot.setDirectionToTarget(new BABYLON.Vector3(0, 0, 0));

The Building is about 3 unity high as the cube floating next to it is at position.y = 3 

post-11235-0-73772000-1440592435.png

Share this post


Link to post
Share on other sites

I tried with a directional light and with a directional light I cannot cast any shadow at all, however when all mesh parts are kept separate, I can cast shadows with a spot light, but not with everything combined to one mesh

Share this post


Link to post
Share on other sites

Hey,

Could you post the part of your code where you push the mesh (or meshes) into the shadowMaps renderList?

Mostly dealing with .babylon imports - but I usually do this inside the callback of e.g. my SceneLoader like this:

BABYLON.SceneLoader.Load(modelPath, modelName, engine, (scene) => {
    // Wait for textures and shaders to be ready
    scene.executeWhenReady( () => {
        
        for (var i = 0; i < scene.meshes.length; i++) {
            let mesh = scene.meshes[i];

            shadowGenerator.getShadowMap().renderList.push(mesh);
            mesh.receiveShadows = true;
        }
    
    });
});

 

Share this post


Link to post
Share on other sites

Hiya ariespranata, welcome to the forum.

    Imported meshes make great BabylonJS shadows... but sometimes it takes a little adjusting.

http://www.babylonjs-playground.com/#JUKXQD#19

For a moment, let's look at the #17 playground... lines 25-32:

BABYLON.SceneLoader.ImportMesh("", "scenes/", "skull.babylon", scene, function (newMeshes) {
     // Set the target of the camera to the first imported mesh
     camera.target = newMeshes[0];
     shadowGenerator.getShadowMap().renderList.push(newMeshes[0]);
     //shadowGenerator.addShadowCaster(mesh);
     newMeshes[0].receiveShadows = true;
     // add stuff here
     // add stuff here

     // add stuff here
});

This red area... is called the loader "onSuccess" callback function.  Now look at the #19 playground.

BABYLON.SceneLoader.ImportMesh("", "scenes/", "skull.babylon", scene, function (newMeshes) {
     // Set the target of the camera to the first imported mesh
     camera.target = newMeshes[0];
     var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
     shadowGenerator.useBlurExponentialShadowMap = true;
     shadowGenerator.useKernelBlur = true;
     shadowGenerator.blurKernel = 64;
     shadowGenerator.forceBackFacesOnly = true;
     shadowGenerator.getShadowMap().refreshRate = BABYLON.RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
     shadowGenerator.getShadowMap().renderList.push(newMeshes[0]);

     var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 20, scene);
     sphere.position.x = 50;
     sphere.position.z = 50;

     shadowGenerator.getShadowMap().renderList.push(sphere);
     newMeshes[0].receiveShadows = true;
});

Notice how I placed MANY things that happened AFTER the onSuccess callback... INTO the callback function "block".  When importing mesh using the sceneLoader, MOST of the JS code needs to happen INSIDE the callback function, and not after it. 

Later, you may wish to use the more-modern BabylonJS AssetsManager... to do your mesh loading.  It is sometimes easier to understand and more efficient than sceneLoader.

I hope I have been helpful.  For fun, I tried to spin the spotlight around the skull, while keeping it aimed at the skull.  http://www.babylonjs-playground.com/#JUKXQD#22  Fun!

Share this post


Link to post
Share on other sites

Thanks, Wingnut! you're a lifesaver. 😁

This is my first time using BabylonJS, I'll surely explore the AssetsManager.

Btw, just in case anyone else having the same problem, my actual imported mesh is consists of many mesh(es). It was exported from 3ds Max as a group. Thus, instead of 

shadowGenerator.getShadowMap().renderList.push(newMeshes[0]);

we should use:

newMeshes.forEach(function (mesh) {
    shadowGenerator.getShadowMap().renderList.push(mesh);
});

at first I thought BabylonJS would recognize my 'group' as 1 mesh, but apparently that's not how it's working.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.