I am looking to make a Phong Bump Effect in FX File. I work with Managed DirectX 9 and NVIDIA FX Composer 2.5 to help me with the HLSL.
I try to run the basic template given by NVIDIA on FX Composer. After simplifying it a little bit I reach to this version which, as the original one, fail to compile when I load the FX File in my program.
I load the file with Microsoft.DirectX.Direct3D.Effect.FromFile() method.
Do you know what is wrong with this FX File ?
//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////
float4x4 WorldITXf;
float4x4 WvpXf;
float4x4 WorldXf;
float4x4 ViewIXf;
//// TWEAKABLE PARAMETERS ////////////////////
/// Point Lamp 0 ////////////
float3 Lamp0Pos = {-0.5f,2.0f,1.25f};
float3 Lamp0Color = {1.0f,1.0f,1.0f};
// Ambient Light
float3 AmbiColor = {0.07f,0.07f,0.07f};
float Ks = 0.4;
float SpecExpon = 55.0;
float Bump = 1.0;
//float Kr = 0.5;
//////// COLOR & TEXTURE /////////////////////
texture ColorTexture <
string ResourceName = "default_color.dds";
string ResourceType = "2D";
>;
sampler2D ColorSampler = sampler_state {
Texture = ColorTexture;
FILTER = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
texture NormalTexture <
string ResourceName = "default_bump_normal.dds";
string ResourceType = "2D";
>;
sampler2D NormalSampler = sampler_state {
Texture = NormalTexture;
FILTER = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
//////// CONNECTOR DATA STRUCTURES ///////////
/* data from application vertex buffer */
struct VS_INPUT {
float3 Position : POSITION;
float4 UV : TEXCOORD0;
float4 Normal : NORMAL;
float4 Tangent : TANGENT0;
float4 Binormal : BINORMAL0;
};
/* data passed from vertex shader to pixel shader */
struct VS_OUTPUT {
float4 HPosition : POSITION;
float2 UV : TEXCOORD0;
float3 LightVec : TEXCOORD1;
float3 WorldNormal : TEXCOORD2;
float3 WorldTangent : TEXCOORD3;
float3 WorldBinormal : TEXCOORD4;
float3 WorldView : TEXCOORD5;
};
///////// VERTEX SHADING /////////////////////
/*********** Generic Vertex Shader ******/
VS_OUTPUT std_VS(VS_INPUT IN) {
VS_OUTPUT OUT;
OUT.WorldNormal = mul(IN.Normal,WorldITXf).xyz;
OUT.WorldTangent = mul(IN.Tangent,WorldITXf).xyz;
OUT.WorldBinormal = mul(IN.Binormal,WorldITXf).xyz;
float4 Po = float4(IN.Position.xyz,1);
float3 Pw = mul(Po,WorldXf).xyz;
OUT.LightVec = (Lamp0Pos - Pw);
#ifdef FLIP_TEXTURE_Y
OUT.UV = float2(IN.UV.x,(1.0-IN.UV.y));
#else /* !FLIP_TEXTURE_Y */
OUT.UV = IN.UV.xy;
#endif /* !FLIP_TEXTURE_Y */
OUT.WorldView = normalize(ViewIXf[3].xyz - Pw);
OUT.HPosition = mul(Po,WvpXf);
return OUT;
}
///////// PIXEL SHADING //////////////////////
struct PS_OUTPUT {
float4 Color : COLOR;
};
PS_OUTPUT std_PS(VS_OUTPUT IN) : COLOR {
PS_OUTPUT OUT;
float3 diffContrib;
float3 specContrib;
float3 Ln = normalize(IN.LightVec);
float3 Vn = normalize(IN.WorldView);
float3 Nn = normalize(IN.WorldNormal);
float3 Tn = normalize(IN.WorldTangent);
float3 Bn = normalize(IN.WorldBinormal);
float3 bump = Bump * (tex2D(NormalSampler,IN.UV).rgb - float3(0.5,0.5,0.5));
Nn = Nn + bump.x*Tn + bump.y*Bn;
Nn = normalize(Nn);
float3 Hn = normalize(Vn + Ln);
float4 litV = lit(dot(Ln,Nn),dot(Hn,Nn),SpecExpon);
diffContrib = litV.y * Lamp0Color;
specContrib = litV.y * litV.z * Ks * Lamp0Color;
float3 diffuseColor = tex2D(ColorSampler,IN.UV).rgb;
float3 combineColor = specContrib+(diffuseColor*(diffContrib+AmbiColor));
OUT.Color = float4(combineColor.x, combineColor.y,combineColor.z,1.0f);
return OUT;
}
///// TECHNIQUES /////////////////////////////
technique Main {
pass p0
{
VertexShader = compile vs_2_0 std_VS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
AlphaBlendEnable = false;
CullMode = None;
PixelShader = compile ps_2_0 std_PS();
}
}
Must be some sort of syntax or general form of the file. I try several changes in the PixelShader version 2_0, 2_a, 2_b... but nothing works on this side.
FX Composer debugger point out error on the return OUT; of the PixelShader but I do not see the problem.
Many thanks for your help !