surface_shader reading specific values causes wrong color

OnkelDagobertOnkelDagobert Join Date: 2013-01-31 Member: 182694Members
Hi,
I'm having Problems writing the shader for a material.
<shader>	
	<inputs>		
	</inputs>
	<params>
		<param name="osPosition">true</param>
		<param name="wsPosition">true</param>
		<param name="wsNormal">true</param>
		<param name="vsNormal">true</param>
		<param name="twoSided">true</param>
		<param name="alphaTest">true</param>
	</params>
	<code>
	<![CDATA[				
		void MaterialShader(in Material_Input input, inout Material material)
		{				
			material.emissive   =    float3(0,0,1);
		}
	]]>		
	</code>
</shader>
this shader works fine and gives me this (the blue glass):
simple.jpg

but if i try to read some vars it give me a total wrong result:
<shader>	
	<inputs>		
	</inputs>
	<params>
		<param name="osPosition">true</param>
		<param name="wsPosition">true</param>
		<param name="wsNormal">true</param>
		<param name="vsNormal">true</param>
		<param name="twoSided">true</param>
		<param name="alphaTest">true</param>
	</params>
	<code>
	<![CDATA[				
		void MaterialShader(in Material_Input input, inout Material material)
		{						
			float3 neverused = viewPoint.xyz ;			
			material.emissive   =    float3(0,0,1);
		}
	]]>		
	</code>
</shader>
simple_wrong.jpg
the material color ALLWAYS becomes purple, no matter what i write into material.emissive

this happens if i try to read following vars:
- viewPoint
- worldToScreenMatrix
- worldToCameraMatrix
and maybe some more

even though it makes the Color purple it reads the right values from this varibales

does anyone know what to do?

or is there a way get the distance from the cam to the displaying object without using viewPoint?

Comments

  • Ineluki80Ineluki80 Join Date: 2013-01-20 Member: 180772Members
    edited January 2013
    I'm fairly new to shaders and all (only wrote a pixel shader so far), so I wonder: where do you take viewPoint from ?
    I can not see where this variable is defined nor is it an intrinsic HLSL function.
    So maybe you are accessing an undefinded variable, causing a shader compile error and as a result get some default or uninitialized material instead ?
    Have you had a look at the debug output messages withing the console ?
  • OnkelDagobertOnkelDagobert Join Date: 2013-01-31 Member: 182694Members
    it is defined in core\renderer\Deferred.shader_template

    and as i said the value in viewPoint is correct. I tested it by adding a glow to the material depending on the viewPoint
  • OnkelDagobertOnkelDagobert Join Date: 2013-01-31 Member: 182694Members
    ok got it..
    it seems that the purple color is the right behavior.
    the shader used the default values specified in Deferred.shader_template:
    // Setup default values so that the shader can only set the values it wants to.
    material.albedo   			= float3(1, 0, 1);
    material.tsNormal 			= float3(0, 0, 1);
    material.opacity  			= 1;
    material.specular 			= float3(0, 0, 0);
    material.gloss    			= 0;
    material.emissive 			= float3(0, 0, 0);
    material.wsOffset 			= float3(0, 0, 0);
    material.infestationShell 	= 0;	// Hack to make the infestation shell pass run
    material.scale 				= 1;
    material.ssDistortion 		= float2(0, 0);
    material.transmissionColor 	= float3(1, 1, 1);
    

    so a
    material.albedo  = float3(0, 0,0);
    
    fixed it

    but why does the shader sometimes use these default values and sometimes not?
Sign In or Register to comment.