【边玩边学Unity3d】Mesh属性

转载:http://blog.csdn.net/orionid_java/article/details/12897099

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


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


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


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


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

代码直接拖到mainCamera中即可。可在场景中建几个Cube、Plane什么的看看。

[csharp]  view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. public class MeshPrinter : MonoBehaviour {  
  6.       
  7.     private string text_name;  
  8.     private string text_vertices;  
  9.     private string text_normals;  
  10.     private string text_triangles;  
  11.     private string text_uv;  
  12.     private string text_tangents;  
  13.       
  14.     public MeshFilter mCurrentFilter;  
  15.       
  16.     public List<MeshFilter> targets;  
  17.       
  18.     void Start () {  
  19.           
  20.         targets = new List<MeshFilter>();  
  21.         AddAllTargets();  
  22.           
  23.         mCurrentFilter = null;  
  24.   
  25.     }  
  26.   
  27.     void Update () {  
  28.           
  29.         if(Input.GetKeyUp(KeyCode.Tab)){  
  30.             TargetMesh();  
  31.             FillText();  
  32.         }  
  33.     }  
  34.       
  35.     void OnGUI(){  
  36.       
  37.           
  38.         float _x = 10;  
  39.         Vector2 textSize = GUI.skin.label.CalcSize (new GUIContent(text_name));  
  40.         GUI.Label(new Rect(_x, 10 ,textSize.x, textSize.y), text_name);  
  41.           
  42.         textSize = GUI.skin.label.CalcSize (new GUIContent(text_triangles));  
  43.         GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_triangles);  
  44.           
  45.         _x += textSize.x +20;  
  46.         textSize = GUI.skin.label.CalcSize (new GUIContent(text_vertices));  
  47.         GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_vertices);  
  48.           
  49.         _x += textSize.x +20;  
  50.         textSize = GUI.skin.label.CalcSize (new GUIContent(text_normals));  
  51.         GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_normals);  
  52.           
  53.         _x += textSize.x +20;  
  54.         textSize = GUI.skin.label.CalcSize (new GUIContent(text_tangents));  
  55.         GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_tangents);  
  56.           
  57.         _x += textSize.x +20;  
  58.         textSize = GUI.skin.label.CalcSize (new GUIContent(text_uv));  
  59.         GUI.Label(new Rect(_x, 30 ,textSize.x, textSize.y), text_uv);  
  60.           
  61.     }  
  62.       
  63.     public void AddAllTargets(){  
  64.           
  65.         GameObject[] gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];  
  66.         foreach(GameObject go in gos)  
  67.             if(go.GetComponent<MeshFilter>() != null)  
  68.                 AddTarget( go.GetComponent<MeshFilter>());  
  69.     }  
  70.       
  71.     public void AddTarget(MeshFilter target){  
  72.           
  73.         targets.Add(target);  
  74.     }  
  75.       
  76.     private void TargetMesh(){  
  77.           
  78.         if(mCurrentFilter == null){  
  79.             mCurrentFilter = targets[0];  
  80.         }else{  
  81.             int index = targets.IndexOf(mCurrentFilter);  
  82.             if(index < targets.Count-1){  
  83.                 index ++;  
  84.             }else{  
  85.                 index = 0;  
  86.             }  
  87.             mCurrentFilter = targets[index];  
  88.         }  
  89.     }  
  90.       
  91.     private void FillText(){  
  92.   
  93.         text_name = "Name: " + mCurrentFilter.gameObject.name;  
  94.   
  95.         Mesh mesh = mCurrentFilter.mesh;  
  96.   
  97.         int size = mesh.vertexCount;  
  98.         text_vertices = "vertices: "+ size + "\n";  
  99.         for(int i = 0; i<size; i++){  
  100.             text_vertices +=  i + ": " + mesh.vertices[i][0]+","+mesh.vertices[i][1]+","+mesh.vertices[i][2]+";\n";   
  101.         }  
  102.   
  103.         size = mesh.normals.Length;  
  104.         text_normals = "normals: " + size + "\n";  
  105.         for(int i = 0; i<size; i++){  
  106.             text_normals += mesh.normals[i].x +","+ mesh.normals[i].y +","+mesh.normals[i].z +";\n";  
  107.         }  
  108.   
  109.         size = mesh.triangles.Length ;  
  110.         text_triangles = "triangles: " + size + "\n";  
  111.         for(int i = 0; i<size/3; i++){  
  112.             text_triangles += mesh.triangles [3*i] +","+ mesh.triangles [3*i+1] +","+mesh.triangles [3*i+2] +";\n";  
  113.         }  
  114.   
  115.         size = mesh.uv.Length ;  
  116.         text_uv = "uv: " + size + "\n";  
  117.         for(int i = 0; i<size; i++){  
  118.             text_uv += mesh.uv [i][0] +","+ mesh.uv [i][1] +";\n";  
  119.   
  120.         }  
  121.         size = mesh.tangents.Length ;text_tangents = "tangents: " + size + "\n";  
  122.         for(int i = 0; i<size; i++){  
  123.             text_tangents += mesh.tangents[i][0] + ", "+ mesh.tangents[i][1] + ", "+mesh.tangents[i][2] + ", "+mesh.tangents[i][3] +";\n";  
  124.         }  
  125.     }  
  126. }  


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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值