Home Technical Talk

Problem with HLSL

I am trying to make a Fresnel shader, the problem is that if I move the object from the [0,0,0] position , it looks broken.
I am using 3d Max 2008.
string ParamID = "0x003";    // this string tells 3ds Max to use the DXSAS compiler

float4x4 wvp : WorldViewProjection < string UIWidget = "None"; >;
float4x4 ViewInverse : ViewInverse < string UIWidget = "None"; >;
float4x4 World : WORLD < string UIWidget = "None"; >;

struct app2vertex
{ 
	float4 position		: POSITION;
	float4 normal		: NORMAL;
}; 

struct vertex2pixel
{ 
        float4 position    	: POSITION;
	float3 eyeVec    	: TEXCOORD0;
	float4 normal		: TEXCOORD1;
}; 


vertex2pixel vertex(app2vertex In) 
{ 
	vertex2pixel Out = (vertex2pixel)0;
	Out.position = mul(In.position, wvp);
	Out.normal = mul(In.normal, World);
	//-------------------------------------- 
	float3 worldP = mul(In.position, World);	
	Out.eyeVec = (ViewInverse[3].xyz-worldP);
	return Out; 
} 


float4 pixel(vertex2pixel In) : COLOR 
{ 
	float3 P = normalize(In.eyeVec);
	float3 N = normalize(In.normal);
	float col = 1-dot(P,N);
	return float4(col,col,col,1); 
} 

technique Complete 
{  
	pass simple  
    {		 
	VertexShader = compile vs_1_1 vertex(); 
	ZEnable = true; 
	ZWriteEnable = true; 
 	CullMode = cw; 
	AlphaBlendEnable = false; 
	PixelShader = compile ps_2_0 pixel(); 
	}  
}  

Thanks.

Replies

  • CrazyButcher
    Options
    Offline / Send Message
    CrazyButcher polycounter lvl 18
    Out.normal = mul(In.normal, World);
    

    you are multiplying a float4 with a float4x4 matrix, which means rotation/scale AND translation is applied. But: "Thou shalt not translate your model's normals"

    feed your normals as float3 (thats what they are!) and maybe you need to cast the float4x4 to float3x3 in that mul.
  • KikiAlex
    Options
    Offline / Send Message
    Thanks :) it works !
    string ParamID = "0x003";    // this string tells 3ds Max to use the DXSAS compiler
    
    
    float3 colorB
    <
        string UIName = "Color 1";
        string UIWidget = "color";
    > = {0.9f, 0.7f, 0.4f};
    
    
    
    float3 colorA
    <
        string UIName = "Color 2";
        string UIWidget = "color";
    > = {0.7f, 0.5f, 0.5f};
    
    
    float4x4 wvp : WorldViewProjection < string UIWidget = "None"; >;
    float4x4 ViewInverse : ViewInverse < string UIWidget = "None"; >;
    float3x3 World : WORLD < string UIWidget = "None"; >;
    struct app2vertex
    { 
    	float4 position		: POSITION;
    	float4 normal		: NORMAL;
    }; 
    
    struct vertex2pixel
    { 
            float4 position    	: POSITION;
    	float3 eyeVec    	: TEXCOORD0;
    	float3 normal		: TEXCOORD1;
    }; 
    
    
    vertex2pixel vertex(app2vertex In) 
    { 
    	vertex2pixel Out = (vertex2pixel)0;
    	Out.position = mul(In.position, wvp);
    	Out.normal = mul(In.normal,World);
    	//-------------------------------------- 
    	float3 worldP = mul(In.position,World);	
    	Out.eyeVec = (ViewInverse[3].xyz - mul(In.position,World));
    	return Out; 
    } 
    
    
    float4 pixel(vertex2pixel In) : COLOR 
    { 
    	float3 P = normalize(In.eyeVec);
    	float3 N = normalize(In.normal);
    	float col = pow((dot(P,N)),5);
    	float3 col2 = lerp(colorA,colorB,col);
    	return float4(col2,col); 
    } 
    
    technique Complete 
    {  
    	pass simple  
        {		 
    	VertexShader = compile vs_1_1 vertex(); 
    	ZEnable = true; 
    	ZWriteEnable = true; 
     	CullMode = cw; 
    	AlphaBlendEnable = true; 
    	PixelShader = compile ps_2_0 pixel(); 
    	}  
    }  
    

    You are the Man CrazyButcher :)
    Thanks!!
  • CrazyButcher
    Options
    Offline / Send Message
    CrazyButcher polycounter lvl 18
    I'd still recommend passing normals from app to vertex as float3. because its very "unsafe" what the 4th coordinate might be. Most applications feed normals as float3, and the w coordinate is filled by driver (many cases 1.0, but it can be "undefined" as well).
    struct app2vertex
    { 
    	float4 position		: POSITION;
    	float4 normal		: NORMAL; // change this to float3
    }; 
    
Sign In or Register to comment.