Unity Mesh教程 之 使用Mesh画一个三角形

 时间:2024-10-12 00:24:36

Unity Mesh教程 之 使用Mesh画一个三角形。本节介绍如何使用Mesh画一个三角形出来的简单案例,具体如下

工具/原料

Unity

Mesh

一、知识要点

1、Mesh:A class that allows creat坡纠课柩ing or modifying meshes from scripts.Meshes contain vertices and multiple triangle arrays. See theProcedural example projectfor examples of using the mesh interface.The triangle arrays are simply indices into the vertex arrays; three indices for each triangle.For every vertex there can be a normal, two texture coordinates, color and tangent. These are optional though and can be removed at will. All vertex information is stored in separate arrays of the same size, so if your mesh has 10 vertices, you would also have 10-size arrays for normals and other attributes.

2、MeshFilter:class in UnityEngine Inherits from:ComponentA class to access theMeshof themesh filter.Use this with a procedural mesh interface. See Also:Meshclass.

3、MeshRenderer:Inherits from:RendererRenders meshes inserted by theMeshFilterorTextMesh.

4、方法提示:1)要求必须添加“MeshFilter”和“MeshRender”组件2)把三角形值赋给“MeshFilter.mesh”

二、Mesh教程 之 使用Mesh画一个三角形

1、打开Unity,新建一个空工程,具体如下

Unity Mesh教程 之 使用Mesh画一个三角形Unity Mesh教程 之 使用Mesh画一个三角形

4、“MeshTriangle哌囿亡噱Test”脚本的具体代码如下:usingSystem.Collections.Generic;usingUnityEngine;[Require潮贾篡绐Component(typeof(MeshFilter))][RequireComponent(typeof(MeshRenderer))]publicclassMeshTriangleTest:MonoBehaviour{privateList<Vector3>points=newList<Vector3>();//Usethisforinitialization voidStart(){ points.Add(newVector3(0,0,0)); points.Add(newVector3(0,1,0)); points.Add(newVector3(1,0,0));MeshDrawTriangle(); }privatevoidMeshDrawTriangle(){//新建一个Mesh MeshtriangleMesh=newMesh();//把列表的顶点坐标赋给Mesh的vertexs triangleMesh.vertices=points.ToArray();//设置三角形顶点数量 int[]trianglePoints=newint[3]; trianglePoints[0]=0; trianglePoints[1]=1; trianglePoints[2]=2;//把三角形的数量给Mesh的三角形 triangleMesh.triangles=trianglePoints;//设置三角形的相关参数 triangleMesh.RecalculateBounds(); triangleMesh.RecalculateNormals(); triangleMesh.RecalculateTangents();//把三角形的Mesh赋给MeshFilter组件 GetComponent<MeshFilter>().mesh=triangleMesh; }}

5、脚本编译正确,回到Unity界面,在场景中新建一个“GameObject”,把脚本“MeshTriangleTest”赋给“GameObject”,你会看到自动加上组件“MeshFilter”和“MeshRender”,具体如下图

Unity Mesh教程 之 使用Mesh画一个三角形

7、到此,《Unity Mesh教程 之 使用Mesh画一个三角形》讲解结束,谢谢

  • solidworks怎么识别特征
  • SW如何为棱体创建对称的镜面
  • solidworks怎么对模型倒圆角
  • Solidworks 2018如何创建成形特征
  • SolidWorks汽车车轮建模教程(2)
  • 热门搜索
    邢台旅游局 贵州旅游攻略景点必去 仁化旅游景点大全 无锡有哪些旅游景点 杭州市旅游网 三亚旅游费用 太原到海南旅游 旅游点评 国外旅游的好地方 辽宁旅游地图