Xna transform a 2d texture like photoshop transforming tool

百般思念 提交于 2020-01-25 04:55:10

问题


I want to create the same transforming effect on XNA 4 as Photoshop does: Transform tool is used to scale, rotate, skew, and just distort the perspective of any graphic you’re working with in general

This is what all the things i want to do in XNA with any textures http://www.tutorial9.net/tutorials/photoshop-tutorials/using-transform-in-photoshop/

Skew: Skew transformations slant objects either vertically or horizontally. Distort: Distort transformations allow you to stretch an image in ANY direction freely. Perspective: The Perspective transformation allows you to add perspective to an object. Warping an Object(Im interesting the most).

Hope you can help me with some tutorial or somwthing already made :D, iam think vertex has the solution but maybe.

Thanks.


回答1:


Probably the easiest way to do this in XNA is to pass a Matrix to SpriteBatch.Begin. This is the overload you want to use: MSDN (the transformMatrix argument).

You can also do this with raw vertices, with an effect like BasicEffect by setting its World matrix. Or by setting vertex positions manually, perhaps transforming them with Vector3.Transform().

Most of the transformation matrices you want are provided by the Matrix.Create*() methods (MSDN). For example, CreateScale and CreateRotationZ.

There is no provided method for creating a skew matrix. It should be something like this:

Matrix skew = Matrix.Identity;
skew.M12 = (float)Math.Tan(MathHelper.ToRadians(36.87f));

(That is to skew by 36.87f degrees, which I pulled off this old answer of mine. You should be able to find the full maths for a skew matrix via Google.)

Remember that transformations happen around the origin of world space (0,0). If you want to, for example, scale around the centre of your sprite, you need to translate that sprite's centre to the origin, apply a scale, and then translate it back again. You can combine matrix transforms by multiplying them. This example (untested) will scale a 200x200 image around its centre:

Matrix myMatrix = Matrix.CreateTranslation(-100, -100, 0)
                * Matrix.CreateScale(2f, 0.5f, 1f)
                * Matrix.CreateTranslation(100, 100, 0);

Note: avoid scaling the Z axis to 0, even in 2D.

For perspective there is CreatePerspective. This creates a projection matrix, which is a specific kind of matrix for projecting a 3D scene onto a 2D display, so it is better used with vertices when setting (for example) BasicEffect.Projection. In this case you're best off doing proper 3D rendering.

For distort, just use vertices and place them manually wherever you need them.



来源:https://stackoverflow.com/questions/16895006/xna-transform-a-2d-texture-like-photoshop-transforming-tool

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!