我想要实现的目标如下:我的传递将返回一个巨大的数组,其中包含一些重复的重复数字,我需要在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();
}
}