Various coordinate systems in the Unity3d

Source: Internet
Author: User

What coordinate systems are in the Unity3d?

?? The concept of the coordinate system was first proposed by the French mathematician Descartes, and thus created the mathematical branch of geometry-analytic geometry-using algebraic methods. The basic idea of analytic geometry is to abstract the geometry into a point motion trajectory, so that points can be used as the basic elements of the composition of the graph, and the position of a point must first establish a suitable coordinate system. So, first of all, let's understand what coordinate systems are in the Unity3d. The coordinate system in Unity3d can now be divided into the following four categories: world coordinates, screen coordinates, view coordinates, and GUI coordinates. Let's take a detailed description of these 5 types of coordinates:

World Coordinates

World coordinates are defined in the Cartesian coordinate system, and the following coordinate systems are built on the basis of world coordinates. We know that any point in the two-dimensional plane can be represented by two-dimensional coordinates (x, y), and if the concept is extended to a three-dimensional space, then any point in the three-dimensional space can be represented by three-dimensional coordinates (x, y, z). This is the concept of world coordinates, the coordinate system usually can be divided into left-handed and right-handed coordinate system, and unity3d using left-handed coordinate system. In Unity3d we can use transform.position to get the world coordinates of an object in the scene, usually the inspector window in the editor describes the position of a 3D object in world coordinates, unless it describes its position in relative coordinates when a 3D object has a parent object.

screen coordinates

The screen coordinates are defined in pixels and are scoped to the lower-left corner (0,0) and the upper-right corner of the rectangle (screen.width,screen.height) defined. The screen coordinates are a 3D coordinate, and the z-axis is measured in the camera's world unit. The screen coordinates and the camera meet the two conditions: Screen.width=camera.pixelwidth and Screen.height=camera.pixelheight. For example, we put the camera in the scene of the origin (0,0,0), the camera's z-axis component is-10, according to the definition of screen coordinates, assuming that the screen is 800x640 size, then the origin is converted to screen coordinates should be (400,320,10). In Unity3d we can use Worldtoscreenpoint to convert a world coordinate to screen coordinates.

Viewport coordinates

Viewport coordinates are the normalized screen coordinates. The concept of standardization we can extend to the standardization of vectors, such as a vector (x, y) will be normalized after a unit vector (x ', Y '). Similarly, viewport coordinates are represented by numbers from 0 to 1, which are in the lower-left corner (0,0), and the upper-right corner is defined as a rectangle. Viewport coordinates are a 3D coordinate, and the z-axis is measured in the camera's world unit. By contrast, you can see that the viewport coordinates and screen coordinates are particularly similar, so you can compare them here to learn. Also in the screen coordinates of the example to convert here, for example, we are the camera is facing the scene of the origin (0,0,0), the camera's z-axis component is-10, according to the definition of screen coordinates, assuming that the screen is 800x640 size, then the origin is converted to screen coordinates should be (0.5,0.5,10). In Unity3d we can use Worldtoviewportpoint to convert a world coordinate to viewport coordinates.

GUI coordinates

GUI coordinates are the coordinates used when drawing the UI by means of the Ongui method. This coordinate system is similar to the screen coordinates, it is also defined in pixels, its range is in the upper-left corner (0,0), the lower-right corner is (screen.width,screen.height) the definition of such a rectangle, the GUI coordinates is a 2D coordinate (absolute coordinates). Because the GUI has been designed in the early articles, belongs to the more basic content in the Unity3d, moreover we know that uses the absolute coordinates to carry on the layout to be not able to do the self-adaptation, therefore this part content we not to unfold to say, the UI adaptive one main idea is not to use absolute coordinates! Do not use absolute coordinates! Do not use absolute coordinates! The important thing to say three times! In particular, I would like to say something about Unity4.6 's new UI support: Ugui. In Ugui's screen space mode, the Unity3d editor displays the position of the UI element in the display coordinates, and in world Space, the editor of Unity3d displays the location of the UI element in global coordinates. Believe this point in the use of people do not notice, recently in a forum to see the post of the relevant knowledge of the Ugui coordinate conversion is a blank, so on this part of the deep research, the final conclusion is: Ugui coordinates are essentially special screen coordinates, Because Ugui's anchor determines the origin of the coordinate system, pivot determines the origin of the element's own coordinate system, which makes the coordinates of the Ugui seem confusing. Recttransform components inherit from transform, so their position and localposition are equivalent, all world coordinates; anchoredposition is the screen coordinate of the UI element, You should consider using this coordinate when working with UI elements.

Ii. Unity3d Conversion between various coordinate systems

Although Unity3d provides a variety of coordinate between the conversion API, but we are writing a blog is not to say that the API is not? So from a practical point of view, here we provide a unity3d conversion between various coordinate systems faq!

1. How does screen coordinates convert to world coordinates?

In Unity3d, a screen coordinate can be converted to world coordinates by means of the Camera.screentoworldpoint (Vector3 V) method. Among them, camera is the scene camera in the game scene. The screen coordinates of the mouse or single finger can be obtained by input.mouseposition or input.touches[0].position. A typical application scenario is the first person shooter game in which bullets are launched and the RPG game hits the ground moving character.

2. How can world coordinates be converted to screen coordinates?

In Unity3d, a world coordinate can be converted to screen coordinates by means of the Camera.worldtoscreenpoint (Vector3 V) method. Where camera is the UI camera in the game scene. For example, when we need to convert a world coordinate to a ngui coordinate, you can use the scene camera to turn world coordinates into screen coordinates, then use the UI camera to convert screen coordinates to world coordinates, and finally assign to the Ngui control. The typical application scenario is the display of text on the head of the NPC and the blood bar above the monster's head.

3. How screen coordinates are converted to viewport coordinates?

In Unity3d, a screen coordinate can be converted to viewport coordinates by Camera.screentoviewportpoint (), where camera is the scene in the game scene. When we understand that the nature of viewport coordinates is the normalized coordinates of screen coordinates, there is nothing to say about this.

4. How can world coordinates be converted to viewport coordinates?

In Unity3d, a world coordinate can be converted to viewport coordinates by Camera.worldtoviewportpoint (), where camera is the scene in the game scene. When we understand that the nature of viewport coordinates is the normalized coordinates of screen coordinates, there is nothing to say about this.

5. How can screen coordinates be converted to GUI coordinates?

We note that the fundamental difference between screen coordinates and GUI coordinates is that the coordinate system origin is selected differently, that is, the origin of the screen coordinates is in the lower-left corner, and the origin of the GUI coordinates is in the lower-left corner. So we can define a Screentoguipoint method:

[Mw_shl_code=csharp,true]private Vector2 Screentoguipoint (Vector2 v)
{
return new Vector2 (V.X,SCREEN.HEIGHT-V.Y)
}[/mw_shl_code]

6, how to convert GUI coordinates to screen coordinates?

A more magical thing is that you can continue to invoke the Screentoguipoint method.

7, how to convert screen coordinates to UGUI coordinates?

When we come into contact with Ugui, we often encounter problems like Ngui need to assign values to Ugui control coordinates, at this point we have the following two ways to deal with it:

directly to the screen coordinates assigned to transform.position, this way simple rough in the Screenspace-overlay mode is not a problem, but in Screenspace-camera mode will appear because the depth of the incorrect value caused by the problem, you need to pay attention to (and need to ensure that the object is assigned to the root node of the canvas), some time ago, someone in the group asked me position and localposition difference, I am oh da, now people are general impetuous ah, would like to delve into the usage of such a plugin all day not willing to delve into the API document, Computer graphics, 3D math these things.

The Recttransformutility.screenpointtoworldpointinrectangle method converts the screen coordinates to world coordinates and then assigns the value to Transform.position. Both approaches look much the same, but this approach is more secure because it works stably in both modes. More importantly, it can be perfectly integrated with the Ugui event system. I guess it's a direct change to the world coordinates of the UI element because it is assigned to Transform.position. But then again, Before the monster to show the blood bar is directly to the world coordinates to the screen coordinates to assign value to transform.position, so it seems that the original idea may be wrong ah, if you want to give UI control screen coordinates that should not give anchoredposition assignment? Well, this piece is still not understand, if a friend know how, can tell me, thank you!

Various coordinate systems in the Unity3d

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.