Tanoda
TransformPoseSpline.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
11using System.Collections;
12using System.Collections.Generic;
13using UnityEngine;
14using System;
15
17
18 [ExecuteInEditMode]
19 [AddComponentMenu("")]
20 public class TransformPoseSpline : MonoBehaviour, IRuntimeGizmoComponent {
21
22 [QuickButton("Spawn Default", "spawnDefaultSource0")]
23 public Transform pose0Source;
24
25 [QuickButton("Spawn Default", "spawnDefaultSource1")]
26 public Transform pose1Source;
27
28 [Header("Debug View")]
29 [Range(0f, 1f)]
30 public float renderPoseAtT = 0f;
31
32 private HermitePoseSpline? maybePoseSpline;
33
34 private void Update() {
35 if (pose0Source != null && pose1Source != null) {
36
37 var pose0 = pose0Source.ToPose();
38 var pose1 = pose1Source.ToPose();
39
40 var movement0 = Movement.identity;
41 if (pose0Source.childCount > 0) {
42 var pose0SourceChild = pose0Source.GetChild(0);
43 movement0 = new Movement(pose0, pose0SourceChild.ToPose(), 0.1f);
44 }
45
46 var movement1 = Movement.identity;
47 if (pose1Source.childCount > 0) {
48 var pose1SourceChild = pose1Source.GetChild(0);
49 movement1 = new Movement(pose1, pose1SourceChild.ToPose(), 0.1f);
50 }
51
52 maybePoseSpline = new HermitePoseSpline(pose0, pose1,
53 movement0, movement1);
54 }
55 }
56
57 private void spawnDefaultSource0() { spawnDefaultSource(false); }
58 private void spawnDefaultSource1() { spawnDefaultSource(true); }
59 private void spawnDefaultSource(bool isSecondSource) {
60 var newPoseSourceObj = new GameObject("Pose Source " + (isSecondSource ? 1 : 0));
61 newPoseSourceObj.transform.parent = this.transform;
62 newPoseSourceObj.transform.ResetLocalTransform();
63 newPoseSourceObj.transform.localPosition += Vector3.right * 0.2f
64 * (isSecondSource ? 1 : -1);
65 if (isSecondSource) { pose1Source = newPoseSourceObj.transform; }
66 else { pose0Source = newPoseSourceObj.transform; }
67 }
68
70 if (maybePoseSpline.HasValue) {
71 var spline = maybePoseSpline.Value;
72
73 drawer.color = LeapColor.brown.WithAlpha(0.4f);
74
75 Vector3? prevPos = null;
76 int numSteps = 32;
77 int drawPosePer = 8, counter = 0;
78 float tStep = 1f / numSteps;
79 for (float t = 0f; t <= 1f; t += tStep) {
80 var pose = spline.PoseAt(t);
81
82 if (counter % drawPosePer == 0) {
83 drawer.DrawPose(pose, 0.02f);
84 }
85
86 if (prevPos.HasValue) {
87 drawer.DrawLine(prevPos.Value, pose.position);
88 }
89
90 prevPos = pose.position;
91 counter++;
92 }
93
94 var renderPose = spline.PoseAt(renderPoseAtT);
95 var len = 0.06f;
96 var thick = 0.01f;
97 drawer.PushMatrix();
98 drawer.matrix = Matrix4x4.TRS(renderPose.position, renderPose.rotation, Vector3.one);
99 drawer.color = Color.red;
100 drawer.DrawCube(Vector3.right * ((len / 2f) + (thick / 2f)), new Vector3(len, thick, thick));
101 drawer.color = Color.green;
102 drawer.DrawCube(Vector3.up * ((len / 2f) + (thick / 2f)), new Vector3(thick, len, thick));
103 drawer.color = Color.blue;
104 drawer.DrawCube(Vector3.forward * ((len / 2f) + (thick / 2f)), new Vector3(thick, thick, len));
105 }
106 }
107 }
108
109}
UnityEngine.Color Color
Definition: TestScript.cs:32
void OnDrawRuntimeGizmos(RuntimeGizmoDrawer drawer)
void PushMatrix()
Saves the current gizmo matrix to the gizmo matrix stack.
void DrawLine(Vector3 a, Vector3 b)
Draws a gizmo line that connects the two positions.
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.
Have your MonoBehaviour implement this interface to be able to draw runtime gizmos....
Represents a spline for poses – positions and rotations – that travel from one position and rotation ...
static readonly Movement identity
Definition: Movement.cs:26