10using System.Collections.Generic;
31 if (m !=
null) {
PushMatrix(m.Value);
return true; }
else {
return false; }
47 if (maybeColor !=
null) {
implSetColor(maybeColor.Value); }
50 #region Implementations
69 private static Drawer _unityDebugDrawer;
70 private static Color _unityDebugColor =
Color.white;
72 var drawer = Utils.Require(ref _unityDebugDrawer);
75 drawer.implDrawLine = (a, b) => {
79 drawer.implSetColor = (c) => {
85 private static Drawer _unityGizmoDrawer;
88 var drawer = Utils.Require(ref _unityGizmoDrawer);
90 drawer.implDrawLine = (a, b) => {
91 Gizmos.DrawLine(a, b);
93 drawer.implSetColor = (c) => {
96 drawer.implDrawUnitSphere = (m) => {
97 var origM = Gizmos.matrix;
99 Gizmos.DrawSphere(Vector3.zero, 1f);
100 Gizmos.matrix = origM;
105 private static Drawer _unityGizmoHandlesDrawer;
108 var drawer = Utils.Require(ref _unityGizmoHandlesDrawer);
110 drawer.implDrawLine = (a, b) => {
115 drawer.implSetColor = (c) => {
120 drawer.implDrawUnitSphere = (m) => {
125 Color.RGBToHSV(origColor, out h, out s, out v);
126 UnityEditor.Handles.color = origColor.WithHSV(h, s * 1f, v * 3f);
129 UnityEditor.Handles.SphereHandleCap(0, Vector3.zero, Quaternion.identity, 1f, EventType.Repaint);
140 public void Line(Vector3 a, Vector3 b) {
142 _currMatrix.MultiplyPoint3x4(a),
150 private Action<Vector3, Vector3> drawLineAction {
get {
151 return Utils.Require(ref _b_drawLineAction, () => (a, b) =>
Line(a, b));
153 private Action<Vector3, Vector3> _b_drawLineAction;
156 System.Action<System.Action<Vector3, Vector3>> drawLineFuncFunc,
160 drawLineFuncFunc(drawLineAction);
165 #region Sphere Drawing
167 public void Sphere(Vector3 center =
default(Vector3),
float radius = 1f,
168 Color?
color =
null, Matrix4x4? matrix =
null)
172 var useMatrix = (matrix ?? Matrix4x4.identity) * Matrix4x4.TRS(center, Quaternion.identity, Vector3.one * radius);
177 this.WireSphere(center, radius,
color, matrix);
179 }
catch (System.Exception e) {
Debug.LogError(e); }
186 public static class DrawerExtensions {
188 public static void WireSphere(
this Drawer drawer,
189 Vector3 center =
default(Vector3),
float radius = 1f,
Color? color =
null,
190 Matrix4x4? matrix =
null)
192 drawer.
Lines(drawLineFunc =>
193 new Geometry.Sphere(center, radius, overrideMatrix: matrix)
194 .DrawLines(drawLineFunc), color);
197 public static void Lines(
this Drawer drawer, Vector3 a, Vector3 b,
200 drawer.Line(a, b); drawer.Line(b, c);
203 public static void Lines(
this Drawer drawer, Vector3 a, Vector3 b,
204 Vector3 c, Vector3 d)
206 drawer.Line(a, b); drawer.Line(b, c); drawer.Line(c, d);
209 public static void Lines(
this Drawer drawer, Vector3 a, Vector3 b,
210 Vector3 c, Vector3 d, Vector3 e)
212 drawer.Line(a, b); drawer.Line(b, c); drawer.Line(c, d);
216 private static Color[] _basisColors;
217 public static void DrawBasis(
this Transform t, Drawer drawer,
218 float? length =
null,
219 Color? overrideAxesColor =
null,
220 float? axesHueShift =
null,
221 Matrix4x4? overrideMatrix =
null)
223 var useLength = length.UnwrapOr(0.5f);
224 var useMatrix = overrideMatrix.UnwrapOr(t.localToWorldMatrix);
227 if (overrideAxesColor !=
null) {
228 xColor = yColor = zColor = overrideAxesColor.Value;
230 if (axesHueShift !=
null) {
231 var hueShift = axesHueShift.Value;
232 xColor = xColor.ShiftHue(hueShift);
233 yColor = yColor.ShiftHue(hueShift);
234 zColor = zColor.ShiftHue(hueShift);
236 Utils.Require(ref _basisColors, 3);
237 _basisColors[0] = xColor;
238 _basisColors[1] = yColor;
239 _basisColors[2] = zColor;
241 var octahedron =
new Geometry.Bipyramid(a: a, b:
Vector3.zero,
242 polySegments: 4, lengthFraction: 0.5f,
243 overrideMatrix: useMatrix);
244 for (var bIdx = 0; bIdx < 3; bIdx++) {
245 var b =
Vector3.zero; b[bIdx] = useLength;
247 drawer.color = _basisColors[bIdx];
248 octahedron.DrawLines(drawer.implDrawLine);
258 public static void Draw(
this Pose pose, Drawer drawer,
259 float length,
Color? overrideAxesColor =
null,
260 float? hueShift =
null)
263 if (overrideAxesColor !=
null) {
264 xColor = yColor = zColor = overrideAxesColor.Value;
266 if (hueShift !=
null) {
267 var useHueShift = hueShift.Value;
268 xColor = xColor.ShiftHue((
float)useHueShift);
269 yColor = yColor.ShiftHue((
float)useHueShift);
270 zColor = zColor.ShiftHue((
float)useHueShift);
273 Utils.Require(ref _basisColors, 3);
274 _basisColors[0] = xColor;
275 _basisColors[1] = yColor;
276 _basisColors[2] = zColor;
278 var bipyramid =
new Geometry.Bipyramid(a:
Vector3.zero, b:
Vector3.zero,
279 polySegments: 16, lengthFraction: 0.5f, overrideMatrix: pose.matrix);
280 for (var bIdx = 0; bIdx < 3; bIdx++) {
281 var b =
Vector3.zero; b[bIdx] = length;
283 drawer.color = _basisColors[bIdx];
284 bipyramid.DrawLines(drawer.implDrawLine);
288 public static void Draw(
this Matrix4x4 m, Drawer drawer,
289 float axisLengths,
float? hueShift =
null,
292 bool drawBips =
false)
294 var useXColor = xColor ?? color ??
Color.red;
295 var useYColor = yColor ?? color ??
Color.green;
296 var useZColor = zColor ?? color ??
Color.blue;
297 if (hueShift !=
null) {
298 useXColor = useXColor.ShiftHue(hueShift.Value);
299 useYColor = useYColor.ShiftHue(hueShift.Value);
300 useZColor = useZColor.ShiftHue(hueShift.Value);
303 var bipyramid =
new Geometry.Bipyramid(a:
Vector3.zero, b:
Vector3.zero,
304 polySegments: 16, lengthFraction: 0.5f, overrideMatrix: m);
305 for (var bIdx = 0; bIdx < 3; bIdx++) {
306 var b =
Vector3.zero; b[bIdx] = axisLengths;
308 if (bIdx == 0) { drawer.color = useXColor; }
309 if (bIdx == 1) { drawer.color = useYColor; }
310 if (bIdx == 2) { drawer.color = useZColor; }
311 bipyramid.DrawLines(drawer.implDrawLine);
314 for (var i = 0; i < 3; i++) {
315 var pos = m.GetPosition();
316 if (i == 0) { drawer.color = useXColor; }
317 if (i == 1) { drawer.color = useYColor; }
318 if (i == 2) { drawer.color = useZColor; }
319 drawer.implDrawLine(pos, pos + m.GetAxis(i) * axisLengths);
Simple drawing interface abstraction (intended for debug drawing, not production!) with statically-ac...
void Lines(System.Action< System.Action< Vector3, Vector3 > > drawLineFuncFunc, Color? color=null)
System.Action< Vector3, Vector3 > implDrawLine
Stack< Matrix4x4 > _matrices
static Drawer UnityDebugDrawer
void MaybeSetColor(Color? maybeColor)
bool MaybePushMatrix(Matrix4x4? m)
static Drawer UnityGizmoHandlesDrawer
For use in OnDrawGizmos and OnDrawGizmosSelected via the Handles API. By default, draws on top of any...
void Sphere(Vector3 center=default(Vector3), float radius=1f, Color? color=null, Matrix4x4? matrix=null)
void Line(Vector3 a, Vector3 b, Color? color)
void PushMatrix(Matrix4x4 m)
static Drawer UnityGizmoDrawer
For use in OnDrawGizmos and OnDrawGizmosSelected.
void Line(Vector3 a, Vector3 b)
System.Action< Matrix4x4 > implDrawUnitSphere
System.Action< Color > implSetColor
Color color
Calls the setColor delegate.