private Color OffsetColorHSL(Color input, float offsetAlpha, float hueOffset, float saturationOffset, float luminanceOffset) { float h, s, l; #if UNITY_5_3 Color.RGBToHSV(input, out h, out s, out l); #else EditorGUIUtility.RGBToHSV(input, out h, out s, out l); #endif float newH = h + hueOffset * offsetAlpha; if (newH < 0.0f) { h = 1.0f + newH; } else if (newH > 1.0f) { h = newH - 1.0f; } else { h = newH; } s = Mathf.Clamp(s + saturationOffset * offsetAlpha, 0.0f, 1.0f); l = Mathf.Clamp(l + luminanceOffset * offsetAlpha, 0.0f, 1.0f); #if UNITY_5_3 return(Color.HSVToRGB(h, s, l)); #else return(EditorGUIUtility.HSVToRGB(h, s, l)); #endif }
public static Color HSVtoRGB(Vector3 hsvColor) { #if UNITY_4_6 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 return(EditorGUIUtility.HSVToRGB(hsvColor.x / 360f, hsvColor.y / 255f, hsvColor.z / 255f)); #else return(Color.HSVToRGB(hsvColor.x / 360f, hsvColor.y / 255f, hsvColor.z / 255f)); #endif }
void Update() { hsv.x += Time.deltaTime * colorShiftSpeed; if (hsv.x > 1) { hsv.x = 0; } cam.backgroundColor = EditorGUIUtility.HSVToRGB(hsv.x, hsv.y, hsv.z); }
void DrawGizmos() { var spline = (Spline)target; var cps = spline.cps; System.Func <int, Vector3> GetCP = spline.GetCP; if (cps == null || cps.Length < 2) { return; } var dt = 1f / GIZMO_SMOOTH_LEVEL; var kMin = float.MaxValue; var kMax = 0f; for (var i = 0; i < cps.Length; i++) { var t = (float)i; for (var j = 0; j < GIZMO_SMOOTH_LEVEL; j++) { var k = CatmullSplineUtil.Curvature(t, spline.GetCP); k = Mathf.Clamp(k, JET_K_MIN, JET_K_MAX); if (k < kMin) { kMin = k; } else if (kMax < k) { kMax = k; } t += dt; } } var jetA = 0.66666f / (kMin - kMax); var jetB = -jetA * kMax; var startPos = CatmullSplineUtil.Position(0f, GetCP); for (var i = 0; i < cps.Length; i++) { var t = (float)i; for (var j = 0; j < GIZMO_SMOOTH_LEVEL; j++) { var k = CatmullSplineUtil.Curvature(t, spline.GetCP); k = Mathf.Clamp(k, kMin, kMax); Handles.color = EditorGUIUtility.HSVToRGB(jetA * k + jetB, 1f, 1f); var endPos = CatmullSplineUtil.Position(t += dt, GetCP); Handles.DrawLine(startPos, endPos); startPos = endPos; } } }
// Use this for initialization void OnEnable() { var mf = GetComponent <MeshFilter>(); var mesh = mf.sharedMesh; var tr = transform; var vertices = (from v in mesh.vertices select tr.TransformPoint(v)).ToArray(); triangles = mesh.triangles; var triVertices = (from i in Enumerable.Range(0, triangles.Length / 3) select new Vector3[] { vertices[triangles[3 * i]], vertices[triangles[3 * i + 1]], vertices[triangles[3 * i + 2]] } ).ToArray(); var angles = new float[triangles.Length]; for (var i = 0; i < triVertices.Length; i++) { var tri = triVertices[i]; for (var j = 0; j < 3; j++) { var tan0 = (tri[(j + 1) % 3] - tri[j]).normalized; var tan1 = (tri[(j + 2) % 3] - tri[j]).normalized; angles[3 * i + j] = Mathf.Acos(Vector3.Dot(tan0, tan1)); } } normals = (from tri in triVertices select Vector3.Cross(tri[1] - tri[0], tri[2] - tri[0]).normalized).ToArray(); //this.normals = mesh.normals; vnormals = new Vector3[vertices.Length]; enormals = new Dictionary <Edge, Vector3>(); for (var i = 0; i < triVertices.Length; i++) { for (var j = 0; j < 3; j++) { var index = 3 * i + j; vnormals[triangles[index]] += angles[index] * normals[i]; var edge = new Edge(triangles[index], triangles[3 * i + (j + 1) % 3]); var en = (enormals.ContainsKey(edge) ? enormals[edge] : Vector3.zero); en += normals[i]; enormals[edge] = en; } } vnormals = (from vn in vnormals select vn.normalized).ToArray(); mesh.normals = vnormals; mesh.colors = (from i in Enumerable.Range(0, vertices.Length) select EditorGUIUtility.HSVToRGB((float)i / vertices.Length, 1f, 1f)).ToArray(); this.transTriSpaces = (from tri in triVertices select calcTriangleSpace(tri)).ToArray(); this.transTriSpacesInv = (from m in transTriSpaces select m.inverse).ToArray(); this.triVertsInLocal = (from i in Enumerable.Range(0, transTriSpaces.Length) select new Vector2[] { (Vector2)transTriSpaces[i].MultiplyPoint3x4(triVertices[i][0]), (Vector2)transTriSpaces[i].MultiplyPoint3x4(triVertices[i][1]), (Vector2)transTriSpaces[i].MultiplyPoint3x4(triVertices[i][2]) }).ToArray(); }
// Update is called once per frame void Update() { this.degrees = (this.degrees + Time.deltaTime * 50f) % 360f; #if (UNITY_5_2_3 || UNITY_5_2_4) this.textShadow.effectColor = EditorGUIUtility.HSVToRGB(degrees / 360f, 1f, 1f); this.text.color = EditorGUIUtility.HSVToRGB(((degrees + 180f) % 360f) / 360f, 1f, 1f); #else this.textShadow.effectColor = Color.HSVToRGB(degrees / 360f, 1f, 1f); this.text.color = Color.HSVToRGB(((degrees + 180f) % 360f) / 360f, 1f, 1f); #endif }
void UpdateHueWheel(bool sizeChanged) { if (sizeChanged) { CleanTexture(m_WheelTexture); m_WheelTexture = MakeTexture(m_Diameter, m_Diameter); } Color[] pixels = m_WheelTexture.GetPixels(); for (int y = 0; y < m_Diameter; y++) { for (int x = 0; x < m_Diameter; x++) { int index = y * m_Diameter + x; float dx = (float)(x - m_Radius) / m_Radius; float dy = (float)(y - m_Radius) / m_Radius; float d = Mathf.Sqrt(dx * dx + dy * dy); // Out of the wheel, early exit if (d >= 1f) { pixels[index] = new Color(0f, 0f, 0f, 0f); continue; } // Red (0) on top, counter-clockwise (industry standard) float saturation = d; float hue = Mathf.Atan2(dx, dy); hue = 1f - ((hue > 0) ? hue : CLib.PI2 + hue) / CLib.PI2; #if UNITY_5_3_PLUS Color color = Color.HSVToRGB(hue, saturation, 1f); #else Color color = EditorGUIUtility.HSVToRGB(hue, saturation, 1f); #endif // Quick & dirty antialiasing color.a = (saturation > 0.99) ? (1f - saturation) * 100f : 1f; pixels[index] = color; } } m_WheelTexture.SetPixels(pixels); m_WheelTexture.Apply(); }
static GUIStyle getColoredBoxStyle(int totalComponents, int index) { GUIStyle[] styles; if (!_coloredBoxStyles.TryGetValue(totalComponents, out styles)) { styles = new GUIStyle[totalComponents]; for (int i = 0; i < styles.Length; i++) { var hue = (float)i / (float)totalComponents; var componentColor = EditorGUIUtility.HSVToRGB(hue, 0.7f, 1f); componentColor.a = 0.15f; var style = new GUIStyle(GUI.skin.box); style.normal.background = createTexture(2, 2, componentColor); styles[i] = style; } _coloredBoxStyles.Add(totalComponents, styles); } return(styles[index]); }
void Update() { if (Input.GetButton("Jump")) { transform.position += Vector3.up * speed * Time.deltaTime; float h, s, v; EditorGUIUtility.RGBToHSV(cam.backgroundColor, out h, out s, out v); h += colorSpeed * Time.deltaTime; if (h > 1) { h -= 1; } cam.backgroundColor = EditorGUIUtility.HSVToRGB(h, s, v); } }
static ColorRing() { float hueAngleStep = Mathf.Clamp(45f, 1f, 360f); float hueLoopOffset = Mathf.Clamp(20f, 1f, 360f); int numColors = (int)(360f / hueAngleStep) * (int)(360f / hueLoopOffset); mColors.Capacity = numColors; for (int i = 0; i < numColors; ++i) { float hueAngle = i * hueAngleStep; float loops = (int)(hueAngle / 360f); float hue = ((hueAngle % 360f + (loops * hueLoopOffset % 360f)) / 360f); #if UNITY_5_0_0 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 mColors.Add(EditorGUIUtility.HSVToRGB(hue, 1f, 1f)); #else mColors.Add(Color.HSVToRGB(hue, 1f, 1f)); #endif } }
internal Color DoGUI(Color color, int diameter) { float alpha = color.a; diameter = Mathf.Clamp(diameter, kMinSize, kMaxSize); Vector3 hsv; #if UNITY_5_3_PLUS Color.RGBToHSV(color, out hsv.x, out hsv.y, out hsv.z); #else EditorGUIUtility.RGBToHSV(color, out hsv.x, out hsv.y, out hsv.z); #endif if (diameter != m_Diameter) { m_Diameter = diameter; m_Radius = diameter / 2f; UpdateHueWheel(true); } EditorGUILayout.BeginVertical(); { // Title EditorGUILayout.BeginHorizontal(GUILayout.Width(m_Diameter - 1)); { var centeredStyle = new GUIStyle(GUI.skin.GetStyle("Label")); centeredStyle.alignment = TextAnchor.UpperCenter; GUILayout.Label(m_Title, centeredStyle); } EditorGUILayout.EndHorizontal(); // Hue wheel EditorGUILayout.BeginHorizontal(GUILayout.Width(m_Diameter)); { Rect wheelRect = GUILayoutUtility.GetRect(m_Diameter, m_Diameter); wheelRect.x += 3; if (Event.current.type == EventType.Repaint) { // Wheel GUI.DrawTexture(wheelRect, m_WheelTexture); // Thumb Vector2 thumbPos = Vector2.zero; float theta = hsv.x * CLib.PI2; float len = hsv.y * m_Radius; thumbPos.x = Mathf.Cos(theta + CLib.PI_2); thumbPos.y = Mathf.Sin(theta - CLib.PI_2); thumbPos *= len; GUI.DrawTexture(new Rect(wheelRect.x + m_Radius + thumbPos.x - 4f, wheelRect.y + m_Radius + thumbPos.y - 4f, 8f, 8f), m_ThumbTexture); } hsv = GetInput(wheelRect, hsv); } EditorGUILayout.EndHorizontal(); #if UNITY_5_3_PLUS color = Color.HSVToRGB(hsv.x, hsv.y, hsv.z); #else color = EditorGUIUtility.HSVToRGB(hsv.x, hsv.y, hsv.z); #endif color.a = GUILayout.HorizontalSlider(alpha, 0f, 1f); } EditorGUILayout.EndVertical(); return(color); }
/// <summary> /// コネクトボックス位置更新 /// </summary> void UpdateConnect() { int index = 0; // コネクトボックス表示 int[] count = new int[(int)ConnectAreaType.AREA_MAX]; foreach (var box in connectBoxList) { if (box.connectArea == ConnectAreaType.AREA_LEFT) { count[(int)ConnectAreaType.AREA_LEFT]++; box.boxRect.x = windowRect.x - box.boxRect.width; int total = areaConnectCount[(int)ConnectAreaType.AREA_LEFT]; int cnt = count[(int)ConnectAreaType.AREA_LEFT]; float offset = windowRect.height / (total + 1) * cnt; box.boxRect.y = windowRect.y + offset - box.boxRect.height / 2; } else if (box.connectArea == ConnectAreaType.AREA_RIGHT) { count[(int)ConnectAreaType.AREA_RIGHT]++; box.boxRect.x = windowRect.x + windowRect.width; int total = areaConnectCount[(int)ConnectAreaType.AREA_RIGHT]; int cnt = count[(int)ConnectAreaType.AREA_RIGHT]; float offset = windowRect.height / (total + 1) * cnt; box.boxRect.y = windowRect.y + offset - box.boxRect.height / 2; } else if (box.connectArea == ConnectAreaType.AREA_TOP) { count[(int)ConnectAreaType.AREA_TOP]++; int total = areaConnectCount[(int)ConnectAreaType.AREA_TOP]; int cnt = count[(int)ConnectAreaType.AREA_TOP]; float offset = windowRect.width / (total + 1) * cnt; box.boxRect.x = windowRect.x + offset - box.boxRect.width / 2; box.boxRect.y = windowRect.y - box.boxRect.height; } else if (box.connectArea == ConnectAreaType.AREA_BOTTOM) { count[(int)ConnectAreaType.AREA_BOTTOM]++; int total = areaConnectCount[(int)ConnectAreaType.AREA_BOTTOM]; int cnt = count[(int)ConnectAreaType.AREA_BOTTOM]; float offset = windowRect.width / (total + 1) * cnt; box.boxRect.x = windowRect.x + offset - box.boxRect.width / 2; box.boxRect.y = windowRect.y + windowRect.height; } box.boxRect.width = ConnectBox.CONNECT_SIZE * parentWindow.Grid.m_GridZoomRate; box.boxRect.height = ConnectBox.CONNECT_SIZE * parentWindow.Grid.m_GridZoomRate; // コネクトボックスの色 float add = 0.15f; #if (UNITY_5_3) Color col = Color.HSVToRGB(add * (index % (1.0f / add)), 0.3f, 1.0f); #else Color col = EditorGUIUtility.HSVToRGB(add * (index % (1.0f / add)), 0.3f, 1.0f); #endif if (box.active) { col = Color.red; } box.color = col; index++; } }
public static Color HSVtoRGB(Vector3 hsvColor) { return(EditorGUIUtility.HSVToRGB(hsvColor.x / 360f, hsvColor.y / 255f, hsvColor.z / 255f)); }
static void PaintVertexColors(bool delete) { Ray r = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; if (Physics.Raycast(r, out hit, float.MaxValue)) { Vector3[] vertices = currentSelectionMesh.vertices; Color[] colrs = currentSelectionMesh.colors; Undo.RegisterUndo(currentSelectionMesh, "Vertex Paint"); Vector3 pos = currentSelection.transform.InverseTransformPoint(hit.point); float finalBlendFactor = blendFactor * 0.2f; if (finalBlendFactor >= 0.2f) { finalBlendFactor = 1; } for (int i = 0; i < vertices.Length; i++) { float sqrMagnitude = (vertices[i] - pos).magnitude; if (sqrMagnitude > radius) { continue; } float rc = colrs[i].r, gc = colrs[i].g, bc = colrs[i].b, ac = colrs[i].a; switch (ColorPaintMode) { //(Background Layer inverted, divided by Active Layer, and the quotient is then inverted). case global::ColorPaintMode.ColorBurn: { if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : (1 - (1 - rc) / currentColor.r), finalBlendFactor); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : (1 - (1 - gc) / currentColor.g), finalBlendFactor); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : (1 - (1 - bc) / currentColor.b), finalBlendFactor); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : (1 - (1 - ac) / currentColor.a), finalBlendFactor); } } break; case global::ColorPaintMode.Screen: { if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : (1 - (1 - currentColor.r) * (1 - rc)), finalBlendFactor); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : (1 - (1 - currentColor.g) * (1 - gc)), finalBlendFactor); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : (1 - (1 - currentColor.b) * (1 - bc)), finalBlendFactor); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : (1 - (1 - currentColor.a) * (1 - ac)), finalBlendFactor); } } break; case global::ColorPaintMode.Lighten: { if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : (rc > currentColor.r) ? rc : currentColor.r, finalBlendFactor); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : (gc > currentColor.g) ? gc : currentColor.g, finalBlendFactor); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : (bc > currentColor.b) ? bc : currentColor.b, finalBlendFactor); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : (ac > currentColor.a) ? ac : currentColor.a, finalBlendFactor); } } break; case global::ColorPaintMode.Darken: { if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : (rc < currentColor.r) ? rc : currentColor.r, finalBlendFactor); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : (gc < currentColor.g) ? gc : currentColor.g, finalBlendFactor); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : (bc < currentColor.b) ? bc : currentColor.b, finalBlendFactor); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : (ac < currentColor.a) ? ac : currentColor.a, finalBlendFactor); } } break; case global::ColorPaintMode.Hue: { float H, H2, S, S2, V, V2; EditorGUIUtility.RGBToHSV(colrs[i], out H, out S, out V); EditorGUIUtility.RGBToHSV(currentColor, out H2, out S2, out V2); colrs[i] = EditorGUIUtility.HSVToRGB(Mathf.Lerp(H, H2, finalBlendFactor), S, V); rc = colrs[i].r; gc = colrs[i].g; bc = colrs[i].b; ac = colrs[i].a; if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : rc - currentColor.r, 0); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : gc - currentColor.g, 0); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : bc - currentColor.b, 0); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : ac - currentColor.a, 0); } } break; case global::ColorPaintMode.Color: { float H, H2, S, S2, V, V2; EditorGUIUtility.RGBToHSV(colrs[i], out H, out S, out V); EditorGUIUtility.RGBToHSV(currentColor, out H2, out S2, out V2); colrs[i] = EditorGUIUtility.HSVToRGB(Mathf.Lerp(H, H2, finalBlendFactor), Mathf.Lerp(S, S2, finalBlendFactor), V); rc = colrs[i].r; gc = colrs[i].g; bc = colrs[i].b; ac = colrs[i].a; if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : rc - currentColor.r, 0); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : gc - currentColor.g, 0); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : bc - currentColor.b, 0); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : ac - currentColor.a, 0); } } break; case global::ColorPaintMode.Substract: { if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : rc - currentColor.r, finalBlendFactor); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : gc - currentColor.g, finalBlendFactor); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : bc - currentColor.b, finalBlendFactor); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : ac - currentColor.a, finalBlendFactor); } } break; case global::ColorPaintMode.Add: { if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : rc + currentColor.r, finalBlendFactor); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : gc + currentColor.g, finalBlendFactor); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : bc + currentColor.b, finalBlendFactor); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : ac + currentColor.a, finalBlendFactor); } } break; case global::ColorPaintMode.Multiply: { if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : currentColor.r * rc, finalBlendFactor); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : currentColor.g * gc, finalBlendFactor); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : currentColor.b * bc, finalBlendFactor); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : currentColor.a + ac, finalBlendFactor); } } break; default: { if (ChannelsToPaint.Red) { rc = Mathf.Lerp(rc, delete ? 0 : currentColor.r, finalBlendFactor); } if (ChannelsToPaint.Green) { gc = Mathf.Lerp(gc, delete ? 0 : currentColor.g, finalBlendFactor); } if (ChannelsToPaint.Blue) { bc = Mathf.Lerp(bc, delete ? 0 : currentColor.b, finalBlendFactor); } if (ChannelsToPaint.Alpha) { ac = Mathf.Lerp(ac, delete ? 0 : currentColor.a, finalBlendFactor); } } break; } colrs[i] = new Color(rc, gc, bc, ac); } currentSelectionMesh.colors = colrs; } }