Last active
May 10, 2022 12:15
-
-
Save FaffyWaffles/91ff39020c32689d91d9f75795f66485 to your computer and use it in GitHub Desktop.
GizmoScripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using System.Collections; | |
| using UnityEngine; | |
| using Sirenix.OdinInspector; | |
| public class GizmoBehavior : MonoBehaviour | |
| { | |
| public GameObject target; | |
| public Vector3[] vertices; | |
| [Range(0.01f, .25f)] | |
| public float gizmoSize = 0.075f; | |
| public Color[] gizmoColor; | |
| public bool paintersAlgorithm; | |
| public bool depthCulling; | |
| public bool heatMap; //just for fun. only for painters algorithm | |
| private void Start() | |
| { | |
| if (target == null) | |
| target = this.gameObject; | |
| vertices = target.GetComponent<MeshFilter>().sharedMesh.vertices; | |
| DistinctRandomColors(vertices.Length, out gizmoColor, UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 1f, 1f)); | |
| } | |
| private IOrderedEnumerable<Vector3> sorted; | |
| private void OnDrawGizmos() | |
| { | |
| sorted = vertices.OrderByDescending((x) => Vector3.Distance(Camera.current.transform.position, x)); | |
| //if (paintersAlgorithm) | |
| if ((paintersAlgorithm) || ((paintersAlgorithm) && (heatMap))) | |
| PaintersAlgorithm(); | |
| else | |
| for (int i = 0; i < vertices.Length; i++) | |
| DrawCube(vertices[i], i); | |
| } | |
| private void PaintersAlgorithm() | |
| { | |
| int n = 0; | |
| foreach (Vector3 position in sorted) | |
| { | |
| DrawCube(position, n); | |
| n++; | |
| } | |
| } | |
| private bool DepthCulling(Vector3 v) ///needs to be mesh collider | |
| { | |
| if (Physics.Linecast(Camera.current.transform.position, v)) | |
| return false; | |
| return true; | |
| } | |
| private void DrawCube(Vector3 position, int n) | |
| { | |
| if (heatMap) | |
| Gizmos.color = gizmoColor[n]; //Heatmap Behaviour | |
| else | |
| Gizmos.color = gizmoColor[Array.IndexOf(vertices, position)]; | |
| if (((DepthCulling(target.transform.TransformPoint(position))) && (depthCulling)) || ((!depthCulling))) | |
| Gizmos.DrawCube(target.transform.TransformPoint(position), new Vector3(gizmoSize, gizmoSize, gizmoSize)); | |
| } | |
| public bool on; | |
| [Button] | |
| void Kaleidoscope() | |
| { | |
| StartCoroutine(Lerp()); | |
| } | |
| public IEnumerator Lerp() | |
| { | |
| float lerpDuration = 10; | |
| float startValue = 1; | |
| float endValue = 0; | |
| float timeElapsed = 0; | |
| while (timeElapsed < lerpDuration) | |
| { | |
| target.transform.Rotate(0, 6.0f * 1f * Time.deltaTime, 0); | |
| colorRange = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration); | |
| ColorChange(); | |
| timeElapsed += Time.deltaTime; | |
| yield return null; | |
| } | |
| if (on) | |
| StartCoroutine(Lerp()); | |
| else | |
| colorRange = endValue; | |
| } | |
| [Range(0f, 1f)] | |
| [OnValueChanged("ColorChange")] | |
| public float colorRange; | |
| private void ColorChange() | |
| { | |
| Color seedColor = UnityEngine.Random.ColorHSV(colorRange, colorRange, 1f, 1f, 1f, 1f); | |
| DistinctRandomColors(vertices.Length, out gizmoColor, seedColor); | |
| } | |
| void DistinctRandomColors(int colorCount, out Color[] colors, Color initColor) | |
| { | |
| colors = new Color[colorCount]; | |
| colors[0] = initColor; | |
| float hue, saturation, value; | |
| Color.RGBToHSV(initColor, out hue, out saturation, out value); | |
| float normalizedHue = Mathf.Clamp01(hue); | |
| for (int i = 1; i < colorCount; i++) | |
| { | |
| float angle = (((360 * colorCount) - (360 * i)) / colorCount); | |
| float normalized = (angle / 360) + normalizedHue; | |
| Color newColor = Color.HSVToRGB(normalized % 1, saturation, value); | |
| colors[i] = newColor; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

