unity mesh的自定义创建
来源:互联网 发布:中昌数据董事长 编辑:程序博客网 时间:2024/04/29 06:32
using System.Collections;using System.Collections.Generic;using UnityEngine;using System;public class MeshCreat : MonoBehaviour {private int VERTICES_COUNT = 6;// Use this for initializationvoid Start () {//三角形//MeshFilter meshFilter = (MeshFilter)this.GetComponent(typeof(MeshFilter));//Mesh mesh = meshFilter.mesh;//Vector3[] vertices = new Vector3[3];//int[] triangles = new int[3];//vertices[0] = new Vector3(0,0,0);//vertices[1] = new Vector3(0,1,0);//vertices[2] = new Vector3(1,0,0);//triangles[0] = 0;//triangles[1] = 1;//triangles[2] = 2;//mesh.vertices = vertices;//mesh.triangles = triangles;//长方形,4个三角形//MeshFilter meshFilter = (MeshFilter)this.GetComponent(typeof(MeshFilter));//Mesh mesh = meshFilter.mesh;////一共有多少个定点//Vector3[] vertices = new Vector3[VERTICES_COUNT];////三角形数量//int triangles_count = VERTICES_COUNT - 2;////三角形顶点ID数组//int[] triangles = new int[triangles_count * 3];//vertices[0] = new Vector3(0,0,0);//vertices[1] = new Vector3(0,1,0);//vertices[2] = new Vector3(1,0,0);//vertices[3] = new Vector3(1,1,0);//vertices[4] = new Vector3(2,0,0);//vertices[5] = new Vector3(2,1,0);////绘制三角形//mesh.vertices = vertices;////起始三角形顶点//int start = 0;////结束三角形顶点//int end = 3;//for (int i = start;i < end;i++) {//for (int j = 0;j < 3;j++) {//if (i % 2 == 0)//{//triangles[3 * i + j] = i + j;//}//else {//triangles[3 * i + j] = i + 2 - j;//}//}//}//mesh.triangles = triangles;//画一个自定义图形Vector3[] va = new Vector3[] { new Vector3(1.21f,1.27f,30.00f),new Vector3(2.25f,2.22f,30.00f),new Vector3(3.29f,3.18f,30.00f),new Vector3(4.34f,4.14f,30.00f),new Vector3(5.38f,5.10f,30.00f),new Vector3(6.42f,6.07f,30.00f),new Vector3(7.21f,7.27f,30.00f),new Vector3(8.25f,6.22f,30.00f),new Vector3(9.29f,5.18f,30.00f),new Vector3(10.34f,4.14f,30.00f),new Vector3(11.38f,3.10f,30.00f),new Vector3(12.42f,2.07f,30.00f),new Vector3(13.25f,6.22f,30.00f),new Vector3(14.29f,9.18f,30.00f),new Vector3(15.34f,10.14f,30.00f),new Vector3(16.38f,6.10f,30.00f),new Vector3(17.42f,2.07f,30.00f),};Vector3[] vb = new Vector3[] { new Vector3(1.21f,1.27f,35.00f),new Vector3(2.25f,2.22f,35.00f),new Vector3(3.29f,3.18f,35.00f),new Vector3(4.34f,4.14f,35.00f),new Vector3(5.38f,5.10f,35.00f),new Vector3(6.42f,6.07f,35.00f),new Vector3(7.21f,7.27f,35.00f),new Vector3(8.25f,6.22f,35.00f),new Vector3(9.29f,5.18f,35.00f),new Vector3(10.34f,4.14f,35.00f),new Vector3(11.38f,3.10f,35.00f),new Vector3(12.42f,2.07f,35.00f),new Vector3(13.25f,6.22f,35.00f),new Vector3(14.29f,9.18f,35.00f),new Vector3(15.34f,10.14f,35.00f),new Vector3(16.38f,6.10f,35.00f),new Vector3(17.42f,2.07f,35.00f),};Vector3[] vd = new Vector3[] {new Vector3(1.21f,1.27f,40.00f),new Vector3(2.25f,2.22f,40.00f),new Vector3(3.29f,3.18f,40.00f),new Vector3(4.34f,4.14f,40.00f),new Vector3(5.38f,5.10f,40.00f),new Vector3(6.42f,6.07f,40.00f),new Vector3(7.21f,7.27f,40.00f),new Vector3(8.25f,6.22f,40.00f),new Vector3(9.29f,5.18f,40.00f),new Vector3(10.34f,4.14f,40.00f),new Vector3(11.38f,3.10f,40.00f),new Vector3(12.42f,2.07f,40.00f),new Vector3(13.25f,6.22f,40.00f),new Vector3(14.29f,9.18f,40.00f),new Vector3(15.34f,10.14f,40.00f),new Vector3(16.38f,6.10f,40.00f),new Vector3(17.42f,2.07f,40.00f),};Vector3[] ve = new Vector3[] {new Vector3(1.21f,1.27f,45.00f),new Vector3(2.25f,2.22f,45.00f),new Vector3(3.29f,3.18f,45.00f),new Vector3(4.34f,4.14f,45.00f),new Vector3(5.38f,5.10f,45.00f),new Vector3(6.42f,6.07f,45.00f),new Vector3(7.21f,7.27f,45.00f),new Vector3(8.25f,6.22f,45.00f),new Vector3(9.29f,5.18f,45.00f),new Vector3(10.34f,4.14f,45.00f),new Vector3(11.38f,3.10f,45.00f),new Vector3(12.42f,2.07f,45.00f),new Vector3(13.25f,6.22f,45.00f),new Vector3(14.29f,9.18f,45.00f),new Vector3(15.34f,10.14f,45.00f),new Vector3(16.38f,6.10f,45.00f),new Vector3(17.42f,2.07f,45.00f),};//GameObject[] allV = new GameObject[5];CreatMesh(va,vb);//CreatMesh(vd,ve);}void CreatMesh(Vector3[] va,Vector3[] vb) {MeshFilter meshFilter = (MeshFilter)this.gameObject.GetComponent(typeof(MeshFilter));Mesh mesh = meshFilter.mesh;Vector3[] vertices = new Vector3[va.Length * 2];int triangles_count = (va.Length - 1) * 2;int[] triangles = new int[triangles_count * 3];int index_v = 0;for (int i = 0; i < va.Length; i++){vertices[index_v++] = va[i];Debug.Log("vertices: " + index_v + " va:" + i);vertices[index_v++] = vb[i];Debug.Log("vertices: " + index_v + " vb:" + i);}int index_t = 0;for (int i = 0; i < va.Length - 1; i++){triangles[index_t++] = i * 2;Debug.Log("triangles: " + index_t);triangles[index_t++] = 1 + i * 2;Debug.Log("triangles: " + index_t);triangles[index_t++] = (i + 1) * 2;Debug.Log("triangles" + index_t);triangles[index_t++] = (i + 1) * 2 + 1;Debug.Log("triangles" + index_t);triangles[index_t++] = (i + 1) * 2;Debug.Log("triangles" + index_t);triangles[index_t++] = i * 2 + 1;Debug.Log("triangles" + index_t);if (index_t == triangles.Length){break;}}mesh.vertices = vertices;mesh.triangles = triangles;}// Update is called once per framevoid Update () {}}
准则:三角形有两面,正面可见,背面不可见。三角形的渲染顺序与三角形的正面法线呈左手螺旋定则。
阅读全文
0 0
- unity mesh的自定义创建
- Unity中创建自己的mesh以及动态修改mesh
- Unity中创建自己的mesh以及动态修改mesh
- [Unity实战]自定义mesh
- [Unity实战]自定义mesh
- unity代码创建mesh
- Unity 创建一个扇形Mesh
- Unity 动态创建mesh API
- unity 脚本创建 MESH(网格)
- 用unity的mesh画圆
- Unity里的Mesh属性
- Unity里的Mesh属性
- Unity 3D 创建Mesh(一)
- Unity 3D 创建Mesh(二)
- unity中利用mesh动态创建墙体
- Unity 3D 创建Mesh(一)
- Unity 3D 创建Mesh(二)
- Unity Mesh
- 追踪 Netty 异常占用堆外内存的经验分享
- bzoj 4999: This Problem Is Too Simple! 树链剖分+线段树
- 基于CentOS7.3安装Zabbix3.2手记
- pandas读取csv文件
- HDU 1036 Average is not Fast Enough! (水题)
- unity mesh的自定义创建
- javascriptinterface与java交互在android4.2以下版本的解决方案
- 页面跳转和重定向
- 明明的随机数(数组、排序、去重)
- 初识html5
- localStorage用法总结
- oracle rac后台进程介绍
- 暑假集训第六周
- HDU 4082(计算几何+相似三角形)