Detailed explanation of various coordinate systems in Unity

foreword

There are many coordinate systems in Unity, such as world coordinate system, local coordinate system, screen coordinate system, viewport coordinate system, etc. These coordinate systems often cause a lot of trouble for novices (me), but they are indispensable . For example, when you need to obtain the position of the mouse in world coordinates, you need to understand what world coordinates and screen coordinates are, and how to convert them. This article introduces various coordinate systems and their conversion methods and applications in detail. Help everyone.

Reminder in the front row: This article only represents personal opinions for exchange and learning. If you have different opinions, please leave a comment. The author must study hard and make progress every day.

When reading this article, if there is something you don’t understand, it is recommended to watch the reference materials listed in this article for comparison.

The author's mathematics is relatively sloppy. Readers should watch the explanation of the coordinate system in this article with a scrutiny. If there are any mistakes, please correct me. The author is very grateful!

Unity version [2019.4.10f1] Dream Xiaotianyou & reprinting is prohibited

Video explanation:
Detailed explanation of various coordinate systems of Unity_BiLiBiLi


1. The coordinate system in Unity

1. World coordinates

When you create a new object object from Unity, its Transform parameter adopts the world coordinate system, which is divided into left-hand coordinate system and right-hand coordinate system, as shown in the figure. Among them, the left-handed coordinate system is the world coordinate system in Unity , and the right-handed coordinate system is more commonly used in mathematics, so I will not discuss it here.

insert image description here

I hope readers will not confuse the concept, there are two coordinate systems, left-handed and right-handed coordinate systems, and the left-handed coordinate system is adopted in Unity, that is, the Z axis is positive, the X axis is right, and the Y axis is up.

The coordinate information of an object in Unity is stored by Transform.position, which is a Vector3 variable, which is a three-dimensional vector and stores XYZ information.

insert image description here

In Unity, the positive direction can be represented by Transform.forward, and the positive direction can also be represented by Vector3.forward. The difference between the two is:

The value of Vector3.forward is always (0,0,1), while the value of transform.forward is based on the Z axis of the current object’s own coordinate system, which is not necessarily equal to (0,0,1). How to understand it, for example .
First, the transform.position of an object must be the position of the object in the world coordinates, and the corresponding transform.localPosition is the local coordinates of the object. Then, when transform += Vector3.forward, it is equivalent to advancing to the Z axis of the world coordinates , and when transform += transform.forward, it is equal to advancing to the Z axis of the local coordinates.

2. Screen coordinate system

The screen coordinate system is to regard the screen as a coordinate system, starting from the lower left corner, which is (0,0), and the upper right corner is (Screen.widht, Screen.height), so it is also called the pixel coordinate system. Now I Running the following line of code is to get the screen resolution of my current game window.

    void Start()
    {
        Debug.Log("当前窗口的分辨率为:" + Screen.width + "X" + Screen.height);
        Debug.Log("当前屏幕的分辨率为:" + Screen.currentResolution);
    }

insert image description here

The mouse position coordinates belong to the screen coordinate system. Through the mutual conversion between the screen coordinates and the world coordinates, the actual interactive position of the mouse in Unity3D can be obtained, and then feedback can be made through logic.

3. Viewport coordinate system

The calculation method of this coordinate system is similar to that of the screen coordinate system, but its parameters are standardized, which is more suitable for proportional calculations.
The lower left corner is (0,0) and the upper right corner is (1,1)

insert image description here

Here, whether it is viewport coordinates or screen coordinates, its z value is still available, which indicates depth.

4. GUI coordinate system

The coordinates are calculated from the upper left corner, the upper left corner is (0,0), and the lower right corner is (Screen.width, Screen.height);


Two, the mutual conversion of the coordinate system

The use of multiple coordinate systems in Unity is to use different coordinates in different situations to make it more convenient. Since a variety of coordinates are used, relevant conversion methods must be provided. The following is a brief summary of the mutual conversion of the above coordinates.
In fact, these coordinate systems look dazzling, with many appearances. In fact, it is worth discussing the world-to-screen (or screen-to-world) in practice, and the others are easy to implement.

1. Mutual conversion between world coordinates and screen coordinates

Screen coordinates to world coordinates
Camera.ScreenToWorldPoint(Vector3 Pos);

World coordinates to screen coordinates
Camera.WorldToScreenPoint(Vector3 Pos);

About the application:
These transformations are generally applied to the world objects that want to be manipulated by the mouse, such as objects moving with the mouse...

Case presentation

The object moves with the mouse, xyz moves
insert image description here

Maybe you don’t understand this code when you look at it. For example, you feel that you can use the following sentence directly. Why bother? Seeing this, you should try it and then look down

Camera.main.WorldToScreenPoint(Input.mousePosition);

Well, you should have understood that if you use the above sentence directly, the world coordinates will lack the Z value, so the coordinates after conversion must be wrong. So we need to make up for this
bug in a clever way, the code is as follows. (This wording was found on the Internet, I can't find the source)

    void Update()
    {
        // 首先将要操纵的物体的世界坐标转为屏幕坐标
        Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
        // 然后获取鼠标的屏幕坐标
        Vector3 mousePos = Input.mousePosition;
        // 然后将已经转换好的物体的屏幕坐标的Z值赋给缺失z值的鼠标屏幕坐标
        mousePos.z = screenPos.z;
        // 然后将已经完整的鼠标屏幕坐标转成世界坐标
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
        // 最后每帧修改物体世界坐标位置
        transform.position = worldPos;
    }

2. Mutual conversion between world coordinates and viewport coordinates

Convert world coordinates to viewport coordinates
Camera.WorldToViewportPoint(Vector3 Pos)

Viewport coordinates to world coordinates
Camera.ViewportToWorldPoint(Vector3 Pos);
This principle is similar to the previous one, because the screen and viewport can be converted to each other, so it is also true to replace the above conversion function with the following one, provided that the last one is used When the viewport is converted to world coordinates, the screen coordinates of the mouse position must be converted to the viewport before use. If you don’t understand, I will simply change it and paste the code. The dynamic diagram will not be displayed, because it has the same effect.

        // 首先将要操纵的物体的世界坐标转为视口做坐标
        Vector3 screenPos = Camera.main.WorldToViewportPoint(transform.position);
        // 然后获取鼠标的屏幕坐标
        Vector3 mousePos = Input.mousePosition;
        // 然后将已经转换好的物体的屏幕坐标的Z值赋给缺失z值的鼠标屏幕坐标
        mousePos.z = screenPos.z;
        // 将屏幕坐标转为视口坐标
        mousePos = Camera.main.ScreenToViewportPoint(mousePos);
        // 然后将已经完整的视口坐标转成世界坐标
        Vector3 worldPos = Camera.main.ViewportToWorldPoint(mousePos);
        // 最后每帧修改物体世界坐标位置
        transform.position = worldPos;

3. Mutual conversion between screen coordinates and viewport coordinates

Convert screen coordinates to viewport coordinates
Camera.ScreenToViewportPoint(Vector3 Pos)

Viewport coordinates to screen coordinates
Camera.ViewportToScreenPoint(Vector3 Pos);

An example has been given above, and it will not be demonstrated here.

4. Mutual conversion between world coordinates and local coordinates

The usage here is more complicated, and the content is not comprehensive, and this part of knowledge will be rewritten in the future, and the flag will be set

Convert world coordinates to local coordinates
transform.InverseTransformPoint(Vector3 Pos);
transform.worldToLocalMatrix

Transform local coordinates to world coordinates
transform.TransformPoint(Vector3 Pos);
transform.localToworldMatrix


3. Coordinate system confusion

When you use the coordinate system, you may have heard many, many aliases. For novices, it is easy to have doubts about some of the more uncommon names. Let me sort out them here.

  • World coordinates, global coordinates, left-handed coordinates, absolute coordinates
  • Local coordinates, own coordinates, object coordinates, local coordinates, relative coordinates
  • screen coordinates, pixel coordinates
  • viewport coordinates, window coordinates
  • GUI coordinates, UI coordinates

These are probably the ones I have heard, and I will come across other names in the future, and I will add them.

4. Summary and References

1. Summary

Unity's coordinate system

  • World coordinates (absolute coordinates, the position coordinates of objects in the world)
  • local coordinates (coordinates that the object itself has)
  • Screen coordinates (the pixel size of the game window, starting from the lower left corner)
  • Viewport coordinates (bottom left [0,0] top right [1,1])
  • GUI coordinates (UI-specific coordinate system, calculation starts from the upper left corner, and the lower right corner is the screen pixel size)

Unity coordinate system conversion

  • The only thing to pay attention to: the screen to the world
    • The screen needs to have a z value, and the correct value can be obtained when turning the world

2. References

[1]. Call me Oriental Little Paris. Space rectangular coordinate system, left-handed coordinate system, right-handed coordinate system
[2].qq_38806355. Unity’s idea of ​​converting screen coordinates to world coordinates
[3]. GolDHeaven. Screen coordinates and views in Unity Mutual conversion between coordinates and world coordinates
[4].AbnerHu. Unity screen coordinates converted to world coordinates, the object moves with the mouse
[5].Peter_Gao_. Positions in each space of Unity: world coordinates, screen coordinates, camera coordinates, local coordinates
[6] ].Htlas. Unity3D coordinate system analysis (2): screen coordinate system, view coordinate system

Guess you like

Origin blog.csdn.net/weixin_43147385/article/details/124230124