# PSSM stabilisation approach

Hey, I just found this googeling

Has anyone already tried this approach, before I start wasting time? Or can give reasons why this will/won’t work?

1 Like

ho cool that’s impressive.
I can’t see no reason why it wouldn’t work, but the description of the technique is quite light to start an implementation.
Feel free to look into it and ask here for assistance if you like. I’m kind of busy lately so I won’t be able to look into this until some time.
All the light matrix calculation is done in the ShadowUtil class.

Go for it, man! It’s something that’s really bothered me when using PSSM. It’ll be cool to see how effective it is in large scenes.

@EmpirePhoenix I was looking into this again and found a very nice post on the matter on game dev. The concept is explained more thoroughly, and there is even code example (XNA though but still)

Oh cool, maight help a bit.

ok so i continued my research a bit and found this post
The author is talking about this technique described in shaderx 6 and that he will make an article in shaderx 7 about an approximation method that is better in most case.
And…it just happen that I have this book

so I tried, and it adapts really well to our existing matrix calculation. I achieve to have stable shadows with translating the camera. unfortunately for now there is still a flickering when you rotate the camera, but that kind of encouraging.

I’ll keep you updated.

3 Likes

Ok so I finally got a good result with this. And it’s now committed to core
here is a video to explain the effect

I used a small shadow map resolution and no edge filtering so the flickering is very obvious.
The nice thing is that I just added around 5 lines of codes in the ShadowUtils class. That’s just some kind of math magic.
That’s an approximation solution that gives better shadows quality than the exact solution, but the downside is that there are still occasional flickerings.

3 Likes

Wow, you rally managed to understand that math? I failed countless times, thanks so much for doing this.

Thanks, works like a charm - shadows are not flickering anymore for me on surfaces which are almost parallel to light direction.

@Empire Phoenix said: Wow, you rally managed to understand that math?
well...not completely to be honest, but enough to make it work :p

In the same article in ShaderX7 they explain how to increase shadow quality when the light and camera frustum are aligned. It also can lead to some possibility to optimize geometries culling when rendering the shadow maps. I’ll look into this a bit more.

Looking forward to test this on large scale self-shadowed terrain (now if only there were more hours in the day)

@jmaasing said: Looking forward to test this on large scale self-shadowed terrain (now if only there were more hours in the day) :-)

There’s plenty of hours in the day. You sleep too much or work too many hours. Your hobby is what matters most.

madjack - you said it

Just updated my RC2-nightly but I guess the changes @nehon committed won’t be built until tomorrow. Been out of jME touch for a while, I guess it’s the DirectionalLightShadowRenderer that’s the new black So I updated my code to use that instead of the PssmShadowRenderer.

But I get shader compilation errors (yeah I’m on a Mac). Is there something I missed updating?

```WARNING: Bad compile of: 1 #define HARDWARE_SHADOWS 1 2 #define PSSM 1 3 #define SHADOWMAP_SIZE 1024.0 4 #ifdef HARDWARE_SHADOWS 5 #define SHADOWMAP sampler2DShadow 6 #define SHADOWCOMPARE(tex,coord) shadow2DProj(tex, coord).r 7 #else 8 #define SHADOWMAP sampler2D 9 #define SHADOWCOMPARE(tex,coord) step(coord.z, texture2DProj(tex, coord).r) 10 #endif … ```

``` SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main] com.jme3.renderer.RendererException: compile error in:ShaderSource[name=Common/MatDefs/Shadow/PostShadow.frag, defines, type=Fragment, language=GLSL100] error:ERROR: 0:12: '' : syntax error incorrect preprocessor directive WARNING: 0:12: unexpected tokens following the preprocessor directive - expected a newline at com.jme3.renderer.lwjgl.LwjglRenderer.updateShaderSourceData(LwjglRenderer.java:1015) at com.jme3.renderer.lwjgl.LwjglRenderer.updateShaderData(LwjglRenderer.java:1038) ```

What’s up with the forum, it says: ERROR: Your reply cannot be empty but it wasn’t. Was it that I started with a @madjack?

…I’m just a curious bystander but I wish you’d left line 12 in what you posted since that’s where the error is.

@jmaasing only updating the DirectionalLightShadowRenderer might not be enough
there has been more changes. and this one for example is in ShadowUtils.java

You should wait for the next nightly.

For your issue though, as Paul said, please post line 12

Oh, is that what the 12 means :roll: facepalm
``` WARNING: Bad compile of: 1 #define HARDWARE_SHADOWS 1 2 #define PSSM 1 3 #define SHADOWMAP_SIZE 1024.0 4 #ifdef HARDWARE_SHADOWS 5 #define SHADOWMAP sampler2DShadow 6 #define SHADOWCOMPARE(tex,coord) shadow2DProj(tex, coord).r 7 #else 8 #define SHADOWMAP sampler2D 9 #define SHADOWCOMPARE(tex,coord) step(coord.z, texture2DProj(tex, coord).r) 10 #endif 11 12 #if FILTER_MODE == 0 13 #define GETSHADOW Shadow_DoShadowCompare 14 #define KERNEL 1.0 15 #elif FILTER_MODE == 1 16 #ifdef HARDWARE_SHADOWS 17 #define GETSHADOW Shadow_DoShadowCompare 18 #else 19 #define GETSHADOW Shadow_DoBilinear_2x2 20 #endif 21 #define KERNEL 1.0 ```

I didn’t manually update DirectionalLightShadowRenderer, I meant that the SDK did the usual update-plugins-to-nightly.
I was thinking that if I run the current nightly now I’d get a “before” look and then tomorrow I could get a “stable shadows” view of my app with nehons updates. Or do you mean that I should do something more than?
[java]
[/java]

@jmaasing said: I didn't manually update DirectionalLightShadowRenderer, I meant that the SDK did the usual update-plugins-to-nightly. I was thinking that if I run the current nightly now I'd get a "before" look and then tomorrow I could get a "stable shadows" view of my app with nehons updates. Or do you mean that I should do something more than?
Nope that's ok

so…the #if seems to be the issue…
I’ll look into it

1 Like
@nehon said: Nope that's ok

so…the #if seems to be the issue…
I’ll look into it

I could be wrong, but I think in…
#if FILTER_MODE == 0

If FILTER_MODE is not defined then you will get an error. Being undefined is different than being 0.

1 Like

yep must be it, I tried and I have an error too when the edgeFiltering is not set. Another error though…But this one must be something similar

Way to go Well done!