Skip to content

Instantly share code, notes, and snippets.

@MadLittleMods
Last active November 22, 2016 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MadLittleMods/06b9a1e3c914d88efc6d to your computer and use it in GitHub Desktop.
Save MadLittleMods/06b9a1e3c914d88efc6d to your computer and use it in GitHub Desktop.
Custom Unity Handles - `HandlesExtensions.DrawSolidHollowDiscArc(...)` - Just like `Handles.DrawSolidDisc` but has a inner and outer radius for hollow discs: http://docs.unity3d.com/ScriptReference/Handles.DrawSolidDisc.html
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Reflection;
using self = HandlesExtensions;
public class HandlesExtensions {
static PropertyInfo handlesHandleWireMaterial_PropertyInfo;
static MethodInfo handleUtilityApplyWireMaterial_MethodInfo;
static HandlesExtensions() {
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
self.handleUtilityApplyWireMaterial_MethodInfo = typeof(HandleUtility).GetMethod("ApplyWireMaterial", bindingFlags);
self.handlesHandleWireMaterial_PropertyInfo = typeof(HandleUtility).GetProperty("handleWireMaterial", bindingFlags);
}
static void DrawSolidHollowDiscArc(
Vector3 position,
Vector3 normal,
Vector3 alongPlane,
float angle,
float innerRadius,
float outerRadius
) {
if (Event.current.type != EventType.Repaint) {
return;
}
int numSamples = 60;
Shader.SetGlobalColor("_HandleColor", Handles.color * new Color(1f, 1f, 1f, 0.5f));
Shader.SetGlobalFloat("_HandleSize", 1f);
if(self.handleUtilityApplyWireMaterial_MethodInfo != null) {
self.handleUtilityApplyWireMaterial_MethodInfo.Invoke(null, null);
}
else if(handlesHandleWireMaterial_PropertyInfo != null) {
((Material)handlesHandleWireMaterial_PropertyInfo.GetValue(null, null)).SetPass(0);
}
GL.PushMatrix();
GL.MultMatrix(Handles.matrix);
GL.Begin(GL.TRIANGLES);
alongPlane.Normalize();
Vector3 outerArcEdge = alongPlane * outerRadius;
Vector3 innerArcEdge = alongPlane * innerRadius;
for (int i = 0; i <= numSamples; i++) {
Quaternion rotation = Quaternion.AngleAxis((((float)i)/numSamples)*angle, normal);
Vector3 outerVertice = position + rotation * outerArcEdge;
Vector3 innerVertice = position + rotation * innerArcEdge;
Quaternion rotationNext = Quaternion.AngleAxis((Mathf.Clamp((float)i+1, 0f, numSamples)/numSamples)*angle, normal);
Vector3 outerVerticeNext = position + rotationNext * outerArcEdge;
Vector3 innerVerticeNext = position + rotationNext * innerArcEdge;
GL.Color(Handles.color);
// 1____2
// | /
// | /
// 3|/
GL.Vertex(outerVertice);
GL.Vertex(outerVerticeNext);
GL.Vertex(innerVertice);
// /|2
// / |
// / |
// 1‾‾‾‾3
GL.Vertex(innerVertice);
GL.Vertex(outerVerticeNext);
GL.Vertex(innerVerticeNext);
}
GL.End();
GL.PopMatrix();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment