【问题标题】:Microsoft Xna Texture2D and rotationMicrosoft Xna Texture2D 和旋转
【发布时间】:2012-05-10 04:55:32
【问题描述】:

我有一组图像,其中每个图像都需要能够旋转到 90 度、180 度和 270 度。所有这些图像都是 Texture2D 类型。是否有内置功能可以为我完成此任务?或者我应该加载每个图像的额外旋转图像?或者有没有更好的方法来完成这个任务?

【问题讨论】:

    标签: c# rotation xna texture2d


    【解决方案1】:

    您可以在使用SpriteBatch.Draw 将纹理绘制到缓冲区时旋转(和缩放)纹理,尽管您需要指定大部分(或全部)参数。角度以弧度表示。

    SpriteBatch.Begin();
    angle = (float)Math.PI / 2.0f;  // 90 degrees
    scale = 1.0f;
    SpriteBatch.Draw(myTexture, sourceRect, destRect, Color.White, angle,
                     position, scale, SpriteEffects.None, 0.0f);
    SpriteBatch.End();
    

    http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.draw.aspx

    您还可以加载图像的预旋转副本,但您可能会得到通常的过早优化讲座。

    【讨论】:

      【解决方案2】:

      如果您只想旋转 Texture2D 字段而不更改 Draw 方法中的任何内容,您可以使用它(它将输入顺时针旋转 90 度):

      public static Texture2D RotateTexture90Deegrees(Texture2D input)
      {
          Texture2D rotated = null;
          if (input != null)
          {
              rotated = new Texture2D(input.GraphicsDevice, input.Width, input.Height);
              Color[] data = new Color[input.Width * input.Height];
              Color[] rotated_data = new Color[data.Length];
      
              input.GetData<Color>(data);
              var Xcounter = 1;
              var Ycounter = 0;
              for (int i = 0; i < data.Length; i++)
              {
                  rotated_data[i] = data[((input.Width * Xcounter)-1) - Ycounter];
                  Xcounter += 1;
                  if (Xcounter > input.Width)
                  {
                      Xcounter = 1;
                      Ycounter += 1;
                  }
              }
      
              rotated.SetData<Color>(rotated_data);
          }
          return rotated;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-21
        • 1970-01-01
        相关资源
        最近更新 更多