【问题标题】:(XNA) Make a new Texture2D out of another Texture2D(XNA) 用另一个 Texture2D 制作一个新的 Texture2D
【发布时间】:2017-11-29 08:10:19
【问题描述】:

我想使用 for 循环将图像分成多个 texture2ds。

我想做这样的事情:

newTexture = Texture2D.CopyImage(biggerTexture, x, y, width, height);

可以这样做吗?

【问题讨论】:

    标签: c# graphics xna monogame texture2d


    【解决方案1】:

    您是否考虑过使用GetDataSetData

    我只是为了测试目的写了一个扩展方法:

    public static class TextureExtension
    {
        /// <summary>
        /// Creates a new texture from an area of the texture.
        /// </summary>
        /// <param name="graphics">The current GraphicsDevice</param>
        /// <param name="rect">The dimension you want to have</param>
        /// <returns>The partial Texture.</returns>
        public static Texture2D CreateTexture(this Texture2D src, GraphicsDevice graphics, Rectangle rect)
        {
            Texture2D tex = new Texture2D(graphics, rect.Width, rect.Height);
            int count = rect.Width * rect.Height;
            Color[] data = new Color[count];
            src.GetData(0, rect, data, 0, count);
            tex.SetData(data);
            return tex;
        }
    }
    

    你现在可以这样称呼它:

    newTexture = sourceTexture.CreateTexture(GraphicsDevice, new Rectangle(50, 50, 100, 100));
    

    如果你只想绘制纹理的一部分,你可以像 domi1819 建议的那样使用SpriteBatch 重载。

    【讨论】:

    • 我在我的游戏中使用了 sourceRectangle 解决方案,所以现在,我正在为我的游戏在每个图块上绘制整个图块集,但使用 sourceRectangle 对其进行裁剪。不过感谢这个解决方案,如果我需要它,我可能会使用它。
    【解决方案2】:

    我唯一想到的是使用Texture2D.FromStream(),但它会读取整个图像文件,因此在您的情况下它不会真正起作用。

    我为我的一个游戏所做的是围绕Texture2D 创建一个包装器,它只使用接受源矩形和目标矩形的SpriteBatch.Draw() 重载来绘制纹理的特定部分。

    【讨论】:

    • 我在我的游戏中使用了 sourceRectangle 解决方案,所以现在,我正在为我的游戏在每个图块上绘制整个图块集,但使用 sourceRectangle 对其进行裁剪。
    • @emil1000123 仅供参考,当您使用源矩形裁剪它时,您实际上并没有绘制整个纹理,您的 GPU 能够在运行时有效地裁剪。
    猜你喜欢
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多