Re: 高光贴图+法线贴图
最近整理好了.fx文件,公布一下
float SpecalurPow = 8;
float4 posLight : POSITION = { 100, 1000, -1000, 1.0 }; // light(world space)
float4 colorLight = {1.0, 1.0, 1.0, 1.0 }; // light's color
float4 colorAmbient = {0.2, 0.2, 0.2, 0.2 }; // ambient light color
float3 posCamera : CAMERAPOSITION; // camera position
float4x4 matWorld : WORLDMATRIXARRAY;
float4x4 matInvWorld;// : MATRIX;
float4x4 matViewProj : WORLDVIEWPROJECTION;
texture tex_basetex;
texture tex_normalmap;
texture tex_specularmap;
///
struct VS_INPUT_SN
{
float3 Pos : POSITION;
float3 Normal : NORMAL;
float2 Tex0 : TEXCOORD0;
float3 Tangent : TANGENT;
float3 BiNormal : BINORMAL;
};
struct VS_OUTPUT_SN
{
float4 Pos : POSITION;
float2 Tex0 : TEXCOORD0;
float3 LightDir : TEXCOORD1;
float3 Eye : TEXCOORD2;
float3 HalfAngle : TEXCOORD3;
};
sampler BaseMapSampler=sampler_state
{
texture=(tex_basetex);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
sampler SpecularMapSampler=sampler_state
{
texture=(tex_specularmap);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
sampler NormalMapSampler=sampler_state
{
texture=(tex_normalmap);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
///
// specular + normalmap
VS_OUTPUT_SN vshade_sn(VS_INPUT_SN In)
{
VS_OUTPUT_SN Out;
float3 Normal = normalize( In.Normal );
float3 Tangent = normalize( In.Tangent );
float3 BiNormal = normalize( In.BiNormal );
// TBN 坐标系
half3x3 objTangentXf;
objTangentXf[0] = Tangent.xyz;
objTangentXf[1] = BiNormal.xyz;
objTangentXf[2] = Normal.xyz;
half4 objSpaceLightPos = mul( posLight, matInvWorld );
half3 objLightVec = objSpaceLightPos.xyz - In.Pos.xyz;
Out.LightDir.xyz = mul( objTangentXf, objLightVec );
half4 objSpaceEyePos = mul( posCamera, matInvWorld );
half3 objEyeVec = objSpaceEyePos.xyz - In.Pos.xyz;
Out.Eye = mul(objTangentXf, objEyeVec);
Out.HalfAngle = normalize(Out.Eye)+normalize(Out.LightDir.xyz);
Out.Tex0 = In.Tex0.xy;
Out.Pos = mul( float4(In.Pos, 1.0f), matViewProj );
return Out;
}
float4 pshade_sn(VS_OUTPUT_SN In):COLOR
{
float4 baseTexture = tex2D(BaseMapSampler, In.Tex0);
float4 specTexture = tex2D(SpecularMapSampler, In.Tex0);
float4 bumpTexture = tex2D(NormalMapSampler, In.Tex0);
float3 bump = 2*bumpTexture.xyz - 1;
float3 L = normalize( In.LightDir.xyz ); // light dir
float3 N = normalize( bump );
float3 E = normalize( In.Eye.xyz ); // eye
float3 H = normalize( In.HalfAngle.xyz ); // halfangle vector
float NdotL = saturate( dot(N, L) );
float NdotH = saturate( dot(N, H) );
float NdotE = saturate( dot(N, E) );
float EdotH = saturate( dot(E, H) );
float3 Lighting = lit( NdotL, NdotH, SpecalurPow );
float3 specular = Lighting.z*specTexture;
float4 Out;
Out.xyz = saturate( colorAmbient.xyz + Lighting.y*colorLight )*baseTexture;
Out.xyz += specular;
Out.a = baseTexture.a;
return Out;
}
technique model_specular_normalmap
{
pass p0
{
vertexshader = compile vs_2_0 vshade_sn();
pixelshader = compile ps_2_0 pshade_sn();
}
}
加入了shadowmap
//float4 mtrlSpecalur = {1.0, 1.0, 1.0, 1.0 };
int bSpecularNormal = 1; // 是否使用高光贴图+法线贴图
float SpecalurPow = 8;
float4 posLight : POSITION = { 0.577, 0.577, -0.577, 1.0 }; // light(world space)
float4 colorLight = {1.0, 1.0, 1.0, 1.0 }; // light's color
float4 colorAmbient = {0.2, 0.2, 0.2, 0.2 }; // ambient light color
float3 posCamera : CAMERAPOSITION; // camera position
//float3 posLookAt : POSITION;
//float4x4 matWorld : WORLDMATRIXARRAY;
float4x4 matWorldView;
float4x4 matInvWorld;// : MATRIX;
//float4x4 matView : WORLDVIEW;
float4x4 matWorldViewProj : WORLDVIEWPROJECTION;
// shadow map
#define SHADOW_EPSILON 0.0005f
#define SHADOW_SMAP_SIZE 512
float4x4 matViewToLightProj;
float4x4 matLightViewProj;
texture tex_basetex< string name = "rockwall.dds"; >;
texture tex_normalmap< string name = "aaa.nmf"; >;
texture tex_specularmap< string name = "aaa.smf"; >;
texture tex_shadowmap;
///
struct VS_INPUT_SN
{
float3 Pos : POSITION;
float3 Normal : NORMAL;
float2 Tex0 : TEXCOORD0;
float3 Tangent : TANGENT;
float3 BiNormal : BINORMAL;
};
struct VS_OUTPUT_SN
{
float4 Pos : POSITION;
float2 Tex0 : TEXCOORD0;
float3 LightDir : TEXCOORD1;
float3 Eye : TEXCOORD2;
float3 HalfAngle : TEXCOORD3;
// shadow map
float4 PosLight : TEXCOORD4;
};
sampler BaseMapSampler=sampler_state
{
texture=;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
sampler SpecularMapSampler=sampler_state
{
texture=;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
sampler NormalMapSampler=sampler_state
{
texture=;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
sampler ShadowMapSampler=sampler_state
{
texture=;
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
AddressU = Clamp;
AddressV = Clamp;
};
///
// specular + normalmap
VS_OUTPUT_SN vshade_sn(VS_INPUT_SN In)
{
VS_OUTPUT_SN Out;
if( bSpecularNormal ) // 是否使用高光贴图+法线贴图
{
float3 Normal = normalize( In.Normal );
float3 Tangent = normalize( In.Tangent );
float3 BiNormal = normalize( In.BiNormal );
// TBN 坐标系
half3x3 objTangentXf;
objTangentXf[0] = Tangent.xyz;
objTangentXf[1] = BiNormal.xyz;
objTangentXf[2] = Normal.xyz;
half4 objSpaceLightPos = mul( posLight, matInvWorld );
half3 objLightVec = objSpaceLightPos.xyz - In.Pos.xyz;
Out.LightDir.xyz = mul( objTangentXf, objLightVec );
half4 objSpaceEyePos = mul( posCamera, matInvWorld );
half3 objEyeVec = objSpaceEyePos.xyz - In.Pos.xyz;
Out.Eye = mul(objTangentXf, objEyeVec);
Out.HalfAngle = normalize(Out.Eye)+normalize(Out.LightDir.xyz);
}
else
{
Out.LightDir = normalize( In.Normal );
//Out.LightDir = normalize( mul( float4(In.Normal,1.0f), matWorld ) ); // 记录normal
Out.Eye = 0.0f;
Out.HalfAngle = 0.0f;
}
// shadow map
Out.PosLight = mul( float4(In.Pos, 1.0f), matWorldView );
Out.PosLight = mul( Out.PosLight, matViewToLightProj );
Out.Tex0 = In.Tex0.xy;
Out.Pos = mul( float4(In.Pos, 1.0f), matWorldViewProj );
return Out;
}
float4 pshade_sn(VS_OUTPUT_SN In):COLOR
{
float4 baseTexture = tex2D(BaseMapSampler, In.Tex0);
// shadow map
//transform from RT space to texture space.
float2 ShadowTexC = 0.5 * In.PosLight.xy / In.PosLight.w + float2( 0.5, 0.5 );
ShadowTexC.y = 1.0f - ShadowTexC.y;
float depth = In.PosLight.z / In.PosLight.w;
// shadow虚化
float diffuse = (tex2D(ShadowMapSampler, ShadowTexC) + SHADOW_EPSILON < depth )? 1.0f:saturate(0.05f*depth);
float4 Out;
if( bSpecularNormal ) // 是否使用高光贴图+法线贴图
{
float4 specTexture = tex2D(SpecularMapSampler, In.Tex0);
float4 bumpTexture = tex2D(NormalMapSampler, In.Tex0);
float3 bump = 2*bumpTexture.xyz - 1;
float3 L = normalize( In.LightDir.xyz ); // light dir
float3 N = normalize( bump );
float3 E = normalize( In.Eye.xyz ); // eye
float3 H = normalize( In.HalfAngle.xyz ); // halfangle vector
float NdotL = saturate( dot(N, L) );
float NdotH = saturate( dot(N, H) );
float NdotE = saturate( dot(N, E) );
float EdotH = saturate( dot(E, H) );
float3 Lighting = lit( NdotL, NdotH, SpecalurPow );
float3 specular = Lighting.z*specTexture;
Out.xyz = saturate( colorAmbient.xyz + Lighting.y*colorLight )*baseTexture.xyz;
Out.xyz += specular;
Out.a = baseTexture.a;
}
else
{
float3 L = normalize( posLight.xyz ); // light dir
float3 N = normalize( In.LightDir );
float NdotL = saturate( dot(N, L) );
Out.xyz = saturate(colorAmbient.xyz + (1-colorAmbient)*NdotL*colorLight*diffuse)*baseTexture.xyz;
Out.a = baseTexture.a;
}
return Out;
}
///
// shadow map
void VertShadow( float4 Pos : POSITION,
float3 Normal : NORMAL,
out float4 oPos : POSITION,
out float2 Depth : TEXCOORD0 )
{
// Compute the projected coordinates
oPos = mul( Pos, matLightViewProj );
// Store z and w in our spare texcoord
Depth.xy = oPos.zw;
}
void PixShadow( float2 Depth : TEXCOORD0,
out float4 Color : COLOR )
{
// Depth is z / w
Color = Depth.x / Depth.y;
}
///
// specular + normalmap
technique model_specular_normalmap
{
pass p0
{
vertexshader = compile vs_2_0 vshade_sn();
pixelshader = compile ps_2_0 pshade_sn();
}
}
///
// shadow map
technique model_shadowmap
{
pass p0
{
vertexshader = compile vs_1_1 VertShadow();
pixelshader = compile ps_2_0 PixShadow();
}
}