열차 목업의 내부 확인용 프로젝트
smchoi
2024-06-14 3d45944b4b2abc384e2539fa72cd7925bbcf2543
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
 
public class DemoScript : MonoBehaviour
{
    public List<GameObject> Mirrors;
    public GameObject LightBulb;
    public UnityEngine.UI.Toggle RecursionToggle;
 
    private float rotationModifier = -1.0f;
    private float moveModifier = 1.0f;
    private Material lightBulbMaterial;
 
    private enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    private RotationAxes axes = RotationAxes.MouseXAndY;
    private float sensitivityX = 15F;
    private float sensitivityY = 15F;
    private float minimumX = -360F;
    private float maximumX = 360F;
    private float minimumY = -60F;
    private float maximumY = 60F;
    private float rotationX = 0F;
    private float rotationY = 0F;
    private Quaternion originalRotation;
 
    // Use this for initialization
    void Start()
    {
        originalRotation = transform.localRotation;
        Renderer r = LightBulb.GetComponent<Renderer>();
        if (Application.isPlaying)
        {
            r.sharedMaterial = r.material;
        }
        lightBulbMaterial = r.sharedMaterial;
    }
 
    // Update is called once per frame
    void Update()
    {
        RotateMirror();
        MoveLightBulb();
        UpdateMouseLook();
        UpdateMovement();
    }
 
    public void MirrorRecursionToggled()
    {
        ChangeMirrorRecursion();
    }
 
    public void ChangeMirrorRecursion()
    {
        foreach (GameObject o in Mirrors)
        {
            MirrorScript s = o.GetComponent<MirrorScript>();
            s.MirrorRecursion = RecursionToggle.isOn;
        }
    }
 
    private void UpdateMovement()
    {
        float speed = 4.0f * Time.deltaTime;
 
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(0.0f, 0.0f, speed);
        }
        else if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(0.0f, 0.0f, -speed);
        }
 
        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(-speed, 0.0f, 0.0f);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(speed, 0.0f, 0.0f);
        }
 
        if (Input.GetKeyDown(KeyCode.M))
        {
            RecursionToggle.isOn = !RecursionToggle.isOn;
        }
    }
 
    private void RotateMirror()
    {
        GameObject Mirror = Mirrors[0];
        float angle = Mirror.transform.rotation.eulerAngles.y;
        if (angle > 65 && angle < 100)
        {
            rotationModifier = -rotationModifier;
            angle = angle - 65.0f;
            Mirror.transform.Rotate(0.0f, -angle, 0.0f);
        }
        else if (angle > 100 && angle < 295)
        {
            rotationModifier = -rotationModifier;
            angle = 295.0f - angle;
            Mirror.transform.Rotate(0.0f, angle, 0.0f);
        }
        else
        {
            Mirror.transform.Rotate(0.0f, rotationModifier * Time.deltaTime * 20.0f, 0.0f);
        }
    }
 
    private void MoveLightBulb()
    {
        float x = LightBulb.transform.position.x;
        if (x > 5)
        {
            moveModifier = -moveModifier;
            x = 5;
        }
        else if (x < -5)
        {
            moveModifier = -moveModifier;
            x = -5;
        }
        else
        {
            x += (Time.deltaTime * moveModifier);
        }
 
        Light l = LightBulb.GetComponent<Light>();
        LightBulb.transform.position = new Vector3(x, LightBulb.transform.position.y, LightBulb.transform.position.z);
        float i = Mathf.Min(1.0f, l.intensity);
        lightBulbMaterial.SetColor("_EmissionColor", new Color(i, i, i));
    }
 
    private void UpdateMouseLook()
    {
        if (axes == RotationAxes.MouseXAndY)
        {
            // Read the mouse input axis
            rotationX += Input.GetAxis("Mouse X") * sensitivityX;
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
 
            rotationX = ClampAngle(rotationX, minimumX, maximumX);
            rotationY = ClampAngle(rotationY, minimumY, maximumY);
 
            Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
            Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, -Vector3.right);
 
            transform.localRotation = originalRotation * xQuaternion * yQuaternion;
        }
        else if (axes == RotationAxes.MouseX)
        {
            rotationX += Input.GetAxis("Mouse X") * sensitivityX;
            rotationX = ClampAngle(rotationX, minimumX, maximumX);
 
            Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
            transform.localRotation = originalRotation * xQuaternion;
        }
        else
        {
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = ClampAngle(rotationY, minimumY, maximumY);
 
            Quaternion yQuaternion = Quaternion.AngleAxis(-rotationY, Vector3.right);
            transform.localRotation = originalRotation * yQuaternion;
        }
    }
 
    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
        {
            angle += 360F;
        }
        if (angle > 360F)
        {
            angle -= 360F;
        }
 
        return Mathf.Clamp(angle, min, max);
    }
}