public void generate() { //Debug.Log(Profiler.maxNumberOfSamplesPerFrame); Profiler.BeginSample("Terrain:Generate"); ChunkProcessor processor = new ChunkProcessor (); for (int i = 0; i < horizontalChunks; i++) { for (int j = 0; j < verticalChunks; j++) { for (int k = 0; k < horizontalChunks; k++) { Vector3 chunkPosition = new Vector3 (chunkSize*i+position.x, chunkSize*j+position.y, chunkSize*k+position.z); Chunk chunk = Chunk.Instantiate (chunkPosition); Profiler.BeginSample("Chunk:ApplyDensity"); chunk.applyDensity (density); Profiler.EndSample(); Profiler.BeginSample("Proccessor:ProcessChunk"); processor.process (chunk); Profiler.EndSample(); } } } Profiler.EndSample(); }
public ChunkMover(WorldData worldData, ChunkProcessor chunkProcessor) { m_WorldData = worldData; m_ChunkProcessor = chunkProcessor; }
public MeshDataGenerator(IBatchProcessor<Chunk> batchProcessor, WorldData worldData, ChunkProcessor chunkProcessor) { m_BatchProcessor = batchProcessor; m_WorldData = worldData; m_ChunkProcessor = chunkProcessor; }
public LightProcessor(IBatchProcessor<Chunk> batchProcessor, WorldData worldData, ChunkProcessor chunkProcessor) { m_BatchProcessor = batchProcessor; m_WorldData = worldData; m_ChunkProcessor = chunkProcessor; }
private Vector3 interpolatedVertex(int x, int y, int z, int edge, ChunkProcessor processor) { Vector3 dir1 = edgeVertex [edge].First; Vector3 dir2 = edgeVertex [edge].Second; return interpolate(x + dir1.x, y + dir1.y, z + dir1.z, x + dir2.x, y + dir2.y, z + dir2.z, processor); }
private Vector3 interpolate(float v1x, float v1y, float v1z, float v2x, float v2y, float v2z, ChunkProcessor processor) { float density1 = processor.getChunk ().getDensityAt ((int)v1x, (int)v1y, (int)v1z); float density2 = processor.getChunk ().getDensityAt ((int)v2x, (int)v2y, (int)v2z); //Debug.Log ("d1:"+density1+" d2:"+density2); //Debug.Log (v1.x+","+v1.y+","+v1.z+" - "+v2.x+","+v2.y+","+v2.z); tempVector.x = interpolateValues(v1x, v2x, density1, density2)* Chunk.DELTA ; tempVector.y = interpolateValues(v1y, v2y, density1, density2)* Chunk.DELTA ; tempVector.z = interpolateValues(v1z, v2z, density1, density2)* Chunk.DELTA ; //return (v1*density1+v2*density2)/(density1+density2)* Chunk.DELTA ; return tempVector; }
/// <summary> /// Generates the vertices needed to create the triangles of the voxel in the specified position. /// </summary> /// <param name="x">The x coordinate of the voxel.</param> /// <param name="y">The y coordinate of the voxel.</param> /// <param name="z">The z coordinate of the voxel.</param> /// <param name="vertexIndex">All vertices of the chunk.</param> private void generateVertices(int x, int y, int z, ChunkProcessor processor) { int edges = edgesTable [cubeIndex]; int c = 1<<11; int index = 11; while (c > 0) { if((c & edges) > 0){ //index = bitToIndex(c); //index = MSB(c); //Debug.Log(" index: "+index); generateVertex(x, y, z, index, processor); } c >>= 1; index--; } }
/// <summary> /// Finds the neightbor vertex in the specified edge or creates a new one if it doesn't exist. /// </summary> /// <param name="x">The x coordinate.</param> /// <param name="y">The y coordinate.</param> /// <param name="z">The z coordinate.</param> /// <param name="edge">Edge.</param> private void generateVertex(int x, int y, int z, int edge, ChunkProcessor processor) { Pair<Vector3, int>[] neighbors = neighborsIndexation[edge]; int index; for (int c = 0; c < neighbors.Length; c++) { Vector3 dir = neighbors[c].First; int neighborEdge = neighbors[c].Second; if(processor.existVertex(x + (int)dir.x, y + (int)dir.y, z + (int)dir.z, neighborEdge)){ index = processor.getVertexIndex(x + (int)dir.x, y + (int)dir.y, z + (int)dir.z, neighborEdge); processor.setVertexIndex( x, y, z, edge, index); vertexIndeces[edge] = index; return; } } index = processor.getVertices ().Count; vertexIndeces[edge] = index; processor.setVertexIndex( x, y, z, edge, index); processor.addVertexToGraph ( index); processor.getVertices ().Add (interpolatedVertex( x, y, z, edge, processor)); }
/// <summary> /// Generates the triangles. /// </summary> /// <param name="x">The x coordinate.</param> /// <param name="y">The y coordinate.</param> /// <param name="z">The z coordinate.</param> /// <param name="processor">Processor.</param> private void generateTriangles(int x, int y, int z, ChunkProcessor processor) { int[] triangles = trianglesTable [cubeIndex]; int c3; for (int c = 0; c < 5; c++) { c3 = 3*c; if(triangles[c3] >= 0){ generateTriangle(x, y, z, triangles[c3], triangles[c3+1], triangles[c3+2], processor); } else break; } }
/// <summary> /// Generates the triangle that lies on the specified edges in the specified voxel. /// </summary> /// <param name="x">The voxel x coordinate.</param> /// <param name="y">The voxel y coordinate.</param> /// <param name="z">The voxel z coordinate.</param> /// <param name="edge1">Edge1.</param> /// <param name="edge2">Edge2.</param> /// <param name="edge3">Edge3.</param> /// <param name="processor">Processor.</param> private void generateTriangle(int x, int y, int z, int edge1, int edge2, int edge3, ChunkProcessor processor) { /*processor.getTriangles ().Add (processor.getVertexIndex(x, y, z, edge1)); processor.getTriangles ().Add (processor.getVertexIndex(x, y, z, edge2)); processor.getTriangles ().Add (processor.getVertexIndex(x, y, z, edge3));*/ /*processor.addTriangle (processor.getVertexIndex(x, y, z, edge1), processor.getVertexIndex(x, y, z, edge2), processor.getVertexIndex(x, y, z, edge3));*/ processor.addTriangle (vertexIndeces[edge1], vertexIndeces[edge2], vertexIndeces[edge3]); }
public void march(int x, int y, int z, float[,,] chunkDensity, ChunkProcessor processor) { Profiler.BeginSample("Marching:Density"); int xpo = x + 1, ypo = y + 1, zpo = z + 1; densities [0] = chunkDensity [x, y, z]; densities [1] = chunkDensity [xpo, y, z]; densities [2] = chunkDensity [xpo, y, zpo]; densities [3] = chunkDensity [x, y, zpo]; densities [4] = chunkDensity [x, ypo, z]; densities [5] = chunkDensity [xpo, ypo, z]; densities [6] = chunkDensity [xpo, ypo, zpo]; densities [7] = chunkDensity [x, ypo, zpo]; updateCubeIndex (); Profiler.EndSample(); Profiler.BeginSample("Marching:Vertices"); generateVertices (x, y, z, processor); Profiler.EndSample(); Profiler.BeginSample("Marching:Triangles"); generateTriangles (x, y, z, processor); Profiler.EndSample(); }
private void Start() { m_PrefabsByName = new Dictionary<string, Transform>(); InitializeDecoratorPrefabs(); m_ChunkProcessor = new ChunkProcessor(); m_WorldData = new WorldData(m_ChunkProcessor); m_ChunksParent = transform.FindChild("Chunks"); m_ChunkMover = new ChunkMover(m_WorldData, m_ChunkProcessor); // Only the dual layer terrain w/ medium valleys and standard terrain medium caves // currently work, I haven't updated the others to return sunlit blocks. BatchPoolProcessor<Chunk> batchProcessor = new BatchPoolProcessor<Chunk>(); WorldDecorator worldDecorator = new WorldDecorator(WorldData, batchProcessor); m_MeshDataGenerator = new MeshDataGenerator(batchProcessor, WorldData, m_ChunkProcessor); m_World = new World(WorldData, new TerrainGenerator(WorldData, m_ChunkProcessor, batchProcessor, new DualLayerTerrainWithMediumValleys()), new LightProcessor(batchProcessor, WorldData, m_ChunkProcessor), m_MeshDataGenerator, worldDecorator, m_ChunkProcessor); m_World.InitializeGridChunks(); InitializeTextures(); Player.transform.position = new Vector3(WorldData.WidthInBlocks / 2, 120, WorldData.HeightInBlocks / 2); CreateWorldChunkPrefabs(); m_World.StartProcessingThread(); m_PlayerChunkPosition = CurrentPlayerChunkPosition(); m_LastPlayerLocation = Player.position; }