zoukankan      html  css  js  c++  java
  • Unity_屏幕/Viewport/世界坐标的转换

    Unity_屏幕/Viewport/世界/UI坐标的转换

    参考:

      https://www.jianshu.com/p/b5b6ac9ab145 -- 世界、视口、屏幕坐标转换
      https://docs.unity3d.com/ScriptReference/RectTransformUtility.ScreenPointToLocalPointInRectangle.html -- API

    不同坐标系:

    世界坐标系: World Space
      transform.position/ transform.rotation都是基于世界坐标系的
      游戏场景的原点世界坐标为(0,0,0)

    观察坐标: Eye Space
      在Game视图中的画面是由摄像机提供的,而基于某一个摄像机的坐标系即为观察坐标系
      即把摄像机的位置作为原点位置

    视口坐标: View Port
      针对整个游戏画面,左下角为(0,0),右上角为(1,1)
      设计分屏功能时可以通过设置摄像机所占的视口空间来控制

    屏幕坐标: Screen Space
      与游戏画面的分辨率有关,注意是游戏屏幕画面的分辨率
      屏幕的左下角为(0,0), 右上角为(Screen.width, Screen.height)
      获取鼠标位置(Input.mousePosition)时的坐标即为屏幕坐标,返回的为Vector3(x,y,0)

    常见方法:

    // 1.屏幕转世界坐标
    Vector3 Camera.main.ScreenToWorldPoint(new Vector3(screenPos.x , screenPos.y , zInfo));
    // 世界转屏幕坐标
    Vector3 Camera.main.WorldToScreenPoint(new Vector3(worldPos.x , worldPos.y , worldPos.z));
    // 2.世界转视口坐标
    Vector3 Camera.main.WorldToViewportPoint();
    // 视口转世界坐标
    Vector3 Camera.main.ViewportToWorldPoint(new Vector3(viewPortPos.x , viewPortPos.y , zInfo));
    // 3.视口转屏幕坐标
    Vector3 Camera.main.ViewportToScreenPoint();
    // 屏幕转视口坐标
    Vector3 Camera.main.ScreenToViewportPoint();

    屏幕坐标与世界坐标的转换: 
      注意: z表示的是世界坐标相对于摄像机的深度信息
        例: 世界坐标(0, 0, 1),摄像机位置(0, 0, -10)  --  WorldToScreenPoint()返回结果为(Screen.width/2, Screen.height/2, 1-(-10))

    视口坐标和世界坐标的转换也类似:
      z也表示的为深度信息(距离)

    应用:

    1. 有些游戏会对物体的运动范围进行限制,防止跑出边界,比如雷电

    思路:
      用世界坐标和视口坐标的转化
      将物体的视口坐标限制在(0,0,x)~(1,1,x)内/ 或用(0,0,x)(1,1,x)的视口坐标算出物体的世界坐标的x和y的限制(四条边界)

    注意:
      上述雷电游戏由于摄像机使用了正交投影,所以z轴数据没有关系
      但是如果使用的是透视投影,则在不同深度下的边界范围会发生变化,因此需要输入正确的z轴数据

    屏幕坐标转UI坐标:

    方法: 将屏幕上的一点screenPoint,转换为UI RectTransform的局部空间中的坐标

    public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint);

    参数详解:
      RectTransform rect: The RectTransform to find a point inside
      Camera cam: The camera associated with the screen space position
        1. For a RectTransform in a Canvas set to Screen Space-Overlay mode, cam parameter should be null.
        2. When ScreenPointToLocalPointInRectangle is used from within an event handler that provides a PointerEventData,
          the correct camera can be obtained by using PointerEventData.enterEventData (for hover)
          or  PointerEventData.pressedEventCamera (for click)
          it will auto use the correct camera( or null) for the given event
      out Vector2 localPoint: Point in local space of the rect
      return: returns true if the plane of the RectTransform in hit, regardless of whether the point is inside the rect
        整个平面,而不是矩形内部

     

  • 相关阅读:
    第二阶段冲刺--每日立会(6)
    第二阶段冲刺--每日立会(5)
    第十六周进度表
    第十五周进度表
    梦断代码阅读笔记之六
    梦断代码阅读笔记之五
    梦断代码阅读笔记之四
    梦断代码阅读笔记之三
    梦断代码阅读笔记之二
    梦断代码阅读笔记之一
  • 原文地址:https://www.cnblogs.com/FudgeBear/p/10306705.html
Copyright © 2011-2022 走看看