struct a2v {
half4 position : POSITION;
half2 texCoord : TEXCOORD0;
half3 tangent : TANGENT;
half3 binormal : BINORMAL;
half3 normal : NORMAL;
};
struct v2f {
half4 position : POSITION;
half2 texCoord : TEXCOORD0;
float3 eyeVec : TEXCOORD1;
half3 lightVec : TEXCOORD2;
};
half4 blinn2(half3 N,
half3 L,
half3 V,
uniform half4 diffuseColor,
uniform half4 specularColor,
uniform half shininess
)
{
half3 H = normalize(V+L);
half4 lighting = lit(dot(L,N), dot(H,N), shininess);
return diffuseColor*lighting.y + specularColor*lighting.z;
}
v2f v(a2v In,
uniform half4x4 worldViewProj, uniform half4x4 WorldIMatrix, uniform half4 lightPosition,
uniform half4x4 ViewInvTrans,
uniform half4x4 world )
{
v2f Out;
Out.position = mul(In.position, worldViewProj);
Out.texCoord = In.texCoord;
half3x3 objTangentXf;
objTangentXf[0] = In.binormal.xyz;
objTangentXf[1] = -In.tangent.xyz;
objTangentXf[2] = In.normal.xyz;
half4 objSpaceLightPos = mul(lightPosition, WorldIMatrix);
half3 objLightVec = objSpaceLightPos.xyz - In.position.xyz;
Out.lightVec = mul(objTangentXf, objLightVec );
half4 objSpaceEyePos = mul(ViewInvTrans[3], WorldIMatrix);
half3 objEyeVec = objSpaceEyePos.xyz - In.position.xyz;
Out.eyeVec = mul(objTangentXf, objEyeVec);
return Out;
}
float4 f(v2f In,
uniform sampler2D colorTex,
uniform sampler2D bumpTex,
uniform half4 ambient,
uniform half4 diffuseColor,
uniform half4 specularColor,
uniform half shininess,
uniform half4 light1Color,
uniform float offsetBias
) : COLOR
{
half3 V = normalize(In.eyeVec);
half3 L = normalize(In.lightVec);
half2 newV = V.xy;
newV.y = -newV.y;
half height = tex2D(bumpTex, In.texCoord.xy).a;
half2 offset = newV * (height * 2.0 - 1.0) * offsetBias;
half2 newTexCoord = In.texCoord.xy + offset;
half4 colorMap = tex2D(colorTex, newTexCoord);
half3 normal = tex2D(bumpTex, newTexCoord).xyz * 2.0 - 1.0;
half3 N = normal;
half4 C = ambient*colorMap;
C += light1Color * blinn2(N, L, V, colorMap*diffuseColor, specularColor*colorMap.a, shininess);
return C;
}
float4 f2(v2f In,
uniform sampler2D colorTex,
uniform sampler2D bumpTex,
uniform half4 ambient,
uniform half4 diffuseColor,
uniform half4 specularColor,
uniform half shininess,
uniform half4 light1Color,
uniform float offsetBias
) : COLOR
{
half3 V = normalize(In.eyeVec);
half3 L = normalize(In.lightVec);
half2 newV = V.xy;
newV.y = -newV.y;
half height = tex2D(bumpTex, In.texCoord.xy).r;
half2 offset = newV * (height * 2.0 - 1.0) * offsetBias;
half2 newTexCoord = In.texCoord.xy + offset;
half4 colorMap = tex2D(colorTex, newTexCoord);
half3 normal = tex2D(bumpTex, newTexCoord).wyz * 2.0 - 1.0;
normal.z = sqrt(1 - normal.x * normal.x - normal.y * normal.y);
half3 N = normal;
half4 C = ambient*colorMap;
C += light1Color * blinn2(N, L, V, colorMap*diffuseColor, specularColor*colorMap.a, shininess);
return C;
}
sampler2D colorTextureSampler = sampler_state
{
Texture = <colorTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Anisotropic;
AddressU = Wrap;
AddressV = Wrap;
};
sampler2D normalMapSampler = sampler_state
{
Texture = <normalMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Anisotropic;
AddressU = Wrap;
AddressV = Wrap;
};
sampler2D CnormalMapSampler = sampler_state
{
Texture = <CnormalMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Anisotropic;
AddressU = Wrap;
AddressV = Wrap;
};
technique Complete
{
pass envPass
{
VertexShader = compile vs_1_1 v(wvp,worldI,light1Pos,viewInvTrans,world);
ZEnable = true;
ZWriteEnable = true;
CullMode = CW;
AlphaBlendEnable = false;
PixelShader = compile ps_2_0 f(colorTextureSampler,normalMapSampler,ambient,surfColor,specularColor,shininess,light1Color,offset);
}
}
technique Compressed
{
pass envPass
{
VertexShader = compile vs_1_1 v(wvp,worldI,light1Pos,viewInvTrans,world);
ZEnable = true;
ZWriteEnable = true;
CullMode = CW;
AlphaBlendEnable = false;
PixelShader = compile ps_2_0 f2(colorTextureSampler,CnormalMapSampler,ambient,surfColor,specularColor,shininess,light1Color,offset);
}
}