1

我正在使用 RenderTarget2D 来绘制我的 3D 世界,然后我使用着色器来添加灯光效果等。如何获取像素着色器中的深度信息?我是着色器编程的新手,我对着色器给出的命令一无所知。

我的着色器:

float4x4 World;
float4x4 View;
float4x4 Projection;
texture output;
texture zBuffer;
float2 screenSize;
bool invert;

texture ModelTexture;

sampler2D textureSampler = sampler_state {
    Texture = (ModelTexture);
    MagFilter = Linear;
    MinFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};


struct VertexShaderInput
{
    float4 Position : POSITION0;
    float2 TextureCoordinate : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 TextureCoordinate : TEXCOORD0;        
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
    output.TextureCoordinate = input.TextureCoordinate;

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input, float2 vPos : VPOS) : COLOR0
{
    int pixelCoordsY = vPos.y * screenSize.y; //get the y coordinate of the pixel 
    int pixelCoordsX = vPos.x * screenSize.x; //get the x coordinate of the pixel 


    float4 textureColor = tex2D(textureSampler, input.TextureCoordinate);
    if (invert)
    {
        textureColor.r = 1 - textureColor.r;
        textureColor.g = 1 - textureColor.g;
        textureColor.b = 1 - textureColor.b;
    }

    return textureColor;
}

technique Technique1
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

我必须在 RenderTarget 设置参数吗?非常感谢!

4

0 回答 0