private void MeshUpdateThread() { Profiler.BeginThreadProfiling("Custom Threads", "Mesh Updater Thread"); while (meshUpdaterThreadStopped == 0) { MeshUpdateThreadStatus status = (MeshUpdateThreadStatus)meshUpdateThreadStatus; if (status == MeshUpdateThreadStatus.Updating) { // Main thread has requested mesh update // Determine which tiles need updating Debug.Log("Determining tiles to update"); GetTilesRequiringUpdate(); // Update tile edge flags Debug.Log("Updating tile edge transition flags"); UpdateTileEdgeFlags(tileRequiresUpdate); // Update tile meshes Debug.Log("Regenerating tile meshes"); foreach (var tileRequiringUpdate in tileRequiresUpdate) { if (tileRequiringUpdate.Value == true) { int x = tileRequiringUpdate.Key.x; int z = tileRequiringUpdate.Key.y; Vector2Int tileLocation = new Vector2Int(x, z); // Don't generate meshes for unloaded tiles if (map.tiles.ContainsKey(tileLocation)) { TerrainMeshGenerator.GenerateTile(tileMeshes[x, z], tileRequiringUpdate.Key, tileSize, tileResolution, tileLODs[x, z], map.tiles[tileLocation].heightmap, tileEdgeTransitionFlags[x, z]); } } } // Combine tile meshes CombineTileMeshes(); // Set update thread status to finished Interlocked.Exchange(ref meshUpdateThreadStatus, (int)MeshUpdateThreadStatus.Finished); } else { Thread.Sleep(100); } } }
// Update is called once per frame void Update() { MeshUpdateThreadStatus updaterStatus = (MeshUpdateThreadStatus)meshUpdateThreadStatus; if (player) { if (updaterStatus == MeshUpdateThreadStatus.Idle) { curPlayerTilePos = GetPlayerTilePos(); if (curPlayerTilePos != prevPlayerTilePos) { // Set updater thread status to updating Debug.Log("Updating tiles"); Debug.Log(curPlayerTilePos); Interlocked.Exchange(ref meshUpdateThreadStatus, (int)MeshUpdateThreadStatus.Updating); prevPlayerTilePos = curPlayerTilePos; } } else if (updaterStatus == MeshUpdateThreadStatus.Finished) { Debug.Log("Updating tiles finished"); // Apploy LOD meshes lodMeshes[0][0].ApplyToMesh(); meshFilters[0][0].sharedMesh = lodMeshes[0][0].mesh; for (int i = 1; i < lodDistances.Length; i++) { for (int j = 0; j < 8; j++) { lodMeshes[i][j].ApplyToMesh(); meshFilters[i][j].sharedMesh = lodMeshes[i][j].mesh; /* Crest ocean depth buffer support * if (oceanDepthCache != null) * { * oceanDepthCache.PopulateCache(); * }*/ } } // Reset updater thread to idle status Interlocked.Exchange(ref meshUpdateThreadStatus, (int)MeshUpdateThreadStatus.Idle); } } }