Created
June 1, 2017 16:56
-
-
Save Q-Bert-Reynolds/956a60e54f23ef30e74ae561f0367487 to your computer and use it in GitHub Desktop.
This file contains 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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(MeshFilter))] | |
[RequireComponent(typeof(MeshRenderer))] | |
[ExecuteInEditMode] | |
public class ColorWheel : MonoBehaviour { | |
[Range(3, 36)] public int segments = 6; | |
[Range(0.0001f, 100)] public float radius = 1; | |
#if UNITY_EDITOR | |
void Update() { | |
if (!Application.isPlaying) SetupMesh(); | |
} | |
#endif | |
void SetupMesh () { | |
MeshFilter meshFilter = GetComponent<MeshFilter>(); | |
List<Vector3> verts = new List<Vector3>(); | |
List<Vector2> uvs = new List<Vector2>(); | |
List<Vector3> norms = new List<Vector3>(); | |
List<Color> colors = new List<Color>(); | |
List<int> tris = new List<int>(); | |
verts.Add(Vector3.zero); | |
uvs.Add(Vector2.one * 0.5f); | |
norms.Add(Vector3.back); | |
colors.Add(Color.white); | |
verts.Add(Vector3.up * radius); | |
uvs.Add(new Vector2(0.5f, 1)); | |
norms.Add(Vector3.back); | |
colors.Add(Color.red); | |
for (int i = 1; i < segments; i++) { | |
float frac = (float)i / (float)segments; | |
float angle = Mathf.PI * 2 * frac; | |
Vector3 dir = new Vector3(-Mathf.Sin(angle), Mathf.Cos(angle), 0); | |
verts.Add(dir * radius); | |
uvs.Add(dir * 0.5f + new Vector3(0.5f, 0.5f, 0)); | |
norms.Add(Vector3.back); | |
colors.Add(Color.HSVToRGB(frac, 1, 1)); | |
tris.AddRange(new int[] {0, i, i+1}); | |
} | |
tris.AddRange(new int[] {0, segments, 1}); | |
Mesh mesh = new Mesh(); | |
mesh.name = "ProcMesh"; | |
mesh.vertices = verts.ToArray(); | |
mesh.uv = uvs.ToArray(); | |
mesh.normals = norms.ToArray(); | |
mesh.colors = colors.ToArray(); | |
mesh.triangles = tris.ToArray(); | |
meshFilter.mesh = mesh; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment