Advertisement
Cookie042

chunky math

Jun 9th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. private void OnDrawGizmos()
  2.     {
  3.         if (!enabled)
  4.             return;
  5.  
  6.         if (samplePoint != null)
  7.         {
  8.             Gizmos.color = Color.red;
  9.             var samplePos = samplePoint.position;
  10.             Gizmos.DrawWireSphere(samplePos, .05f);
  11.  
  12.             //super advanced
  13.             var xform = transform;
  14.             var localUp = xform.up;
  15.            
  16.             Vector3 hitPoint = samplePos - localUp
  17.                                * Vector3.Dot(
  18.                                    samplePos - xform.position,
  19.                                    localUp);
  20.  
  21.             //the hit point in local terrain space
  22.             var hitPointLocal = transform.InverseTransformPoint(hitPoint);
  23.  
  24.             //what point was hit in the overall terrain
  25.             var flooredHit = new Vector2Int(
  26.                 Mathf.FloorToInt(hitPointLocal.x / size.x * TerrainSubdivs * chunkSubdivs),
  27.                 Mathf.FloorToInt(hitPointLocal.z / size.y * TerrainSubdivs * chunkSubdivs));
  28.  
  29.             //what point was hit inside the chunk
  30.             var flooredHitChunk = new Vector2Int(flooredHit.x % chunkSubdivs,flooredHit.y % chunkSubdivs);
  31.  
  32.             // the 2d position of the chunk
  33.             Vector2Int chunkPosition = new Vector2Int(
  34.                 flooredHit.x / chunkSubdivs,
  35.                 flooredHit.y / chunkSubdivs
  36.             );
  37.            
  38.             var ChunkArray1DIndex = chunkPosition.x + chunkPosition.y * TerrainSubdivs;
  39.        
  40.             Debug.Log($"flooredHit: {flooredHit.ToString()} " +
  41.                       $"flooredHitChunk: {flooredHitChunk.ToString()} " +
  42.                       $"chunkPosition: {chunkPosition.ToString()} " +
  43.                       $"chunkArrayIndex: {ChunkArray1DIndex.ToString()}");
  44.  
  45.             chunks[chunkPosition.x, chunkPosition.y].DrawPointGizmo(flooredHitChunk);
  46.  
  47.             Gizmos.DrawLine(samplePos, hitPoint);
  48.  
  49.  
  50.         }
  51.  
  52.         if (chunks == null) return;
  53.  
  54.         Gizmos.matrix = transform.localToWorldMatrix;
  55.         foreach (var chunk in chunks) chunk.DrawGizmo(settings.pointSize, quad);
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement