Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将线渲染为1x1 RenderTarget2D不会更改目标';s像素颜色_C#_Xna_Hlsl_Pixel Shader_Rendertarget - Fatal编程技术网

C# 将线渲染为1x1 RenderTarget2D不会更改目标';s像素颜色

C# 将线渲染为1x1 RenderTarget2D不会更改目标';s像素颜色,c#,xna,hlsl,pixel-shader,rendertarget,C#,Xna,Hlsl,Pixel Shader,Rendertarget,我试图实现的是:我的过程将返回一个巨大的数组,其中有几个唯一的数字反复出现,我需要在CPU上检索和处理这些数字 我尝试渲染成2000x1纹理,然后在CPU上使用RenderTarget2d.GetData()和foreach循环对其进行采样。太慢了:)。所以我避开了我的问题,现在的想法是多次渲染1x1纹理。在两次过程之间,我将扩展着色器中的参数数组,以包含已返回的数字。每个像素将查询数组,如果它包含已出现的数字,则将自行剪裁 现在的问题是,无论渲染什么,像素颜色都不会改变——它总是返回一些随机数

我试图实现的是:我的过程将返回一个巨大的数组,其中有几个唯一的数字反复出现,我需要在CPU上检索和处理这些数字

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

现在的问题是,无论渲染什么,像素颜色都不会改变——它总是返回一些随机数。当我添加
Game.GraphicsDevice.Clear(Color.Transparent)时
在draw调用之前,它开始返回零,尽管着色器代码应该返回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.

您可以尝试使用XYZRHW渲染线条,以渲染到特定像素。这样可以避免几何体被剪裁的情况。你调整过你的视窗了吗?你能不能扩展一下,我不能清楚地理解你的两点:什么是XYZRHW?我试图避免剪切的是写入输出。Position=(0,0,0,0);位置=(1,1,1,1);在顶点着色器中,但没有帮助-像素仍然保持0,0,0,0。其次,请告诉我“调整视口”是什么意思?我认为如果要渲染到另一个维度为屏幕的渲染目标,则必须调整图形设备视口中的大小:。您能试着设置output.Position=(0,0,0,1)吗?XYZRHW是一种vertexformat,用于设置屏幕空间而不是世界空间中的位置,更便于屏幕对齐渲染。哇,我一定要试试这个!为什么它根本不起作用。即使我在整个游戏中将我的视口设置为1像素,它仍然会渲染到屏幕上。Viewport ColViewport=新视口();ColViewport.X=0;ColViewport.Y=0;ColViewport.Width=1;ColViewport.Height=1;Game.graphicsdevelope.Viewport=ColViewport;
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();
    }
}