【问题标题】:Working with primitives in XNA在 XNA 中使用原语
【发布时间】:2012-12-11 05:13:30
【问题描述】:

也许我正在尝试以一种非设计的方式使用 XNA,但我非常希望能够绘制我自己的图元并将它们保存为位图、纹理 2D 或任何可以容纳对我来说几条二维线。我的目标是编写一个程序化创建纹理的游戏,而不是仅仅从内容管理器加载。

如果有帮助的话,我已经创建了一个存储多边形的 Shape 类,我可能只是告诉 spriteBatch 画出每条线,但我试图通过在某处存储常用形状来进行优化。

【问题讨论】:

    标签: bitmap xna drawing primitive texture2d


    【解决方案1】:

    您可以为您的任务使用RenderTarget。像这样的:

    // var to store your drawing
    Texture2D newShape;
    
    // drawing will be on this target
    RenderTarget2D rt = new RenderTarget2D(GraphicsDevice, width, height);
    SpriteBatch sb = new SpriteBatch(GraphicsDevice);
    
    // set to render all to render target
    GraphicsDevice.SetRenderTarget(rt); 
    
    GraphicsDevice.Clear(Color.Transparent); 
    
    sb.Begin();
    
    // Draw what you want here.
    
    sb.End()
    
    // Return to drawing on "main" buffer
    GraphicsDevice.SetRenderTarget(null);
    
    // Save the texture you just drawn
    newShape = rt;
    

    【讨论】:

    • 太棒了!我只需要在添加以前从未添加过的新形状时执行此操作。当我对此进行研究时,我发现经常启动和停止 sprite-batch 来做这种事情是一个坏主意。我将对此进行测试并分享结果。
    • 拜托,我很想看看你的想法
    • 嗯。我无法让它发挥作用。似乎在精灵批处理运行时更改渲染目标会产生一些非常疯狂的结果。
    • @MaxKessler 我不能说我经常使用这种技术,但我已经成功地使用了它。但是我没有在 SpriteBatch 运行中更改 RenderTarget,您应该尝试在单独的批处理运行中将“动态”纹理创建和最终渲染分开。
    猜你喜欢
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多