码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3d修炼之路:用Mesh绘制一个Cube

时间:2014-11-17 22:56:02      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:io   ar   java   for   on   cti   ad   ef   as   

#pragma strict
function Awake(){

	var pMeshFilter : MeshFilter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;//网格过滤器
	var pMeshRender : MeshRenderer = gameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;//网格渲染
	var pMesh : Mesh = pMeshFilter.mesh as Mesh;	//网格过滤器的实例化的Mesh
	
	//u3d中绘制图形都是三角形绘制法,顺时针,所以先记录所有的点,然后在确定三角形绘制的顺序
	
	var pVector : Vector3 [] = new Vector3[36]; 
	var pTriangles : int [] = new int[pVector.length];
	
	
	//forword
	pVector[0] = new Vector3(0.0f,0.0f,0.0f);
	pVector[1] = new Vector3(0.0f,1.0f,0.0f);
	pVector[2] = new Vector3(1.0f,0.0f,0.0f);
	
	pVector[3] = new Vector3(1.0f,0.0f,0.0f);
	pVector[4] = new Vector3(0.0f,1.0f,0.0f);
	pVector[5] = new Vector3(1.0f,1.0f,0.0f);
	//back
	pVector[6] = new Vector3(0.0f,0.0f,1.0f);
	pVector[7] = new Vector3(0.0f,1.0f,1.0f);
	pVector[8] = new Vector3(1.0f,0.0f,1.0f);
	
	pVector[9] = new Vector3(1.0f,0.0f,1.0f);
	pVector[10] = new Vector3(0.0f,1.0f,1.0f);
	pVector[11] = new Vector3(1.0f,1.0f,1.0f);
	//left
	pVector[12] = new Vector3(0.0f,0.0f,0.0f);
	pVector[13] = new Vector3(0.0f,0.0f,1.0f);
	pVector[14] = new Vector3(0.0f,1.0f,1.0f);
	
	pVector[15] = new Vector3(0.0f,1.0f,1.0f);
	pVector[16] = new Vector3(0.0f,1.0f,0.0f);
	pVector[17] = new Vector3(0.0f,0.0f,0.0f);
	//right
	pVector[18] = new Vector3(1.0f,0.0f,0.0f);
	pVector[19] = new Vector3(1.0f,0.0f,1.0f);
	pVector[20] = new Vector3(1.0f,1.0f,0.0f);
	
	pVector[21] = new Vector3(1.0f,1.0f,0.0f);
	pVector[22] = new Vector3(1.0f,0.0f,1.0f);
	pVector[23] = new Vector3(1.0f,1.0f,1.0f);
	//up
	pVector[24] = new Vector3(0.0f,1.0f,0.0f);
	pVector[25] = new Vector3(0.0f,1.0f,1.0f);
	pVector[26] = new Vector3(1.0f,1.0f,0.0f);
	
	pVector[27] = new Vector3(1.0f,1.0f,0.0f);
	pVector[28] = new Vector3(0.0f,1.0f,1.0f);
	pVector[29] = new Vector3(1.0f,1.0f,1.0f);
	//down
	pVector[30] = new Vector3(0.0f,0.0f,0.0f);
	pVector[31] = new Vector3(0.0f,0.0f,1.0f);
	pVector[32] = new Vector3(1.0f,0.0f,0.0f);
	
	pVector[33] = new Vector3(1.0f,0.0f,0.0f);
	pVector[34] = new Vector3(0.0f,0.0f,1.0f);
	pVector[35] = new Vector3(1.0f,0.0f,1.0f);
	
	for(var nIndex : int = 0; nIndex < pTriangles.length;++nIndex){
		pTriangles[nIndex] =nIndex;
	}
	pMesh.Clear();
	pMesh.vertices = pVector;//网格顶点
	pMesh.triangles = pTriangles;//三角形
	pMesh.RecalculateBounds();
}

Unity3d修炼之路:用Mesh绘制一个Cube

标签:io   ar   java   for   on   cti   ad   ef   as   

原文地址:http://blog.csdn.net/xiaxiang123/article/details/41217761

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!