My next pathetic attempt with shadows - help

@Perjin Simple question - is it possible to downgrade your method to the version 3.0 of JME? I want to make the same trick as you did, step by step, learning from your code but using my version. So far after copy-paste of lighting vert/frag into proper PostShadow files I’m getting error on .frag file.
I noticed that difference in LightPos vector and I’m passing radius in LightRadius parameter.

@nehon I’m aware that trunks are always unstable, but I wanted at least to run it. As I said to Perjin, I’m trying to do the same in 3.0. Unfortunatelly, his code uses things that I found only in latest, unstable version.

Hmm yes the concept should work the same in the stable version.
I downloaded the current stable version and look what has to change.

1 Like

Updated the Git Repository, I had only to change things related to instancing (mostly matrix multiplications).

2 Likes

Thank you!!

EDIT: Ok, I have vert able to compile. I’m almost sure that in 3.0 the LightPos is vec3, not vec4. I changed that in j3md and vert, passing additional float parameter which contains 1f/light.getRadius().
Now I’m working on frag. By the way, I was so close with vert :wink:

Funny thing - seeing your code I can say that some time ago I was trying to do something similar. Of course I failed, because my idea was to move PostShadow into Lighting. Now, after few weeks I can only say that it was stupid :wink:

Since .vert is ok now, I started to work on .frag and found a problem.
Importing Optics.glsllib causes an shader compilation error. I traced it by commenting all lines in your code.
To be more accurate this line:
return textureCube(envMap, dir); of Optics.glsllib causes an error. My materials does not uses sphere maps. I don’t know what to thing about it…

The compile error should have told you exactly what line it happened on and the log should have included an entire dump of the expanded shader. Commenting out things line by line shouldn’t really be necessary until the issue has at least been narrowed down.

I will assume that you didn’t comment out any #ifdef’s that might have otherwise skipped that particular line if you weren’t doing env mapping… but I also hate to assume.

@pspeed said: The compile error should have told you exactly what line it happened on and the log should have included an entire dump of the expanded shader.

Ok, I understand. Damn, every time there is a compile error, I just stop the application. Now, after resume I see the additional information. It is so simple to trace bugs now :wink:

com.jme3.renderer.RendererException: compile error in:ShaderSource[name=MatDefs/Shadow/PostShadow15Pretty.frag, defines, type=Fragment, language=GLSL150] error:0(95) : warning C7508: extension NV_shadow_samplers_cube not supported
0(127) : error C7533: global function textureCube is deprecated after version 120

Ok, deprecated function. But Lighting.frag imports the same file Common/ShaderLib/Optics.glsllib and there is no error.

I see that Optics_GetEnvColor is used only when USE_REFLECTION is defined, so I’ll brace that import using proper #ifdef/#endif

Ok, it is working but looks strange…
http://postimg.org/image/ozm2y0dbz/
I’ll check again if I mess something in .vert with my LightPos modification.

EDIT: the only modification I made is

    //vec4 wvLightPos = (g_ViewMatrix * vec4(m_LightPos.xyz,clamp(m_LightColor.w,0.0,1.0)));
    //wvLightPos.w = m_LightPos.w;
    //vec4 lightColor = m_LightColor;
	
    vec4 wvLightPos = (g_ViewMatrix * vec4(m_LightPos,clamp(m_LightColor.w,0.0,1.0))); //m_LightPos.xyz
    wvLightPos.w = m_LightRadius; //m_LightPos.w;
    vec4 lightColor = m_LightColor;

and

        //material.setVector4("LightPos", new Vector4f(light.getPosition().x, light.getPosition().y, light.getPosition().z, 1f/light.getRadius()));
    	material.setVector3("LightPos", new Vector3f(light.getPosition().x, light.getPosition().y, light.getPosition().z));
    	material.setFloat("LightRadius", 1f/light.getRadius());

and

            Vector3 LightPos
             //Vector4 LightPos  // Pretty
             Float LightRadius

Quick fix to ‘endless shadows and lighting’ I encountered in your solution:

In .frag, at the begining of main():

	vec3 lightVector = m_LightPos - worldPos.xyz; 
	float dist = length(lightVector);
	float lrh = (1.0 / m_LightRadius); 
	float initMaxss = (lrh - dist) / lrh;

	if (initMaxss <= 0) return;

The result is: http://postimg.org/image/ewhnuzw2l/

There is a problem: left wall and the floor is not lighten, no matter how close or far the light source is and it don’t matter if I use my quick fix or not.
Actual shader uses my poor lightComputeDiffuse modification, introduced in some of my previous post, but switching to the original formula does not help.

@Perjin I think that I need your assistance or hint with that wall/floor problem.

On some other locations it looks pretty and nice. There is no ‘deep darkness’ effect I encountered with my own shading.

Are all your geometries set to receive shadows( or cast and receive)? Don’t really know what else it could be.

Yes, it was ok with my own shader, here is the proof: http://postimg.org/image/pqf1djqkb/ (ignore the shadow artifacts).

In my 3d scene every wall is just the same geometry, no matter if it is in front of you or to the left. All of them are batched using GeometryBatchFactory.optimize(). There are normals, there are tangents.
Tested it with pure 3.0 and Nehon’s modification (setShadowZExtend()/setShadowZFadeLength()). Also playing with FaceCull does not change anything.
Look at another image, taken from another place and angle. You can see where the light sources are just by tracing the shadows, in addition I marked them with red dots: http://postimg.org/image/ta5r36zab/

Hint: everything renders well if the light is coming to +x, +y, +z.
On the bottom (floor, -y) there is no lighting, wall to the left is -z and, on that second image from my previous post, the left side is on -x.

EDIT: I mean light.y > floor.y and light.x > wall.x from the upper screen of http://postimg.org/image/ewhnuzw2l/

I would try to render only shadows or light similar to the sample screenshots i posted to narrow down which part of your shader is causing this.
Other than that it is really difficult to help without seeing the shader code.

Ha! Found something (blind luck FTW): http://postimg.org/image/4pxtbepaf/

    #else
       vec4 lightDir = vLightDir;
       lightDir.xyz = normalize(lightDir.xyz);
	   lightDir.x = -lightDir.x; // MY MOD!
	   lightDir.y = -lightDir.y; // MY MOD!
	   lightDir.z = -lightDir.z; // MY MOD!
       vec3 viewDir = normalize(vViewDir);

       vec2   light = computeLighting(normal, viewDir, lightDir.xyz);// * spotFallOff;
       #ifdef COLORRAMP
           diffuseColor.rgb  *= texture2D(m_ColorRamp, vec2(light.x, 0.0)).rgb;
           specularColor.rgb *= texture2D(m_ColorRamp, vec2(light.y, 0.0)).rgb;
       #endif

http://postimg.org/image/j39ecxia3/

And my ugly, dirty fix:
`lightDir = abs(lightDir);’

Looking at that chain to the right I think that I need to play with PolyOffset a bit more…

Maybe I’m not skilled in shaders, your brilliant solution will be a good example and starting point for further modifications, but at least I’m lucky and have some ‘feeling’ :wink: I need to remind some math too.

I still have some strange effects with lighting in different directions.

http://postimg.org/image/y2a3ldtil/

On the upper part the scene contains two point lights - one ‘without shadows’, the second uses Pretty renderer.
The lower part is the same, but only with Pretty renderer’s light. Notice the difference in lighting the surfaces - the left part is a screen of south wall, the right shows the wall to the west. Ignore the difference in torch’s angles. Lights are placed exactly at the center of the wall with constant distance to the wall.

Here is the current code with all my modifications:
j3md: http://pastebin.com/7wUnQRym (just use Lighting vert/frag instead of my own files)
vert: http://pastebin.com/etXsJ1MS (changed vec4 LightPos to vec3)
frag: http://pastebin.com/PsUtVnVk (changes: lightComputeDiffuse and main)

I’m trying to find the cause right now, it is not that I’m asking and asking and asking here, all the time without my own efforts.
You may just find that next part of ‘FrozenShade and the Shadow Renderer’ story is interesting. Or boring :wink:

1 Like

your fragment shader upload doesn’t work, but i tried your vertex shader and that worked fine for me.

1 Like

frag: http://pastebin.com/5mqbvzJX

Forget that ‘direction’ thing, looks like the main cause here is the light position. Moving it through the X axis the northern wall is going to be darker, while any western/eastern wall have the same level of light (disable the initial ‘initMaxss’ mechanism to see that). It looks the same while moving through the Z axis and facing western wall.

http://postimg.org/image/qk4ook9xb/

There is some wrong zero value somewhere causing that light is dependent on its distance from the (0, 0, 0) point. You have your scene probably at the beginning of the coordinate system so you cannot see that. Try to move your scene for example 20 units on X axis and see the results.

The problem is your modification to lightComputeDiffuse, without that you can remove your “lightDir = abs(lightDir);” fix, all that “initMaxss” stuff and everything will look fine.

I did that before, I’m smart enough to first remove my own code in case of errors :wink:

Look at the scene without any modification in the frag file: http://postimg.org/image/ewhnuzw2l/ (taken from some previous post)
The same scene from another point of view: http://postimg.org/image/kymuyhjcd/
Cam is placed inside the wall, we see endless shadows of the right door from the first image. That was the cause of my first modification in main() - to cut them.

The abs() was used because of the left wall and the floor (first image).

The lightComputeDiffuse modification was taken from my lighting.frag, check this post: http://hub.jmonkeyengine.org/forum/topic/my-next-pathetic-attempt-with-shadows-help/page/2/#post-298287 It helped me do light up the wall that is very close to the light source. I know that this looks similar to the first image here, almost the same… almost, except the floor which is always dark and the left wall is dark no matter how far the light source is.

Looks like using initMaxss and abs() I’m fighting with the same, these things are somehow connected. Something is missing at the very beginning and I’m unable to find out what it is…