Unity and the interconversion of various coordinate systems (Reference god, a note only)

 As a Unity 3D development engine, his faculties coordinate complex and learned matrix of friends may also have to open around, not learned to accidentally fall into the pit which coordinates the fact that the difference between the coordinates of each department established referring to different coordinate systems, the following first understand, I try to say the next major popular point coordinate it.

1. Coordinate System

   1. World coordinate system: to establish the origin of the world coordinate origin of the three-dimensional coordinate system, all in the game object has its own Unity of the world coordinate system, and to obtain the coordinates of the method is very simple (transform.position), which is similar to the real world we often say that the latitude and longitude, commonly used to define the position and direction of the standard.

  2. Local coordinates (their coordinates): to establish itself as the origin and the three-dimensional coordinate system, which coordinates local coordinates, and world coordinate system, as who Unity game objects have their own coordinates, to obtain the coordinates of the method is also very simple (transform.localposition), used to define a direction of their initial position by topical, for example, a backpack when in fact it is replacing items UI replacement, and replacement of UI UI actually replace the parent, and then let transform.localposition = Vector3.zero let him equal position of its parent object.

3. The screen coordinates: lower left corner of the screen as the origin of the establishment of a two-dimensional coordinate system to the lower left corner of the screen is (0,0), the upper right corner (Screen.width, Screen.height), up to the coordinates of the pit, also we often coordinate around the most, explain here, the first screen you want to know is relative to the screen. For example, the reality of projectors we are very clear that we put on top of a projector in the ceiling, then the projected image is mapped onto a wall or screen, video wall or curtain of this area is the screen when we move the curtain when the size of the screen is also changing, with which we screen the vertical distance of the projector is related to the vertical distance is our Z-axis, turn behind the screen coordinates world coordinates will be mentioned. Coordinate relations with the resolution of the screen is worth noting that, when our resolution is 600 * 800 times, but we intercepted them as 200 * 200 screen, then the coordinates of the lower left corner of the screen is (0,0), the upper right corner of the screen coordinates (200, 200). Commonly used in screen coordinates of the mouse to interact, and get mouse coordinates belong to screen coordinates (input.mouseposition).

4. viewport coordinates: lower left corner of the camera screen as the origin to establish a two-dimensional coordinate system, lower left corner is (0,0), top right (1,1), i.e. its Z-axis is the Z axis of the camera, established in the above example on the projector screen coordinates is viewport coordinate, the coordinate conversion coordinate transition effect mostly used, such as the World coordinate to a screen coordinate, coordinate to a world coordinate of the viewport, the viewport coordinate to a screen coordinate.

There are GUI coordinates, projection coordinates, desktop coordinate grid coordinates and other basic rarely used, so do not bother to enumerate here.

2. Coordinate Transformation

1. Local Huzhuan World: Unity packaged as a method transform.TransformVector, transform.TransformPoint, transform.TransformDirection, not the conversion of meaning, to get directly can be directly obtained by the methods described above speak.

2. turn the screen coordinates of the world coordinate: There are a lot of people and a lot of blog directly switch to direct Camera.main.ScreenToWorldPoint (Input.mouseposition), this is not possible, because Input.mouseposition is a screen coordinate, no Z-axis, and we the world needs to coordinate Z-axis, similar to what we need to know the distance from the projector to the screen we can convert a point on the screen into the world.

                 Vector3 myscreenToworld public (Vector3 mousePos, the Transform targetTransform) {
        // camera to calculate the first target vector
        Vector3 the dir = targetTransform.position - Camera.main.transform.position;
        // calculate projection
        Vector3 normardir = Vector3.Project (dir, Camera . main.transform.forward);
        // calculate the node need to know the distance from the projection screen disposal
        Vector3 worldpos = Camera.main.ScreenToWorldPoint (new new Vector3 (mousepos.x, mousepos.y, normardir.magnitude));
        return worldpos;
    }

3. World turn the screen: 

Camera.main.WorldToScreenPoint()

World turn the screen of process is this: [World -> Viewport] * [viewport -> Screen], learned last matrix can become know [World -> Screen], so the above is Unity is encapsulated the result, Find interested underlying turn himself under test

4. World turn viewport

Camera.main.WorldToViewportPoint()

The viewport turn the screen

Camera.main.ViewportToScreenPoint()或者自己手写Vector2 pos=new Vector2(Viewposition.x*Screen.width,Viewposition.y*Screen.hight).

Guess you like

Origin www.cnblogs.com/Damon-3707/p/11495358.html