I'm trying to use the "metal" shader from Nvidia and the effect works well except that the underlying texture appears to be flipped or something. I'm not even sure what's wrong with the texture, but it's in completely the wrong place. There's nothing wrong with the texture because it looks fine with the shader is NOT applied, but as soon as I apply the shader it gets screwed up. If this doesn't make much sense I can post a screenshot.
I'm very, very new to shaders, so I'm not sure how to solve this problem. Here's the Nvidia shader source. Can anyone help me?
/*********************************************************************NVMH3****
File: $Id: //sw/devtools/FXComposer2/2.0/SDK/MEDIA/HLSL_FX/metal.fx#1 $
Copyright NVIDIA Corporation 2007
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
% A phong-shaded metallic surface lit from either
% a point or directional source.
% Textured, untextured, quadratic falloff or not
keywords: material classic
******************************************************************************/
#define FLIP_TEXTURE_Y
float Script : STANDARDSGLOBAL <
string UIWidget = "none";
string ScriptClass = "object";
string ScriptOrder = "standard";
string ScriptOutput = "color";
string Script = "Technique=Technique?SimplePS:TexturedPS:SimpleFalloffPS:TexturedFalloffPS;";
> = 0.8;
/************* UN-TWEAKABLES **************/
//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////
float4x4 WorldITXf : WorldInverseTranspose < string UIWidget="None"; >;
float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >;
float4x4 WorldXf : World < string UIWidget="None"; >;
float4x4 ViewIXf : ViewInverse < string UIWidget="None"; >;
/************* TWEAKABLES **************/
// "DirPos" Lamp 0 /////////
float4 Lamp0DirPos : Position < // or direction if W==0
string Object = "Light0";
string UIName = "Lamp 0 Position/Direction";
string Space = "World";
> = {-0.5f,2.0f,1.25f,1.0};
float3 Lamp0Color : Specular <
string UIName = "Lamp 0";
string Object = "Light0";
string UIWidget = "Color";
> = {1.0f,1.0f,1.0f};
float Lamp0Intensity <
string UIWidget = "slider";
float UIMin = 1.0;
float UIMax = 10000.0f;
float UIStep = 0.01;
string UIName = "Lamp 0 Quadratic Intensity";
> = 1.0f;
float3 AmbiColor : Ambient <
string UIName = "Ambient Light";
string UIWidget = "Color";
> = {0.07f, 0.07f, 0.07f};
float3 SurfColor : DIFFUSE <
string UIName = "Surface";
string UIWidget = "Color";
> = {1.0f, 0.6f, 0.1f};
float SpecExpon : SpecularPower <
string UIWidget = "slider";
float UIMin = 1.0;
float UIMax = 128.0;
float UIStep = 1.0;
string UIName = "Specular Power";
> = 12.0;
float Kd <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.05;
string UIName = "Diffuse";
> = 0.1;
//////////
texture ColorTexture : DIFFUSE <
string ResourceName = "default_color.dds";
string UIName = "Diffuse Texture";
string ResourceType = "2D";
>;
sampler2D ColorSampler = sampler_state {
Texture = <ColorTexture>;
MinFilter = Linear;
MipFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
/************* DATA STRUCTS **************/
/* data from application vertex buffer */
struct appdata {
float3 Position : POSITION;
float4 UV : TEXCOORD0;
float4 Normal : NORMAL;
float4 Tangent : TANGENT0;
float4 Binormal : BINORMAL0;
};
/* data passed from vertex shader to pixel shader */
struct vertexOutput {
float4 HPosition : POSITION;
float2 UV : TEXCOORD0;
float3 LightVec : TEXCOORD1;
float3 WorldNormal : TEXCOORD2;
float3 WorldTangent : TEXCOORD3;
float3 WorldBinormal : TEXCOORD4;
float3 WorldView : TEXCOORD5;
};
/*********** vertex shader for pixel-shaded versions ******/
/*********** Generic Vertex Shader ******/
vertexOutput std_dp_VS(appdata IN) {
vertexOutput OUT = (vertexOutput)0;
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;
if (Lamp0DirPos.w == 0) {
OUT.LightVec = -normalize(Lamp0DirPos.xyz);
} else {
// we are still passing a (non-normalized) vector
OUT.LightVec = Lamp0DirPos.xyz - 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 shaders ********/
void metal_shared(vertexOutput IN,
float3 LightColor,
out float3 DiffuseContrib,
out float3 SpecularContrib)
{
float3 Ln = normalize(IN.LightVec.xyz);
float3 Nn = normalize(IN.WorldNormal);
float3 Vn = normalize(IN.WorldView);
float3 Hn = normalize(Vn + Ln);
float4 litV = lit(dot(Ln,Nn),dot(Hn,Nn),SpecExpon);
DiffuseContrib = litV.y * Kd * LightColor + AmbiColor;
SpecularContrib = litV.z * LightColor;
}
float4 metalPS(vertexOutput IN) : COLOR {
float3 diffContrib;
float3 specContrib;
metal_shared(IN,Lamp0Color,diffContrib,specContrib);
float3 result = SurfColor * (specContrib + diffContrib);
return float4(result,1);
}
float4 metalPS_t(vertexOutput IN) : COLOR {
float3 diffContrib;
float3 specContrib;
metal_shared(IN,Lamp0Color,diffContrib,specContrib);
float3 map = tex2D(ColorSampler,IN.UV).xyz;
float3 result = SurfColor * map * (specContrib + diffContrib);
return float4(result,1);
}
// same as above with the addition of quadratic falloff
float4 metalQPS(vertexOutput IN) : COLOR {
float3 diffContrib;
float3 specContrib;
float3 Cl = (Lamp0Intensity/dot(IN.LightVec.xyz,IN.LightVec.xyz)) * Lamp0Color;
metal_shared(IN,Cl,diffContrib,specContrib);
float3 result = SurfColor * (specContrib + diffContrib);
return float4(result,1);
}
float4 metalQPS_t(vertexOutput IN) : COLOR {
float3 diffContrib;
float3 specContrib;
float3 Cl = (Lamp0Intensity/dot(IN.LightVec.xyz,IN.LightVec.xyz)) * Lamp0Color;
metal_shared(IN,Cl,diffContrib,specContrib);
float3 map = tex2D(ColorSampler,IN.UV).xyz;
float3 result = SurfColor * map * (diffContrib+specContrib);
return float4(result,1);
}
//////////////////////////////////////////////////////////////////////
// TECHNIQUES ////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
technique SimplePS {
pass p0 {
VertexShader = compile vs_2_0 std_dp_VS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a metalPS();
}
}
technique TexturedPS {
pass p0 {
VertexShader = compile vs_2_0 std_dp_VS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a metalPS_t();
}
}
// pixel shaded, quadratic falloff
technique SimpleFalloffPS {
pass p0 {
VertexShader = compile vs_2_0 std_dp_VS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a metalQPS();
}
}
technique TexturedFalloffPS {
pass p0 {
VertexShader = compile vs_2_0 std_dp_VS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a metalQPS_t();
}
}
/***************************** eof ***/