Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Update() {
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- Vector3 pos = Viewer.position;
- float updateTime = Time.realtimeSinceStartup;
- int numChunksAdded = 0;
- int minCoord = -Radius + 1;
- int maxCoord = Radius + 1;
- BoundsInt previousLodBounds = new BoundsInt();
- for(int LOD = 0; LOD < LODs; LOD++) {
- int scale = (int)Mathf.Pow(2, LOD);
- int chunkSize = (int)MinimumChunkSize * scale;
- int snapSize = chunkSize * 2;
- Vector3Int minExtents = Vector3Int.zero;
- Vector3Int maxExtents = Vector3Int.zero;
- Vector3Int snappedViewerPosition = new Vector3Int((int)((pos.x)/snapSize)*snapSize,
- (int)((pos.y)/snapSize)*snapSize,
- (int)((pos.z)/snapSize)*snapSize);
- if(LOD == 0) {
- if(Initialized && snappedViewerPosition == PreviousPosition) {
- return;
- }
- PreviousPosition = snappedViewerPosition;
- }
- for(int x = minCoord; x < maxCoord; x++) {
- for(int y = minCoord; y < maxCoord; y++) {
- for(int z = minCoord; z < maxCoord; z++) {
- Vector3Int localCoords = new Vector3Int(x, y, z) * chunkSize - Vector3Int.one * chunkSize;
- if(x == minCoord && y == minCoord && z == minCoord) {
- minExtents = localCoords;
- }
- if(x == maxCoord && y == maxCoord && z == maxCoord) {
- maxExtents = localCoords;
- }
- if(LOD != 1 && previousLodBounds.Contains(localCoords)) {
- continue;
- }
- Vector3Int cpos = snappedViewerPosition + localCoords;
- Vector4 key = new Vector4(cpos.x, cpos.y, cpos.z, LOD); //getChunkHash(cpos, LOD);
- if(Chunks.ContainsKey(key)) {
- (Chunks[key] as Chunk).CreationTime = updateTime;
- }
- else {
- Chunk chunk = new Chunk();
- numChunksAdded++;
- chunk.Position = cpos;
- chunk.Key = key;
- chunk.LOD = 0;
- chunk.CreationTime = updateTime;
- chunk.State = ChunkState.Blank;
- Chunks.Add(key, chunk);
- ChunksToMesh.Enqueue(chunk);
- }
- }
- }
- }
- Vector3Int size = maxExtents - minExtents;
- previousLodBounds = new BoundsInt(minExtents.x, minExtents.y, minExtents.z, size.x, size.y, size.z);
- }
- long ElapsedMilliseconds1 = sw.ElapsedMilliseconds;
- sw.Restart();
- if(numChunksAdded > 0) {
- Debug.Log("Added " + numChunksAdded + " chunks.");
- }
- Hashtable newChunkTable = new Hashtable();
- foreach(Chunk chunk in Chunks.Values) {
- if(chunk.CreationTime == updateTime) {
- newChunkTable.Add(chunk.Key, chunk);
- }
- else {
- if(chunk.UnityObject != null) {
- UnityEngine.Object.Destroy(chunk.UnityObject);
- }
- chunk.State = ChunkState.Cancelled;
- }
- }
- Chunks = newChunkTable;
- long ElapsedMilliseconds2 = sw.ElapsedMilliseconds;
- string msg = "Chunk Coordinates Update: Stage 1 took " + ElapsedMilliseconds1 + "ms, Stage 2 took " + ElapsedMilliseconds2 + "ms, total is " + (ElapsedMilliseconds1 + ElapsedMilliseconds2) + "ms";
- UConsole.Print(msg);
- Debug.Log(msg);
- sw.Stop();
- Initialized = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement