void AddVoxelDataToChunk(Vector3 pos) { for (int faceIdx = 0; faceIdx < 6; faceIdx++) { // if the current voxel has no solid neighboor in the current faceIdx direction then draw it // else, it is being hidden and we do not need to draw it if (!IsVoxelFaceHidden(pos, faceIdx)) { // add vertex data vertices.Add(pos + VoxelData.voxelVerts[VoxelData.voxelTris[faceIdx, 0]]); vertices.Add(pos + VoxelData.voxelVerts[VoxelData.voxelTris[faceIdx, 1]]); vertices.Add(pos + VoxelData.voxelVerts[VoxelData.voxelTris[faceIdx, 2]]); vertices.Add(pos + VoxelData.voxelVerts[VoxelData.voxelTris[faceIdx, 3]]); // add texture map data Blocks.BlockType blockType = voxelMap[(int)pos.x, (int)pos.y, (int)pos.z]; AddTexture(Blocks.blockTypes[blockType].GetTextureId(faceIdx)); // add triangles triangles.Add(vertexIndex); triangles.Add(vertexIndex + 1); triangles.Add(vertexIndex + 2); triangles.Add(vertexIndex + 2); triangles.Add(vertexIndex + 1); triangles.Add(vertexIndex + 3); vertexIndex += 4; } } }
// World generation algorithm public Blocks.BlockType GetVoxel(Vector3 pos) { int yPos = Mathf.FloorToInt(pos.y); /* IMMUTABLE PASS */ // If out of world, always return air if (!IsVoxelInWorld(pos)) { return(Blocks.BlockType.AIR); } // Bottom is always bedrock if (yPos == 0) { return(Blocks.BlockType.BEDROCK); } /* BASIC TERRAIN PASS */ int terrainHeight = Mathf.FloorToInt(biome.terrainHeight * Noise.Get2DPerlin(new Vector2(pos.x, pos.z), 0, biome.terrainScale)) + biome.solidGroundHeight; Blocks.BlockType blockType = Blocks.BlockType.AIR; if (yPos == terrainHeight) { blockType = Blocks.BlockType.GRASS; } else if (yPos < terrainHeight && yPos > terrainHeight - 4) { blockType = Blocks.BlockType.DIRT; } else if (yPos > terrainHeight) { return(Blocks.BlockType.AIR); } else { blockType = Blocks.BlockType.STONE; } /* SECOND PASS */ if (blockType == Blocks.BlockType.STONE) { foreach (Lode lode in biome.lodes) { if (yPos > lode.minHeight && yPos < lode.maxHeight) { if (Noise.Get3DPerlin(pos, lode.noiseOffset, lode.scale, lode.threshold)) { blockType = lode.blockType; } } } } return(blockType); }
/// <summary> /// Gets the BlockInfo associated with the specified block type. /// </summary> public static BlockInfo GetInfo(BlockType type) { return(Blocks[(ushort)type]); }
private static void AddSingle(BlockType type, uint health, uint mass, BlockSides connectSides) { Blocks[(ushort)type] = new SingleBlockInfo(type, health, mass, Resources.Load("Blocks/" + type) as GameObject, connectSides); }