XNA两个方向的投影纹理(一个是方向相反)

时间:2012-08-05 16:29:31

标签: xna shader hlsl deferred

我创建了Projector:

Matrix.CreateLookAt(position,direction,Vector3.Up); Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),1,1,2);

我传递给这些矩阵的着色器乘法(在着色器中称为View),然后在着色器中我做:

float4 proj(float3 Position)
{
    float4 texCoord = mul(float4(Position, 1.0), View);
    texCoord.x = ( (texCoord.x / texCoord.w)/2) + 0.5;
    texCoord.y = (-(texCoord.y / texCoord.w)/2) + 0.5;
    return tex2D(shape, texCoord.xy);
}

纹理的uvw是Clamped。我在deffered阴影的光阶段使用它。得到的图像(红色箭头是正确的方向): image

我该怎么做才能让它朝正确的方向前进?

解决: 问题是反投影简直解决了:

float4 proj(float3 Position)
{
    float4 texCoord = mul(float4(Position, 1.0), View);
        if(texCoord.z < 0)
            return 0;
    texCoord.x = ( (texCoord.x / texCoord.w)/2) + 0.5;
    texCoord.y = (-(texCoord.y / texCoord.w)/2) + 0.5;
    return tex2D(shape, texCoord.xy);
}

1 个答案:

答案 0 :(得分:0)

问题在于反投影,只需解决:

float4 proj(float3 Position)
{
    float4 texCoord = mul(float4(Position, 1.0), View);
        if(texCoord.z < 0)
            return 0;
    texCoord.x = ( (texCoord.x / texCoord.w)/2) + 0.5;
    texCoord.y = (-(texCoord.y / texCoord.w)/2) + 0.5;
    return tex2D(shape, texCoord.xy);
}