Tanoda
Box.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
10using System.Collections;
11using System.Collections.Generic;
12using UnityEngine;
13
14namespace Leap.Unity.Geometry {
15
16 public struct Box {
17
18 public Transform transform;
19 public Vector3 center;
20 public Vector3 radii;
21 public Matrix4x4? overrideMatrix;
22
23 private Box(LocalBox localBox) {
24 this.center = localBox.center;
25 this.radii = localBox.radii;
26 this.transform = null;
27 this.overrideMatrix = null;
28 }
29
30 public Box(LocalBox localBox, Transform withTransform)
31 : this(localBox) {
32 this.transform = withTransform;
33 }
34
35 public Box(Vector3 center, Vector3 radii, Component transformSource = null) {
36 this.center = center;
37 this.radii = radii;
38 if (transformSource != null) { this.transform = transformSource.transform; }
39 else { this.transform = null; }
40 this.overrideMatrix = null;
41 }
42
43 public Box(Vector3 radii) : this(Vector3.zero, radii) { }
44
45 public Box(Vector3 radii, Component transformSource) :
46 this(Vector3.zero, radii, transformSource) { }
47
48 public Box(float radius) : this(Vector3.one * radius) { }
49
50 public Box(float radius, Component transformSource) :
51 this(Vector3.one * radius, transformSource) { }
52
53 public static Box unit { get { return new Box(1f); }}
54
58 public Matrix4x4 matrix {
59 get {
60 if (overrideMatrix != null) {
61 return overrideMatrix.Value * Matrix4x4.Translate(center);
62 }
63 if (transform == null) {
64 return Matrix4x4.Translate(center);
65 }
66 return transform.localToWorldMatrix * Matrix4x4.Translate(center);
67 }
68 }
69
70 private Vector3 corner000 {
71 get { return matrix.MultiplyPoint3x4(-radii); }
72 }
73 private Vector3 corner100 {
74 get { return matrix.MultiplyPoint3x4(new Vector3(
75 radii.x, -radii.y, -radii.z
76 )); }
77 }
78 private Vector3 corner010 {
79 get { return matrix.MultiplyPoint3x4(new Vector3(
80 -radii.x, radii.y, -radii.z
81 )); }
82 }
83 private Vector3 corner110 {
84 get { return matrix.MultiplyPoint3x4(new Vector3(
85 radii.x, radii.y, -radii.z
86 )); }
87 }
88 private Vector3 corner001 {
89 get { return matrix.MultiplyPoint3x4(new Vector3(
90 -radii.x, -radii.y, radii.z
91 )); }
92 }
93 private Vector3 corner101 {
94 get { return matrix.MultiplyPoint3x4(new Vector3(
95 radii.x, -radii.y, radii.z
96 )); }
97 }
98 private Vector3 corner011 {
99 get { return matrix.MultiplyPoint3x4(new Vector3(
100 -radii.x, radii.y, radii.z
101 )); }
102 }
103 private Vector3 corner111 {
104 get { return matrix.MultiplyPoint3x4(radii); }
105 }
106
110 public Vector3 Sample(Vector3 coords01) {
111 var coordsN1to1 = coords01.CompWise(f => Mathf.Clamp01(f) * 2 - 1);
112 return matrix.MultiplyPoint3x4(this.radii.CompMul(coordsN1to1));
113 }
114
115 public void DrawLines(System.Action<Vector3, Vector3> drawLineFunc,
116 int divisions = 0)
117 {
118 divisions = Mathf.Max(1, divisions);
119 var frac = 1f / divisions;
120
121 drawDividedLines(draw: drawLineFunc, step: frac, a: corner000, b: corner100);
122 drawDividedLines(draw: drawLineFunc, step: frac, a: corner000, b: corner010);
123 drawDividedLines(draw: drawLineFunc, step: frac, a: corner110, b: corner100);
124 drawDividedLines(draw: drawLineFunc, step: frac, a: corner110, b: corner010);
125
126 drawDividedLines(draw: drawLineFunc, step: frac, a: corner000, b: corner001);
127 drawDividedLines(draw: drawLineFunc, step: frac, a: corner100, b: corner101);
128 drawDividedLines(draw: drawLineFunc, step: frac, a: corner010, b: corner011);
129 drawDividedLines(draw: drawLineFunc, step: frac, a: corner110, b: corner111);
130
131 drawDividedLines(draw: drawLineFunc, step: frac, a: corner001, b: corner101);
132 drawDividedLines(draw: drawLineFunc, step: frac, a: corner001, b: corner011);
133 drawDividedLines(draw: drawLineFunc, step: frac, a: corner111, b: corner101);
134 drawDividedLines(draw: drawLineFunc, step: frac, a: corner111, b: corner011);
135 }
136
137 private void drawDividedLines(System.Action<Vector3, Vector3> draw,
138 float step, Vector3 a, Vector3 b)
139 {
140 step = Mathf.Max(0.01f, step);
141 var a_t = a;
142 for (var t = step; t <= 1f; t += step) {
143 var b_t = Vector3.Lerp(a, b, t);
144 draw(a_t, b_t);
145 a_t = b_t;
146 }
147 }
148
149 public void Draw(Drawer drawer, Color? color = null) {
150 if (color != null) { drawer.color = color.Value; }
151 DrawLines(drawer.Line);
152 }
153
155 if (transform != null) {
156 drawer.PushMatrix();
157
158 drawer.matrix = Matrix4x4.TRS(transform.TransformPoint(center),
159 transform.rotation,
160 Quaternion.Inverse(transform.rotation)
161 * transform.TransformVector(radii));
162
163 drawBoxGizmos(drawer, Vector3.zero, Vector3.one);
164
165 drawer.PopMatrix();
166 }
167 else {
168 drawBoxGizmos(drawer, center, radii);
169 }
170 }
171
172 private void drawBoxGizmos(RuntimeGizmoDrawer drawer, Vector3 center, Vector3 radii) {
173 drawer.DrawWireCube(center, radii * 2f);
174
175 drawer.color = drawer.color.WithAlpha(0.05f);
176 int div = 3;
177 float invDiv = 1f / div;
178 for (int i = 0; i < div; i++) {
179 for (int j = 0; j < div; j++) {
180 for (int k = 0; k < div; k++) {
181 drawer.DrawWireCube((center + (new Vector3(i, j, k) - radii) * invDiv * 2),
182 radii * invDiv * 2f);
183 }
184 }
185 }
186
187 drawer.color = drawer.color.WithAlpha(0.04f);
188 drawer.DrawCube(center, radii * 2f);
189 }
190
191 }
192
193}
UnityEngine.Component Component
UnityEngine.Color Color
Definition: TestScript.cs:32
Simple drawing interface abstraction (intended for debug drawing, not production!) with statically-ac...
Definition: Drawer.cs:19
void Line(Vector3 a, Vector3 b)
Definition: Drawer.cs:140
Color color
Calls the setColor delegate.
Definition: Drawer.cs:26
void DrawWireCube(Vector3 position, Vector3 size)
Draws a wire gizmo cube at the given position with the given size.
void PushMatrix()
Saves the current gizmo matrix to the gizmo matrix stack.
void PopMatrix()
Restores the current gizmo matrix from the gizmo matrix stack.
Matrix4x4 matrix
Sets or gets the matrix used to transform all gizmos.
void DrawCube(Vector3 position, Vector3 size)
Draws a filled gizmo cube at the given position with the given size.
Color color
Sets or gets the color for the gizmos that will be drawn next.
Matrix4x4? overrideMatrix
Definition: Box.cs:21
Box(LocalBox localBox, Transform withTransform)
Definition: Box.cs:30
Transform transform
Definition: Box.cs:18
Vector3 center
Definition: Box.cs:19
Vector3 Sample(Vector3 coords01)
Returns the world-space position of the normalized coordinates (X, Y, Z each from 0 to 1) given this ...
Definition: Box.cs:110
void DrawLines(System.Action< Vector3, Vector3 > drawLineFunc, int divisions=0)
Definition: Box.cs:115
Box(Vector3 radii, Component transformSource)
Definition: Box.cs:45
Box(Vector3 radii)
Definition: Box.cs:43
Box(float radius)
Definition: Box.cs:48
Box(float radius, Component transformSource)
Definition: Box.cs:50
static Box unit
Definition: Box.cs:53
Box(Vector3 center, Vector3 radii, Component transformSource=null)
Definition: Box.cs:35
void DrawRuntimeGizmos(RuntimeGizmoDrawer drawer)
Definition: Box.cs:154
void Draw(Drawer drawer, Color? color=null)
Definition: Box.cs:149
Matrix4x4 matrix
Local-to-world matrix for this Box.
Definition: Box.cs:58