Example #1
0
	public void ConvertTrianglesToVoxels(Blocks MyBlocks, Mesh InputMesh) {
		resolution = Mathf.Pow (resolution, 2);
		InputMesh.RecalculateBounds ();
		MyBounds = InputMesh.bounds;
		// draws the bounding box
		DebugShapes.DrawCube (transform.position + InputMesh.bounds.center, InputMesh.bounds.extents, Color.green);


		Debug.LogError ("Converting mesh to voxels");
		MyBlocks = GetManager.GetDataManager().BlockStructuresList[BlockStructureIndex].MyBlocks;
		float SizeOfGrid = 1f;
		//real block size is x2 whatever i set here
		
		Vector3 Bounds = InputMesh.bounds.extents;

		float LargestSize = Bounds.x;
		if (LargestSize < Bounds.y)
			LargestSize = Bounds.y;
		if (LargestSize < Bounds.z)
			LargestSize = Bounds.z;
		//LargestSize = Mathf.CeilToInt (LargestSize);
		GridLength = Mathf.CeilToInt (LargestSize * resolution);
		Vector3 GridSize = new Vector3 (GridLength, GridLength, GridLength);
		BlockLength = Mathf.CeilToInt (LargestSize) / resolution;
		BlockSize = new Vector3 (BlockLength, BlockLength, BlockLength);


		//GridSize = GridSize / 2f;

		// calculate block size depending on the bounds size of the model
		// so if bounds size is 3, have block size as 3
		// then set grid size to block size divided by resolution
		
		MyBlocks.Size = GridSize;
		MyBlocks.InitilizeData();

		Vector3[] Verticies = InputMesh.vertices;
		int[] Indicies = InputMesh.GetIndices (0);
		Vector3[] Normals = InputMesh.normals;

		// i have to make the grid size the same as the bounding box
		 
		//Vector3 BlockSize = new Vector3(resolution, resolution, resolution); 
		//BlockSize = 2f*(BlockSize/(MyBounds.extents.magnitude));
		Debug.LogError("Block Size is: " + BlockSize.ToString());
		Debug.LogError("Indicies Size is: " + Indicies.Length.ToString());
		for (int z = 0; z < Indicies.Length; z += 3) {
			Vector3 Vertex1 = Verticies[Indicies[z+0]];
			Vector3 Vertex2 = Verticies[Indicies[z+1]];
			Vector3 Vertex3 = Verticies[Indicies[z+2]];
			if (IsNormalsDebug) {
				Debug.DrawLine (transform.position+Vertex1,transform.position+Vertex1 + Normals[Indicies[z+0]]*NormalsLength, Color.blue, 5);
				Debug.DrawLine (transform.position+Vertex2,transform.position+Vertex2 + Normals[Indicies[z+1]]*NormalsLength, Color.blue, 5);
				Debug.DrawLine (transform.position+Vertex3,transform.position+Vertex3 + Normals[Indicies[z+2]]*NormalsLength, Color.blue, 5);
			}

			//Debug.LogError ("Triangle " + (z/3) + ": " + Vertex1.ToString() + "---" + Vertex2.ToString() + "---" + Vertex3.ToString());

			//Debug.DrawLine (transform.position+new Vector3(startX, startY, startZ),transform.position+new Vector3(endX,endY,endZ), Color.red, 5);
			// should only do this for the blocks around the triangles
			// need to optimize this asap
			// for blocks around triangle?
			//Vector3 StartBlock = new Vector3();
			//StartBlock.x = Mathf.FloorToInt();	// minimum size of the Vertex AARB

			// find the size of each triangle as an Axis Aligned Rectangle Bounds - AARB
			Vector3 TrianglePosition = (Vertex2 + Vertex3)/2f;
			Vector3 TriangleMinimum = new Vector3 (Mathf.Min (Vertex1.x, Vertex2.x, Vertex3.x), Mathf.Min (Vertex1.y, Vertex2.y, Vertex3.y), Mathf.Min (Vertex1.z, Vertex2.z, Vertex3.z));
			Vector3 TriangleMaximum = new Vector3 (Mathf.Max (Vertex1.x, Vertex2.x, Vertex3.x), Mathf.Max (Vertex1.y, Vertex2.y, Vertex3.y), Mathf.Max (Vertex1.z, Vertex2.z, Vertex3.z));
			Vector3 TriangleSize = new Vector3();
			TriangleSize = TriangleMaximum - TriangleMinimum;
			TriangleSize *= 0.5f;
			TrianglePosition = TriangleMinimum + TriangleSize;
			TrianglePosition += InputMesh.bounds.extents;
			int StartX =  Mathf.FloorToInt((TrianglePosition.x-TriangleSize.x));	// before it was 0
			int EndX = Mathf.CeilToInt((TrianglePosition.x+TriangleSize.x)/resolution);//MyBlocks.Size.x; i++)
			int StartY =  Mathf.FloorToInt((TrianglePosition.y-TriangleSize.y));	
			int EndY = Mathf.CeilToInt((TrianglePosition.y+TriangleSize.y)/resolution);
			int StartZ =  Mathf.FloorToInt((TrianglePosition.z-TriangleSize.z));
			int EndZ = Mathf.CeilToInt((TrianglePosition.z+TriangleSize.z)/resolution);
			StartX = 0; StartY = 0; StartZ = 0;
			EndX = Mathf.CeilToInt(MyBlocks.Size.x)-1; EndY = Mathf.CeilToInt(MyBlocks.Size.y)-1; EndZ = Mathf.CeilToInt(MyBlocks.Size.z)-1;

			Debug.LogError (z + " || StartX: " + StartX + " - EndX: " + EndX + 
			                " || StartY: " + StartY + " - EndY: " + EndY + 
			                " || StartZ: " + StartZ + " - EndZ: " + EndZ);

			Debug.Break ();
			for (int i = StartX; i <= EndX; i++)
				for (int j = StartY; j <= EndY; j++)
					for (int k = StartZ; k <= EndZ; k++) 
				{
					if (MyBlocks.GetBlockType(new Vector3(i,j,k)) == 0) {
						Vector3 BlockPosition = BlockSize + MyBounds.center;
						BlockPosition += 2f*(new Vector3(i*BlockSize.x,j*BlockSize.y,k*BlockSize.z));
						if (IsTriangleInGrid(BlockPosition, BlockSize, TrianglePosition, TriangleSize)) {
								MyBlocks.UpdateBlock(new Vector3(i,j,k), 1);
						}// else
								//MyBlocks.UpdateBlock(new Vector3(i,j,k), 0);
					}
				}
			//break;	// for now test it once
		}

		Debug.LogError ("Size of grid: " + MyBlocks.Size.ToString ());
		if (IsDebugGrid)
		for (int i = 0; i < MyBlocks.Size.x; i++)
				for (int j = 0; j < MyBlocks.Size.y; j++)
					for (int k = 0; k < MyBlocks.Size.z; k++) 
				{
					Vector3 BlockPosition = BlockSize + MyBounds.center;
					BlockPosition += 2f*(new Vector3(i*BlockSize.x,j*BlockSize.y,k*BlockSize.z));

				if (MyBlocks.GetBlockType(new Vector3(i,j,k)) == 0) {
							if (IsDebugAllBlocks)
							DebugShapes.DrawCube (BlockPosition+transform.position - MyBounds.extents,
						                      BlockSize, 
					                      Color.white, true);
				}
				else {
						DebugShapes.DrawCube (BlockPosition+transform.position - MyBounds.extents,
					                      BlockSize, 
				                      Color.red, true);
				}

		}
	}