0

我要实现的目标如下:我的通行证将返回一个巨大的数组,其中有几个重复的唯一数字,我需要在 CPU 上检索和处理。

我尝试渲染成 2000x1 纹理,然后使用 RenderTarget2d.GetData<>() 和 foreach 循环在 CPU 上对其进行采样。它非常慢:)。所以我回避了我的问题,现在的想法是多次渲染到 1x1 纹理。在传递之间,我将在我的着色器中扩展一个参数数组以包含已经返回的数字。如果每个像素包含一个已经出现的数字,则将查询该数组并剪辑自身。

现在的问题是,无论我渲染什么,像素颜色都不会改变——它总是返回一些随机数。当我Game.GraphicsDevice.Clear(Color.Transparent);在绘制调用之前添加时,它开始返回零,尽管着色器代码应该返回 0.2f(或其 0 到 255 等效值)。可能是因为我在其中渲染了太小的内容(我在屏幕上画了一条线并沿线对场景的颜色进行采样)并且它在某些时候会受到影响?

C#/XNA 代码:

                            CollisionRT = new RenderTarget2D(Game.GraphicsDevice, 1, 1, false, SurfaceFormat.Color, DepthFormat.None);

...             
                Game.GraphicsDevice.BlendState = BlendState.AlphaBlend;
                Game.GraphicsDevice.SetRenderTarget(CollisionRT);
                Game.GraphicsDevice.Clear(Color.Transparent);
                Game.GraphicsDevice.DepthStencilState = DepthStencilState.None;

                foreach (ModelMesh mesh in PPCBulletInvis.Model.Meshes)
                    // PPCBulletInvis is a line that covers 1/18 of the screen (approx).
                {
                    foreach (Effect effect in mesh.Effects)
                    {
                        //effect.Parameters["Texture"].SetValue(vfTex);
                        effect.Parameters["halfPixel"].SetValue(halfPixel);
                        effect.Parameters["sceneMap"].SetValue(sceneRT);
                        effect.Parameters["World"].SetValue(testVWall.World);
                        effect.Parameters["View"].SetValue(camera.View);
                        effect.Parameters["Projection"].SetValue(camera.Projection);
                    }
                    mesh.Draw();
                }

                Game.GraphicsDevice.SetRenderTarget(null);

                Rectangle sourceRectangle =
                    new Rectangle(0, 0, 1, 1);

                Color[] retrievedColor = new Color[1];
                CollisionRT.GetData<Color>(retrievedColor);

                Console.WriteLine(retrievedColor[0].R); // Returns zeroes.

着色器代码:

float4x4 World;
float4x4 View;
float4x4 Projection;

texture sceneMap;
sampler sceneSampler = sampler_state
{
    Texture = (sceneMap);
    AddressU = CLAMP;
    AddressV = CLAMP;
    MagFilter = POINT;
    MinFilter = POINT;
    Mipfilter = POINT;
};

float2 halfPixel;

struct VS_INPUT
{
    float4 Position     : POSITION0;
};
struct VS_OUTPUT
{
    float4 Position         : POSITION0;
    float4 ScreenPosition   : TEXCOORD2;
};


VS_OUTPUT VertexShaderFunction(VS_INPUT input)
{
    VS_OUTPUT output;

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

    output.ScreenPosition = output.Position;
    return output;
}

float4 PixelShaderFunction(VS_OUTPUT input) : COLOR0
{
    input.ScreenPosition.xy /= input.ScreenPosition.w;

    float2 screenTexCoord = 0.5f * (float2(input.ScreenPosition.x,-input.ScreenPosition.y) + 1);

    screenTexCoord -=halfPixel;

    return (0.2f,0.2f,0.2f,0.2f);//tex2D(sceneSampler, screenTexCoord);
}

technique Technique1
{
    pass Pass1
    {
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}
4

0 回答 0