t1 / TFDContents / Assets / KinectDemos / OverlayDemo / Scripts / LinePainter.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (2.68 KB)
1 |
using UnityEngine; |
---|---|
2 |
using System.Collections; |
3 |
using System.Collections.Generic; |
4 |
|
5 |
public class LinePainter : MonoBehaviour |
6 |
{ |
7 |
[Tooltip("Line renderer used for the line drawing.")] |
8 |
public LineRenderer linePrefab; |
9 |
|
10 |
[Tooltip("GUI-Text to display information messages.")] |
11 |
public GUIText infoText; |
12 |
|
13 |
|
14 |
private HandOverlayer handOverlayer = null; |
15 |
private List<GameObject> linesDrawn = new List<GameObject>(); |
16 |
private LineRenderer currentLine; |
17 |
private int lineVertexIndex = 2; |
18 |
|
19 |
void Start() |
20 |
{ |
21 |
handOverlayer = GetComponent<HandOverlayer>(); |
22 |
} |
23 |
|
24 |
void Update() |
25 |
{ |
26 |
if(Input.GetKeyDown(KeyCode.U)) |
27 |
{ |
28 |
// U-key means Undo |
29 |
DeleteLastLine(); |
30 |
} |
31 |
|
32 |
// display info message when a user is detected |
33 |
KinectManager manager = KinectManager.Instance; |
34 |
if(manager && manager.IsInitialized() && manager.IsUserDetected()) |
35 |
{ |
36 |
if(infoText) |
37 |
{ |
38 |
infoText.text = "Grip hand to start drawing. Press [U] to undo the last line."; |
39 |
} |
40 |
} |
41 |
|
42 |
|
43 |
if(currentLine == null && |
44 |
(handOverlayer && (handOverlayer.GetLastHandEvent() == InteractionManager.HandEventType.Grip))) |
45 |
{ |
46 |
// start drawing lines |
47 |
currentLine = Instantiate(linePrefab).GetComponent<LineRenderer>(); |
48 |
currentLine.name = "Line" + linesDrawn.Count; |
49 |
currentLine.transform.parent = transform; |
50 |
|
51 |
Vector3 cursorPos = handOverlayer.GetCursorPos(); |
52 |
cursorPos.z = Camera.main.nearClipPlane; |
53 |
|
54 |
Vector3 cursorSpacePos = Camera.main.ViewportToWorldPoint(cursorPos); |
55 |
currentLine.SetPosition(0, cursorSpacePos); |
56 |
currentLine.SetPosition(1, cursorSpacePos); |
57 |
|
58 |
lineVertexIndex = 2; |
59 |
linesDrawn.Add(currentLine.gameObject); |
60 |
|
61 |
StartCoroutine(DrawLine()); |
62 |
} |
63 |
|
64 |
if (currentLine != null && |
65 |
(handOverlayer != null && (handOverlayer.GetLastHandEvent() == InteractionManager.HandEventType.Release))) |
66 |
{ |
67 |
// end drawing lines |
68 |
currentLine = null; |
69 |
} |
70 |
} |
71 |
|
72 |
// undo the last drawn line |
73 |
public void DeleteLastLine() |
74 |
{ |
75 |
if (linesDrawn.Count > 0) |
76 |
{ |
77 |
GameObject goLastLine = linesDrawn[linesDrawn.Count-1]; |
78 |
|
79 |
linesDrawn.RemoveAt(linesDrawn.Count-1); |
80 |
Destroy(goLastLine); |
81 |
} |
82 |
} |
83 |
|
84 |
// continue drawing line |
85 |
IEnumerator DrawLine() |
86 |
{ |
87 |
while(handOverlayer && (handOverlayer.GetLastHandEvent() == InteractionManager.HandEventType.Grip)) |
88 |
{ |
89 |
yield return new WaitForEndOfFrame(); |
90 |
|
91 |
if (currentLine != null) |
92 |
{ |
93 |
lineVertexIndex++; |
94 |
currentLine.SetVertexCount(lineVertexIndex); |
95 |
|
96 |
Vector3 cursorPos = handOverlayer.GetCursorPos(); |
97 |
cursorPos.z = Camera.main.nearClipPlane; |
98 |
|
99 |
Vector3 cursorSpacePos = Camera.main.ViewportToWorldPoint(cursorPos); |
100 |
currentLine.SetPosition(lineVertexIndex - 1, cursorSpacePos); |
101 |
} |
102 |
} |
103 |
} |
104 |
|
105 |
} |