Unity3D之Mesh(七)绘制长方体
前言:
从现在开始,终于感觉进入一点点正题了!动态创建三维立体模型mesh!依然从简单入手:长方体。
一、基本思路
由于是创建长方体mesh,由之前的研究得知,两个数据必须要有,即:顶点的数据:vertices与索引的三角形(即负责管理每个三角形的三点的索引顺序):triangles。长方体:一般会得知:长宽高;即今天我们由长宽高为参数得到vertices与triangles。
二、基本程序框架
创建一个empty的gameobject,挂在脚本。
由基本思路可得基本框架,之后,实现函数功能即可;
using UnityEngine; [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class cube_mesh : MonoBehaviour
{
public float Length = ; //长方体的长
public float Width = ; //长方体的宽
public float Heigth = ; //长方体的高
private MeshFilter meshFilter; void Start()
{
meshFilter = GetComponent<MeshFilter>();
meshFilter.mesh = CreateMesh(Length, Width, Heigth);
} Mesh CreateMesh(float length, float width, float heigth)
{
//vertices(顶点、必须):
//......... //triangles(索引三角形、必须):
//......... //uv:
//......... //负载属性与mesh
Mesh mesh = new Mesh();
//.........
return mesh;
}
}
三、绘制函数的实现以及整个程序代码
using UnityEngine; [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class cube_mesh : MonoBehaviour
{
public float Length = ; //长方体的长
public float Width = ; //长方体的宽
public float Heigth = ; //长方体的高
private MeshFilter meshFilter; void Start()
{
meshFilter = GetComponent<MeshFilter>();
meshFilter.mesh = CreateMesh(Length, Width, Heigth);
} Mesh CreateMesh(float length, float width, float heigth)
{ //vertices(顶点、必须):
int vertices_count = *; //顶点数(每个面4个点,六个面)
Vector3[] vertices = new Vector3[vertices_count];
vertices[] = new Vector3(, , ); //前面的左下角的点
vertices[] = new Vector3(, heigth, ); //前面的左上角的点
vertices[] = new Vector3(length, , ); //前面的右下角的点
vertices[] = new Vector3(length, heigth, ); //前面的右上角的点 vertices[] = new Vector3(length, , width); //后面的右下角的点
vertices[] = new Vector3(length, heigth, width); //后面的右上角的点
vertices[] = new Vector3(, , width); //后面的左下角的点
vertices[] = new Vector3(, heigth, width); //后面的左上角的点 vertices[] = vertices[]; //左
vertices[] = vertices[];
vertices[] = vertices[];
vertices[] = vertices[]; vertices[] = vertices[]; //右
vertices[] = vertices[];
vertices[] = vertices[];
vertices[] = vertices[]; vertices[] = vertices[]; //上
vertices[] = vertices[];
vertices[] = vertices[];
vertices[] = vertices[]; vertices[] = vertices[]; //下
vertices[] = vertices[];
vertices[] = vertices[];
vertices[] = vertices[]; //triangles(索引三角形、必须):
int 分割三角形数 = * ;
int triangles_cout = 分割三角形数 * ; //索引三角形的索引点个数
int[] triangles = new int [triangles_cout]; //索引三角形数组
for(int i=,vi=;i< triangles_cout;i+=,vi+=)
{
triangles[i] = vi;
triangles[i+] = vi+;
triangles[i+] = vi+; triangles[i+] = vi+;
triangles[i+] = vi+;
triangles[i+] = vi+; } //uv:
//......... //负载属性与mesh
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
return mesh;
}
}
四、效果图
五、其他相关的说明
1、冗余的顶点坐标
正方体6个面,每个面由2个三角形组成,所以共需要36个三角形顶点索引。但是正方体只有8个顶点,为什么需要24个顶点坐标数据呢?
答案是:Unity3D的Mesh.triangles是三角形索引数组,不仅依靠这个索引值索引三角形顶点坐标,而且索引纹理坐标,索引法线向量。即正方体的每个顶点都参与了3个平面,而这3个平面的法线向量是不同的,该顶点在渲染这3个平面的时候需要索引到不同的法线向量。而由于顶点坐标和法线向量是由同一个索引值triangles[Index]取得的,例如,根据vertices[0],vertices[10],vertices[22]在vertices中索引到的顶点都为(0,0,0),但是在normals中索引到的法向量值各不相同。这就决定了在正方体中一个顶点,需要有3份存储。(如果你需要创建其它模型,需要根据实际情况决定顶点坐标的冗余度。实质上顶点坐标的冗余正是方便了法线坐标、纹理坐标的存取。)
2、三角形的渲染
准则:三角形有两面,正面可见,背面不可见。三角形的渲染顺序与三角形的正面法线呈左手螺旋定则。
这就决定了,如果我们需要渲染如下一个正方形面,那么就需要保证组成这个正方形的两个小三角形的正面法线都是指向屏幕外的。
程序中的顶点顺序为,三角形1: 0--->1--->2,三角形2:3--->2--->1 。
【欢迎转载】
转载请表明出处: 乐学习
Unity3D之Mesh(七)绘制长方体的更多相关文章
- Unity3D之Mesh(五)绘制圆
前言: Unity3D中Mesh的基本单位是三角形,而圆形就是由许许多多的三角形组成的.那么我们就知道了绘制圆形的Mesh需要两个变量:圆的半径 以及分割数: 一.实现过程 基本过程与之前的类似,最 ...
- Unity3D之Mesh(一)绘制三角形
前言: Unity自带几种简单的模型,如cube等:一般情况下,其余模型有3D建模软件生成,以合适的文件格式导入unity中:而mesh(以我目前很粗浅的了解)的一般用途就是:对现有的模型进行变形,以 ...
- Unity3D学习笔记2——绘制一个带纹理的面
目录 1. 概述 2. 详论 2.1. 网格(Mesh) 2.1.1. 顶点 2.1.2. 顶点索引 2.2. 材质(Material) 2.2.1. 创建材质 2.2.2. 使用材质 2.3. 光照 ...
- Istio Ambient Mesh七层服务治理图文详解
摘要:本文主要集中剖析Ambient mesh七层服务治理相关内容. 本文分享自华为云社区<Istio Ambient Mesh七层服务治理图文详解>,作者:华为云云原生团队. 由于Amb ...
- Unity3D之Mesh(四)绘制多边形
来自https://www.cnblogs.com/JLZT1223/p/6086191.html 1. 总的来说绘制平面的思想十分简单,就是将需要的平面拆分成几个三角形然后进行绘制就可以啦,主要的思 ...
- Unity3D之Mesh(六)绘制扇形、扇面、环形
前言: 绘制了圆,就想到绘制与之相关的几何图形,以便更灵活的掌握Mesh动态创建模型的机制与方法. 一.分析: 首先,结合绘制圆的过程绘制环形: 圆形是由segments个等腰三角形组成的(上一篇中, ...
- Unity3D之Mesh(三)绘制四边形
前言: 由於之前的基本介紹,所以有關的知識點不做贅述,只上案例,知識作爲自己做試驗的記錄,便於日後查看. 步驟: 1.創建一個empty 的gameobject: 2.添加一個脚本給這個game ob ...
- Unity3D学习笔记1——绘制一个三角形
目录 1. 绪论 2. 概述 3. 详论 3.1. 准备 3.2. 实现 3.3. 解析 3.3.1. 场景树对象 3.3.2. 绘制方法 4. 结果 1. 绪论 最近想学习一下Unity3d,无奈发 ...
- Unity3D之Mesh【创建动态Mesh的学习网站】
觉得不错!做记录! 1.http://gamerboom.com/archives/76484 2.http://jayelinda.com/ 3.几个私人的博客,可能有启发:http://blog. ...
随机推荐
- Android异步处理三:Handler+Looper+MessageQueue深入详解
在<Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面>中,我们讲到使用Thread+Handler的方式来实现界面的更新,其实是在非UI线程发送消息到U ...
- error items-9022:missing required icon file.the bundle does not contain an app icon for iPhone/iPad Touch of exactly '120x120' pixels,in.pen format for ios versions >= 7.0
error items-9022:missing required icon file.the bundle does not contain an app icon for iPhone/iPad ...
- 自定义circleindicator
在此申明,并不是自己写的,只是为了方便日后使用 我使用的circleindicator是从大神的gitHub中弄来的, 使用如下: 一.在配置中导入 compile 'me.relex:circlei ...
- 下周要搞大事情(ASP.NET Core & WebForms)!
下周要搞大事情(ASP.NET Core & WebForms)!
- java拾遗3----XML解析(三) StAX PULL解析
使用PULL方式解析XML: Pull是STAX的一个实现 StAX是The Streaming API for XML的缩写,一种利用拉模式解析(pull-parsing)XML文档的API StA ...
- SpringMVC拦截器实现登录认证
项目结构如图: 需要的jar:有springMVC配置需要的jar和jstl需要的jar SpringMVC包的作用说明: aopalliance.jar:这个包是AOP联盟的API包,里面包含了针对 ...
- ORDER BY today_used ASC' % (MAX_USED_TIMES)
python D:\pymine\clean\spider_map\get_bd_uid_rest_b.py python D:\pymine\clean\spider_map\get_bd_uid_ ...
- PHP计算多少秒/分/时/天/周/月/年之前 : timeago
function timeago( $ptime ) { $etime = time() - $ptime; if ($etime < 59) return '刚刚'; $interval = ...
- mysql语句, 空的 order by , 空的 where
SQL语句里, 空的 where, where 1 AND status=1 空的 order by, order by null, updatetime desc 这种空值的情况, 是很有用处的: ...
- ASP跳出FOR循环
由于ASP不能使用GOTO语句,我在FOR循环中加入一个FOR循环,若需要跳出,即退出最里面那个FOR循环. DEMO: <%dim aa = 0for i = 1 to 10 for j ...