zoukankan      html  css  js  c++  java
  • u3d中的坐标系

    任何子级游戏对象 (Child GameObject) 的检视器 (Inspector) 中的变换 (Transform) 值都会相对于父级 (Parent) 的变换 (Transform) 值而显示。这些值又被称局部坐标 (Local Coordinate) 

    就是说。u3d界面上设置的坐标都是其本地坐标

    转换小图示切换 (Transform Gizmo Toggles) -- 影响场景视图 (Scene View) 显示   该图标只是为了旋转和移动位置时方便操作。。pivot 中心点在父对象本身的中心点。。 center在父对象整个容器的中心点。。。  global是全局的坐标系。。。local是本地的坐标系(旋转后,会改变坐标方向)

    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)。

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

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

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

    • 视口坐标→世界坐标:camera.ViewportToWorldPoint();
    • 案例1——在鼠标点击的位置上绘制一张图片出来(关于绘制GUI界面坐标系与屏幕坐标系之间的关系)。
      [csharp] view plaincopy
       
      1. using UnityEngine;  
      2.  using System.Collections;  
      3.  public class test : MonoBehaviour   {          
      4.       //图片    
      5.     public Texture img;      
      6.      //储存鼠标的位置坐标      
      7.     private Vector2 pos;     
      8.      void OnGUI()     
      9.       {             
      10.           //鼠标左击,获取当前鼠标的位置         
      11.        if (Input.GetMouseButton(0))           
      12.           {         
      13.                pos = Input.mousePosition;             
      14.            }          
      15.              //绘制图片        
      16.         GUI.DrawTexture(new Rect(pos.x,Screen.height - pos.y,100,100), img);         
      17.       }     
      18. }  
    • 案例2——坐标显示和坐标转换(这个是触摸方面的。如果没有触摸屏,那就将那个if去掉吧!)
      [csharp] view plaincopy
       
      1. using UnityEngine;     
      2. using System.Collections;     
      3. public class test: MonoBehaviour   {         
      4.      //场景的相机,拖放进来        
      5.     public Camera camera;          
      6.      //场景的物体        
      7.     private GameObject obj;      
      8.       void Start()        
      9.        {          
      10.             //初始化        
      11.         obj = GameObject.Find("Plane");         
      12.         }         
      13.          void Update ()       
      14.          {              
      15.                //有触摸           
      16.           if (Input.touchCount > 0)              
      17.                {                   
      18.                    print("世界坐标" + obj.transform.position);                    
      19.                     print("屏幕坐标" + Input.GetTouch(0).position);                    
      20.                     print("世界坐标→屏幕坐标" + camera.WorldToScreenPoint(obj.transform.position));                    
      21.                     print("屏幕坐标→视口坐标" + camera.ScreenToViewportPoint(Input.GetTouch(0).position));                   
      22.                    print("世界坐标→视口坐标" + camera.WorldToViewportPoint(obj.transform.position));               
      23.             }         
      24.       }   
      25. }  
  • 相关阅读:
    《DSP using MATLAB》Problem 6.17
    一些老物件
    《DSP using MATLAB》Problem 6.16
    《DSP using MATLAB》Problem 6.15
    《DSP using MATLAB》Problem 6.14
    《DSP using MATLAB》Problem 6.13
    《DSP using MATLAB》Problem 6.12
    《DSP using MATLAB》Problem 6.11
    P1414 又是毕业季II
    Trie树
  • 原文地址:https://www.cnblogs.com/zhepama/p/4331419.html
Copyright © 2011-2022 走看看