Shadow for an whole building

Hello guys,
at the moment im struggeling with the shadow in Babylonjs. I got an building but without any shadow is, there an fast way to get in every room shadow?

I use two light souce the HemisphericLight and the Directionallight for the shadow.


at the end of the Import i Use this mesh[0].receiveShadows = true;

I cant create an playground but i got this on an live Server:
Babylon Test (rundgang-fb3.de)

I hope u can help me

Hi,
It’s not easy to determine just exactly what is wrong without a PG.
I believe using the directional light to cast your shadows is a good choice.
I believe casting it from underground (I do this all the time) is also ok.
The piece I am missing here with this scenario is the minZ and maxZ.
May be you also don’t want to put the z direction of light at zero (may be slightly displaced from -2 to 2)
I believe I can understand that mesh[0] is your ground, is it?
Or is it your building? Did you make sure that your reference to mesh[0] is not actually the transform node created on import. By any means, once you are sure you reference correctly all your meshes for casting and the mesh/receiver of your shadows, it should work. Providing your minZ accounts the entire building size and your maxZ is at or beyond ground/receiver mesh.
Edit: Just realized I also can’t see the position of the light (light2). If it’s not declared or at zero this might just be the source of your issue.

Edit1: May be if you can do a lighter (dummy) version of your building (with just the walls and ground(s)), then may be I can fix it for you. But this version is just too complex (sry). And then if you have multiple floors, it will likely not work just like this for all floors.

Your shadowGenerator is inside the for loop. There should only be one shadowGenerator associated with our directional light. Can you try to instantiate your shadowGenerator outside the loop? And then add shadow casters to the same shadowGenerator instance.

There are some shadows in your scene, but seems they are outside the building.

1 Like

Mesh[0] is the building but i want shadows inside of the Building. so i need to Add the Shadows to every child of the building with mesh[0].getChildMeshes()[i]

here is an Playground:

Babylon.js Playground (babylonjs-playground.com)

wierd i dont get the Shadow

Hi,
I am fixing (parts of your issue) just now.
Know already that there’s a good a reason why you do not get any sort of shadow.
Currently your code does not pick through the child meshes and your meshes do not cast neither receive shadows (you can check this with the inspector).
Second, as pointed by @slin you are actually (attempting to) create a shadow generator for each mesh by placing it in the ‘for’ (each) loop. You certainly don’t want that and lucky for you, your code also didn’t enable this :wink: Luckily, because there’s no chance this would work.
Already take a look at this PG that (for now) relies on a promess before creating a shadow generator and run through all child meshes to set shadow casting and receiving.
Next, I just need to adjust the light and shadow but it looks like your building is just at a tiny/mini scale (like a building for ants :wink:). Let me check on that and send you an update shortly.

Ok, so it’s what I thought (nearly). Not an ants building but a 5-stars building for bees :honeybee: :wink:.
The 3 floors hold in a box of height 10. This means your values for setting the light (position and more importantly direction) need to account this scaling. You can already remove a decimal or 2 to the direction.
Next thing, I would need to understand is what sort of shadow do you want from this generator. Is it from the sun or for interior lighting? For interior lighting, I can recommend you would rather bake your lights/shadows. It will have you master your lighting much better and have MUCH lower impact on performance. Already now, you should start merging your meshes of exterior and interior walls and other structural parts. The scene is already very slow. Too many meshes and too many transform nodes for which I do not just know exactly what they are here for. I’ll send you a new PG casting shadows from the sun shortly.

I would link to have an shadow from the sun.the Most meshes just for the building I try to merch them but when i do it the whole building got damaged ( i got the building from an friend).

You can merge them inside babylon and it shouldn’t affect anything from your UVs and materials.
By default, it will create a multimat and keep everything the same.
Just go like:

const mygroup = BABYLON.Mesh.MergeMeshes([mesh0, mesh1, mesh2], true, true, undefined, false, true);

And try to make merges of all parts that need to hold together.
Create one for exterior walls, one for interior walls and one for each structural element.
This should help already.
Do this after the promess (after the object and all submeshes are loaded).

OK, so quickly added another camera because (sry) couldn’t see what I’m doing with yours.
It allowed me to understand that you also have a basement level.
First thing I would do here is:

  1. Make the ground a ground. Isolate it from other meshes/groups, call it ‘ground’ and let it be at zero origin.
  2. Make all your floors level grounds to ground (ground0, ground1, ground2
), isolate them from other meshes and groups.
    Grounds will receive shadows casted by the sun and other structural parts.
    Walls and structural parts will also receive and cast shadows from the sun.
    For interior lighting, as I said, (ideally) walls and structural parts should have baked lighting.
    https://www.babylonjs-playground.com/#PKX4YW#5

Edit: Once this is done, I can help you set shadows from the sun, but as of just know I don’t even know what the ground is (and I don’t want to browse through all meshes to find it). I can already say the ground is not at zero on y (which doesn’t make it easier to set shadows).

I tried to merch the Ground, but it dint worked or it is still solo in the inspector.

I called the Floor Ground_UG, Ground_EG, Ground_1OG,Ground_2OG.
Is it possible to merge them through an For While?

Babylon.js Playground (babylonjs-playground.com)

I still can’t find my external ground. The ground that’s outside the building. What’s the name of it?
I speak german. Although the forum is in english, you can add a small text in german when you reply to my posts if you are more confortable with it. First thing I need, is to have the ground (where the building sits) for external shadows. Next, you do not need (and preferably don’t want to) merge your floors/levels.
I just need to be able to identify them. Thanks,

Edit: Just found it. It’s ‘BodenAussen’.

So now I get the the merch different floor element.

The ground from the outside is called BodenAußen.
And the floors name are Ground_UG, Ground_EG, Ground_1OG,Ground_2OG

Babylon.js Playground (babylonjs-playground.com)

Yes, got it. But you don’t need to merge your floors (better not) and your code for doing this is wrong by any means. I hope you’re not in a hurry. It’s soon time for me to make dinner but I will try send you an update ASAP (if not tonight then tomorrow morning, if that’s ok with you?)

ok yeah im bit stressed i need it to friday but other wise i dont got the shadow.

ok Thank you very much :smiley:

I believe we can make it so that by then (thursday at the latest), you will also have your shadows :smiley:

1 Like

Meanwhile, I would have a task for you.
It looks like everything in your scene is a mesh. I have dozen of doors meshes and it looks like they are mostly all the same. I suggest you take a look at the docs for how to create instances
Ideally, all of your windows, doors, structural elements that are the same should be instances.
It will remove a LOT of load and make the scene A LOT BETTER in terms of performance and user experience.
Why don’t you take a look at this while I will take some time to bring shadows to your scene? Deal?

I take an look on the instances but i cant create per Babylon an incstance of the window and the door because when i want to do it i need the cordinate of the original place.

So i think in this case it is the better to create the instance via 3D programm.
Or im wrong wit the cordinate part?

German:
Ich habe mir die instances angeschaut und habe festgestellt, dass man die Fenster sowie TĂŒren aus dem 3D Objekt löschen mĂŒsste um eine Instance zu erstellen. Diese Instance mĂŒsste dann an den selben Stellen wieder hinzugefĂŒgt werden, aber dafĂŒr brĂ€uchte ich immer die Kordinaten was ja recht aufwĂ€ndig ist. WĂ€re es ĂŒber ein 3D Programm dann nicht einfacher?

Yes/Klar. Are you using Blender?
Wenn Ja, dann bitte schau dich dies an Instances | Babylon.js Documentation
Easiest way to replace all your duplicate meshes with instances is to do straight in Blender (or 3DS).
Versuchs mal mit nur ein Objekt (z.B. TĂŒre oder Fenster)


1 Like