I was reading up on how shadow mapping works. We create basically a depth map from the light’s perspective and if an object is in the way then we make the fragment darker on the object behind the first object. For that you need a) the light source, b) an object and b) some other object on which the shadow is cast.
But I wonder if I can do the same shadow mapping but with just one plane object and cast the shadow on itself.
I have a plane that is showing a terrain using tiles. Each quad of the plane have a different terrain tile. But there is also a z component that is simulating the depth of the terrain. Currently all I do is to add an alpha value so that the greater the z the darker the tile is drawn. For example, z=0 is alpha=0, z=1 is alpha=0.3, z=2 is alpha=0.6, and so on.
To show the depth better to the user I want to add a shadow.
For that I was thinking that I use the same shadow mapping technique. I create a depth map not from other objects but from the alpha value of the fragments on the terrain.
Then alpha=0 would be solid and would cast a shadow “down” to the tiles on the same object.
Is that even possible?
Was something like this done before and I could read somewhere how somebody else done it?
Google is not really a help because I only find descriptions the normal shadow mapping technique.
I did the example in Gimp how it should look like.
The first image is how it should look like (made in Gimp), the second is how it looks now.
Is your map only ever viewed from above like that?
Yes. The light source is not directly above. I calculate the position of the sun based on the in-game time, based on the earth’s model. So, the shadow should also reflect the position of the sun.
I’m also just using the JME3 TerrainQuad.
I was thinking about a little more and it shouldn’t be that hard. Basically, I need to create a depth map texture, but not using the gl_FragCoord.z for the depth but gl_FragColor.a (where gl_FragColor.a==0 means fragmentdepth=0.0). Based on this tutorial: Tutorial 16 : Shadow mapping
Then I use the depth map texture in the fragment shader of the terrain plane and “cast” a shadow based on that depth map. The only problem is that I need to “cast” the shadow outside the uv-coordinates of the tile.
Maybe I’m thinking it all wrong. It’s not really a shadow. All I need is basically an alpha texture that looks like a shadow that I draw on top of the texture of the tile.
Maybe multi-pass is the easiest solution?
I will just generate an alpha texture from the point of view of the light. Something like this:
In the first pass I will draw the terrain with the tiles texture and in the second pass I will draw the terrain again but now with the generated alpha texture with the shadow. That should work?
The thing with developing games is that half of what you think you see is usually a trick. Smoke and daggers.
You’re explaining your dilemma as best you can - and I comprehend what you’re trying to do, but I think it would be a lot easier to find a solution if we had the “big picture”.
What game is closest to what you’re trying to make? Is it a side scroller? A top-down tile-based game like the old Zelda games? I think if we knew that we could probably point you in the right direction. Most solutions are not what appears to be the most natural.
Right now all we know is you want to project a shadow - but there are many ways to achieve that - and the best way to find out what will suit you is by seeing the “big picture” - or at least see a little more of the canvas than a few tiles with a shadow.
Can’t I just rise up the terrain tile? Would there be a shadow cast from the “mountain”? I didn’t actually looked into TerrainQuad and shadows. That would also solve my problem.
You are of course right. But wouldn’t I have then like 16,384 individual objects for a 128x128 terrain (and even more individual objects for a bigger terrain)? I don’t think that’s a good idea because of performance?
Btw, currently it’s not two blocks high, it’s totally flat. The depth is just simulated.
Yes, but the shadows would be smooth… like diagonal. Imagine the different side views:
Mesh of flat quads:
Then imagine the difference from the shadows cast sideways by those terrain profiles.
Your image seemed like you were looking for the second way. Else you need to keep in mind that the elevations gradually increase between tiles… so that raised tile is only raised at one edge (or the lower tile is only lower at one edge).
No. Because you’d be making a custom mesh. One object. One draw call. Split the world up how you like but you’d have some utility where an array of tiles goes in, mesh comes out.
Edit: to be most accurate, the mesh of quads way would be like:
…because you likely don’t even need the side walls unless your sun will be extremely horizontal.
Note: if making a custom mesh seems scary and/or you just want to try it and don’t care too much about raw generation efficiency, you could build a big array of regular JME Quad mesh Geometry and then batch them down to one spatial (assuming they all had the same material… one spatial per material otherwise).
I didn’t know about this. I was thinking that I just generate vertices and indices buffers.
But I can use GeometryBatchFactory#mergeGeometries() to merge multiple individual quads into one single mesh?
Who wrote GeometryBatchFactory? It’s missing copyright and documentation.
The JME team wrote it and maintains it. It’s part of jme-core. Specifically, I think normen probably wrote the original version. It’s licensed the same as the rest of the engine and the copyright notice in the source file is just an oversight.
Yes, if you have a bunch of compatible meshes it will merge them together into one mesh.
Making your own mesh is more efficient because you can control everything and won’t have a bunch of intermediate data structures that you will throw away… but it is harder.
I want to thank pspeed and jayfella for your help here. The custom mesh works good for casting shadows. It’s the effect I wanted. If you want to see my implementation for the custom mesh, I put it on Github. It’s basically a terrain made of 5 faces boxes (no need for the bottom face because it’s a terrain).
I think I just need to adjust the shadow effects. But in principle it works.