Advertisement
epitaque_

Untitled

Mar 1st, 2018
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. public void Update() {
  2.     System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  3.     sw.Start();
  4.  
  5.     Vector3 pos = Viewer.position;
  6.     float updateTime = Time.realtimeSinceStartup;
  7.     int numChunksAdded = 0;
  8.     int minCoord = -Radius + 1;
  9.     int maxCoord = Radius + 1;
  10.     BoundsInt previousLodBounds = new BoundsInt();
  11.  
  12.     for(int LOD = 0; LOD < LODs; LOD++) {
  13.         int scale = (int)Mathf.Pow(2, LOD);
  14.         int chunkSize = (int)MinimumChunkSize * scale;
  15.         int snapSize = chunkSize * 2;
  16.  
  17.         Vector3Int minExtents = Vector3Int.zero;
  18.         Vector3Int maxExtents = Vector3Int.zero;
  19.  
  20.         Vector3Int snappedViewerPosition = new Vector3Int((int)((pos.x)/snapSize)*snapSize,
  21.                                         (int)((pos.y)/snapSize)*snapSize,
  22.                                         (int)((pos.z)/snapSize)*snapSize);
  23.  
  24.         if(LOD == 0) {
  25.             if(Initialized && snappedViewerPosition == PreviousPosition) {
  26.                 return;
  27.             }
  28.             PreviousPosition = snappedViewerPosition;
  29.         }
  30.  
  31.         for(int x = minCoord; x < maxCoord; x++) {
  32.             for(int y = minCoord; y < maxCoord; y++) {
  33.                 for(int z = minCoord; z < maxCoord; z++) {
  34.                     Vector3Int localCoords = new Vector3Int(x, y, z) * chunkSize - Vector3Int.one * chunkSize;
  35.                     if(x == minCoord && y == minCoord && z == minCoord) {
  36.                         minExtents = localCoords;
  37.                     }
  38.                     if(x == maxCoord && y == maxCoord && z == maxCoord) {
  39.                         maxExtents = localCoords;
  40.                     }
  41.  
  42.  
  43.                     if(LOD != 1 && previousLodBounds.Contains(localCoords)) {
  44.                         continue;
  45.                     }
  46.  
  47.                        
  48.                     Vector3Int cpos = snappedViewerPosition + localCoords;
  49.  
  50.                     Vector4 key = new Vector4(cpos.x, cpos.y, cpos.z, LOD); //getChunkHash(cpos, LOD);
  51.  
  52.                     if(Chunks.ContainsKey(key)) {
  53.                         (Chunks[key] as Chunk).CreationTime = updateTime;
  54.                     }
  55.                     else {
  56.                         Chunk chunk = new Chunk();
  57.  
  58.                         numChunksAdded++;
  59.                         chunk.Position = cpos;
  60.                         chunk.Key = key;
  61.                         chunk.LOD = 0;
  62.                         chunk.CreationTime = updateTime;
  63.                         chunk.State = ChunkState.Blank;
  64.                         Chunks.Add(key, chunk);
  65.                         ChunksToMesh.Enqueue(chunk);
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.  
  71.         Vector3Int size = maxExtents - minExtents;
  72.         previousLodBounds = new BoundsInt(minExtents.x, minExtents.y, minExtents.z, size.x, size.y, size.z);
  73.  
  74.     }
  75.  
  76.     long ElapsedMilliseconds1 = sw.ElapsedMilliseconds;
  77.     sw.Restart();
  78.  
  79.  
  80.     if(numChunksAdded > 0) {
  81.         Debug.Log("Added " + numChunksAdded + " chunks.");
  82.     }
  83.  
  84.     Hashtable newChunkTable = new Hashtable();
  85.     foreach(Chunk chunk in Chunks.Values) {
  86.         if(chunk.CreationTime == updateTime) {
  87.             newChunkTable.Add(chunk.Key, chunk);
  88.         }
  89.         else {
  90.             if(chunk.UnityObject != null) {
  91.                 UnityEngine.Object.Destroy(chunk.UnityObject);
  92.             }
  93.             chunk.State = ChunkState.Cancelled;
  94.         }
  95.     }
  96.     Chunks = newChunkTable;
  97.     long ElapsedMilliseconds2 = sw.ElapsedMilliseconds;
  98.  
  99.     string msg = "Chunk Coordinates Update: Stage 1 took " + ElapsedMilliseconds1 + "ms, Stage 2 took " + ElapsedMilliseconds2 + "ms, total is " + (ElapsedMilliseconds1 + ElapsedMilliseconds2) + "ms";
  100.  
  101.     UConsole.Print(msg);
  102.     Debug.Log(msg);
  103.     sw.Stop();
  104.  
  105.     Initialized = true;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement