-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvrCam.cs
115 lines (98 loc) · 3.7 KB
/
vrCam.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR;
using Unity.XR.MockHMD;
using System;
public class DataType_vrCam
{
public enum SensorType
{
gyro, accel
}
}
public class vrCam : MonoBehaviour
{
//public GameObject textMeshPro;
public bool isVR;
DataType_vrCam.SensorType usedSensorType;
private Queue<float> accelerationDataTemp = new Queue<float>();
private float accelerationZData{
get {
float average = 0;
foreach (float data in accelerationDataTemp) {
average += data;
}
average /= accelerationDataTemp.Count;
return average;
}
set {
var numStored = 10;
if (accelerationDataTemp.Count > numStored)
{
accelerationDataTemp.Dequeue();
}
accelerationDataTemp.Enqueue(value);
}
}
protected void OnEnable()
{
// All sensors start out disabled so they have to manually be enabled first.
InputSystem.EnableDevice(Accelerometer.current);
if(UnityEngine.InputSystem.Gyroscope.current != null) InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);
}
protected void OnDisable()
{
InputSystem.DisableDevice(Accelerometer.current);
if (UnityEngine.InputSystem.Gyroscope.current != null) InputSystem.DisableDevice(UnityEngine.InputSystem.Gyroscope.current);
}
// Start is called before the first frame update
void Start()
{
usedSensorType = (DataType_vrCam.SensorType)PlayerPrefs.GetInt("usedVRSensor", 0);
if (isVR)
{
XRSettings.enabled = true;
XRSettings.gameViewRenderMode = GameViewRenderMode.BothEyes;
//buat layar gabisa mati
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
else {
XRSettings.gameViewRenderMode = GameViewRenderMode.LeftEye;
XRSettings.enabled = false;
MockHMD.SetRenderMode(MockHMDBuildSettings.RenderMode.SinglePassInstanced);
Screen.sleepTimeout = SleepTimeout.SystemSetting;
}
}
// Update is called once per frame
void Update()
{
if (isVR)
{
UnityEngine.InputSystem.Gyroscope gyro = UnityEngine.InputSystem.Gyroscope.current;
//kalau gyroscope ada
if (gyro != null && usedSensorType == DataType_vrCam.SensorType.gyro)
{
var angularVelocity = gyro.angularVelocity.ReadValue();
transform.eulerAngles = new Vector3(
transform.eulerAngles.x - angularVelocity.x,
transform.eulerAngles.y - angularVelocity.y,
0
);
}
//kalau gyroscope tidak ada pakai Accelerometer
else
{
var acceleration = Accelerometer.current.acceleration.ReadValue();
//x=>y
//z=>x
var rotateSpeed = 350f * Time.deltaTime;
accelerationZData = ((float)decimal.Round(((decimal)acceleration.z), 2));
if (acceleration.x >= 0.1) transform.eulerAngles = new Vector3(-accelerationZData * 180f, transform.rotation.eulerAngles.y + (Math.Abs(acceleration.x) * rotateSpeed), 0);
else if (acceleration.x <= -0.1) transform.eulerAngles = new Vector3(-accelerationZData * 180f, transform.rotation.eulerAngles.y - (Math.Abs(acceleration.x) * rotateSpeed), 0);
else transform.eulerAngles = new Vector3(-accelerationZData * 180f, transform.rotation.eulerAngles.y, 0);
}
}
}
}