public BlockDefinition GetDefinition() { return(BlockDefinition.DefinitionOfType(type)); }
void GenerateBlocksThread(object chunkInstance) { Chunk chunk = chunkInstance as Chunk; ChunkSubspacePosition position; BlockSpacePosition checkPosition; for (position.x = 0; position.x < SIZE; position.x++) { checkPosition.x = worldPosition.x * SIZE + position.x; for (position.z = 0; position.z < SIZE; position.z++) { checkPosition.z = worldPosition.z * SIZE + position.z; isShorelineCache[position.x, position.z] = true; for (position.y = worldPosition.y * SIZE + SIZE; position.y < Configuration.HEIGHT; position.y++) { checkPosition.y = position.y; Block checkBlock = ChunkRepository.GetBlockAtPosition(checkPosition); if (checkBlock.IsActive() && checkBlock.IsNotTransparent()) { isShorelineCache[position.x, position.z] = false; break; } } } } BlockType[,,] blockTypes = worldGenerator.GenerateBlocks(chunk); for (position.x = 0; position.x < SIZE; position.x++) { for (position.y = 0; position.y < SIZE; position.y++) { for (position.z = 0; position.z < SIZE; position.z++) { BlockDefinition blockDefinition = BlockDefinition.DefinitionOfType(blockTypes[position.x, position.y, position.z]); chunk.SetBlock(position, blockDefinition, false); if (blockDefinition.IsActive() && blockDefinition.IsNotTransparent() && blockDefinition.GetBlockType() != BlockType.Sand) { isShorelineCache[position.x, position.z] = false; } if (blockDefinition.IsLightEmitter()) { BlockLight light; light.chunk = this; light.chunkPosition = position; light.blockDefinition = blockDefinition; lock (chunk.padlock) { chunk.lights.Add(light); } } } } } // Generate and apply the models from this and all nearby chunks List <Model> generatedModels = worldGenerator.GenerateModels(chunk); lock (generatingModelsLock) { for (int i = 0; i < generatedModels.Count; i++) { AddModel(generatedModels[i]); } } List <Chunk> lockedChunks = LockNearbyChunkModels(); ApplyModels(); UnlockChunkModels(lockedChunks); // Cleanup pass for (position.x = 0; position.x < SIZE; position.x++) { for (position.z = 0; position.z < SIZE; position.z++) { for (position.y = SIZE - 1; position.y >= 0; position.y--) { chunk.GetBlock(position); // If the block is water, make sure it's surrounded on the bottom and sides if (chunk.GetBlock(position).IsWater()) { for (int i = 0; i < 5; i++) { ChunkSubspacePosition solidCheckPosition = position; if (i == 0) { solidCheckPosition.y -= 1; } else if (i == 1) { solidCheckPosition.x -= 1; } else if (i == 2) { solidCheckPosition.x += 1; } else if (i == 3) { solidCheckPosition.z -= 1; } else if (i == 4) { solidCheckPosition.z += 1; } if (solidCheckPosition.x >= 0 && solidCheckPosition.x < SIZE && solidCheckPosition.y >= 0 && solidCheckPosition.y < SIZE && solidCheckPosition.z >= 0 && solidCheckPosition.z < SIZE) { if (chunk.GetBlock(solidCheckPosition).IsNotActive()) { chunk.SetBlock(solidCheckPosition, BlockType.Stone, false); } } else { BlockSpacePosition checkWorldPosition = solidCheckPosition.GetBlockSpacePosition(chunk); if (ChunkRepository.GetBlockAtPosition(checkWorldPosition).IsNotActive()) { ChunkRepository.SetBlockAtPosition(checkWorldPosition, BlockType.Stone, false); } } } } } } } LoadTransparencyCache(); chunk.SetLoadState(ChunkLoadState.BlockGenerationComplete); }
public void Initialize() { Vector3[] vertices = new Vector3[4 * 6]; Vector3[] normals = new Vector3[4 * 6]; int[] triangles = new int[6 * 6]; Vector2[] uvs = new Vector2[4 * 6]; Color[] colors = new Color[4 * 6]; Mesh mesh = new Mesh(); float s = 1.0f; Vector3 normal; int i = 0; MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>() as MeshFilter; meshFilter.mesh = mesh; CubeMesh = mesh; // Top normal = Vector3.up; vertices[i] = new Vector3(-s, s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(-s, s, s); normals[i] = normal; i++; vertices[i] = new Vector3(s, s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(s, s, s); normals[i] = normal; i++; // Bottom normal = Vector3.down; vertices[i] = new Vector3(s, -s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(s, -s, s); normals[i] = normal; i++; vertices[i] = new Vector3(-s, -s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(-s, -s, s); normals[i] = normal; i++; // North normal = Vector3.back; vertices[i] = new Vector3(-s, -s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(-s, s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(s, -s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(s, s, -s); normals[i] = normal; i++; // South normal = Vector3.forward; vertices[i] = new Vector3(s, -s, s); normals[i] = normal; i++; vertices[i] = new Vector3(s, s, s); normals[i] = normal; i++; vertices[i] = new Vector3(-s, -s, s); normals[i] = normal; i++; vertices[i] = new Vector3(-s, s, s); normals[i] = normal; i++; // West normal = Vector3.left; vertices[i] = new Vector3(-s, -s, s); normals[i] = normal; i++; vertices[i] = new Vector3(-s, s, s); normals[i] = normal; i++; vertices[i] = new Vector3(-s, -s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(-s, s, -s); normals[i] = normal; i++; // East normal = Vector3.right; vertices[i] = new Vector3(s, -s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(s, s, -s); normals[i] = normal; i++; vertices[i] = new Vector3(s, -s, s); normals[i] = normal; i++; vertices[i] = new Vector3(s, s, s); normals[i] = normal; i++; // Finish BlockDefinition blockDefinition = BlockDefinition.DefinitionOfType(BlockType.Dirt); Block block = new Block(); block.Set(blockDefinition); for (int j = 0; j < 6; j++) { Vector2 textureCoordinates = block.GetTextureCoordinates(CubeSide.Top, true, true, (byte)255); Vector2 overallTextureSize = block.GetOverallTextureSize(); Vector2 individualTextureSize = block.GetIndividualTextureSize(); Vector2 lowerUVs, upperUVs; lowerUVs.x = textureCoordinates.x / overallTextureSize.x; lowerUVs.y = 1.0f - textureCoordinates.y / overallTextureSize.y; upperUVs.x = (textureCoordinates.x + individualTextureSize.x) / overallTextureSize.x; upperUVs.y = 1.0f - (textureCoordinates.y + individualTextureSize.y) / overallTextureSize.y; Vector2 uv; uv.x = lowerUVs.x; uv.y = upperUVs.y; uvs[j * 4 + 0] = uv; uv.x = lowerUVs.x; uv.y = lowerUVs.y; uvs[j * 4 + 1] = uv; uv.x = upperUVs.x; uv.y = upperUVs.y; uvs[j * 4 + 2] = uv; uv.x = upperUVs.x; uv.y = lowerUVs.y; uvs[j * 4 + 3] = uv; triangles[j * 6 + 0] = j * 4 + 0; triangles[j * 6 + 1] = j * 4 + 1; triangles[j * 6 + 2] = j * 4 + 2; triangles[j * 6 + 3] = j * 4 + 1; triangles[j * 6 + 4] = j * 4 + 3; triangles[j * 6 + 5] = j * 4 + 2; colors[j * 4 + 0] = Color.white; colors[j * 4 + 1] = Color.white; colors[j * 4 + 2] = Color.white; colors[j * 4 + 3] = Color.white; } mesh.vertices = vertices; mesh.triangles = triangles; mesh.uv = uvs; mesh.normals = normals; mesh.colors = colors; mesh.RecalculateBounds(); mesh.Optimize(); mesh.UploadMeshData(false); gameObject.SetActive(false); startingScale = transform.localScale * 0.5f; }
private void SetBlock(ChunkSubspacePosition position, BlockType type, bool triggerLightingUpdate) { SetBlock(position, BlockDefinition.DefinitionOfType(type), triggerLightingUpdate); }