관리 메뉴

그냥한다

유니티VR - DrawMgr.cs 본문

유니티

유니티VR - DrawMgr.cs

hololol 2019. 7. 23. 10:34
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
 
public class DrawMgr : MonoBehaviour
{
    [Header("Collider")]
    public Vector3 offset = new Vector3(0-0.05f, 0.03f);
    public float radius = 0.05f;
 
    [Header("Controller")]
    public SteamVR_Input_Sources righthand = SteamVR_Input_Sources.RightHand;
    public SteamVR_Action_Boolean trigger = SteamVR_Actions.default_InteractUI;
    public SteamVR_Action_Pose pose = SteamVR_Actions.default_Pose;
 
    [Header("Line")]
    public float lineWidth = 0.01f;
    public Color lineColor = new Color(111);
 
    private LineRenderer line;
    
 
    private void Start()
    {
        //Rigidbody와 Collider 컴포넌트 추가 및 설정
        Rigidbody rb = this.gameObject.AddComponent<Rigidbody>();
        rb.isKinematic = true;
 
        SphereCollider coll = this.gameObject.AddComponent<SphereCollider>();
        coll.isTrigger = true;
        coll.center = offset;
        coll.radius = this.radius;
 
    }
 
    private void Update()
    {
        //트리거 버튼을 클릭했을 때 새로운 라인렌더러를 포함한 객체를 생성
        if(trigger.GetStateDown(righthand))
        {
            CreateLineObject();
        }
 
        //트리거 버튼을 계속 클릭하고 있을 때 라인렌더러의 노드를 추가
        if (trigger.GetState(righthand))
        {
            //컨트롤러의 현재 위치값을 추출
            Vector3 position = pose.GetLocalPosition(righthand);
            //라인렌더러의 노드 개수를 증가
            ++line.positionCount;
            //새로 만든 노드의 위치값을 설정, 노드가 3개로 늘어났으면 2번 위치를 조정
            line.SetPosition(line.positionCount - 1, position);
        }
    }
 
    private void CreateLineObject()
    {
        //라인렌더러를 추가할 게임오브젝트 생성
        GameObject lineObject = new GameObject("Line");
        line = lineObject.AddComponent<LineRenderer>();
 
        //라인렌더러에 연결할 머티리얼 생성
        Material mt = new Material(Shader.Find("Unlit/Color"));
        mt.color = lineColor;
 
        line.material = this.material;
        line.useWorldSpace = false;
 
        //라인의 끝부분을 부드럽게 하기 위한 버텍스 개수 설정
        line.numCapVertices = 20;
 
        //라인렌더러의 너비 설정
        line.widthMultiplier = 0.1f;
        line.startWidth = lineWidth;
        line.endWidth = lineWidth;
 
        //라인렌더러의 노드 개수를 1로 설정
        line.positionCount = 1;
 
        //첫 번째 노드의 위치를 컨트롤러의 위치로 설정
        Vector3 position = pose.GetLocalPosition(righthand);
        line.SetPosition(0, position);
    }
 
    private void OnTriggerEnter(Collider coll)
    {
        switch (coll.tag)
        {
            case "RED":
                lineColor = Color.red;
                break;
 
            case "GREEN":
                lineColor = Color.red;
                break;
 
            case "BLUE":
                lineColor = Color.red;
                break;
 
            case "WHITE":
                lineColor = Color.red;
                break;
 
        }
    }
}
 
cs