Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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# 用于RenderToTarget的XNA第二个图形设备_C#_Xna_Rendertarget - Fatal编程技术网

C# 用于RenderToTarget的XNA第二个图形设备

C# 用于RenderToTarget的XNA第二个图形设备,c#,xna,rendertarget,C#,Xna,Rendertarget,我正在创建一个XNA游戏,它从多个精灵创建随机岛。它在单独的线程中创建它们,然后使用RenderTarget2D将它们编译为单个纹理 要创建RenderTarget2D,我需要一个图形设备。如果我使用自动创建的图形设备,除了主游戏线程中的draw调用与之冲突外,大多数情况下都可以正常工作。在图形设备上使用lock()会导致闪烁,即使这样,纹理有时也无法正确创建 如果我创建自己的图形设备,就不会有冲突,但孤岛永远不会正确渲染,而是呈现出纯黑白。我不知道为什么会这样。基本上,我需要一种方法来创建第二

我正在创建一个XNA游戏,它从多个精灵创建随机岛。它在单独的线程中创建它们,然后使用RenderTarget2D将它们编译为单个纹理

要创建RenderTarget2D,我需要一个图形设备。如果我使用自动创建的图形设备,除了主游戏线程中的draw调用与之冲突外,大多数情况下都可以正常工作。在图形设备上使用lock()会导致闪烁,即使这样,纹理有时也无法正确创建

如果我创建自己的图形设备,就不会有冲突,但孤岛永远不会正确渲染,而是呈现出纯黑白。我不知道为什么会这样。基本上,我需要一种方法来创建第二个图形设备,让我得到相同的结果,而不是黑/白。有人有什么想法吗

下面是我尝试创建第二个图形设备以供TextureBuilder专用的代码:

var presParams = game.GraphicsDevice.PresentationParameters.Clone();
            // Configure parameters for secondary graphics device
            GraphicsDevice2 = new GraphicsDevice(game.GraphicsDevice.Adapter, GraphicsProfile.HiDef, presParams);
下面是我用来将岛屿渲染为单个纹理的代码:

public IslandTextureBuilder(List<obj_Island> islands, List<obj_IslandDecor> decorations, SeaGame game, Vector2 TL, Vector2 BR, int width, int height)
    {
        gDevice = game.Game.GraphicsDevice; //default graphics
        //gDevice = game.GraphicsDevice2 //created graphics

        render = new RenderTarget2D(gDevice, width, height, false, SurfaceFormat.Color, DepthFormat.None);

        this.islands = islands;
        this.decorations = decorations;
        this.game = game;

        this.width = width;
        this.height = height;

        this.TL = TL; //top left coordinate
        this.BR = BR; //bottom right coordinate
    }

    public Texture2D getTexture()
    {
        lock (gDevice)
        {
            //Set render target. Clear the screen.
            gDevice.SetRenderTarget(render);
            gDevice.Clear(Color.Transparent);

            //Point camera at the island
            Camera cam = new Camera(gDevice.Viewport);
            cam.Position = TL;
            cam.Update();

            //Draw all of the textures to render
            SpriteBatch batch = new SpriteBatch(gDevice);
            batch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.Transform);
            {
                foreach (obj_Island island in islands)
                {
                    island.Draw(batch);
                }
                foreach (obj_IslandDecor decor in decorations)
                {
                    decor.Draw(batch);
                }
            }
            batch.End();

            //Clear render target
            gDevice.SetRenderTarget(null);

            //Copy to texture2D for permanant storage
            Texture2D texture = new Texture2D(gDevice, render.Width, render.Height);
            Color[] color = new Color[render.Width * render.Height];
            render.GetData<Color>(color);
            texture.SetData<Color>(color);

            Console.WriteLine("done");

            return texture;
        }
public IslandTextureBuilder(列表岛、列表装饰、SeaGame游戏、Vector2 TL、Vector2 BR、整数宽度、整数高度)
{
gDevice=game.game.GraphicsDevice;//默认图形
//gDevice=game.GraphicsDevice2//创建的图形
render=新的RenderTarget2D(gDevice,width,height,false,SurfaceFormat.Color,DepthFormat.None);
这个岛屿=岛屿;
这个。装饰=装饰;
这个游戏=游戏;
这个。宽度=宽度;
高度=高度;
this.TL=TL;//左上角坐标
this.BR=BR;//右下角坐标
}
公共纹理2d getTexture()
{
锁(gDevice)
{
//设置渲染目标。清除屏幕。
gDevice.SetRenderTarget(渲染);
设备。清晰(颜色。透明);
//岛上的摄像机
摄像机摄像机=新摄像机(gDevice.Viewport);
凸轮位置=TL;
cam.Update();
//绘制所有要渲染的纹理
SpriteBatch批次=新SpriteBatch(gDevice);
batch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend,null,null,null,cam.Transform);
{
foreach(岛屿中的obj_岛)
{
岛.抽(批);
}
foreach(obj_岛装饰装饰)
{
花色画(批);
}
}
batch.End();
//清除渲染目标
gDevice.SetRenderTarget(null);
//复制到texture2D以便永久存储
Texture2D纹理=新纹理2D(gDevice,render.Width,render.Height);
颜色[]颜色=新颜色[render.Width*render.Height];
render.GetData(颜色);
设置数据(颜色);
控制台。写入线(“完成”);
返回纹理;
}
下面是应该发生的事情,有一个透明的背景(如果我使用默认设备,通常会发生)

当默认设备发生冲突并且主线程设法调用Clear()时(即使它也被锁定),就会发生这种情况 NotSoGoodIsland.png(需要10个声誉)

下面是我使用自己的图形设备时发生的情况


提前感谢您提供的帮助!

我可能已经解决了这个问题,方法是将RenderToTarget代码移动到Draw()方法中,并在第一次调用Draw()时从主线程中调用它