Unity mesh属性理解

Unity没有建模工具,大多是在其他3D软件中建模后导入Unity资源中使用。但通过脚本可以修改网格的顶点属性,所以理论上可以实现在Unity中从无到有地建模。 文章出自 【狗刨学习网】

     建模,就是建网格;建网格,就是画些三角形;画个三角形呢,也就是定位三个点。 


     不过首先了解下Unity中网格的特性。Unity中的对象就是GameObject了,每个GameObject都可以有一个MeshFilter组件(也可以没有),该组件又有mesh属性(这个一定有),而该属性又有个vertives,也就是一个Vector3数组,储存着顶点信息。 


     下面就是写个脚本来看看mesh里的东东都是些什么了。 


     代码功能即:点击Tab键轮询场景中所有GameObject,以获取其MeshFilter.mesh,并在GUI中显示mesh的主要属性内容顶点坐标,法线,三角形的绘制序列等等。(代码写的很仓促,只为了显示内容。) 
     代码直接拖到mainCamera中即可。可在场景中建几个Cube、Plane什么的看看。 
   

  1. using UnityEngine; 
  2. using System.Collections; 
  3. using System.Collections.Generic; 


  4. public class MeshPrinter : MonoBehaviour 



  5.    private string text_name; 
  6.    private string text_vertices; 
  7.    private string text_normals; 
  8.    private string text_triangles; 
  9.    private string text_uv; 
  10.    private string text_tangents; 


  11.    public MeshFilter mCurrentFilter; 


  12.    public List targets; 


  13.    void Start() 
  14.    { 


  15.        targets = new List(); 
  16.        AddAllTargets(); 


  17.        mCurrentFilter = null; 


  18.    } 


  19.    void Update() 
  20.    { 


  21.        if (Input.GetKeyUp(KeyCode.Tab)) 
  22.        { 
  23.            TargetMesh(); 
  24.            FillText(); 
  25.        } 
  26.    } 


  27.    void OnGUI() 
  28.    { 




  29.        float _x = 10; 
  30.        Vector2 textSize = GUI.skin.label.CalcSize(new GUIContent(text_name)); 
  31.        GUI.Label(new Rect(_x, 10, textSize.x, textSize.y), text_name); 


  32.        textSize = GUI.skin.label.CalcSize(new GUIContent(text_triangles)); 
  33.        GUI.Label(new Rect(_x, 30, textSize.x, textSize.y), text_triangles); 


  34.        _x += textSize.x + 20; 
  35.        textSize = GUI.skin.label.CalcSize(new GUIContent(text_vertices)); 
  36.        GUI.Label(new Rect(_x, 30, textSize.x, textSize.y), text_vertices); 


  37.        _x += textSize.x + 20; 
  38.        textSize = GUI.skin.label.CalcSize(new GUIContent(text_normals)); 
  39.        GUI.Label(new Rect(_x, 30, textSize.x, textSize.y), text_normals); 


  40.        _x += textSize.x + 20; 
  41.        textSize = GUI.skin.label.CalcSize(new GUIContent(text_tangents)); 
  42.        GUI.Label(new Rect(_x, 30, textSize.x, textSize.y), text_tangents); 


  43.        _x += textSize.x + 20; 
  44.        textSize = GUI.skin.label.CalcSize(new GUIContent(text_uv)); 
  45.        GUI.Label(new Rect(_x, 30, textSize.x, textSize.y), text_uv); 


  46.    } 


  47.    public void AddAllTargets() 
  48.    { 


  49.        GameObject[] gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[]; 
  50.        foreach (GameObject go in gos) 
  51.            if (go.GetComponent() != null) 
  52.                AddTarget(go.GetComponent()); 
  53.    } 


  54.    public void AddTarget(MeshFilter target) 
  55.    { 


  56.        targets.Add(target); 
  57.    } 


  58.    private void TargetMesh() 
  59.    { 


  60.        if (mCurrentFilter == null) 
  61.        { 
  62.            mCurrentFilter = targets[0]; 
  63.        } 
  64.        else 
  65.        { 
  66.            int index = targets.IndexOf(mCurrentFilter); 
  67.            if (index < targets.Count - 1) 
  68.            { 
  69.                index++; 
  70.            } 
  71.            else 
  72.            { 
  73.                index = 0; 
  74.            } 
  75.            mCurrentFilter = targets[index]; 
  76.        } 
  77.    } 


  78.    private void FillText() 
  79.    { 


  80.        text_name = "Name: " + mCurrentFilter.gameObject.name; 


  81.        Mesh mesh = mCurrentFilter.mesh; 


  82.        int size = mesh.vertexCount; 
  83.        text_vertices = "vertices: " + size + ""; 
  84.        for (int i = 0; i < size; i++) 
  85.        { 
  86.            text_vertices += i + ": " + mesh.vertices[0] + "," + mesh.vertices[1] + "," + mesh.vertices[2] + ";"; 
  87.        } 


  88.        size = mesh.normals.Length; 
  89.        text_normals = "normals: " + size + ""; 
  90.        for (int i = 0; i < size; i++) 
  91.        { 
  92.            text_normals += mesh.normals.x + "," + mesh.normals.y + "," + mesh.normals.z + ";"; 
  93.        } 


  94.        size = mesh.triangles.Length; 
  95.        text_triangles = "triangles: " + size + ""; 
  96.        for (int i = 0; i < size / 3; i++) 
  97.        { 
  98.            text_triangles += mesh.triangles[3 * i] + "," + mesh.triangles[3 * i + 1] + "," + mesh.triangles[3 * i + 2] + ";"; 
  99.        } 


  100.        size = mesh.uv.Length; 
  101.        text_uv = "uv: " + size + ""; 
  102.        for (int i = 0; i < size; i++) 
  103.        { 
  104.            text_uv += mesh.uv[0] + "," + mesh.uv[1] + ";"; 


  105.        } 
  106.        size = mesh.tangents.Length; text_tangents = "tangents: " + size + ""; 
  107.        for (int i = 0; i < size; i++) 
  108.        { 
  109.            text_tangents += mesh.tangents[0] + ", " + mesh.tangents[1] + ", " + mesh.tangents[2] + ", " + mesh.tangents[3] + ";"; 
  110.        } 
  111.    } 
  112. }
复制代码

下图即是一个立方体的mesh内容: 

 


      从中可以看出Vertices是所有点的坐标,坐标是以模型的中心点为原点的  
      triangles: 包含顶点数组索引的三角形列表。三角形数组的大小是3的倍数。  
     normals:是这个点的法线,可以理解为从这个方向看过来才能看到这个点,其它方向看过来看不到这个点,三个点的法线相同才能构成一个三角面。  
     uv: 基础纹理坐标, 左下角为(0,0) 左上角为(0,1) 右上角为(1,1) 右下角为(1,0)设置相应的四个点就可以贴出图片里的内容  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值