// public override void Build(TempMeshData tmd, int i, ref int vc) { // create a box! [ ] float xf0 = Main.tempVoxelPosRel.x, yf0 = Main.tempVoxelPosRel.y, zf0 = Main.tempVoxelPosRel.z; float xf1 = xf0 + 1, yf1 = yf0 + 1, zf1 = zf0 + 1; Voxel nv; // neighbour //yf0 *= 0.25f; //yf1 *= 0.25f; // bottom nv = Main.tempVoxels[i - Main.chunkSizeA2P2]; if (nv.isAir || !nv.HasFullWall(1)) { // Main.voxelTemplates[nv].HasFullWall(2)) { tmd.vertices.Add(new Vector3(xf0, yf0, zf1)); tmd.vertices.Add(new Vector3(xf0, yf0, zf0)); tmd.vertices.Add(new Vector3(xf1, yf0, zf0)); tmd.vertices.Add(new Vector3(xf1, yf0, zf1)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsDown4); tmd.uv.AddRange(_uvs); vc += 4; } // top nv = Main.tempVoxels[i + Main.chunkSizeA2P2]; if (nv.isAir || !nv.HasFullWall(0)) { tmd.vertices.Add(new Vector3(xf0, yf1, zf0)); tmd.vertices.Add(new Vector3(xf0, yf1, zf1)); tmd.vertices.Add(new Vector3(xf1, yf1, zf1)); tmd.vertices.Add(new Vector3(xf1, yf1, zf0)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsUp4); tmd.uv.AddRange(_uvs); vc += 4; } // back nv = Main.tempVoxels[i - Main.chunkSizeA2]; if (nv.isAir || !nv.HasFullWall(3)) { tmd.vertices.Add(new Vector3(xf0, yf0, zf0)); tmd.vertices.Add(new Vector3(xf0, yf1, zf0)); tmd.vertices.Add(new Vector3(xf1, yf1, zf0)); tmd.vertices.Add(new Vector3(xf1, yf0, zf0)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsBack4); tmd.uv.AddRange(_uvs); vc += 4; } // front nv = Main.tempVoxels[i + Main.chunkSizeA2]; if (nv.isAir || !nv.HasFullWall(2)) { tmd.vertices.Add(new Vector3(xf1, yf0, zf1)); tmd.vertices.Add(new Vector3(xf1, yf1, zf1)); tmd.vertices.Add(new Vector3(xf0, yf1, zf1)); tmd.vertices.Add(new Vector3(xf0, yf0, zf1)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsForward4); tmd.uv.AddRange(_uvs); vc += 4; } // left nv = Main.tempVoxels[i - 1]; if (nv.isAir || !nv.HasFullWall(5)) { tmd.vertices.Add(new Vector3(xf0, yf0, zf1)); tmd.vertices.Add(new Vector3(xf0, yf1, zf1)); tmd.vertices.Add(new Vector3(xf0, yf1, zf0)); tmd.vertices.Add(new Vector3(xf0, yf0, zf0)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsLeft4); tmd.uv.AddRange(_uvs); vc += 4; } // right nv = Main.tempVoxels[i + 1]; if (nv.isAir || !nv.HasFullWall(4)) { tmd.vertices.Add(new Vector3(xf1, yf0, zf0)); tmd.vertices.Add(new Vector3(xf1, yf1, zf0)); tmd.vertices.Add(new Vector3(xf1, yf1, zf1)); tmd.vertices.Add(new Vector3(xf1, yf0, zf1)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsRight4); tmd.uv.AddRange(_uvs); vc += 4; } }
Chunk CreateChunk(Chunk chunk) { #if UNITY_EDITOR if (chunk == null) { Debug.LogError("Chunk must not be null!"); } #endif tempChunk = chunk; tempVoxelPosRel.Reset(); // normal chunk data // TODO can be optimized? // TODO different standard landscapes int cx = chunk.pos.x * chunkSize; int cy = chunk.pos.y * chunkSize; int cz = chunk.pos.z * chunkSize; //int noAirVoxelCount = 0; for (int i = 0, y = -1; y < chunkSizeA1; ++y) { for (int z = -1; z < chunkSizeA1; ++z) { for (int x = -1; x < chunkSizeA1; ++x, ++i) { tempVoxels[i] = GetStandardVoxel(x + cx, y + cy, z + cz); // if (!tempVoxels[i].isAir) noAirVoxelCount++; } } } // load additional voxels data for (int i = 0; i < chunk.additionalVoxelsDataNum; ++i) { int vi = chunk.additionalVoxelsDataPosIndices[i]; // if (!tempVoxels[vi].isAir) noAirVoxelCount--; tempVoxels[vi] = chunk.additionalVoxelsDataBoxIndices[i]; // if (!tempVoxels[vi].isAir) noAirVoxelCount++; } /* * if (noAirVoxelCount == 0) * return chunk; */ // build chunk if (!chunk.built) { chunk.m = new Mesh(); chunk.go = new GameObject("chunk " + chunk.pos); chunk.go.transform.parent = chunkParent; chunk.go.transform.position = chunk.pos.ToVector() * chunkSize; chunk.mf = chunk.go.AddComponent <MeshFilter>(); chunk.go.AddComponent <MeshRenderer>().material = material; chunk.mc = chunk.go.AddComponent <MeshCollider>(); } if (!chunk.built && randomSpawnObjects != null && randomSpawnObjects.Length > 0) { for (int y = 0; y < chunkSize; ++y) { for (int z = 0; z < chunkSize; ++z) { for (int x = 0; x < chunkSize; ++x) { if (Random.value > 0.998f && tempVoxels[GetVoxelRelPosIndex(x, y, z)].isAir) { GameObject go = (GameObject)Instantiate(randomSpawnObjects[Random.Range(0, randomSpawnObjects.Length)], new Vector3(x + cx + 0.5f, y + cy + 0.5f, z + cz + 0.5f), Random.rotationUniform); go.transform.parent = chunk.go.transform; } } } } } // create chunk mesh data TempMeshData tmd = new TempMeshData(chunkSizeA2P2 * 2); int vc = 0; // current vertex count int start = (chunkSize + 3) * chunkSizeA2 + 1; int index = start; for (tempVoxelPosRel.y = 0; tempVoxelPosRel.y < chunkSize; ++tempVoxelPosRel.y, index += chunkSizeA2M2) { for (tempVoxelPosRel.z = 0; tempVoxelPosRel.z < chunkSize; ++tempVoxelPosRel.z, index += 2) { for (tempVoxelPosRel.x = 0; tempVoxelPosRel.x < chunkSize; ++tempVoxelPosRel.x, ++index) { if (!tempVoxels[index].isAir) { tempVoxels[index].Build(tmd, index, ref vc); } } } } // mesh creation chunk.m.Clear(); tmd.Assign(chunk.m); chunk.m.Optimize(); chunk.mf.sharedMesh = chunk.m; chunk.mc.sharedMesh = null; chunk.mc.sharedMesh = chunk.m; chunk.built = true; System.GC.Collect(); return(chunk); }
// public virtual void Build(TempMeshData tmd, int i, ref int vc) { }
// override public void Build(TempMeshData tmd, int i, ref int vc) { // create a box! [ ] float xf0 = Main.tempVoxelPosRel.x, yf0 = Main.tempVoxelPosRel.y, zf0 = Main.tempVoxelPosRel.z; float xf1 = xf0 + 1, yf1 = yf0 + 1, zf1 = zf0 + 1; Voxel nv; // neighbour //yf0 *= 0.25f; //yf1 *= 0.25f; // bottom nv = Main.tempVoxels[i - Main.chunkSizeA2P2]; if (nv.isAir || !nv.HasFullWall(1)) // Main.voxelTemplates[nv].HasFullWall(2)) { { tmd.vertices.Add(new Vector3(xf0, yf0, zf1)); tmd.vertices.Add(new Vector3(xf0, yf0, zf0)); tmd.vertices.Add(new Vector3(xf1, yf0, zf0)); tmd.vertices.Add(new Vector3(xf1, yf0, zf1)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsDown4); tmd.uv.AddRange(_uvs); vc += 4; } // top nv = Main.tempVoxels[i + Main.chunkSizeA2P2]; if (nv.isAir || !nv.HasFullWall(0)) { tmd.vertices.Add(new Vector3(xf0, yf1, zf0)); tmd.vertices.Add(new Vector3(xf0, yf1, zf1)); tmd.vertices.Add(new Vector3(xf1, yf1, zf1)); tmd.vertices.Add(new Vector3(xf1, yf1, zf0)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsUp4); tmd.uv.AddRange(_uvs); vc += 4; } // back nv = Main.tempVoxels[i - Main.chunkSizeA2]; if (nv.isAir || !nv.HasFullWall(3)) { tmd.vertices.Add(new Vector3(xf0, yf0, zf0)); tmd.vertices.Add(new Vector3(xf0, yf1, zf0)); tmd.vertices.Add(new Vector3(xf1, yf1, zf0)); tmd.vertices.Add(new Vector3(xf1, yf0, zf0)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsBack4); tmd.uv.AddRange(_uvs); vc += 4; } // front nv = Main.tempVoxels[i + Main.chunkSizeA2]; if (nv.isAir || !nv.HasFullWall(2)) { tmd.vertices.Add(new Vector3(xf1, yf0, zf1)); tmd.vertices.Add(new Vector3(xf1, yf1, zf1)); tmd.vertices.Add(new Vector3(xf0, yf1, zf1)); tmd.vertices.Add(new Vector3(xf0, yf0, zf1)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsForward4); tmd.uv.AddRange(_uvs); vc += 4; } // left nv = Main.tempVoxels[i - 1]; if (nv.isAir || !nv.HasFullWall(5)) { tmd.vertices.Add(new Vector3(xf0, yf0, zf1)); tmd.vertices.Add(new Vector3(xf0, yf1, zf1)); tmd.vertices.Add(new Vector3(xf0, yf1, zf0)); tmd.vertices.Add(new Vector3(xf0, yf0, zf0)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsLeft4); tmd.uv.AddRange(_uvs); vc += 4; } // right nv = Main.tempVoxels[i + 1]; if (nv.isAir || !nv.HasFullWall(4)) { tmd.vertices.Add(new Vector3(xf1, yf0, zf0)); tmd.vertices.Add(new Vector3(xf1, yf1, zf0)); tmd.vertices.Add(new Vector3(xf1, yf1, zf1)); tmd.vertices.Add(new Vector3(xf1, yf0, zf1)); tmd.triangles.Add(vc); tmd.triangles.Add(vc + 1); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 2); tmd.triangles.Add(vc + 3); tmd.triangles.Add(vc); tmd.normals.AddRange(Main.normalsRight4); tmd.uv.AddRange(_uvs); vc += 4; } }
Chunk CreateChunk(Chunk chunk) { #if UNITY_EDITOR if (chunk == null) { Debug.LogError("Chunk must not be null!"); } #endif tempChunk = chunk; tempVoxelPosRel.Reset(); // normal chunk data // TODO can be optimized? // TODO different standard landscapes int cx = chunk.pos.x * chunkSize; int cy = chunk.pos.y * chunkSize; int cz = chunk.pos.z * chunkSize; //int noAirVoxelCount = 0; for (int i = 0, y = -1; y < chunkSizeA1; ++y) { for (int z = -1; z < chunkSizeA1; ++z) { for (int x = -1; x < chunkSizeA1; ++x, ++i) { tempVoxels[i] = GetStandardVoxel(x + cx, y + cy, z + cz); // if (!tempVoxels[i].isAir) noAirVoxelCount++; } } } // load additional voxels data for (int i = 0; i < chunk.additionalVoxelsDataNum; ++i) { int vi = chunk.additionalVoxelsDataPosIndices[i]; // if (!tempVoxels[vi].isAir) noAirVoxelCount--; tempVoxels[vi] = chunk.additionalVoxelsDataBoxIndices[i]; // if (!tempVoxels[vi].isAir) noAirVoxelCount++; } /* if (noAirVoxelCount == 0) return chunk; */ // build chunk if (!chunk.built) { chunk.m = new Mesh(); chunk.go = new GameObject("chunk " + chunk.pos); chunk.go.transform.parent = chunkParent; chunk.go.transform.position = chunk.pos.ToVector() * chunkSize; chunk.mf = chunk.go.AddComponent<MeshFilter>(); chunk.go.AddComponent<MeshRenderer>().material = material; chunk.mc = chunk.go.AddComponent<MeshCollider>(); } if (!chunk.built && randomSpawnObjects != null && randomSpawnObjects.Length > 0) { for (int y = 0; y < chunkSize; ++y) { for (int z = 0; z < chunkSize; ++z) { for (int x = 0; x < chunkSize; ++x) { if (Random.value > 0.998f && tempVoxels[GetVoxelRelPosIndex(x, y, z)].isAir) { GameObject go = (GameObject)Instantiate(randomSpawnObjects[Random.Range(0, randomSpawnObjects.Length)], new Vector3(x + cx + 0.5f, y + cy + 0.5f, z + cz + 0.5f), Random.rotationUniform); go.transform.parent = chunk.go.transform; } } } } } // create chunk mesh data TempMeshData tmd = new TempMeshData(chunkSizeA2P2 * 2); int vc = 0; // current vertex count int start = (chunkSize + 3) * chunkSizeA2 + 1; int index = start; for (tempVoxelPosRel.y = 0; tempVoxelPosRel.y < chunkSize; ++tempVoxelPosRel.y, index += chunkSizeA2M2) { for (tempVoxelPosRel.z = 0; tempVoxelPosRel.z < chunkSize; ++tempVoxelPosRel.z, index+= 2) { for (tempVoxelPosRel.x = 0; tempVoxelPosRel.x < chunkSize; ++tempVoxelPosRel.x, ++index) { if (!tempVoxels[index].isAir) { tempVoxels[index].Build(tmd, index, ref vc); } } } } // mesh creation chunk.m.Clear(); tmd.Assign(chunk.m); chunk.m.Optimize(); chunk.mf.sharedMesh = chunk.m; chunk.mc.sharedMesh = null; chunk.mc.sharedMesh = chunk.m; chunk.built = true; System.GC.Collect(); return chunk; }
// virtual public void Build(TempMeshData tmd, int i, ref int vc) { }
public void Build(TempMeshData tmd, int i, ref int vc) { Main.voxelTemplates[templateIndex].Build(tmd, i, ref vc); }