//MakeCube - Draws cubes void MakeCube(float cubeScale, Vector3 cubePos, int x, int y, int z, VoxelData data, int type, int meshNum) { int count = 0; //counts number of faces added to mesh. List <int> directions = new List <int>(); for (int i = 0; i < 6; i++) { if ((data.GetNeighbour(x, y, z, (Direction)i) != type + 1)) { count += 1; MakeFace((Direction)i, cubeScale, cubePos, type, meshNum); //makes the face and adds to count. directions.Add(i); } } if (count != 0) //if a face has been added, add the position of the voxel and number of faces. { meshList[meshNum].AddCube(new Vector3(x, y, z), count); int hn = GetHashNum(new Vector3(x, y, z)); if (hashTest) { table.Add(hn, meshNum); if (!table.ContainsKey(hn)) { table.Add(hn, meshNum); } else { table[hn] = meshNum; } } } }
void MakeCube(float cubeScale, Vector3 cubePos, int x, int z, VoxelData data) { for (int i = 0; i < 6; i++) { if (data.GetNeighbour(x, z, (Direction)i) == 0) { MakeFace((Direction)i, cubeScale, cubePos); } } }
private void MakeCube(float cubeScale, Vector3 cubePos, int x, int y, int z, VoxelData data) { for (var i = 0; i < 6; i++) { if (data.GetNeighbour(x, y, z, pos.x, pos.y, pos.z, (Direction)i, this) == 0) { MakeFace((Direction)i, cubeScale, cubePos); } } }
//decide where to make a face // which cube void MakeCube(float cubeScale, Vector3 cubePos, int x, int y, int z, VoxelData patternData) { for (int i = 0; i < 6; i++) // one cube has 6 faces { if (patternData.GetNeighbour(x, y, z, (FaceDirection)i) == false) // if this cube has no neighbour on that face direction { MakeFace((FaceDirection)i, cubeScale, cubePos); } } }
void MakeCube(float cubeScale, Vector3 cubePos, int x, int y, int z, VoxelData data) { Mesh mesh = new Mesh(); vertices = new List <Vector3>(); triangles = new List <int>(); GameObject cube = new GameObject(); for (int i = 0; i < 6; i++) { //only make face if neighbour is open space if (data.GetNeighbour(x, y, z, (Direction)i) == 0) { MakeFace((Direction)i, cubeScale, cubePos); } } //cube.AddComponent<Rigidbody>(); cube.AddComponent <MeshFilter>(); cube.AddComponent <MeshRenderer>(); cube.AddComponent <MeshCollider>(); cube.transform.parent = GetComponent <Transform>(); //mesh = cube.GetComponent<MeshFilter>().mesh; MeshCollider meshCol = cube.GetComponent <MeshCollider>(); meshCol.sharedMesh = mesh; meshCol.convex = true; cube.GetComponent <MeshRenderer>().material = mat; cube.GetComponent <MeshFilter>().mesh = mesh; Rigidbody rb = cube.GetComponent <Rigidbody>(); //rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ; mesh.Clear(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateNormals(); }
public int RemoveVoxel(RaycastHit hit, int operation) { List <int> toUpdate = new List <int>(); //new list of meshes which are edited //gets the position of the cube that is clicked int x = (int)Math.Round((hit.point.x / scale) - (hit.normal.x / 2)); int y = (int)Math.Round((hit.point.y / scale) - (hit.normal.y / 2)); int z = (int)Math.Round((hit.point.z / scale) - (hit.normal.z / 2)); //removing the old faces Vector3 voxel = new Vector3(x, y, z); int removed = data.GetCell(x, y, z) - 1; if (operation == 0 || (operation == 1 && removed == 5)) { if (hashTest) { if (table.ContainsKey(GetHashNum(voxel))) { int num = (int)table[GetHashNum(voxel)]; meshList[num].RemoveCube(voxel); data.SetVoxel(x, y, z, 0); toUpdate.Add(num); table.Remove(GetHashNum(voxel)); if (removed == 2 || removed == 3) //decay { int type = 7; int meshNum = GetFreeMesh(type); MakeCube(adjScale, new Vector3(x * scale, y * scale, z * scale), x, y, z, data, type, meshNum); meshList[meshNum].UpdateMesh(); toUpdate.Add(meshNum); table[GetHashNum(voxel)] = meshNum; } } else { //UpdateMesh(); for (int i = 0; i < meshList.Count; i++) { if (meshList[i].voxelPosition.Contains(voxel)) { meshList[i].UpdateMesh(); } } // Debug.Log(x + " " + y + " " + z); return(0); } } else { for (int i = 0; i < meshList.Count; i++) //check which mesh it belongs to { if (meshList[i].ContainsCube(voxel)) { meshList[i].RemoveCube(voxel); //remove cube from the mesh toUpdate.Add(i); break; } } } //drawing the new faces for (int i = 0; i < 6; i++) { int type = data.GetNeighbour(x, y, z, (Direction)i) - 1; //gets type of neighbour in direction if (type != -1 && type != 7) //if not -1, reveal the face behind it. { toUpdate.Add(MakeFace(GetDirection(i), adjScale, GetVector(new Vector3(x, y, z) * scale, i), type)); } } toUpdate = toUpdate.Distinct().ToList(); for (int i = 0; i < toUpdate.Count; i++) { meshList[toUpdate[i]].UpdateMesh(); } if (operation == 0) { //scoring element if (removed == 2 || removed == 3) { data.SetVoxel(x, y, z, 8); return(50); //if decay } else if (removed == 4) { return(-1000); } else if (removed == 5) { return(-10); } else if (removed == 6) { return(0); //if filler } else { return(-10); //if healthy tooth } } else { return(50); } } return(0); }