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?
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.
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]
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 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 ). 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 .
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:
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:
Make the ground a ground. Isolate it from other meshes/groups, call it âgroundâ and let it be at zero origin.
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 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,
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?)
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)âŠ