HLSL与贴图的问题
各位:
我下载了一个PSSM的DEMO(http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/)
然后加入了DXSDK的那个木头小螺旋桨飞机,但贴图无法正确地贴上去,感觉上是错位了。请各位帮忙看看我改写的HLSL是否有问题:
#define SMAP_SIZE 64//512
#define SHADOW_EPSILON 0.005f
float4x4 g_mViewProj;
void VS_RenderShadowMap(
float4 vPos : POSITION,
out float4 vPosOut : POSITION,
out float3 vPixelOut : TEXCOORD0)
{
// pass vertex position through as usual
vPosOut = mul(vPos, g_mViewProj);
// output pixel pos
vPixelOut=vPosOut.xyz;
}
float4 PS_RenderShadowMap(float3 vPixelPos : TEXCOORD0): COLOR
{
// write z coordinate (linearized depth) to texture
return vPixelPos.z;
}
// This technique is used when rendering meshes to the shadowmap
//
technique RenderShadowMap
{
pass p0
{
// render back faces to hide artifacts
CullMode = CW;
VertexShader = compile vs_2_0 VS_RenderShadowMap();
PixelShader = compile ps_2_0 PS_RenderShadowMap();
}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
float3 g_vLightDir;
float3 g_vLightColor;
float3 g_vAmbient;
float g_fShadowMapSize;
float4x4 g_mShadowMap;
float4x4 g_mWorldView;
float4x4 g_mProj;
float4x4 g_mViewToLightProj; // Transform from view space to light projection space
float4 g_vMaterial;
texture g_txScene;
texture g_txShadow;
// no filtering in floating point texture
sampler2D g_samShadowMap =
sampler_state
{
MinFilter = Point;
MagFilter = Point;
MipFilter = None;
AddressU = Border;
AddressV = Border;
BorderColor = 0xFFFFFFFF;
};
sampler2D g_samScene =
sampler_state
{
Texture = <g_txScene>;
MinFilter = Point;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D g_samShadow =
sampler_state
{
Texture = <g_txShadow>;
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
AddressU = Clamp;
AddressV = Clamp;
};
void VS_Shadowed(
in float4 vPos : POSITION,
in float3 vNormal : NORMAL,
in float fAmbientIn : TEXCOORD0,
in float2 iTex : TEXCOORD1,
out float4 vPosOut : POSITION,
out float4 vShadowTex : TEXCOORD0,
out float fAmbientOut : TEXCOORD1,
out float2 Tex : TEXCOORD2,
out float3 vDiffuse : COLOR0)
{
// pass vertex position through as usual
vPosOut = mul(vPos, g_mViewProj);
// calculate per vertex lighting
vDiffuse = g_vLightColor * saturate(dot(-g_vLightDir, vNormal));
// coordinates for shadowmap
vShadowTex = mul(vPos, g_mShadowMap);
// ambient occlusion
fAmbientOut = saturate(0.5f+fAmbientIn);
Tex=mul(iTex, g_mViewProj);
}
float4 PS_Shadowed(
float4 vShadowTex : TEXCOORD0,
float fAmbientOcclusion : TEXCOORD1,
float2 Tex : TEXCOORD2,
float4 vDiffuse : COLOR0) : COLOR
{
float fTexelSize=1.0f/g_fShadowMapSize;
// project texture coordinates
vShadowTex.xy/=vShadowTex.w;
// 2x2 PCF Filtering
//
float fShadow[4];
//fShadow[0] = (vShadowTex.z < tex2D(g_samShadowMap, vShadowTex).r);
//fShadow[1] = (vShadowTex.z < tex2D(g_samShadowMap, vShadowTex + float2(fTexelSize,0)).r);
//fShadow[2] = (vShadowTex.z < tex2D(g_samShadowMap, vShadowTex + float2(0,fTexelSize)).r);
//fShadow[3] = (vShadowTex.z < tex2D(g_samShadowMap, vShadowTex + float2(fTexelSize,fTexelSize)).r);
fShadow[0] = (vShadowTex.z < tex2D(g_samShadow, vShadowTex).r);
fShadow[1] = (vShadowTex.z < tex2D(g_samShadow, vShadowTex + float2(fTexelSize,0)).r);
fShadow[2] = (vShadowTex.z < tex2D(g_samShadow, vShadowTex + float2(0,fTexelSize)).r);
fShadow[3] = (vShadowTex.z < tex2D(g_samShadow, vShadowTex + float2(fTexelSize,fTexelSize)).r);
float2 vLerpFactor = frac(g_fShadowMapSize * vShadowTex);
float fLightingFactor = lerp(lerp( fShadow[0], fShadow[1], vLerpFactor.x ),
lerp( fShadow[2], fShadow[3], vLerpFactor.x ),
vLerpFactor.y);
// multiply diffuse with shadowmap lookup value
vDiffuse*=fLightingFactor;
// final color
float4 vColor=1;
vColor.rgb = saturate(g_vAmbient*fAmbientOcclusion + vDiffuse).rgb;
vDiffuse= tex2D( g_samScene, Tex );
return vColor*vDiffuse;
//return vDiffuse;
}
// This technique is used to render the final shadowed meshes
//
technique Shadowed
{
pass p0
{
// render front faces
CullMode = CCW;
VertexShader = compile vs_2_0 VS_Shadowed();
PixelShader = compile ps_2_0 PS_Shadowed();
}
}
[解决办法]
那你检查一下iTex 是否输入正确,可能vertex declar错误,也可能顶点定义的和shader不匹配。尤其是直接用drawsubset可能出现这种问题。如果使用drawsubset,你需要重新定义自己的vertex declare和vertexbuffer
[解决办法]
嗯,应该是顶点格式和shader中的声明不匹配。
顶点格式里面纹理坐标是TEXCOORD0,在shader里面变成TEXCOORD1了。