/// <summary> /// Calculate the elevation data and create it as a Mesh (on CPU to use Physics) /// </summary> public void CaluclateVertex(PlanetChunkObject chunk) { int hCount2 = ChunkSegments + 2; int vCount2 = ChunkSegments + 2; int numVertices = hCount2 * vCount2; vertexTmp.Clear(); normalTmp.Clear(); uvsTmp.Clear(); float Scale = chunk.Properties.Size / (float)ChunkSegments; for (float y = 0; y < vCount2; y++) { for (float x = 0; x < hCount2; x++) { float px = chunk.Properties.BottomLeft.x + x * Scale - 0.5f; float py = chunk.Properties.BottomLeft.y + y * Scale - 0.5f; vertexTmp.Add(GeVertex(chunk.Properties.Rotation, SphereRadius, px, py)); uvsTmp.Add(chunk.Properties.BottomLeft + new Vector2(x * Scale, y * Scale)); } } VertexComputeBuffer.SetData(vertexTmp); NormalComputeBuffer.SetData(normalTmp); VertexComputeShader.SetFloat("Scale", Scale); VertexComputeShader.SetFloat("TerrainScale", TerrainScale); VertexComputeShader.SetFloat("TerrainBumpScale", TerrainBumpScale); VertexComputeShader.Dispatch(0, numVertices / 16, 1, 1); AsyncGPUReadback.Request(VertexComputeBuffer, chunk.ApplyVertexData); AsyncGPUReadback.Request(NormalComputeBuffer, chunk.ApplyNormalData); chunk.MarkCalculatingVertexData(); chunk.name = "Recycled Mesh"; chunk.Filter.sharedMesh.SetUVs(0, uvsTmp); chunk.SetVisible(true); if (chunk.Properties != null) { if (chunk.Properties.LODLevel >= LodColliderStart) { chunk.Collider.sharedMesh = null; chunk.Collider.sharedMesh = chunk.Filter.sharedMesh; chunk.Collider.enabled = true; } else { chunk.Collider.enabled = false; } chunk.Properties.Active = true; } }
/// <summary> /// Update the Chunk mesh /// </summary> public void UpdateChunkMesh(PlanetChunkObject chunk) { if (chunk.Filter.sharedMesh == null) { chunk.Filter.sharedMesh = CopyMesh(BasicPlane); } CaluclateVertex(chunk); }
/// <summary> /// Get Free Chunk object if none exists, create /// </summary> public PlanetChunkObject GetFreeChunkObject(PlanetChunkProperties chunkProperties) { PlanetChunkObject c = GetChunkObjectFromPool(); if (c == null) { c = CreateBasicChunkObject(); } UpdateChunkObject(chunkProperties, c); return(c); }
/// <summary> /// Create a basic chunk /// </summary> private PlanetChunkObject CreateBasicChunkObject() { GameObject chunkObject = new GameObject("Chunk", typeof(MeshFilter), typeof(MeshCollider), typeof(MeshRenderer), typeof(PlanetChunkObject)); PlanetChunkObject chunk = chunkObject.GetComponent <PlanetChunkObject>(); chunk.Filter = chunk.GetComponent <MeshFilter>(); chunk.Collider = chunk.GetComponent <MeshCollider>(); chunk.Renderer = chunk.GetComponent <Renderer>(); chunk.Renderer.sharedMaterial = mat; return(chunk); }
/// <summary> /// add to chunk pool /// </summary> public void AddToChunkPool(PlanetChunkObject chunk) { MeshPool.Push(chunk); }
//Update the chunk's position public PlanetChunkObject UpdateChunkObject(PlanetChunkProperties chunkProperties, PlanetChunkObject chunk) { chunk.transform.localPosition = Vector3.zero; chunk.Properties = chunkProperties; chunkProperties.ChunkObject = chunk; UpdateChunkMesh(chunk); return(chunk); }