public void Generate() { System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); timer.Start(); MeshData meshData = MarchingCubes.Poligonyze( new Bounds(center, terrainMaster.chunkSize), Sample, terrainMaster.resolution, (1 - terrainMaster.soilToAirVolumetricRatio) * 2 - 1); timer.Stop(); Debug.Log("Chunk " + center + ": MarchingCubes.Poligonyze in " + timer.ElapsedMilliseconds + " milliseconds."); Matrix4x4 matrix = Matrix4x4.Scale(Vector3.one * terrainMaster.postProcessingScale) * Matrix4x4.Translate(-center); meshData.Transform(matrix); ThreadWorkManager.RequestMainThreadWork(() => { gameObject = new GameObject(); gameObject.name = center.ToString(); gameObject.layer = LayerMask.NameToLayer("Terrain"); Mesh mesh = meshData.ToMesh(); gameObject.AddComponent <MeshFilter>().sharedMesh = mesh; gameObject.AddComponent <MeshRenderer>().sharedMaterial = terrainMaster.chunkMaterial; gameObject.AddComponent <MeshCollider>().sharedMesh = mesh; gameObject.transform.parent = terrainMaster.transform; gameObject.transform.position = center * terrainMaster.postProcessingScale; gameObject.SetActive(isVisible); }); }
public TerrainChunk(Vector3 center, TerrainMaster terrainMaster) { this.center = center; this.terrainMaster = terrainMaster; ThreadWorkManager.RequestWork(Generate); }
void Start() { voxelMap = new VoxelMap(width, height); ThreadWorkManager.RequestWork(() => { GenerateMap(); SpawnMap(); SpawnMarchingMap(); }); }
void SpawnMarchingMapChunk(Vector3Int center, Vector3Int size) { MeshData meshData = MarchingCubes.Poligonyze(new PolygonizableVoxelMap(voxelMap), new Bounds(center, size), Vector3.one * 2, 0.5f); //Debug.Log("Finished MarchingCubes.Poligonyze at " + center); ThreadWorkManager.RequestMainThreadWork(() => { GameObject go = new GameObject(); go.AddComponent <MeshFilter>().mesh = meshData.ToMesh(); go.AddComponent <MeshRenderer>().sharedMaterial = voxelMaterial; go.transform.rotation = Quaternion.Euler(-90, 0, 0); }); }
void Start() { Vector3 playerPosition = playerTransform.position; ThreadWorkManager.RequestWork(() => UpdateChunks(playerPosition, visibleChunks)); chunkMaterial.SetFloat("_SimplexNoiseFrequency", TessellationSimplexNoiseFrequency); chunkMaterial.SetFloat("_SimplexNoiseAmplitude", TessellationSimplexNoiseAmplitude); foreach (var item in FindObjectsOfType <ColliderGenerator>()) { item.TessellationSimplexNoiseFrequency = TessellationSimplexNoiseFrequency; item.TessellationSimplexNoiseAmplitude = TessellationSimplexNoiseAmplitude; } }
void SpawnMarchingMap() { Vector3Int size = new Vector3Int(Mathf.Min(32, voxelMap.Width), Mathf.Min(32, voxelMap.Height), Mathf.Min(32, voxelMap.Depth)); for (int x = size.x / 2; x <= voxelMap.Width - size.x / 2; x += size.x) { for (int y = size.y / 2; y <= voxelMap.Height - size.y / 2; y += size.y) { for (int z = size.z / 2; z <= voxelMap.Depth - size.z / 2; z += size.z) { Vector3Int center = new Vector3Int(x, y, z); ThreadWorkManager.RequestWork(() => SpawnMarchingMapChunk(center, size)); } } } }
private void OnDestroy() { ThreadWorkManager.Destroy(); }
private void Update() { ThreadWorkManager.Update(); }
void SpawnMap() { MeshData meshData = new MeshData(); Vector3Int[] directions = new Vector3Int[] { new Vector3Int(1, 0, 0), new Vector3Int(-1, 0, 0), new Vector3Int(0, 1, 0), new Vector3Int(0, -1, 0), new Vector3Int(0, 0, 1), new Vector3Int(0, 0, -1) }; // Invert y and z for visualization purposes voxelMap.Foreach((x, y, z, voxelPresent) => { if (voxelPresent) { //Instantiate(voxelPrefab, new Vector3(x, y, z), Quaternion.identity, transform); for (int i = 0; i < directions.Length; i++) { if (voxelMap.CheckVoxelAt(x + directions[i].x, y + directions[i].y, z + directions[i].z) == false) { CreateSquare(new Vector3Int(x, y, z), meshData, directions[i]); } } } }); meshData.CalculateNormals(); ThreadWorkManager.RequestMainThreadWork(() => { GameObject go = new GameObject(); go.AddComponent <MeshFilter>().mesh = meshData.ToMesh(); go.AddComponent <MeshRenderer>().sharedMaterial = voxelMaterial; go.transform.rotation = Quaternion.Euler(-90, 0, 0); /*MeshData m = new MeshData(); * * //CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(1, 0, 0)); * CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(-1, 0, 0)); * CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(0, 1, 0)); * CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(0, -1, 0)); * //CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(0, 0, 1)); * CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(0, 0, -1)); * * CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(1, 0, 0)); * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(-1, 0, 0)); * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, 1, 0)); * CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, -1, 0)); * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, 0, 1)); * CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, 0, -1)); * * CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(1, 0, 0)); * CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(-1, 0, 0)); * CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, 1, 0)); * //CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, -1, 0)); * //CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, 0, 1)); * CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, 0, -1)); * * //CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(1, 0, 0)); * CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(-1, 0, 0)); * CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(0, 1, 0)); * CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(0, -1, 0)); * CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(0, 0, 1)); * //CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(0, 0, -1)); * * CreateSquare(new Vector3Int(101, 100, 101), m, new Vector3Int(1, 0, 0)); * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(-1, 0, 0)); * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, 1, 0)); * CreateSquare(new Vector3Int(101, 100, 101), m, new Vector3Int(0, -1, 0)); * CreateSquare(new Vector3Int(101, 100, 101), m, new Vector3Int(0, 0, 1)); * //CreateSquare(new Vector3Int(101, 100, 101), m, new Vector3Int(0, 0, -1)); * * CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(1, 0, 0)); * CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(-1, 0, 0)); * CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(0, 1, 0)); * //CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, -1, 0)); * CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(0, 0, 1)); * //CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(0, 0, -1)); * * m.Smooth(); * go = new GameObject(); * go.AddComponent<MeshFilter>().mesh = m.ToMesh(); * go.AddComponent<MeshRenderer>().sharedMaterial = voxelMaterial;*/ }); }