Inheritance: UnityEngine.MonoBehaviour
Exemple #1
0
 public void Awake()
 {
     DebugUtility.active = this;
 }
		public static void DrawCubes (Vector3[] topVerts, Vector3[] bottomVerts, Color[] vertexColors, float width) {
			
			if (active == null) {
				active = GameObject.FindObjectOfType(typeof(DebugUtility)) as DebugUtility;
			}
			if (active == null) throw new System.NullReferenceException ();
			
			if (topVerts.Length != bottomVerts.Length || topVerts.Length != vertexColors.Length) {
				Debug.LogError ("Array Lengths are not the same");
				return;
			}
			
			//65000 limit divided by 4*6 = 24
			if (topVerts.Length > 2708) {
				Vector3[] topVerts2 = new Vector3[topVerts.Length-2708];
				Vector3[] bottomVerts2 = new Vector3[topVerts.Length-2708];
				Color[] vertexColors2 = new Color[topVerts.Length-2708];
				
				for (int i=2708;i<topVerts.Length;i++) {
					topVerts2[i-2708] = topVerts[i];
					bottomVerts2[i-2708] = bottomVerts[i];
					vertexColors2[i-2708] = vertexColors[i];
				}
				
				Vector3[] topVerts3 = new Vector3[2708];
				Vector3[] bottomVerts3 = new Vector3[2708];
				Color[] vertexColors3 = new Color[2708];
				
				for (int i=0;i<2708;i++) {
					topVerts3[i] = topVerts[i];
					bottomVerts3[i] = bottomVerts[i];
					vertexColors3[i] = vertexColors[i];
				}
				
				DrawCubes (topVerts2,bottomVerts2,vertexColors2, width);
				topVerts = topVerts3;
				bottomVerts = bottomVerts3;
				vertexColors = vertexColors3;
			}
			
			width /= 2F;
			
			Vector3[] vertices = new Vector3[topVerts.Length*4*6];
			int[] tris = new int[topVerts.Length*6*6];
			Color[] colors = new Color[topVerts.Length*4*6];
			
			for (int i=0;i<topVerts.Length;i++) {
				
				Vector3 top = topVerts[i] + new Vector3 (0,active.offset,0);
				Vector3 bottom = bottomVerts[i] - new Vector3 (0,active.offset,0);;
				
				Vector3 top1 = top + new Vector3 (-width,0,-width);
				Vector3 top2 = top + new Vector3 (width,0,-width);
				Vector3 top3 = top + new Vector3 (width,0,width);
				Vector3 top4 = top + new Vector3 (-width,0,width);
				Vector3 bottom1 = bottom + new Vector3 (-width,0,-width);
				Vector3 bottom2 = bottom + new Vector3 (width,0,-width);
				Vector3 bottom3 = bottom + new Vector3 (width,0,width);
				Vector3 bottom4 = bottom + new Vector3 (-width,0,width);
				
				int vIndex = i*4*6;
				
				Color col = vertexColors[i];
				//Color.Lerp (Color.green,Color.red,topVerts[i].y*0.06F);
				//
				
				for (int c=vIndex;c<vIndex+24;c++) {
					colors[c] = col;
				}
				
				//Top
				
				vertices[vIndex] = top1;
				vertices[vIndex+1] = top4;
				vertices[vIndex+2] = top3;
				vertices[vIndex+3] = top2;
				
				int tIndex = i*6*6;
				tris[tIndex] = vIndex;
				tris[tIndex+1] = vIndex+1;
				tris[tIndex+2] = vIndex+2;
				
				tris[tIndex+3] = vIndex;
				tris[tIndex+4] = vIndex+2;
				tris[tIndex+5] = vIndex+3;
				
				//Bottom
				vIndex += 4;
				vertices[vIndex+3] = bottom1;
				vertices[vIndex+2] = bottom4;
				vertices[vIndex+1] = bottom3;
				vertices[vIndex] = bottom2;
				
				tIndex += 6;
				tris[tIndex] = vIndex;
				tris[tIndex+1] = vIndex+1;
				tris[tIndex+2] = vIndex+2;
				
				tris[tIndex+3] = vIndex;
				tris[tIndex+4] = vIndex+2;
				tris[tIndex+5] = vIndex+3;
				
				//Right
				vIndex += 4;
				vertices[vIndex] = bottom2;
				vertices[vIndex+1] = top2;
				vertices[vIndex+2] = top3;
				vertices[vIndex+3] = bottom3;
				
				tIndex += 6;
				tris[tIndex] = vIndex;
				tris[tIndex+1] = vIndex+1;
				tris[tIndex+2] = vIndex+2;
				
				tris[tIndex+3] = vIndex;
				tris[tIndex+4] = vIndex+2;
				tris[tIndex+5] = vIndex+3;
				
				//Left
				vIndex += 4;
				vertices[vIndex+3] = bottom1;
				vertices[vIndex+2] = top1;
				vertices[vIndex+1] = top4;
				vertices[vIndex] = bottom4;
				
				tIndex += 6;
				tris[tIndex] = vIndex;
				tris[tIndex+1] = vIndex+1;
				tris[tIndex+2] = vIndex+2;
				
				tris[tIndex+3] = vIndex;
				tris[tIndex+4] = vIndex+2;
				tris[tIndex+5] = vIndex+3;
				
				//Forward
				vIndex += 4;
				vertices[vIndex+3] = bottom3;
				vertices[vIndex+2] = bottom4;
				vertices[vIndex+1] = top4;
				vertices[vIndex] = top3;
				
				tIndex += 6;
				tris[tIndex] = vIndex;
				tris[tIndex+1] = vIndex+1;
				tris[tIndex+2] = vIndex+2;
				
				tris[tIndex+3] = vIndex;
				tris[tIndex+4] = vIndex+2;
				tris[tIndex+5] = vIndex+3;
				
				//Back
				vIndex += 4;
				vertices[vIndex] = bottom2;
				vertices[vIndex+1] = bottom1;
				vertices[vIndex+2] = top1;
				vertices[vIndex+3] = top2;
				
				tIndex += 6;
				tris[tIndex] = vIndex;
				tris[tIndex+1] = vIndex+1;
				tris[tIndex+2] = vIndex+2;
				
				tris[tIndex+3] = vIndex;
				tris[tIndex+4] = vIndex+2;
				tris[tIndex+5] = vIndex+3;
			}
	
			
			Mesh mesh = new Mesh ();
			mesh.vertices = vertices;
			mesh.triangles = tris;
			mesh.colors = colors;
			
			mesh.name = "VoxelMesh";
			
			mesh.RecalculateNormals ();
			mesh.RecalculateBounds ();
			
			if (active.optimizeMeshes) {
				mesh.Optimize ();
			}
			
			GameObject go = new GameObject ("DebugMesh");
			MeshRenderer rend = go.AddComponent (typeof(MeshRenderer)) as MeshRenderer;
			rend.material = active.defaultMaterial;
			(go.AddComponent (typeof(MeshFilter)) as MeshFilter).mesh = mesh;
		}
Exemple #3
0
 public static void DrawCubes(Vector3[] topVerts, Vector3[] bottomVerts, Color[] vertexColors, float width)
 {
     if (DebugUtility.active == null)
     {
         DebugUtility.active = (UnityEngine.Object.FindObjectOfType(typeof(DebugUtility)) as DebugUtility);
     }
     if (DebugUtility.active == null)
     {
         throw new NullReferenceException();
     }
     if (topVerts.Length != bottomVerts.Length || topVerts.Length != vertexColors.Length)
     {
         Debug.LogError("Array Lengths are not the same");
         return;
     }
     if (topVerts.Length > 2708)
     {
         Vector3[] array = new Vector3[topVerts.Length - 2708];
         Vector3[] array2 = new Vector3[topVerts.Length - 2708];
         Color[] array3 = new Color[topVerts.Length - 2708];
         for (int i = 2708; i < topVerts.Length; i++)
         {
             array[i - 2708] = topVerts[i];
             array2[i - 2708] = bottomVerts[i];
             array3[i - 2708] = vertexColors[i];
         }
         Vector3[] array4 = new Vector3[2708];
         Vector3[] array5 = new Vector3[2708];
         Color[] array6 = new Color[2708];
         for (int j = 0; j < 2708; j++)
         {
             array4[j] = topVerts[j];
             array5[j] = bottomVerts[j];
             array6[j] = vertexColors[j];
         }
         DebugUtility.DrawCubes(array, array2, array3, width);
         topVerts = array4;
         bottomVerts = array5;
         vertexColors = array6;
     }
     width /= 2f;
     Vector3[] array7 = new Vector3[topVerts.Length * 4 * 6];
     int[] array8 = new int[topVerts.Length * 6 * 6];
     Color[] array9 = new Color[topVerts.Length * 4 * 6];
     for (int k = 0; k < topVerts.Length; k++)
     {
         Vector3 a = topVerts[k] + new Vector3(0f, DebugUtility.active.offset, 0f);
         Vector3 a2 = bottomVerts[k] - new Vector3(0f, DebugUtility.active.offset, 0f);
         Vector3 vector = a + new Vector3(-width, 0f, -width);
         Vector3 vector2 = a + new Vector3(width, 0f, -width);
         Vector3 vector3 = a + new Vector3(width, 0f, width);
         Vector3 vector4 = a + new Vector3(-width, 0f, width);
         Vector3 vector5 = a2 + new Vector3(-width, 0f, -width);
         Vector3 vector6 = a2 + new Vector3(width, 0f, -width);
         Vector3 vector7 = a2 + new Vector3(width, 0f, width);
         Vector3 vector8 = a2 + new Vector3(-width, 0f, width);
         int num = k * 4 * 6;
         Color color = vertexColors[k];
         for (int l = num; l < num + 24; l++)
         {
             array9[l] = color;
         }
         array7[num] = vector;
         array7[num + 1] = vector4;
         array7[num + 2] = vector3;
         array7[num + 3] = vector2;
         int num2 = k * 6 * 6;
         array8[num2] = num;
         array8[num2 + 1] = num + 1;
         array8[num2 + 2] = num + 2;
         array8[num2 + 3] = num;
         array8[num2 + 4] = num + 2;
         array8[num2 + 5] = num + 3;
         num += 4;
         array7[num + 3] = vector5;
         array7[num + 2] = vector8;
         array7[num + 1] = vector7;
         array7[num] = vector6;
         num2 += 6;
         array8[num2] = num;
         array8[num2 + 1] = num + 1;
         array8[num2 + 2] = num + 2;
         array8[num2 + 3] = num;
         array8[num2 + 4] = num + 2;
         array8[num2 + 5] = num + 3;
         num += 4;
         array7[num] = vector6;
         array7[num + 1] = vector2;
         array7[num + 2] = vector3;
         array7[num + 3] = vector7;
         num2 += 6;
         array8[num2] = num;
         array8[num2 + 1] = num + 1;
         array8[num2 + 2] = num + 2;
         array8[num2 + 3] = num;
         array8[num2 + 4] = num + 2;
         array8[num2 + 5] = num + 3;
         num += 4;
         array7[num + 3] = vector5;
         array7[num + 2] = vector;
         array7[num + 1] = vector4;
         array7[num] = vector8;
         num2 += 6;
         array8[num2] = num;
         array8[num2 + 1] = num + 1;
         array8[num2 + 2] = num + 2;
         array8[num2 + 3] = num;
         array8[num2 + 4] = num + 2;
         array8[num2 + 5] = num + 3;
         num += 4;
         array7[num + 3] = vector7;
         array7[num + 2] = vector8;
         array7[num + 1] = vector4;
         array7[num] = vector3;
         num2 += 6;
         array8[num2] = num;
         array8[num2 + 1] = num + 1;
         array8[num2 + 2] = num + 2;
         array8[num2 + 3] = num;
         array8[num2 + 4] = num + 2;
         array8[num2 + 5] = num + 3;
         num += 4;
         array7[num] = vector6;
         array7[num + 1] = vector5;
         array7[num + 2] = vector;
         array7[num + 3] = vector2;
         num2 += 6;
         array8[num2] = num;
         array8[num2 + 1] = num + 1;
         array8[num2 + 2] = num + 2;
         array8[num2 + 3] = num;
         array8[num2 + 4] = num + 2;
         array8[num2 + 5] = num + 3;
     }
     Mesh mesh = new Mesh();
     mesh.vertices = array7;
     mesh.triangles = array8;
     mesh.colors = array9;
     mesh.name = "VoxelMesh";
     mesh.RecalculateNormals();
     mesh.RecalculateBounds();
     if (DebugUtility.active.optimizeMeshes)
     {
         mesh.Optimize();
     }
     GameObject gameObject = new GameObject("DebugMesh");
     MeshRenderer meshRenderer = gameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
     meshRenderer.material = DebugUtility.active.defaultMaterial;
     (gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter).mesh = mesh;
 }
		public void Awake () {
			active = this;
		}