Unity中 mesh生成斜坡的示例代码
程序员文章站
2022-03-03 09:41:47
mesh概念:mesh是unity中的一个组件,称为网格组件。通俗的讲,mesh是指模型的网格,3d模型是由多边形拼接而成,而多边形实际上是由多个三角形拼接而成的。所以一个3d模型的表面其实是由多个彼...
mesh概念:
mesh是unity中的一个组件,称为网格组件。通俗的讲,mesh是指模型的网格,3d模型是由多边形拼接而成,而多边形实际上是由多个三角形拼接而成的。所以一个3d模型的表面其实是由多个彼此相连的三角面构成。三维空间中,构成这些三角形的点和边的集合就是mesh。
mesh组成:
1、顶点坐标数组vertexes
2、顶点在uv坐标系中的位置信息数组uvs
3、三角形顶点顺时针或者逆时针索引数组triangles
4、meshfiler组件,用于增加mesh属性
5、meshrender组件,增加材质并渲染出来。
6、可能还需要每个顶点的法线的数组normals
/* /// 功能: /// 时间: /// 版本: */ using system.collections; using system.collections.generic; using unityengine; public class meshcreater : monobehaviour { public vector3 eulerangles; [header("斜坡尺寸")] public float sizex; public float sizey; public float sizez; [header("斜坡后面的立方体尺寸")] public float planesize; public float m_angle; public material material; vector3[] vertices = new vector3[54]; vector2[] uvs = new vector2[54]; list<int> alltris = new list<int>(); vector3[] allpoint; gameobject gameobject; private void update() { if (input.getkeydown(keycode.space)) { drawmesh(m_angle); } } public void drawmesh(float angle) { if (gameobject != null) { destroy(gameobject); } sizex = sizey * mathf.tan(angle * mathf.pi / 180); gameobject = new gameobject("cube"); gameobject.transform.eulerangles = eulerangles; var mf = gameobject.addcomponent<meshfilter>(); var mr = gameobject.addcomponent<meshrenderer>(); allpoint = new[] { //斜坡 new vector3(0, 0, 0), new vector3(0, 0, sizez), new vector3(sizex, 0, sizez), new vector3(sizex, 0, 0), new vector3(0, sizey, 0), new vector3(0, sizey, sizez), //斜坡后的立方体 new vector3(0,-planesize,0), new vector3(sizex,-planesize,0), new vector3(sizex,-planesize,sizez), new vector3(0,-planesize,sizez), }; //斜坡 vertices[0] = allpoint[0]; //正面 vertices[1] = allpoint[4]; vertices[2] = allpoint[3]; vertices[3] = allpoint[0]; //底面1 vertices[4] = allpoint[3]; vertices[5] = allpoint[2]; vertices[6] = allpoint[0];//底面1 vertices[7] = allpoint[2]; vertices[8] = allpoint[1]; vertices[9] = allpoint[1]; //背面1 vertices[10] = allpoint[4]; vertices[11] = allpoint[0]; vertices[12] = allpoint[4];//背面2 vertices[13] = allpoint[1]; vertices[14] = allpoint[5]; vertices[15] = allpoint[5]; //反面 vertices[16] = allpoint[1]; vertices[17] = allpoint[2]; vertices[18] = allpoint[2]; //斜坡1 vertices[19] = allpoint[3]; vertices[20] = allpoint[4]; vertices[21] = allpoint[2]; //斜坡1 vertices[22] = allpoint[4]; vertices[23] = allpoint[5]; //斜坡后的立方体 vertices[24] = allpoint[6]; vertices[25] = allpoint[0]; vertices[26] = allpoint[3]; vertices[27] = allpoint[6]; vertices[28] = allpoint[3]; vertices[29] = allpoint[7]; vertices[30] = allpoint[6]; vertices[31] = allpoint[9]; vertices[32] = allpoint[0]; vertices[33] = allpoint[0]; vertices[34] = allpoint[9]; vertices[35] = allpoint[1]; vertices[36] = allpoint[7]; vertices[37] = allpoint[3]; vertices[38] = allpoint[8]; vertices[39] = allpoint[8]; vertices[40] = allpoint[3]; vertices[41] = allpoint[2]; vertices[42] = allpoint[7]; vertices[43] = allpoint[8]; vertices[44] = allpoint[9]; vertices[45] = allpoint[7]; vertices[46] = allpoint[9]; vertices[47] = allpoint[6]; vertices[48] = allpoint[1]; vertices[49] = allpoint[9]; vertices[50] = allpoint[8]; vertices[51] = allpoint[1]; vertices[52] = allpoint[8]; vertices[53] = allpoint[2]; for (int i = 0; i < vertices.length; i++) { alltris.add(i); } uvs[0] = new vector2(0, 0); uvs[1] = new vector2(0, 1); uvs[2] = new vector2(1, 0); uvs[3] = new vector2(0, 0); uvs[4] = new vector2(1, 0); uvs[5] = new vector2(1, 1); uvs[6] = new vector2(0f, 0f); uvs[7] = new vector2(1f, 1f); uvs[8] = new vector2(0f, 1f); uvs[9] = new vector2(0, 1); uvs[10] = new vector2(1, 0); uvs[11] = new vector2(0, 0); uvs[12] = new vector2(1, 0); uvs[13] = new vector2(0, 1); uvs[14] = new vector2(1, 1); uvs[15] = new vector2(0f, 1f); uvs[16] = new vector2(0, 0); uvs[17] = new vector2(1, 0); uvs[18] = new vector2(1f, 1f); uvs[19] = new vector2(1f, 0f); uvs[20] = new vector2(0f, 0f); uvs[21] = new vector2(1, 1); uvs[22] = new vector2(0, 0); uvs[23] = new vector2(0, 1); uvs[24] = new vector2(1, 1); uvs[25] = new vector2(0, 0); uvs[26] = new vector2(0, 1); uvs[27] = new vector2(1, 1); uvs[28] = new vector2(0, 0); uvs[29] = new vector2(0, 1); uvs[30] = new vector2(1, 1); uvs[31] = new vector2(0, 0); uvs[32] = new vector2(0, 1); uvs[33] = new vector2(1, 1); uvs[34] = new vector2(0, 0); uvs[35] = new vector2(0, 1); uvs[36] = new vector2(1, 1); uvs[37] = new vector2(0, 0); uvs[38] = new vector2(0, 1); uvs[39] = new vector2(1, 1); uvs[40] = new vector2(0, 0); uvs[41] = new vector2(0, 1); uvs[42] = new vector2(1, 1); uvs[43] = new vector2(0, 0); uvs[44] = new vector2(0, 1); uvs[45] = new vector2(1, 1); uvs[46] = new vector2(0, 0); uvs[47] = new vector2(0, 1); uvs[48] = new vector2(1, 1); uvs[49] = new vector2(0, 0); uvs[50] = new vector2(0, 1); uvs[51] = new vector2(1, 1); uvs[52] = new vector2(0, 0); uvs[53] = new vector2(0, 1); mesh mesh = new mesh(); mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = alltris.toarray(); mr.material = material; mesh.recalculatetangents(); mesh.recalculatenormals(); mesh.recalculatebounds(); mf.mesh = mesh; } private void ondrawgizmos() { if (allpoint != null) { for (int i = 0; i < allpoint.length; i++) { gizmos.color = color.yellow; gizmos.drawsphere(allpoint[i], 0.1f); } } } }
以上就是unity中 mesh生成斜坡的示例代码的详细内容,更多关于unity mesh斜坡的资料请关注其它相关文章!
上一篇: Java 将Excel转为SVG的方法
下一篇: React鼠标多选功能的配置方法