Skip to content

Instantly share code, notes, and snippets.

@belzecue
Last active October 12, 2022 22:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belzecue/08858b89836041470ad783b947f16def to your computer and use it in GitHub Desktop.
Save belzecue/08858b89836041470ad783b947f16def to your computer and use it in GitHub Desktop.
Unity3D Tip #1: Color.HSVToRGB(h, s, v) produces smoother, more natural random colors than new Color(r, g, b)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorRotator : MonoBehaviour {
public GameObject objHSV, objPlainColor;
private Material objHSVMaterial, objPlainColorMaterial;
private float timeDelta;
private Rect rect = new Rect(10, 10, 0, 0);
private readonly GUIContent guiContent = new GUIContent(
"HSV coloring on the left, RGB on the right."
);
private GUIStyleState guiStyleState;
private GUIStyle guiStyle;
void Start () {
timeDelta = Random.value;
objHSV.GetComponent<Renderer>().sharedMaterial = objHSVMaterial = (new Material(Shader.Find("Standard")));
objPlainColor.GetComponent<Renderer>().sharedMaterial = objPlainColorMaterial = (new Material(Shader.Find("Standard")));
guiStyleState = new GUIStyleState() { textColor = Color.white };
guiStyle = new GUIStyle() { fontSize = 36, normal = guiStyleState };
}
private Color GetRandomHSVColor()
{
float[] result = Perlin();
return Color.HSVToRGB(result[0], result[1], result[2]);
}
private Color GetRandomColor()
{
float[] result = Perlin();
return new Color(result[0], result[1], result[2]);
}
private float[] Perlin()
{
return new float[3]
{
Mathf.PerlinNoise(0.5f, 23.3f * timeDelta)
, Mathf.PerlinNoise(0.5f, 54.4f * timeDelta)
, Mathf.PerlinNoise(0.5f, 12.6f * timeDelta)
};
}
void Update () {
timeDelta += (Time.deltaTime / 100);
objHSVMaterial.color = GetRandomHSVColor();
objPlainColorMaterial.color = GetRandomColor();
}
private void OnGUI()
{
GUI.Label(rect, guiContent, guiStyle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment