-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraw.cs
164 lines (141 loc) · 4.78 KB
/
Draw.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Draw : MonoBehaviour {
// steam vr controller shenanigans
private SteamVR_TrackedObject trackedObject;
private SteamVR_Controller.Device Controller
{
get
{
return SteamVR_Controller.Input((int)trackedObject.index);
}
}
private Vector3 controllerPosition;
private GameObject UIOverlayObject;
private SpriteRenderer UIOverlay;
private GameObject Cursor;
private Color currentColor = Color.blue;
private float currentStroke = (float)0.02;
private bool StrokePicked = false;
// keep a list of line renderers for each time you pull the trigger
private List<GameObject> lineSegments;
private LineRenderer currentLineRenderer;
private int lineRendererIndex = 0;
private bool drawing = false;
void CreateNewLineSegment()
{
lineSegments.Add(new GameObject());
currentLineRenderer = lineSegments[lineSegments.Count - 1].AddComponent<LineRenderer>();
currentLineRenderer.positionCount = 0;
currentLineRenderer.startWidth = currentStroke;
currentLineRenderer.endWidth = currentStroke;
currentLineRenderer.startColor = currentColor;
currentLineRenderer.endColor = currentColor;
currentLineRenderer.material = new Material(Shader.Find("Particles/Alpha Blended"));
lineRendererIndex = 0;
}
void HandleUndo()
{
if (Controller.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
{
if (lineSegments.Count > 0)
{
Destroy(lineSegments[lineSegments.Count - 1].GetComponent<LineRenderer>());
Destroy(lineSegments[lineSegments.Count - 1]);
lineSegments.RemoveAt(lineSegments.Count - 1);
}
}
}
void HandleReset()
{
if (Controller.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
{
Debug.Log("pushed the destroy button...");
while (lineSegments.Count != 0)
{
Destroy(lineSegments[0].GetComponent<LineRenderer>());
Destroy(lineSegments[0]);
lineSegments.RemoveAt(0);
}
}
}
void HandleDrawing()
{
if (Controller.GetHairTriggerDown() && drawing == false)
{
CreateNewLineSegment();
drawing = true;
}
else if (Controller.GetHairTriggerUp())
{
currentLineRenderer.positionCount = lineRendererIndex - 1;
drawing = false;
}
if (drawing)
{
currentLineRenderer.positionCount = lineRendererIndex + 1;
currentLineRenderer.SetPosition(lineRendererIndex, controllerPosition);
lineRendererIndex++;
}
}
void DrawUIOverlay()
{
UIOverlay.transform.position = controllerPosition;
UIOverlay.transform.rotation = Controller.transform.rot;
UIOverlay.transform.Rotate(90, 0, 0);
UIOverlay.transform.Translate(0, (float)-0.055, (float)-0.01);
}
void HandleColorPicker()
{
if (Controller.GetAxis() != Vector2.zero)
{
if (Mathf.Abs(Controller.GetAxis().y) < 0.33) {
currentColor = Color.HSVToRGB(Controller.GetAxis().x, 1, 1);
}
}
}
void HandleStrokePicker()
{
if (Controller.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad) && StrokePicked == false)
{
if (Controller.GetAxis().y > 0.6)
{
currentStroke += (float)0.01;
}
else if (Controller.GetAxis().y < 0.4)
{
currentStroke -= (float)0.01;
}
}
}
void UpdateCursor()
{
Cursor.transform.position = controllerPosition;
//Cursor.transform.Translate(0, (float)0.0, (float)0.0);
Cursor.transform.localScale = new Vector3(currentStroke, currentStroke, currentStroke);
Cursor.GetComponent<Renderer>().material.color = currentColor;
}
void Awake () {
trackedObject = GetComponent<SteamVR_TrackedObject>();
lineSegments = new List<GameObject>();
UIOverlayObject = new GameObject();
UIOverlay = UIOverlayObject.AddComponent<SpriteRenderer>();
UIOverlay.sprite = Resources.Load<Sprite>("Sprites/ControllerMap");
Cursor = GameObject.CreatePrimitive(PrimitiveType.Sphere);
}
void Update()
{
controllerPosition = trackedObject.transform.position;
UpdateCursor();
DrawUIOverlay();
HandleDrawing();
if (!drawing)
{
HandleUndo();
HandleReset();
HandleColorPicker();
HandleStrokePicker();
}
}
}