Unity---------Mesh理解
-
Mesh顾名思义“网格”,Unity3D里面所有的模型都是由Mesh组成的,UI也不例外。
例如下图,模型上的一个个小网格就是Mesh,这些Mesh有不同的三维顶点(Vector3),共同组成了一个3D模型。
Unity3D中Mesh的基本单位是三角形,学习应该由浅入深,所以今天我们就从最基本最简单的等腰三角形开始画起。
本文作者尚为初学者,如有理解不到位的地方,欢迎指正。
首先我们新建一个名为TestTriangle的CSharp脚本,然后打开TestTriangle,我们开始编写代码。
- using UnityEngine;
- using System.Collections;
- /* ==============================================================================
- * 功能描述:创建三角形Mesh
- * 创 建 者:Eci
- * 创建日期:2016/09/04
- * ==============================================================================*/
- [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
- public class TestTriangle : MonoBehaviour {
- public float sideLength = 2;
- public float angleDegree = 100;
- private MeshFilter meshFilter;
- [ExecuteInEditMode]
- private void Awake()
- {
- meshFilter = GetComponent<MeshFilter>();
- meshFilter.mesh = Create (sideLength, angleDegree);
- }
- private void Update()
- {
- }
- private Mesh Create(float sideLength, float angleDegree)
- {
- Mesh mesh = new Mesh();
- Vector3[] vertices = new Vector3[3];
- float angle = Mathf.Deg2Rad * angleDegree;
- float halfAngle = angle / 2;
- vertices [0] = Vector3.zero;
- float cosA = Mathf.Cos (halfAngle);
- float sinA = Mathf.Sin (halfAngle);
- vertices [1] = new Vector3 (cosA * sideLength, 0, sinA * sideLength);
- vertices [2] = new Vector3 (cosA * sideLength, 0, -sinA * sideLength);
- int[] triangles = new int[3];
- triangles [0] = 0;
- triangles [1] = 1;
- triangles [2] = 2;
- mesh.vertices = vertices;
- mesh.triangles = triangles;
- Vector2[] uvs = new Vector2[vertices.Length];
- for (int i = 0; i < uvs.Length; i++)
- {
- uvs[i] = Vector2.zero;
- }
- mesh.uv = uvs;
- return mesh;
- }
- }
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。
在编辑器里,点击运行,我们就可以看到一个紫色(因为没有材质)的三角形。
但是只能在运行的时候才看得到这个三角形,编辑器里看不到怎么办?添加下面这段代码:
- void OnDrawGizmos()
- {
- Gizmos.color = Color.gray;
- DrawMesh();
- }
- void OnDrawGizmosSelected()
- {
- Gizmos.color = Color.green;
- DrawMesh();
- }
- private void DrawMesh()
- {
- Mesh mesh = Create(sideLength, angleDegree);
- int[] tris = mesh.triangles;
- Gizmos.DrawLine(mesh.vertices[tris[0]], mesh.vertices[tris[1]]);
- Gizmos.DrawLine(mesh.vertices[tris[0]], mesh.vertices[tris[2]]);
- Gizmos.DrawLine(mesh.vertices[tris[1]], mesh.vertices[tris[2]]);
- }
关于OnDrawGizmos和OnDrawGizmosSelected可以参考下面这个链接:
http://www.ceeger.com/Script/Gizmos/Gizmos.html简单来讲就是在编辑器模式下,绘制辅助线框。
这样一个简单的等腰三角形Mesh的绘制就完成了。什么?你不满意?我们稍微整理一下代码:
- using UnityEngine;
- using System.Collections;
- /* ==============================================================================
- * 功能描述:创建三角形Mesh
- * 创 建 者:Eci
- * 创建日期:2016/09/04
- * ==============================================================================*/
- [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
- public class TestTriangle : MonoBehaviour {
- public float sideLength = 2;
- public float angleDegree = 100;
- private static readonly int ANGLE_DEGREE_PRECISION = 1000;
- private static readonly int SIDE_LENGTH_PRECISION = 1000;
- private MeshFilter meshFilter;
- private TriangleMeshCreator creator = new TriangleMeshCreator();
- [ExecuteInEditMode]
- private void Awake()
- {
- meshFilter = GetComponent<MeshFilter>();
- }
- private void Update()
- {
- meshFilter.mesh = creator.CreateMesh(sideLength, angleDegree);
- }
- void OnDrawGizmos()
- {
- Gizmos.color = Color.gray;
- DrawMesh();
- }
- void OnDrawGizmosSelected()
- {
- Gizmos.color = Color.green;
- DrawMesh();
- }
- private void DrawMesh()
- {
- Mesh mesh = creator.CreateMesh(sideLength, angleDegree);
- int[] tris = mesh.triangles;
- Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[0]]), transformToWorld(mesh.vertices[tris[1]]));
- Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[0]]), transformToWorld(mesh.vertices[tris[2]]));
- Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[1]]), transformToWorld(mesh.vertices[tris[2]]));
- }
- private Vector3 transformToWorld(Vector3 src)
- {
- return transform.TransformPoint(src);
- }
- private class TriangleMeshCreator
- {
- private float _sideLength;
- private float _angleDegree;
- private Mesh _cacheMesh ;
- public Mesh CreateMesh(float sideLength, float angleDegree)
- {
- if (checkDiff(sideLength, angleDegree))
- {
- Mesh newMesh = Create(sideLength, angleDegree);
- if (newMesh != null)
- {
- _cacheMesh = newMesh;
- this._sideLength = sideLength;
- this._angleDegree = angleDegree;
- }
- }
- return _cacheMesh;
- }
- private Mesh Create(float sideLength, float angleDegree)
- {
- Mesh mesh = new Mesh();
- Vector3[] vertices = new Vector3[3];
- float angle = Mathf.Deg2Rad * angleDegree;
- float halfAngle = angle / 2;
- vertices [0] = Vector3.zero;
- float cosA = Mathf.Cos (halfAngle);
- float sinA = Mathf.Sin (halfAngle);
- vertices [1] = new Vector3 (cosA * sideLength, 0, sinA * sideLength);
- vertices [2] = new Vector3 (cosA * sideLength, 0, -sinA * sideLength);
- int[] triangles = new int[3];
- triangles [0] = 0;
- triangles [1] = 1;
- triangles [2] = 2;
- mesh.vertices = vertices;
- mesh.triangles = triangles;
- Vector2[] uvs = new Vector2[vertices.Length];
- for (int i = 0; i < uvs.Length; i++)
- {
- uvs[i] = Vector2.zero;
- }
- mesh.uv = uvs;
- return mesh;
- }
- private bool checkDiff(float sideLength, float angleDegree)
- {
- return (int)((sideLength - this._sideLength) * SIDE_LENGTH_PRECISION) != 0 ||
- (int)((angleDegree - this._angleDegree) * ANGLE_DEGREE_PRECISION) != 0;
- }
- }
- }
为GameObject的MeshRenderer添加材质,我们就可以看到有颜色的三角形了。因为uv值都为0,所以是单一颜色。不过没关系,下一堂课,我们会为三角形添加不同的纹理。
版权声明:本文为博主原创文章,未经博主允许不得转载。
Unity---------Mesh理解的更多相关文章
- Unity Mesh 初体验
什么是Mesh Mesh是Unity中的一个组件,称为网格组件.通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而一个复杂的多边形,实际上是由多个三角面拼接而成.所以一个3D模型的表面是 ...
- Unity内存理解(转)
Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个通过AssetBundle,其实两者区别不大. Resources.Load就是从一个缺省打进程序包里的AssetBu ...
- Unity 全面理解加载和内存管理
最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质上我理解没有什么区别.Resources ...
- 初识Unity Mesh
Mesh概念:Mesh是Unity中的一个组件,称为网格组件.通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而多边形实际上是由多个三角形拼接而成的.所以一个3D模型的表面其实是由多个彼 ...
- unity, mesh Collider
关闭mesh Renderer以便查看mesh Collider "For Unity 5, we must also select "Convex" on the Me ...
- Unity mesh 合并
簡介: 基本上就是把 很多物體結合成一個物體 的作法,這種做法有很多優點,例如:1. 提高效能2. 統一材質 (只要建立一個材質,就能控制.分配給所有物體)3. 動畫控制方便 (像是你要在 Unity ...
- unity meshrender理解
网格渲染器,其中unity里面多有的材质在渲染的时候都是会划分成三角形的,所以当添加一些物体的时候,例如3d text的时候,默认添加网格渲染器. 最常用的就是获取材质. 下面是一个利用网格渲染器获得 ...
- unity Mesh(网格)的使用
创建两个三角形合成的矩形网格: GameObject obj= new GameObject(); MeshRenderer meshRenderer=obj.AddComponent<Mesh ...
- [转]解读Unity中的CG编写Shader系列5——理论知识
经过前面的系列文章中的三个例子,尽管代码简单,但是我想应该还有些地方没有100%弄明白,我们现在得回过头来补充一些必备的数学.图形学知识 1.图形管道第一个例子中我有提到顶点着色和片段着色在整个图形绘 ...
- 蚂蚁金服研发的金融级分布式中间件SOFA背后的故事
导读:GIAC大会期间,蚂蚁金服杨冰,黄挺等讲师面向华南技术社区做了<数字金融时代的云原生架构转型路径>和<从传统服务化走向Service Mesh>等演讲,就此机会,高可用架 ...
随机推荐
- A implementaion for 2D blue noise
http://www.redblobgames.com/articles/noise/2d/
- jquery改变元素上下排列的顺序
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 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 ...
- iOS贝塞尔曲线(UIBezierPath)的基本使用方法
简介 UIBezierPath是对Core Graphics框架的一个封装,使用UIBezierPath类我们可以画出圆形(弧线)或者多边形(比如:矩形)等形状,所以在画复杂图形的时候会经常用到. 分 ...
- jQuery实现radio第一次点击选中第二次点击取消功能(转)
转载自:http://www.jb51.net/article/113730.htm 由于项目的需求,要求radio点击两次后为取消状态,不方便修改为checkbox,可以用正面的方法实现. // j ...
- Android ListView的使用(一)
初次接触listview,以为直接在Android Studio 中将控件给拖过去,就能够使用了,结果半天显示不了. 后来总算知道原因了. 先上代码: activity_main.xml 显示页面 里 ...
- flume 多chanel配置
#配置文 a1.sources= r1 a1.sinks= k1 k2 a1.channels= c1 c2 #Describe/configure the source a1.sources.r1. ...
- THEOS的第一个TWeak的成功创建
THEOS的第一个TWeak的成功创建html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMi ...
- 负数在计算机中的表示 Byte-128
本文转载: http://blog.csdn.net/njuitjf/article/details/4585247 原码:将一个整数,转换成二进制,就是其原码.如单字节的5的原码为:0000 010 ...
- Java编程的逻辑 (48) - 剖析ArrayDeque
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...