Problems with g_CameraPosition in pre-shadow shaders

I think g_CameraPosition is not provided properly to my pre-shadow material, because I have tested everything else. Here’s the shader.



[java]

attribute vec3 inPosition;

attribute vec2 inTexCoord;



uniform mat4 g_WorldViewProjectionMatrix;

uniform mat4 g_WorldMatrix;

uniform vec3 g_CameraPosition;



varying vec3 texCoord;



#if !defined(HQ_FADING)

uniform float m_FadeEnd;

uniform float m_FadeRange;

#endif



#ifdef SWAYING

uniform float g_Time;

uniform vec2 m_Wind;

uniform vec3 m_SwayData;

#endif



void main(){

vec4 pos = vec4(inPosition,1.0);



#ifdef SWAYING

float angle = (g_Time + pos.xm_SwayData.y) * m_SwayData.x;

pos.xz += 0.2
m_WindinTexCoord.ysin(angle);

#endif



vec4 worldPos = g_WorldMatrix*pos;



// Fading

vec2 CtoP = worldPos.xz - g_CameraPosition.xz;

float dist = length(CtoP);



#if !defined(HQ_FADING)

float fadeVal = clamp((m_FadeEnd - dist)/m_FadeRange,0.0,1.0);

pos.y *= fadeVal;

#else

texCoord.z = dist;

#endif



gl_Position = g_WorldViewProjectionMatrix * pos;

texCoord.xy = inTexCoord;

}

[/java]



Here’s the technique:



[java]

Technique PreShadow {



VertexShader GLSL100 : se/jod/biomonkey/assets/shaders/grass/GrassPreShadowPSSM.vert

FragmentShader GLSL100 : se/jod/biomonkey/assets/shaders/grass/GrassPreShadowPSSM.frag



WorldParameters {

WorldViewProjectionMatrix

WorldMatrix

CameraPosition

Time

}



Defines {

SWAYING : Swaying

HQ_FADING : HighQualityFading

}



RenderState {

FaceCull Off

DepthTest On

DepthWrite On

PolyOffset 5 0

ColorWrite Off

}



}

[/java]



Everything else works, and I’m pretty sure culling faded out pixels like that also used to work before, which is weird. Using the new pssm filter. RC2 no nightlies.

The question is: Is there some reason the CameraPosition variable should be unavailable in the pre-shadow pass(es)?

Yes there is.

The pre shadow is rendered from the light point of view. Also it’s not render as a classic viewport so I guess the g_CameraPosition is not set properly.

That’s just a hunch though.



But looking at your code, you use the camera position to compute the length of the vector from the cam pos to the vertex position.

Actually this is z in viewspace.



this

[java]

vec4 worldPos = g_WorldMatrixpos;



// Fading

vec2 CtoP = worldPos.xz - g_CameraPosition.xz;

float dist = length(CtoP);

[/java]

can be replaced by

[java]

vec4 viewPos = g_WorldViewMatrix
pos;

float dist = viewPos.z;

[/java]

It’ll be faster since you remove the expensive length operation.



Still, the g_CameraPosition not being properly set is a bug.

@nehon

Nice. Since there is no lights and stuff I guess world pos is completely worthless in shadow shaders. And i just copied and pasted from lighting shaders… Thanks for noticing.



Btw, speaking of which, the world view matrix is referenced in PreShadow.j3md, and declared in the vertex shader, but its not used.

thanks i’ll clean that up.

I still can’t get it to work using worldviewmatrix, which I really don’t want to do btw. since it causes changes in the fading values when the camera is just rotated (looks weird).



Noticed something weird tho, I tried using worldviewmatrix like that in the lighting shader, and it sort of works, but I have to negate the z pos.



[java]

vec4 pos = vec4(inPosition,1.0);

vec4 worldPos = g_WorldMatrixpos;



… stuff …





vec4 wvPos = g_ViewMatrix
worldPos;

float dist = -wvPos.z;

[/java]



This also happens if I skip past the world pos and just do:



[java]

vec4 pos = vec4(inPosition,1.0);

vec4 wvPos = g_WorldViewMatrix*pos;

float dist = -wvPos.z;

[/java]



If I do that, it works.

I don’t get it, when does it work when doesn’t it work?

@nehon

The worldviewmatrix-approach does not work in shadow shader, just like using the camera position does not work there.



The worldviewmatrix approach (or worldmatrix * viewmatrix) works in the regular lighting shader, of course, but i have to negate the ‘wvPos.z’ value there because it’s always negative for some reason.

this is a crap issue btw, the only thing affected in my lib is grass shadow casting (which should not be on), and shadows on tree impostors. It causes a blocky “phasing in” of shadows beyond the impostor range but no probs. Please don’t feel you need to fix…