XNA 4.0 绘制旋转/扭曲纹理

发布于 2024-12-08 08:22:54 字数 296 浏览 1 评论 0原文

大家好。 我希望你能帮助我解答我的问题。

有没有办法像这样转换或直接渲染精灵?

示例 不使用3d。我知道它可以很容易地在 3d 中完成,但我正在从事的项目根本不使用 3d,所以我真的不想仅仅因为这个小东西就包含 3d...

(示例图像是来自一些随机游戏)

所以基本上我需要的是: 将精灵视为矩形,然后以自由方式对其进行变换,这意味着我可以将该精灵的点设置为任何坐标,而不仅仅是矩形。

提前致谢。

Good day everyone.
I hope you can help me with my question.

Is there a way to transform or directly render sprite like so?

example
Without using 3d. I know it can be easily done in 3d, but the project I'm working on doesn't use 3d at all, so I don't really want to also include 3d just because of that small thing...

(example image is from some random game)

So basically what I need:
Take a sprite as a rectangle, and then transform it in a free way, meaning that I can set points of that sprite to any coorditates, not just as rectangle.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦忆晨望 2024-12-15 08:22:54

该图像中使用的技术是 raycasting 2d。首次用于 Wolfenstein 3d,并在 2D 上实现了假 3D。

在这里你可以找到一个教程 http://lodev.org/cgtutor/

虽然这不是你想要的。

实现您想要的效果的最佳方法是定义两个三角形并使用 GraphicsDevice.DrawUserPrimitives 和 basiceffect 来绘制它。

   // Init Triangles with four points A,B,C and D

   VertexPOsitionTexture[] Vertex = new VertexPositionTexture[6];
   Vertex[0].Position = (A.X,A.Y,0);
   Vertex[1].Position = (B.X,B.Y,0);
   Vertex[2].Position = (C.X,C.Y,0);
   Vertex[3].Position = (C.X,C.Y,0);
   Vertex[4].Position = (B.X,B.Y,0);
   Vertex[5].Position = (D.X,D.Y,0);

   Vertex[0].Texture= (0,0);
   Vertex[1].Texture= (1,0);
   Vertex[2].Texture= (0,1);
   Vertex[3].Texture= (0,1);
   Vertex[4].Texture= (1,0);
   Vertex[5].Texture= (1,1);

   // Init Effect from http://blogs.msdn.com/b/shawnhar/archive/2010/04/05/spritebatch-and-custom-shaders-in-xna-game-studio-4-0.aspx

   Matrix projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
   Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);


   BasicEffect effect = new BasicEffect();

   effect.Texture = your texture;
   effect.TextureEnabled = true;
   effect.World = Matrix.Identity;
   effect.View = Matrix.Identity;
   effect.Projection = halfPixelOffset * projection;

   // Draw Triangles
   effect.Apply():
   GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(vertex, TriangleList, ...,...);

这段代码必须被理解为伪代码,它没有经过测试,但它显示了要完成的相关操作。

The technique used in that image is raycasting 2d. that was used first time with wolfenstein 3d, and implements a fake 3d over 2D.

here you can find a tutotial http://lodev.org/cgtutor/

though this is not what you want.

the best way to achieve what you want is define two triangles and use GraphicsDevice.DrawUserPrimitives with a basiceffect to draw it.

   // Init Triangles with four points A,B,C and D

   VertexPOsitionTexture[] Vertex = new VertexPositionTexture[6];
   Vertex[0].Position = (A.X,A.Y,0);
   Vertex[1].Position = (B.X,B.Y,0);
   Vertex[2].Position = (C.X,C.Y,0);
   Vertex[3].Position = (C.X,C.Y,0);
   Vertex[4].Position = (B.X,B.Y,0);
   Vertex[5].Position = (D.X,D.Y,0);

   Vertex[0].Texture= (0,0);
   Vertex[1].Texture= (1,0);
   Vertex[2].Texture= (0,1);
   Vertex[3].Texture= (0,1);
   Vertex[4].Texture= (1,0);
   Vertex[5].Texture= (1,1);

   // Init Effect from http://blogs.msdn.com/b/shawnhar/archive/2010/04/05/spritebatch-and-custom-shaders-in-xna-game-studio-4-0.aspx

   Matrix projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
   Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);


   BasicEffect effect = new BasicEffect();

   effect.Texture = your texture;
   effect.TextureEnabled = true;
   effect.World = Matrix.Identity;
   effect.View = Matrix.Identity;
   effect.Projection = halfPixelOffset * projection;

   // Draw Triangles
   effect.Apply():
   GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(vertex, TriangleList, ...,...);

this code have to be understand as pseudocode, it's not tested but it shows the relevant actions to be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文