Skip to content

Commit

Permalink
begin to add full custom Handles
Browse files Browse the repository at this point in the history
  • Loading branch information
alelievr committed Nov 10, 2017
1 parent f70443e commit fd17e6c
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 6 deletions.
148 changes: 148 additions & 0 deletions Editor/CurveHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace BetterHandles
{
public class CurveHandle : CustomHandle
{
public Gradient curveGradient;
public float width;
public float height;

public int curveSamples = 50;

public void DrawHandle(AnimationCurve curve)
{
switch (Event.current.type)
{
case EventType.MouseDown:
break ;
case EventType.MouseUp:
break ;
case EventType.Repaint:
PushGLContext();
DrawBorders();
DrawCurve(curve);
DrawLabels();
PopGLContext();
break ;
}
}

void PushGLContext()
{
GL.PushMatrix();
GL.MultMatrix(matrix);
HandlesMaterials.vertexColor.SetPass(0);
}

void PopGLContext()
{
GL.PopMatrix();
}

void DrawBorders()
{
//hummm good (or not so) legacy OpenGL ...

Vector3 bottomLeft = Vector3.zero;
Vector3 bottomRight = new Vector3(width, 0, 0);
Vector3 topLeft = new Vector3(0, height, 0);
Vector3 topRight = new Vector3(width, height, 0);

GL.Begin(GL.LINE_STRIP);
{
HandlesMaterials.vertexColor.SetPass(0);
GL.Color(Color.blue);
GL.Vertex(bottomLeft);
GL.Vertex(bottomRight);
GL.Color(Color.red);
GL.Vertex(topRight);
GL.Vertex(topLeft);
GL.Vertex(bottomLeft);
}
GL.End();
}

void DrawCurveQuad(AnimationCurve curve, float f0, float f1)
{
Vector3 bottomLeft = new Vector3(f0 * width, 0, 0);
Vector3 topLeft = new Vector3(f0 * width, curve.Evaluate(f0) * height, 0);
Vector3 topRight = new Vector3(f1 * width, curve.Evaluate(f1) * height, 0);
Vector3 bottomRight = new Vector3(f1 * width, 0, 0);

GL.Color(curveGradient.Evaluate(f0));
GL.Vertex(bottomLeft);
GL.Vertex(topLeft);
GL.Color(curveGradient.Evaluate(f1));
GL.Vertex(topRight);
GL.Vertex(bottomRight);
}

void DrawLabels()
{
Handles.Label(Vector3.zero, "0");
}

void DrawCurvePointHandle(AnimationCurve curve, int index)
{
Keyframe kf = curve.keys[index];

Vector3 pos = new Vector3(kf.time * width, kf.value * height, 0);

pos = Handles.FreeMoveHandle(pos, Quaternion.identity, .05f, Vector3.zero, Handles.DotHandleCap);
Debug.Log("pos: " + pos);

kf.time = pos.x / width;
kf.value = pos.y / height;
curve.keys[index] = kf;
}

void DrawCurve(AnimationCurve curve)
{
if (curveSamples < 0 || curveSamples > 10000)
return ;

//draw curve
GL.Begin(GL.QUADS);
{

for (int i = 0; i < curveSamples; i++)
{
float f0 = (float)i / (float)curveSamples;
float f1 = (float)(i + 1) / (float)curveSamples;

DrawCurveQuad(curve, f0, f1);
}
}
GL.End();

//draw curve handles:
for (int i = 0; i < curve.length; i++)
DrawCurvePointHandle(curve, i);
}

public void SetColors(Color startColor, Color endColor)
{
curveGradient = new Gradient();
GradientColorKey[] colorKeys = new GradientColorKey[2]{new GradientColorKey(startColor, 0), new GradientColorKey(endColor, 1)};
GradientAlphaKey[] alphaKeys = new GradientAlphaKey[2]{new GradientAlphaKey(startColor.a, 0), new GradientAlphaKey(endColor.a, 1)};

curveGradient.SetKeys(colorKeys, alphaKeys);
}

public void SetColors(Gradient gradient)
{
curveGradient = gradient;
}

public void Set2DSize(Vector2 size) { Set2DSize(size.x, size.y); }
public void Set2DSize(float width, float height)
{
this.width = width;
this.height = height;
}
}
}
13 changes: 13 additions & 0 deletions Editor/CurveHandle.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Editor/CustomHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BetterHandles
{
public class CustomHandle
{
public Quaternion rotation = Quaternion.identity;
public Vector3 scale = Vector3.one;
public Vector3 position;

public Matrix4x4 matrix { get { return Matrix4x4.TRS(position, rotation, scale); } }

public void SetTransform(Transform transform)
{
rotation = transform.rotation;
position = transform.position;
scale = transform.localScale;
}
}
}
13 changes: 13 additions & 0 deletions Editor/CustomHandle.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Editor/HandlesExtended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using BetterHandles;

public static class HandlesExtended
{
Expand Down Expand Up @@ -130,6 +131,7 @@ public static void CapsuleBoundsHandle(Vector3 center, Quaternion rotation, Vect
using (new Handles.DrawingScope(trs))
{
capsuleBoundsHandle.heightAxis = heightAxis;
capsuleBoundsHandle.axes = handleAxes;
capsuleBoundsHandle.radius = radius;
capsuleBoundsHandle.height = height;
capsuleBoundsHandle.handleColor = handleColor;
Expand Down Expand Up @@ -191,11 +193,15 @@ public static void SphereBoundsHandle(Vector3 center, Quaternion rotation, Vecto

#endregion

#region Full custom Handles
#region Full custom handles (sources included)

public static void CurveHandle(Vector3 startPoint, Vector3 EndPoint, Gradient curveGradient, float yScale = 1)
static CurveHandle curveHandle = new CurveHandle();

public static void CurveHandle(float width, float height, AnimationCurve curve, Quaternion rotation, Color startColor, Color endColor)
{

curveHandle.SetColors(startColor, endColor);
curveHandle.Set2DSize(width, height);
curveHandle.DrawHandle(curve);
}

#endregion
Expand Down
16 changes: 16 additions & 0 deletions Editor/HandlesMaterials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BetterHandles
{
public static class HandlesMaterials
{
public static Material vertexColor;

static HandlesMaterials()
{
vertexColor = Resources.Load< Material >("vertexColorMaterial");
}
}
}
13 changes: 13 additions & 0 deletions Editor/HandlesMaterials.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions ExamplesScenes/Editor/HandlesExampleWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System;
using UnityEditor.IMGUI.Controls;
using BetterHandles;

public class HandlesExampleWindow : EditorWindow
{
Expand All @@ -13,9 +14,10 @@ public class HandlesExampleWindow : EditorWindow
[SerializeField]
string currentKey = null;

static float angle = 42, radius = 2, capsRadius = .5f, height = 2;
static Vector3 boxSize = new Vector3(2, 1, 2);
static Vector3 minAngles = new Vector3(0, 0, 0), maxAngles = new Vector3(45, 45, 45);
static float angle = 42, radius = 2, capsRadius = .5f, height = 2;
static Vector3 boxSize = new Vector3(2, 1, 2);
static Vector3 minAngles = new Vector3(0, 0, 0), maxAngles = new Vector3(45, 45, 45);
static AnimationCurve curve = new AnimationCurve(new Keyframe(0, 1), new Keyframe(.5f, .7f), new Keyframe(1, 0));

Dictionary< string, Action > handlesActions = new Dictionary< string, Action >()
{
Expand All @@ -29,6 +31,7 @@ public class HandlesExampleWindow : EditorWindow
{"Solid scaled rectangle", () => HandlesExtended.DrawRectange(Vector3.zero, Quaternion.Euler(90, 0, 90), new Vector3(1, 3, 2), new Color(0, 0, 1, 1f))},
{"Solid scaled sphere", () => HandlesExtended.DrawSphere(Vector3.zero, Quaternion.Euler(90, 0, 90), new Vector3(1, 3, 2), new Color(0, 0, 1, .3f))},
{"Full custom Handles", null},
{"Curve Handle", () => HandlesExtended.CurveHandle(3, 2, curve, Quaternion.identity, new Color(1, 0, 0, .3f), new Color(0, 0, 1, .3f))},
{"IMGUI Handles", null},
{"Arc Handle", () => HandlesExtended.ArcHandle(Vector3.zero, Quaternion.identity, Vector3.one, ref angle, ref radius, new Color(1, 0, 0, .1f), new Color(0, 0, 1, 1f))},
{"Box Bounds Handle", () => HandlesExtended.BoxBoundsHandle(Vector3.zero, Quaternion.identity, Vector3.one, ref boxSize, PrimitiveBoundsHandle.Axes.All, new Color(1, 0, 0, 1f), new Color(0, 0, 1, 1f))},
Expand Down Expand Up @@ -88,6 +91,7 @@ void OnGUI()
void OnSceneGUI(SceneView sv)
{
//draw the current shown handles
Debug.Log("evt: " + Event.current.type);
handlesActions[currentKey]();

if (focus)
Expand Down
10 changes: 10 additions & 0 deletions Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions Resources/vertexColorMaterial.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: vertexColorMaterial
m_Shader: {fileID: 4800000, guid: 6536f73c5833c427abd2b03b801cc3ed, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
Loading

0 comments on commit fd17e6c

Please sign in to comment.