您的位置:首页 > 移动开发 > Unity3D

Unity3D的四种坐标系

2015-10-27 17:06 381 查看
Unity3D的四种坐标系

1、World Space(世界坐标):我们在场景中添加物体(如:Cube),他们都是以世界坐标显示在场景中的。transform.position可以获得该位置坐标。

2、Screen Space(屏幕坐标):以像素来定义的,以屏幕的左下角为(0,0)点,右上角为(Screen.width,Screen.height),Z的位置是以相机的世界单位来衡量的。注:鼠标位置坐标属于屏幕坐标,Input.mousePosition可以获得该位置坐标,手指触摸屏幕也为屏幕坐标,Input.GetTouch(0).position可以获得单个手指触摸屏幕坐标。

  Screen.width = Camera.pixelWidth

  Screen.height = Camera.pixelHeigth

3、ViewPort Space(视口坐标):视口坐标是标准的和相对于相机的。相机的左下角为(0,0)点,右上角为(1,1)点,Z的位置是以相机的世界单位来衡量的。

4、绘制GUI界面的坐标系:这个坐标系与屏幕坐标系相似,不同的是该坐标系以屏幕的左上角为(0,0)点,右下角为(Screen.width,Screen.height)

四种坐标系的转换

1、世界坐标→屏幕坐标:camera.WorldToScreenPoint(transform.position);这样可以将世界坐标转换为屏幕坐标。其中camera为场景中的camera对象。

2、屏幕坐标→视口坐标:camera.ScreenToViewportPoint(Input.GetTouch(0).position);这样可以将屏幕坐标转换为视口坐标。其中camera为场景中的camera对象。

3、视口坐标→屏幕坐标:camera.ViewportToScreenPoint();

4、视口坐标→世界坐标:camera.ViewportToWorldPoint();

问题

1.Screen VS Viewport What is the difference

Screen and Viewport represent same area - on screen, but they have different coordinate systems (IIRC):

Screen: from [0,0] to [Screen.width, Screen.height]

Viewport: from [-1,-1] to [1,1]

World point is a point in world space.



noflyzone · 2011年09月19日
09:19 0

In unity3d, why this design? Viewport In what circumstances is used?



Paulius
Liekis · 2011年09月19日
11:29 1

I don't think it's Unity specific design - these are common terms in 3d graphics. Viewport might be useful if you want to check if something is on-screen (you don't care about Screen size), but I'm sure there are
better exampled.



syclamoth · 2011年09月19日
11:49 1

Viewport is used when you need to be sure something will show up the same on all different screen sizes and shapes. It is independent of the pixel width and height of the exact screen in question.



noflyzone · 2011年09月19日
12:36 0

Thank syclamoth and Paulius



RedRival · 2012年12月19日
03:44 3

Correction Viewport: from [0,0] to [1,1] Quote: Viewport space is normalized and relative to the camera. The bottom-left of the camera is (0,0); the top-right is (1,1). The z position is in world units from the
camera.http://docs.unity3d.com/Documentation/ScriptReference/Camera.ViewportToWorldPoint.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: