Advertisement
Cookie042

chunky

Jun 7th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using CookieUtils;
  5. using JetBrains.Annotations;
  6. using UnityEngine;
  7. using Random = UnityEngine.Random;
  8.  
  9. [ExecuteAlways]
  10. public class ChunkyGridGizomos : MonoBehaviour
  11. {
  12.     public Transform samplePoint;
  13.  
  14.     public Mesh quad;
  15.     [Range(0,10)]
  16.     public float chunkSpacing = 0;
  17.  
  18.     public Vector2 size = new Vector2(10,10);
  19.  
  20.     [Range(0,32)]
  21.     public int chunkDivisions = 4;
  22.     [Range(0,10)]
  23.     public int chunkSubdivs = 8;
  24.  
  25.    
  26.     [System.Serializable] public class GizmoSettings
  27.     {
  28.         [Range(0,1)]
  29.         public float pointSize = .02f;
  30.         [Range(0,1)]
  31.         public float alpha = .05f;
  32.     }
  33.     public GizmoSettings settings = new GizmoSettings();
  34.  
  35.     public Chunk[,] chunks;
  36.  
  37.     public void OnValidate()
  38.     {
  39.         OnEnable();
  40.     }
  41.  
  42.     // Start is called before the first frame update
  43.     void OnEnable()
  44.     {
  45.         quad = GetQuad();
  46.  
  47.         chunks = new Chunk[chunkDivisions,chunkDivisions];
  48.  
  49.         Vector2 chunkSubSize = size / chunkDivisions;
  50.  
  51.         var rState = new Random.State();
  52.  
  53.         Random.InitState(42);
  54.  
  55.         for (int j = 0; j < chunkDivisions; j++)
  56.         for (int i = 0; i < chunkDivisions; i++)
  57.         {
  58.             var newLineColor = Random.ColorHSV(0, 1, 1, 1, .5f, 1);
  59.             newLineColor.a = settings.alpha;
  60.  
  61.             var newChunk = new Chunk(chunkSubSize, chunkSubdivs)
  62.             {
  63.                 lineColor = newLineColor,
  64.                 position = new Vector2(i* (chunkSubSize.x + chunkSpacing), j*(chunkSubSize.y + chunkSpacing)),
  65.                 surfaceColor = newLineColor
  66.             };
  67.  
  68.             chunks[i,j] = newChunk;
  69.         }
  70.     }
  71.  
  72.     public class Chunk
  73.     {
  74.         public Vector2 position = Vector2.zero;
  75.         public Vector2 size;
  76.         public int subdivisions;
  77.         public Color lineColor = Color.red;
  78.         public Color surfaceColor = Color.blue;
  79.  
  80.         public Vector2[] points;
  81.  
  82.         public Chunk( Vector2 size, int subdivisions)
  83.         {
  84.             this.subdivisions = subdivisions;
  85.             this.size = size;
  86.  
  87.             var subPlusOne = subdivisions + 1;
  88.  
  89.             Vector2 pointSpacing = size / subdivisions;
  90.  
  91.             points = new Vector2[subPlusOne * subPlusOne];
  92.             for (int j = 0; j <= subdivisions; j++)
  93.             {
  94.                 var rowstart = j * subPlusOne;
  95.                 for (int i = 0; i <= subdivisions; i++)
  96.                 {
  97.                     points[rowstart + i] = pointSpacing.MultiplyComponents(new Vector2(i,j));
  98.                 }
  99.             }
  100.         }
  101.  
  102.         public void DrawGizmo(float pointSize, Mesh quadMesh)
  103.         {
  104.             Matrix4x4 oldXform = Gizmos.matrix;
  105.             //build a transform for this chunks position and add it to the existing matrix
  106.             //this is so that it's relative to whatever drew this
  107.             //the scale can be one cause we're already in local space
  108.             Gizmos.matrix *= Matrix4x4.TRS(new Vector3(position.x, 0, position.y), Quaternion.identity, Vector3.one);
  109.            
  110.             Gizmos.color = surfaceColor;
  111.             if (quadMesh != null)
  112.             {
  113.                 Gizmos.DrawMesh(quadMesh, Vector3.zero, Quaternion.Euler(90,0,0),size);    
  114.             }
  115.  
  116.             Vector2 subsize = size / subdivisions;
  117.            
  118.             Gizmos.color = lineColor;
  119.             if (pointSize > 0.01f)
  120.             {
  121.                 foreach (Vector2 point in points) Gizmos.DrawWireSphere(new Vector3(point.x, 0, point.y), pointSize);
  122.             }
  123.  
  124.             for (int i = 0; i <= subdivisions; i++)
  125.             {
  126.                 var i2 = i * (subdivisions + 1);
  127.                 var px = new Vector3(points[i].x , 0 , points[i].y);
  128.                 var py = new Vector3(points[i2].x , 0 , points[i2].y);
  129.  
  130.                 Gizmos.DrawRay(px, Vector3.forward * size.y);
  131.                 Gizmos.DrawRay(py, Vector3.right * size.x);
  132.             }
  133.  
  134.  
  135.             //reset the transform
  136.             Gizmos.matrix = oldXform;
  137.         }
  138.     }
  139.  
  140.     private void OnDrawGizmos()
  141.     {
  142.         if (samplePoint != null)
  143.         {
  144.             Gizmos.color = Color.red;
  145.             Gizmos.DrawWireSphere(samplePoint.position, .05f);
  146.         }
  147.  
  148.         //super advanced
  149.         Vector3 hitpoint = samplePoint.position - transform.up
  150.                            * Vector3.Dot(
  151.                                samplePoint.position - transform.position,
  152.                                transform.up);
  153.  
  154.         Gizmos.DrawLine(samplePoint.position, hitpoint);
  155.  
  156.         if (chunks == null)  return;
  157.  
  158.         Gizmos.matrix = transform.localToWorldMatrix;
  159.         foreach (var chunk in chunks) chunk.DrawGizmo(settings.pointSize, quad);
  160.     }
  161.  
  162.     private Mesh GetQuad()
  163.     {
  164.         Vector3[] points =
  165.         {
  166.             Vector3.zero,
  167.             Vector3.right,
  168.             Vector3.up,
  169.             Vector2.one
  170.         };
  171.  
  172.         int[] tris =
  173.         {
  174.             0,2,1,
  175.             1,2,3
  176.         };
  177.  
  178.         Vector2[] uvs =
  179.         {
  180.             Vector2.zero,
  181.             Vector2.right,
  182.             Vector2.up,
  183.             Vector2.one
  184.         };
  185.  
  186.         Mesh mesh = new Mesh();
  187.         mesh.name = "quad";
  188.  
  189.         mesh.vertices = points;
  190.         mesh.triangles = tris;
  191.         mesh.uv = uvs;
  192.         mesh.RecalculateNormals();
  193.  
  194.         return mesh;
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement