Mesh顾名思义“网格”,Unity3D里面所有的模型都是由Mesh组成的,UI也不例外。

例如下图,模型上的一个个小网格就是Mesh,这些Mesh有不同的三维顶点(Vector3),共同组成了一个3D模型。

Unity3D中Mesh的基本单位是三角形,学习应该由浅入深,所以今天我们就从最基本最简单的等腰三角形开始画起。

本文作者尚为初学者,如有理解不到位的地方,欢迎指正。

 

首先我们新建一个名为TestTriangle的CSharp脚本,然后打开TestTriangle,我们开始编写代码。

  1. using UnityEngine;
  2. using System.Collections;
  3. /* ==============================================================================
  4. * 功能描述:创建三角形Mesh
  5. * 创 建 者:Eci
  6. * 创建日期:2016/09/04
  7. * ==============================================================================*/
  8. [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
  9. public class TestTriangle : MonoBehaviour {
  10. public float sideLength = 2;
  11. public float angleDegree = 100;
  12. private MeshFilter meshFilter;
  13. [ExecuteInEditMode]
  14. private void Awake()
  15. {
  16. meshFilter = GetComponent<MeshFilter>();
  17. meshFilter.mesh = Create (sideLength, angleDegree);
  18. }
  19. private void Update()
  20. {
  21. }
  22. private Mesh Create(float sideLength, float angleDegree)
  23. {
  24. Mesh mesh = new Mesh();
  25. Vector3[] vertices = new Vector3[3];
  26. float angle = Mathf.Deg2Rad * angleDegree;
  27. float halfAngle = angle / 2;
  28. vertices [0] = Vector3.zero;
  29. float cosA = Mathf.Cos (halfAngle);
  30. float sinA = Mathf.Sin (halfAngle);
  31. vertices [1] = new Vector3 (cosA * sideLength, 0, sinA * sideLength);
  32. vertices [2] = new Vector3 (cosA * sideLength, 0, -sinA * sideLength);
  33. int[] triangles = new int[3];
  34. triangles [0] = 0;
  35. triangles [1] = 1;
  36. triangles [2] = 2;
  37. mesh.vertices = vertices;
  38. mesh.triangles = triangles;
  39. Vector2[] uvs = new Vector2[vertices.Length];
  40. for (int i = 0; i < uvs.Length; i++)
  41. {
  42. uvs[i] = Vector2.zero;
  43. }
  44. mesh.uv = uvs;
  45. return mesh;
  46. }
  47. }

RequireComponent这一行,表示我们需要MeshRenderer和MeshFilter这两个组件,当我们将TestTriangle的代码挂在GameObject上的时候,会自动添加这两个组件。而我们要移除MeshRenderer或MeshFilter的时候,编辑器就会提示不能移除。

然后我们给出了两个公开变量,sideLength边长和angleDegree角度,因为我们这里要画的是等腰三角形,这代表的是等腰边长和等腰边长的夹角。

ExecuteInEditMode表示会在编辑器模式下运行。

Awake里,我们获取了MeshFilter并为它创建了Mesh。

Create方法里面,我们看到,先后为新建的Mesh创建了vertices(定点),triangles(三角形),uv(纹理坐标)。

vertices很简单,就是计算三角形三个顶点的坐标,因为是个二维图形,所以y坐标都为零。

triangles里保存的是vertices的下标。

uv暂时我们用不到,所以全部设为零。在后面文章中我们会介绍uv的用法。

最后返回mesh。

在编辑器里,点击运行,我们就可以看到一个紫色(因为没有材质)的三角形。

但是只能在运行的时候才看得到这个三角形,编辑器里看不到怎么办?添加下面这段代码:

  1. void OnDrawGizmos()
  2. {
  3. Gizmos.color = Color.gray;
  4. DrawMesh();
  5. }
  6. void OnDrawGizmosSelected()
  7. {
  8. Gizmos.color = Color.green;
  9. DrawMesh();
  10. }
  11. private void DrawMesh()
  12. {
  13. Mesh mesh = Create(sideLength, angleDegree);
  14. int[] tris = mesh.triangles;
  15. Gizmos.DrawLine(mesh.vertices[tris[0]], mesh.vertices[tris[1]]);
  16. Gizmos.DrawLine(mesh.vertices[tris[0]], mesh.vertices[tris[2]]);
  17. Gizmos.DrawLine(mesh.vertices[tris[1]], mesh.vertices[tris[2]]);
  18. }

关于OnDrawGizmos和OnDrawGizmosSelected可以参考下面这个链接:
http://www.ceeger.com/Script/Gizmos/Gizmos.html

简单来讲就是在编辑器模式下,绘制辅助线框。

这样一个简单的等腰三角形Mesh的绘制就完成了。什么?你不满意?我们稍微整理一下代码:

  1. using UnityEngine;
  2. using System.Collections;
  3. /* ==============================================================================
  4. * 功能描述:创建三角形Mesh
  5. * 创 建 者:Eci
  6. * 创建日期:2016/09/04
  7. * ==============================================================================*/
  8. [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
  9. public class TestTriangle : MonoBehaviour {
  10. public float sideLength = 2;
  11. public float angleDegree = 100;
  12. private static readonly int ANGLE_DEGREE_PRECISION = 1000;
  13. private static readonly int SIDE_LENGTH_PRECISION = 1000;
  14. private MeshFilter meshFilter;
  15. private TriangleMeshCreator creator = new TriangleMeshCreator();
  16. [ExecuteInEditMode]
  17. private void Awake()
  18. {
  19. meshFilter = GetComponent<MeshFilter>();
  20. }
  21. private void Update()
  22. {
  23. meshFilter.mesh = creator.CreateMesh(sideLength, angleDegree);
  24. }
  25. void OnDrawGizmos()
  26. {
  27. Gizmos.color = Color.gray;
  28. DrawMesh();
  29. }
  30. void OnDrawGizmosSelected()
  31. {
  32. Gizmos.color = Color.green;
  33. DrawMesh();
  34. }
  35. private void DrawMesh()
  36. {
  37. Mesh mesh = creator.CreateMesh(sideLength, angleDegree);
  38. int[] tris = mesh.triangles;
  39. Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[0]]), transformToWorld(mesh.vertices[tris[1]]));
  40. Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[0]]), transformToWorld(mesh.vertices[tris[2]]));
  41. Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[1]]), transformToWorld(mesh.vertices[tris[2]]));
  42. }
  43. private Vector3 transformToWorld(Vector3 src)
  44. {
  45. return transform.TransformPoint(src);
  46. }
  47. private class TriangleMeshCreator
  48. {
  49. private float _sideLength;
  50. private float _angleDegree;
  51. private Mesh _cacheMesh ;
  52. public Mesh CreateMesh(float sideLength, float angleDegree)
  53. {
  54. if (checkDiff(sideLength, angleDegree))
  55. {
  56. Mesh newMesh = Create(sideLength, angleDegree);
  57. if (newMesh != null)
  58. {
  59. _cacheMesh = newMesh;
  60. this._sideLength = sideLength;
  61. this._angleDegree = angleDegree;
  62. }
  63. }
  64. return _cacheMesh;
  65. }
  66. private Mesh Create(float sideLength, float angleDegree)
  67. {
  68. Mesh mesh = new Mesh();
  69. Vector3[] vertices = new Vector3[3];
  70. float angle = Mathf.Deg2Rad * angleDegree;
  71. float halfAngle = angle / 2;
  72. vertices [0] = Vector3.zero;
  73. float cosA = Mathf.Cos (halfAngle);
  74. float sinA = Mathf.Sin (halfAngle);
  75. vertices [1] = new Vector3 (cosA * sideLength, 0, sinA * sideLength);
  76. vertices [2] = new Vector3 (cosA * sideLength, 0, -sinA * sideLength);
  77. int[] triangles = new int[3];
  78. triangles [0] = 0;
  79. triangles [1] = 1;
  80. triangles [2] = 2;
  81. mesh.vertices = vertices;
  82. mesh.triangles = triangles;
  83. Vector2[] uvs = new Vector2[vertices.Length];
  84. for (int i = 0; i < uvs.Length; i++)
  85. {
  86. uvs[i] = Vector2.zero;
  87. }
  88. mesh.uv = uvs;
  89. return mesh;
  90. }
  91. private bool checkDiff(float sideLength, float angleDegree)
  92. {
  93. return (int)((sideLength - this._sideLength) * SIDE_LENGTH_PRECISION) != 0 ||
  94. (int)((angleDegree - this._angleDegree) * ANGLE_DEGREE_PRECISION) != 0;
  95. }
  96. }
  97. }

为GameObject的MeshRenderer添加材质,我们就可以看到有颜色的三角形了。因为uv值都为0,所以是单一颜色。不过没关系,下一堂课,我们会为三角形添加不同的纹理。

附上代码下载链接

 
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

Unity---------Mesh理解的更多相关文章

  1. Unity Mesh 初体验

    什么是Mesh Mesh是Unity中的一个组件,称为网格组件.通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而一个复杂的多边形,实际上是由多个三角面拼接而成.所以一个3D模型的表面是 ...

  2. Unity内存理解(转)

    Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个通过AssetBundle,其实两者区别不大. Resources.Load就是从一个缺省打进程序包里的AssetBu ...

  3. Unity 全面理解加载和内存管理

    最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质上我理解没有什么区别.Resources ...

  4. 初识Unity Mesh

    Mesh概念:Mesh是Unity中的一个组件,称为网格组件.通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而多边形实际上是由多个三角形拼接而成的.所以一个3D模型的表面其实是由多个彼 ...

  5. unity, mesh Collider

    关闭mesh Renderer以便查看mesh Collider "For Unity 5, we must also select "Convex" on the Me ...

  6. Unity mesh 合并

    簡介: 基本上就是把 很多物體結合成一個物體 的作法,這種做法有很多優點,例如:1. 提高效能2. 統一材質 (只要建立一個材質,就能控制.分配給所有物體)3. 動畫控制方便 (像是你要在 Unity ...

  7. unity meshrender理解

    网格渲染器,其中unity里面多有的材质在渲染的时候都是会划分成三角形的,所以当添加一些物体的时候,例如3d text的时候,默认添加网格渲染器. 最常用的就是获取材质. 下面是一个利用网格渲染器获得 ...

  8. unity Mesh(网格)的使用

    创建两个三角形合成的矩形网格: GameObject obj= new GameObject(); MeshRenderer meshRenderer=obj.AddComponent<Mesh ...

  9. [转]解读Unity中的CG编写Shader系列5——理论知识

    经过前面的系列文章中的三个例子,尽管代码简单,但是我想应该还有些地方没有100%弄明白,我们现在得回过头来补充一些必备的数学.图形学知识 1.图形管道第一个例子中我有提到顶点着色和片段着色在整个图形绘 ...

  10. 蚂蚁金服研发的金融级分布式中间件SOFA背后的故事

    导读:GIAC大会期间,蚂蚁金服杨冰,黄挺等讲师面向华南技术社区做了<数字金融时代的云原生架构转型路径>和<从传统服务化走向Service Mesh>等演讲,就此机会,高可用架 ...

随机推荐

  1. A implementaion for 2D blue noise

    http://www.redblobgames.com/articles/noise/2d/

  2. jquery改变元素上下排列的顺序

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. hive sequencefile导入文件遇到FAILED: SemanticException Unable to load data to destination table. Error: The file that you are trying to load does not match the file format of the destination table.错误

    hive sequencefile导入文件遇到FAILED: SemanticException Unable to load data to destination table. Error: Th ...

  4. iOS贝塞尔曲线(UIBezierPath)的基本使用方法

    简介 UIBezierPath是对Core Graphics框架的一个封装,使用UIBezierPath类我们可以画出圆形(弧线)或者多边形(比如:矩形)等形状,所以在画复杂图形的时候会经常用到. 分 ...

  5. jQuery实现radio第一次点击选中第二次点击取消功能(转)

    转载自:http://www.jb51.net/article/113730.htm 由于项目的需求,要求radio点击两次后为取消状态,不方便修改为checkbox,可以用正面的方法实现. // j ...

  6. Android ListView的使用(一)

    初次接触listview,以为直接在Android Studio 中将控件给拖过去,就能够使用了,结果半天显示不了. 后来总算知道原因了. 先上代码: activity_main.xml 显示页面 里 ...

  7. flume 多chanel配置

    #配置文 a1.sources= r1 a1.sinks= k1 k2 a1.channels= c1 c2 #Describe/configure the source a1.sources.r1. ...

  8. THEOS的第一个TWeak的成功创建

    THEOS的第一个TWeak的成功创建html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMi ...

  9. 负数在计算机中的表示 Byte-128

    本文转载: http://blog.csdn.net/njuitjf/article/details/4585247 原码:将一个整数,转换成二进制,就是其原码.如单字节的5的原码为:0000 010 ...

  10. Java编程的逻辑 (48) - 剖析ArrayDeque

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...