How to go about casting shadows on point sprite billboard-like objects?

Hi,

I have implemented/adapted a billboarding shader a couple weeks ago that would take point sprites as input and its vertex shader would displace its position based on the desired object size. It looks like this:

[java]
// ---------- Billboarding effect ----------
if(inSize > 0){
// Convert vertex location to world-view space, ie: camera-relative space
pos = g_WorldViewMatrix * pos;

    // Figure out how to offset it in camera space
    float offset = texCoord.x - 0.5;  // (make it -0.5 to 0.5)
    pos.x += offset * inSize;
}
// -----------------------------------------

if(inSize > 0){
    gl_Position = g_ProjectionMatrix * pos;
}
else{
    gl_Position = g_WorldViewProjectionMatrix * pos;
}

[/java]

So, when the vertex shader sees an inSize parameter, it displaces its vertex position so that it ends up being a vertical quad. You get the point. It enables very fast rendering of thousands of objects.

Now, the problem is that since this is initially just a point (and not a quad) JME3 does not cast its shadow (or does, but itā€™s infinitely small and thus invisible). Is there a way to achieve this? I was thinking about maybe like doubling all those point sprites with a real quad copy, front and back culled or something, just so that its correct shadow is casted but leaving the quad invisible. So like only render it in the shadow passā€¦ not even sure this makes sense. Any ideas?

Thx :smiley:

Actually the shadow renderer does use a normal rendering, you might need to adjust /manipulate the used material from it tho.

there are 2 steps in shadow rendering :

  • generating the shadow map (render the depth of the scene from the light point of view)
  • Apply the shadows on the rendered scene.

Both passes uses a material with a vertex and frag shader.
If your material displaces the vertices in the vert shader, the vertices needs to be displaced as well in the vert shader of those 2 passes.

To do so, your material must have a preshadow technique that has a vert shader that displace the vertices (i recommend using the ShadowFilter in this case because it will save you from doing this also for the post shadow pass)

look at how the preshadow technique is done in the lighting material and look at the preshadow.vert.

1 Like

Hi Eric,

Iā€™m using the shadow FILTER (not renderer) if it makes more sense. I could go back to the shadow RENDERER if itā€™s going to output good fps, but I found that it delivered slightly better performance in my scenario to use the shadow FILTER instead. Iā€™ve read the FILTER performance curve surpasses the RENDERER around >20 shadow receiving objects. Let me know what you guys think if I need to go back to the RENDERER for this to workā€¦

Hi RĆ©my,

@nehon said: To do so, your material must have a preshadow technique that has a vert shader that displace the vertices (i recommend using the ShadowFilter in this case because it will save you from doing this also for the post shadow pass)

This is extremely valuable information to me. Thank you, Iā€™ll look into it. That sounds exactly like what Iā€™m after. At the moment I was n00bly duplicating the thousand objects, rotating them towards the sun on XZ planes, GeometryBatching them and FrontAndBack-culling them so that the only thing that is displayed is their shadows which surprisingly works flawlessly and gives extremely good fps (it literally draws 2fps out of nearly 70fps) when I create this shadow-making object layer. Itā€™s like an impostor but for shadows only.

Iā€™ll now look into adapting the real objectsā€™ material pre-shadow technique so that it effectively displaces its vertices just like it does in the rendering pass and Iā€™ll post some results here. I havenā€™t seen anybody talking about this in the JME3 forums yet.

Thanks :smiley:

Alright, +1 to RĆ©my for this valuable informationā€¦ I have implemented the same expanding point GLSL code in the preShadow technique as in the billboard shader itself and now my billboards are casting shadows just like any other full vertices models. The only thing Iā€™m wondering is this:

Since the billboards will face the sun in the PreShadow pass and face the camera on the render pass, then most of the time the 2 will cross in a X shape. So inevitably there will be half-ish of the tree in its own shadow and the other half in the sun. This self shadowing is not looking good at some time of the day (depending of where the sun is). Is there a way to prevent self shadowing? inb4 ShadowMode.Cast. Already tried this and it does not change anything.

That was another question I had: is there something to add in the material file or in the GLSL shader to behave using JME3ā€™s ShadowMode hint? Setting it to ā€œOffā€ removes effectively its shadow, but setting it to ā€œCastā€ does the same thing as ā€œCastAndReceiveā€ thus the billboard triggers self shadowing.

Am I correct to assume that this would not happen with a PostShadow or would it be the same? What can I do to overcome this issue? :-/

Thx!

If you are using the shadow filter instead of the shadow renderer then I believe everything receives shadows because itā€™s all done in post on the entire frame. So essentially everything is ā€˜receiveā€™.

1 Like

Thanks Paul for this as it explains this strange behaviour. As a matter of fact, itā€™s obvious to me now that itā€™s an architecture problem. Billboards canā€™t possibly behave correctly with shadows since all shadows are casted in the same pass, so there is no difference between self shadow and another objectā€™s shadow, itā€™s all just one big shadow. That causes some serious headaches. How will one go about avoiding self-shadowing on quad billboards. Iā€™ve looked on Google for ideas and Iā€™m stuck, so I might simply have to displace the preShadow pass vertices off enough so that it never touches the billboard shape that is used in the render pass.

That ā€œworksā€ but at the same time it sucks because the shadow is not ā€œconnectedā€ at the billboardā€™s feet. Take a tree for example, the shadow will start like 1 meter away from the trunk/terrain intersectionā€¦ not very realistic, but at least the top part of the tree will not look half self shadowed.

I do not think shadowing billboards is possible, really. Unless they are small and you shadow the whole thingā€¦ but that would be based on some voxel lighting or something, not real shadows.

You may have to rethink the use of billboards for things that are shadowed.