Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using CookieUtils;
- using JetBrains.Annotations;
- using UnityEngine;
- using Random = UnityEngine.Random;
- [ExecuteAlways]
- public class ChunkyGridGizomos : MonoBehaviour
- {
- public Transform samplePoint;
- public Mesh quad;
- [Range(0,10)]
- public float chunkSpacing = 0;
- public Vector2 size = new Vector2(10,10);
- [Range(0,32)]
- public int chunkDivisions = 4;
- [Range(0,10)]
- public int chunkSubdivs = 8;
- [System.Serializable] public class GizmoSettings
- {
- [Range(0,1)]
- public float pointSize = .02f;
- [Range(0,1)]
- public float alpha = .05f;
- }
- public GizmoSettings settings = new GizmoSettings();
- public Chunk[,] chunks;
- public void OnValidate()
- {
- OnEnable();
- }
- // Start is called before the first frame update
- void OnEnable()
- {
- quad = GetQuad();
- chunks = new Chunk[chunkDivisions,chunkDivisions];
- Vector2 chunkSubSize = size / chunkDivisions;
- var rState = new Random.State();
- Random.InitState(42);
- for (int j = 0; j < chunkDivisions; j++)
- for (int i = 0; i < chunkDivisions; i++)
- {
- var newLineColor = Random.ColorHSV(0, 1, 1, 1, .5f, 1);
- newLineColor.a = settings.alpha;
- var newChunk = new Chunk(chunkSubSize, chunkSubdivs)
- {
- lineColor = newLineColor,
- position = new Vector2(i* (chunkSubSize.x + chunkSpacing), j*(chunkSubSize.y + chunkSpacing)),
- surfaceColor = newLineColor
- };
- chunks[i,j] = newChunk;
- }
- }
- public class Chunk
- {
- public Vector2 position = Vector2.zero;
- public Vector2 size;
- public int subdivisions;
- public Color lineColor = Color.red;
- public Color surfaceColor = Color.blue;
- public Vector2[] points;
- public Chunk( Vector2 size, int subdivisions)
- {
- this.subdivisions = subdivisions;
- this.size = size;
- var subPlusOne = subdivisions + 1;
- Vector2 pointSpacing = size / subdivisions;
- points = new Vector2[subPlusOne * subPlusOne];
- for (int j = 0; j <= subdivisions; j++)
- {
- var rowstart = j * subPlusOne;
- for (int i = 0; i <= subdivisions; i++)
- {
- points[rowstart + i] = pointSpacing.MultiplyComponents(new Vector2(i,j));
- }
- }
- }
- public void DrawGizmo(float pointSize, Mesh quadMesh)
- {
- Matrix4x4 oldXform = Gizmos.matrix;
- //build a transform for this chunks position and add it to the existing matrix
- //this is so that it's relative to whatever drew this
- //the scale can be one cause we're already in local space
- Gizmos.matrix *= Matrix4x4.TRS(new Vector3(position.x, 0, position.y), Quaternion.identity, Vector3.one);
- Gizmos.color = surfaceColor;
- if (quadMesh != null)
- {
- Gizmos.DrawMesh(quadMesh, Vector3.zero, Quaternion.Euler(90,0,0),size);
- }
- Vector2 subsize = size / subdivisions;
- Gizmos.color = lineColor;
- if (pointSize > 0.01f)
- {
- foreach (Vector2 point in points) Gizmos.DrawWireSphere(new Vector3(point.x, 0, point.y), pointSize);
- }
- for (int i = 0; i <= subdivisions; i++)
- {
- var i2 = i * (subdivisions + 1);
- var px = new Vector3(points[i].x , 0 , points[i].y);
- var py = new Vector3(points[i2].x , 0 , points[i2].y);
- Gizmos.DrawRay(px, Vector3.forward * size.y);
- Gizmos.DrawRay(py, Vector3.right * size.x);
- }
- //reset the transform
- Gizmos.matrix = oldXform;
- }
- }
- private void OnDrawGizmos()
- {
- if (samplePoint != null)
- {
- Gizmos.color = Color.red;
- Gizmos.DrawWireSphere(samplePoint.position, .05f);
- }
- //super advanced
- Vector3 hitpoint = samplePoint.position - transform.up
- * Vector3.Dot(
- samplePoint.position - transform.position,
- transform.up);
- Gizmos.DrawLine(samplePoint.position, hitpoint);
- if (chunks == null) return;
- Gizmos.matrix = transform.localToWorldMatrix;
- foreach (var chunk in chunks) chunk.DrawGizmo(settings.pointSize, quad);
- }
- private Mesh GetQuad()
- {
- Vector3[] points =
- {
- Vector3.zero,
- Vector3.right,
- Vector3.up,
- Vector2.one
- };
- int[] tris =
- {
- 0,2,1,
- 1,2,3
- };
- Vector2[] uvs =
- {
- Vector2.zero,
- Vector2.right,
- Vector2.up,
- Vector2.one
- };
- Mesh mesh = new Mesh();
- mesh.name = "quad";
- mesh.vertices = points;
- mesh.triangles = tris;
- mesh.uv = uvs;
- mesh.RecalculateNormals();
- return mesh;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement