Jump to content

Can imported meshes cast shadows?


Dinkelborg
 Share

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

Link to comment
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

Link to comment
Share on other sites

  • 2 weeks later...
  • 7 months later...

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;
        }
    
    });
});

 

Link to comment
Share on other sites

  • 2 years later...

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!

Link to comment
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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...