Unity3D的几种坐标系

原地址:http://www.cnblogs.com/martianzone/p/3371789.html

http://www.cnblogs.com/88999660/archive/2013/04/01/2993844.html

  1. World Space(世界坐标):我们在场景中添加物体(如:Cube),他们都是以世界坐标显示在场景中的。transform.position可以获得该位置坐标。            
  2. Screen Space(屏幕坐标,鼠标坐标):以像素来定义的,以屏幕的左下角为(0,0)点,右上角为(Screen.width,Screen.height),Z的位置是以相机的世界单位来衡量的。注:鼠标位置坐标属于屏幕坐标,Input.mousePosition可以获得该位置坐标,手指触摸屏幕也为屏幕坐标,Input.GetTouch(0).position可以获得单个手指触摸屏幕坐标。

  3. ViewPort Space(视口坐标):视口坐标是标准的和相对于相机的。相机的左下角为(0,0)点,右上角为(1,1)点,Z的位置是以相机的世界单位来衡量的。(用的不多,反正我暂时没有用到~呵呵~)

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

  5. LineRender坐标:以屏幕中心为原点,向上向右增加。
  • 世界坐标→屏幕坐标:camera.WorldToScreenPoint(transform.position);这样可以将世界坐标转换为屏幕坐标。其中camera为场景中的camera对象。

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

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

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

案例1——在鼠标点击的位置上绘制一张图片出来(关于绘制GUI界面坐标系与屏幕坐标系之间的关系)。

复制代码
using UnityEngine;
using System.Collections;

public class ScreenToGUI : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
  
      //图片  
    public Texture img;    
     //储存鼠标的位置坐标    
    private Vector2 pos;
    void OnGUI()
    {
        //鼠标左击,获取当前鼠标的位置       
        if (Input.GetMouseButton(0))
        {
            pos = Input.mousePosition; //屏幕坐标
        }
        //绘制图片,屏幕坐标和GUI界面坐标只在Y轴上方向相反,只要被Screen.height减就可以互相转换。      
        GUI.DrawTexture(new Rect(pos.x, Screen.height - pos.y, 100, 100), img);
    }

}
复制代码

案例2——角色头顶的名字(世界坐标转GUI界面坐标)

先世界坐标转屏幕坐标,再屏幕坐标转GUI界面坐标

代码如下:

复制代码
using UnityEngine;
using System.Collections;

public class Blood : MonoBehaviour {
    public static float ScaleWidht = 0f;
    public static float ScaleHeight = 0f;
    private Rect _drawRect = new Rect();
    public float Width = 0f;
    public float Height = 10f;
    public const float DesignStageWidth = 800;
    public const float DesignStageHeight = 480;

    public Vector2 pos2;
    public float size_z;
    // Use this for initialization
    void Start () {
        ScaleWidht = Screen.width / DesignStageWidth;
        ScaleHeight = Screen.height / DesignStageHeight;
        Height = 2f;

        size_z = transform.gameObject.collider.bounds.size.z;
    }


    // Update is called once per frame
    void Update () {
        //世界坐标转换到屏幕坐标
        print(transform.forward);
        pos2 = Camera.main.WorldToScreenPoint(transform.position + transform.forward * (size_z / 2)); 
        //计算角色头顶坐标
        pos2 = new Vector2(pos2.x, Screen.height  - pos2.y - Height); 



        //Vector3 worldPosition = new Vector3(transform.position.x, transform.position.y + Height, transform.position.z);
        //worldPosition = Camera.mainCamera.WorldToScreenPoint(worldPosition);
        //_drawRect = new Rect((worldPosition.x - 100 * ScaleWidht) / ScaleWidht, (Screen.height - worldPosition.y - 50 * ScaleHeight) / ScaleHeight, 200, 50);
    }

    void OnGUI()
    {
        //GUILayout.BeginArea(_drawRect);
        //    GUILayout.Label("======哈哈======");
        //GUILayout.EndArea();

        GUI.Label(new Rect(pos2.x, pos2.y, 100, 50), "=BETTER=");
    }
}
复制代码

 案例3——类似屏幕解锁功能的实现(屏幕坐标转换为世界坐标)

首先是创建LineRenderer。GameObject -> Create Empty ->更名为“LineRendererObj”,

给LineRendererObj添加“Line Renderer”组件,Component ->Effects ->Line Renderer;

将它的Positions 的size 设置为0

接下来是代码touch.CS:

复制代码
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class touch : MonoBehaviour {
    private Event e;

    public Texture2D Point;
    public Color c1 = Color.yellow;
    public Color c2 = Color.red;
    public int lengthOfLineRenderer;
    public GameObject LineRendererPrefab;

    private LineRenderer lineRenderer;
    /// <summary>
    /// 保存创建的Line Renderer
    /// </summary>
    private List<LineRenderer> lineRendArray =new List<LineRenderer>();

    private Vector3 screenPoint;
    private Vector3 scanPos;

    private Color[] color;

    /// <summary>
    /// 记录宫格所在GUI位置
    /// </summary>
    public List<Rect> AreaRect = new List<Rect>();
    /// <summary>
    /// 记录宫格中心点
    /// </summary>
    public List<Vector2> CenterPointList = new List<Vector2>();
    /// <summary>
    /// 宫格标签
    /// </summary>
    public int RectFlag;
    /// <summary>
    /// 记录正确的滑动顺序
    /// </summary>
    public List<int> KeyOrder = new List<int>();
    /// <summary>
    /// 记录玩家滑动顺序
    /// </summary>
    public List<int> PlayerKeyOrder = new List<int>();

    /// <summary>
    /// 判断开始鼠标位置是否可画
    /// </summary>
    public bool CheckStartRect=false;

    /// <summary>
    /// 判断结束鼠标位置是否可画
    /// </summary>
    public bool CheckEndRect = false;

    /// <summary>
    /// 行数
    /// </summary>
    public int Row = 4;
    /// <summary>
    /// 列数
    /// </summary>
    public int Column = 4;

    void Start()
    {
        e = Event.current;

        scanPos = LineRendererPrefab.transform.position;
        lineRenderer = (LineRenderer)LineRendererPrefab.GetComponent("LineRenderer");
        lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
        lengthOfLineRenderer = 0;
        lineRenderer.SetColors(c1, c2);
        lineRenderer.SetWidth(0.7F, 0.7F);
        lineRenderer.SetVertexCount(0);

        color = new Color[8];
        color[0] = Color.yellow;
        color[1] = Color.blue;
        color[2] = Color.cyan;
        color[3] = Color.gray;
        color[4] = Color.green;
        color[5] = Color.grey;
        color[6] = Color.magenta;
        color[7] = Color.red;

        for (int RowCount = 0; RowCount < Row; RowCount++)
        {
            for (int columnCount = 0; columnCount < Column; columnCount++)
            {
                Rect IconRect = new Rect(columnCount * Screen.width / Column + Screen.width / Column / 2 - Point.width / 2, RowCount * Screen.height / Row + Screen.height / Row / 2 - Point.height / 2, Point.width, Point.height);
                AreaRect.Add(IconRect);

                Vector2 CenterP = IconRect.center;//得到每个的中心点
                CenterPointList.Add(CenterP);
            }
        }
    }

    void OnGUI()
    {
        e = Event.current;
        for (int RowCount = 0; RowCount < Row; RowCount++)
        {
            for (int columnCount = 0; columnCount < Column; columnCount++)
            {
                Rect IconRect = new Rect(columnCount * Screen.width / Column + Screen.width / Column / 2 - Point.width / 2, RowCount * Screen.height / Row + Screen.height / Row / 2 - Point.height / 2, Point.width, Point.height);
                GUI.Label(IconRect, Point);
            }
        }
    }

    void Update()
    {
        if (e != null)
        {
            if (e.type == EventType.MouseDown)
            {
                for (int i = 0; i < AreaRect.Count; i++)
                {
                    if (AreaRect[i].Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z)))
                    {
                        CheckStartRect = true;
                        print("Contains");
                        PlayerKeyOrder.Add(i);
                        RectFlag = i;
                        break;
                    }
                    else
                    {
                        CheckStartRect = false;
                    }
                }

                if (CheckStartRect)
                {
                    print("MouseDown_____");

                    //Vector3 curPosition = mousePToLineRendererP(); 
                    Vector3 curPosition = centerPToLineRendererP(RectFlag);
                    GameObject newObj;
                    newObj = (GameObject)Instantiate(LineRendererPrefab, LineRendererPrefab.transform.position, LineRendererPrefab.transform.rotation);
                    lineRenderer = (LineRenderer)newObj.GetComponent("LineRenderer");

                    int n = Random.Range(1, 8);
                    c1 = color[n - 1];
                    n = Random.Range(1, 8);
                    c2 = color[n - 1];
                    lineRenderer.SetColors(c1, c2);

                    lineRenderer.SetVertexCount(1);
                    lineRenderer.SetWidth(0.7F, 0.7F);
                    lineRenderer.SetPosition(0, curPosition);
                    lineRendArray.Add(lineRenderer);
                    lengthOfLineRenderer++;
                }
            }

            if (e.type == EventType.MouseDrag&&CheckStartRect)
            {
                print("MouseDrag_____");
                Vector3 curPosition = mousePToLineRendererP(); 
                DrawRenderLine(lineRendArray[lengthOfLineRenderer - 1], curPosition);
            }

            if (e.type == EventType.MouseUp && CheckStartRect)
            {
                for (int i = 0; i < AreaRect.Count; i++)
                {
                    if (AreaRect[i].Contains(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z)))
                    {
                        CheckEndRect = true;
                        PlayerKeyOrder.Add(i);
                        RectFlag = i;
                        print("EndContains");
                        break;
                    }
                    else
                    {
                        CheckEndRect = false;
                    }
                }

                if (CheckEndRect)
                {
                    Vector3 curPosition = centerPToLineRendererP(RectFlag);
                    DrawRenderLine(lineRendArray[lengthOfLineRenderer - 1], curPosition);
                }
                else
                {
                    PlayerKeyOrder.RemoveAt(PlayerKeyOrder.Count - 1);
                    Destroy(lineRendArray[lengthOfLineRenderer - 1].gameObject);
                    //lengthOfLineRenderer--;
                }

            }
        }        
    }

    void DrawRenderLine(LineRenderer line, Vector3 vect3)
    {
        Vector3 newPos = vect3;
        line.SetVertexCount(2);

        line.SetPosition(1, newPos);
        print("new point: " + newPos);
    }

    //public Vector2 RectCenterPoint(Rect AreaRect)       //计算一个Rect的中心点
    //{
    //    Vector2 CenterPoint=Vector2.zero;
    //    print("Rect:"+AreaRect);
    //    CenterPoint.x=AreaRect.xMin+AreaRect.width/2;
        
    //    CenterPoint.y=AreaRect.yMin+AreaRect.height/2;
    //    print("CenterPoint:"+CenterPoint);
    //    return CenterPoint;
    //}

    /// <summary>
    /// 鼠标所在位置转换为LineRenderer的位置
    /// </summary>
    /// <returns></returns>
    public Vector3 mousePToLineRendererP()      
    {
        screenPoint = Camera.main.WorldToScreenPoint(scanPos);
        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
        print("curScreenPoint: " + curScreenPoint);
        print("curPosition: " + curPosition);
        return curPosition;
    }

    /// <summary>
    /// 鼠标所在区域的中心点转换为LineRenderer的位置
    /// </summary>
    /// <returns></returns>
    public Vector3 centerPToLineRendererP(int Flag)
    {
        screenPoint = Camera.main.WorldToScreenPoint(scanPos);
        Vector3 curScreenPoint = new Vector3(CenterPointList[Flag].x,Screen.height-CenterPointList[Flag].y,screenPoint.z);
        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
        print("curScreenPoint: " + curScreenPoint);
        print("curPosition: " + curPosition);
        return curPosition;
    }
}
复制代码

把touch.CS绑定在Camera上,设置如下:

 运行后可以任意点间连线,如图:

参考坐标系(Reference Coordinate System)列表是让你指定用于变换(移动move、旋转rotate和缩放scale)的坐标系。选项包括视图坐标view,屏幕坐标screen,世界坐标world,父坐标parent,局部坐标local,网格坐标grid和拾取坐标pick。

在屏幕坐标系下,所有的视图(包括***图perspective)使用视口屏幕坐标。 视图坐标view是世界坐标world和屏幕坐标screen的混合坐标系。使用视图坐标时,所有的正交视图(orthgraphic views)使用屏幕坐标screen,然而***图使用世界坐标world。

注意:坐标系被设置在一种变换对另一种变换的基础之上,所以在选择坐标系之前应先选择变化方式。如果你不想改变坐标系,打开Customize menu > Preferences > General tab > Reference Coordinate System group > Constant。

逐项解释:

View视图坐标:在默认坐标系下,在所有的(正交)视图中X,Y,Z三个轴是相同的。当你使用这个坐标系移动一个对象时,你所移动的对象与视口空间有如下关系。
·X轴总是指向(屏幕)右侧。
·Y轴总是指向(屏幕)上方。
·Z轴总是垂直屏幕指向你。

Screen屏幕坐标:使用激活的视口屏幕作为坐标系。
·X轴是水平的,正方向指向(屏幕)右侧。
·Y轴是垂直的,正方向指向(屏幕)上方。
·Z轴是进深的,正方向指向你。
因为屏幕坐标模式是以激活视口作为它的坐标方向,未激活视口中轴三脚架上X,Y,Z的指向显示当前激活视口的方向。当你激活这个视口时,轴三脚架上的指向将会改变。

World世界坐标:从前视图front看:
·X轴正方向指向(屏幕)右侧。
·Z轴正方向指向(屏幕)上方。
·Y轴正方向背离你。

Parent父坐标:使用所选择对象的父对象的坐标系统。如果对象未链接到特定的物体上,那么它是世界的子物体,即父坐标与世界坐标相同。

Local局部坐标:使用所选择对象的坐标系统。一个对象的局部坐标来自它的枢轴点。你可以通过使用选项中的Hierarchy命令面板, 相对于对象调整局部坐标的位置和方向。
当局部坐标处于激活状态,按钮“Use Transform Center”是不起作用的,所用的变换使用局部坐标轴作为变换的中心。在一个选择集中的几个物体,每个分别使用它自己的中心来做变换。

Grid网格坐标:使用激活网格的坐标系。

Pick拾取坐标:使用场景中另一个对象的坐标系。
当你选择Pick以后,单击选择单个物体,此物体的坐标系将被用于变换。对象的名字出现在变换坐标系列表中。
事实是在列表中所存储的对象名称使你可以拾取此对象的坐标系,改变激活的坐标系,而且可以稍后再使用对象坐标系。列表中储存四个最近拾取的对象名称。
当你使用拾取坐标来指定一个对象作为参考坐标系时,你能够按下“H”显示选择对象对话框,并从那里选择对象。


----World Coordinate System--世界坐标系

这个坐标系将世界空间或模型空间作为一个整体。
世界空间是世界坐标系,世界坐标系是软件用于在场景中追踪对象的。当你看到视口中的主珊格,你就见到世界空间坐标系。世界空间是固定不变的。
在世界坐标系下,从前视图front看去,X轴正方向指向(屏幕)右侧,Z轴正方向指向(屏幕)上方,Y轴正方向背离你。


----Local Coordinate System--局部坐标系 局部坐标系是与所选择对象具有特定关系的坐标系。
每个对象有它自己的局部中心和坐标系,这与定义的此对象枢轴点的位置和方向相同。一个对象的局部中心和坐标系联合定义了它的物体空间。
对象的X,Y,Z轴的方向,Z轴依赖于对象的当前变换。与世界坐标系比较。当你无意中旋转一个对象时你能够发现两个坐标系之间的不同,例如一个汽车模型的车轮,用世界坐标轴代替对象局部坐标轴。车轮立即沿一个大的弧线飞出,因为旋转的中心是在世界坐标的原点。
要正确旋转车轮,首先变换坐标系为局部坐标,使用工具条中的参考坐标系列表。车轮绕它自己的中心旋转,那里是他局部坐标的原点。


----Pivot Point--枢轴点

变换中心,或称枢轴点,是发生旋转或发生比例变形的部位。
所有的物体具有一个枢轴点。你可以想象枢轴点为一个对象的局部中心或局部坐标系的代表。
一个对象的枢轴点用于以下几个目的:
·当枢轴点变换中心被选择时,作为旋转和比例变换的中心。
·设置修改器中心的默认位置。
·定义所链接子物体的变换原点。
·定义IK结合的位置。
通过使用“Hierarchy”命令面板中的“Pivot”功能,你能够在任何时候显示和调节一个对象的枢轴点的位置和方向,调节一个对象的枢轴点不会影响任何与其链接的子物体。

  1. World Space(世界坐标,左手坐标系):我们在场景中添加物体(如:Cube),他们都是以世界坐标显示在场景中的。也叫全局坐标系transform.position可以获得该位置坐标。            
  2. 局部坐标系:每个物体都有其独立的物体坐标系,并且随物体进行相同的移动或者旋转,也称模型坐标系或物体坐标系。模型mesh保存的顶点坐标均为局部坐标系下的坐标。
  3. Screen Space(屏幕坐标,鼠标坐标):以像素来定义的,以屏幕的左下角为(0,0)点,右上角为(Screen.width,Screen.height),Z的位置是以相机的世界单位来衡量的。注:鼠标位置坐标属于屏幕坐标,Input.mousePosition可以获得该位置坐标,手指触摸屏幕也为屏幕坐标,Input.GetTouch(0).position可以获得单个手指触摸屏幕坐标。

  4. ViewPort Space(视口坐标):视口坐标是标准的和相对于相机的。相机的左下角为(0,0)点,右上角为(1,1)点,Z的位置是以相机的世界单位来衡量的。使用坐标系可以方便地判断物体是否在相机前方以及物体之间的先后遮挡顺序等(用的不多,反正我暂时没有用到~呵呵~)

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

  6. LineRender坐标:以屏幕中心为原点,向上向右增加。
    • 世界坐标→屏幕坐标:camera.WorldToScreenPoint(transform.position);这样可以将世界坐标转换为屏幕坐标。其中camera为场景中的camera对象。

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

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

    • 视口坐标→世界坐标:camera.ViewportToWorldPoint();
原文地址:https://www.cnblogs.com/123ing/p/3853171.html