How AutoShader calculates the normals?

How AutoShader calculates the normals? I mean texture with normals which i can get through gbuffer.renderSceneInto(colortex = albedo, depthtex = depth, auxtex = normal,auxbits = AuxBitplaneAttrib.ABOAuxNormal) and setShaderAuto. I want to get the same result in custom shader without setting autoshader. I tryed to multiple vtx_normal by itp_modelview matrix, but got a different result.

In vertex shader:

l_eye_normal.xyz = mul((float3x3)tpose_view_to_model, vtx_normal.xyz);
l_eye_normal.w = 0;

In fragment shader:

l_eye_normal.xyz = normalize(l_eye_normal.xyz);
o_aux.rgb = (l_eye_normal.xyz*0.5) + float3(0.5,0.5,0.5);
1 Like

Thanks! It’s works, but it seems that autoshader don’t normalizes l_eye_normal.

Well, I’m seeing this code in shaderGenerator.cxx:

      text << "\t l_eye_normal.xyz  = normalize(l_eye_normal.xyz);\n";

I do not understand anything. ><
I was playing with sample from first message of this topic [[UPDATE] - Fur - Deferred shading)
and I got different result from what I saw in the default buffer if I used normalization.

This is modified sample (I added make_aux_buffer procedure, test shader, and used standart models)
panda3d.org.ru/files/deferred_light.zip

My test shader

//Cg
//

void vshader(float4 vtx_position : POSITION, 
		float4 vtx_normal : NORMAL, 
		uniform float4x4 mat_modelproj,
		uniform float4x4 tpose_view_to_model,
		out float4 l_position : POSITION,
		out float4 l_eye_normal : TEXCOORD1)
{
	l_position = mul(mat_modelproj, vtx_position);
	l_eye_normal.xyz = mul((float3x3)tpose_view_to_model, vtx_normal.xyz);
	l_eye_normal.w = 0;
}

void fshader(float2 l_texcoord0 : TEXCOORD0,
			float3 l_eye_normal : TEXCOORD1,
			out float3 o_color : COLOR)
{
	l_eye_normal.xyz = normalize(l_eye_normal.xyz);
	o_color.rgb = (l_eye_normal * 0.5) + float3(0.5,0.5,0.5);
}

Well, unless you’re outputting to a float buffer, you have to normalise the vector or you risk it getting clamped. If the shader generator doesn’t do that in some cases, that’s a mistake. What is the output of the shader generator? You can get this using dump-generated-shaders.

Wow! I didn’t know about this feature ) Thanks!

genshader0

//Cg
void vshader(
	 in float4 vtx_texcoord0 : TEXCOORD0,
	 out float4 l_texcoord0 : TEXCOORD0,
	 uniform float4x4 tpose_view_to_model,
	 out float4 l_eye_normal : TEXCOORD1,
	 in float4 vtx_normal : TEXCOORD1,
	 float4 vtx_position : POSITION,
	 out float4 l_position : POSITION,
	 uniform float4x4 mat_modelproj
) {
	 l_position = mul(mat_modelproj, vtx_position);
	 l_eye_normal.xyz = mul((float3x3)tpose_view_to_model, vtx_normal.xyz);
	 l_eye_normal.w = 0;
	 l_texcoord0 = vtx_texcoord0;
}

void fshader(
	 in float4 l_eye_normal : TEXCOORD1,
	 uniform sampler2D tex_0,
	 in float4 l_texcoord0 : TEXCOORD0,
	 out float4 o_aux : COLOR1,
	 out float4 o_color : COLOR0,
	 uniform float4 attr_color,
	 uniform float4 attr_colorscale
) {
	 float4 result;
	 o_aux = float4(0,0,0,0);
	 // Fetch all textures.
	 float4 tex0 = tex2D(tex_0, l_texcoord0.xy);
	 // Output the camera-space surface normal
	 o_aux.rgb = (l_eye_normal.xyz*0.5) + float3(0.5,0.5,0.5);
	 result = float4(1,1,1,1);
	 result.rgb *= tex0;
	 result *= attr_colorscale;
	 o_color = result * 1.000001;
}

genshader1

//Cg
void vshader(
	 uniform float4x4 tpose_view_to_model,
	 out float4 l_eye_normal : TEXCOORD0,
	 in float4 vtx_normal : TEXCOORD0,
	 float4 vtx_position : POSITION,
	 out float4 l_position : POSITION,
	 uniform float4x4 mat_modelproj
) {
	 l_position = mul(mat_modelproj, vtx_position);
	 l_eye_normal.xyz = mul((float3x3)tpose_view_to_model, vtx_normal.xyz);
	 l_eye_normal.w = 0;
}

void fshader(
	 in float4 l_eye_normal : TEXCOORD0,
	 out float4 o_aux : COLOR1,
	 out float4 o_color : COLOR0,
	 uniform float4 attr_color,
	 uniform float4 attr_colorscale
) {
	 float4 result;
	 o_aux = float4(0,0,0,0);
	 // Fetch all textures.
	 // Output the camera-space surface normal
	 o_aux.rgb = (l_eye_normal.xyz*0.5) + float3(0.5,0.5,0.5);
	 result = float4(1,1,1,1);
	 result *= attr_colorscale;
	 o_color = result * 1.000001;
}

Ah, I see. It fails to normalise the vector when there are no lights applied to the node. I’ve committed a fix, will be in 1.8.1.

Ok, thanks ) May I ask when you plan to release version 1.8.1? I wait it mostly for GLSL fix.

Soon. In the meantime, you can use the devel version.

update: vs Cg on 1.10.9:

    l_eye_normal.xyz = normalize(mul((float3x3)itp_modelview, vtx_normal.xyz));
    l_eye_normal.w = 0;

… and apply the cast-down to 3x3 of the other transforms