forked from macdja38/ryke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhoneControls.js
120 lines (69 loc) · 2.51 KB
/
PhoneControls.js
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
/**
* Created by Sam Zhao on 2017-01-11.
*/
THREE.VRControls = function ( object, onError ) {
var scope = this;
var vrInputs = [];
function filterInvalidDevices( devices ) {
// Exclude Cardboard position sensor if Oculus exists.
var oculusDevices = devices.filter( function ( device ) {
return device.deviceName.toLowerCase().indexOf('oculus') !== -1;
} );
if ( oculusDevices.length >= 1 ) {
return devices.filter( function ( device ) {
return device.deviceName.toLowerCase().indexOf('cardboard') === -1;
} );
} else {
return devices;
}
}
function gotVRDevices( devices ) {
devices = filterInvalidDevices( devices );
for ( var i = 0; i < devices.length; i ++ ) {
if ( devices[ i ] instanceof PositionSensorVRDevice ) {
vrInputs.push( devices[ i ] );
}
}
if ( onError ) onError( 'HMD not available' );
}
if ( navigator.getVRDevices ) {
navigator.getVRDevices().then( gotVRDevices );
}
// the Rift SDK returns the position in meters
// this scale factor allows the user to define how meters
// are converted to scene units.
this.scale = 1;
this.update = function () {
for ( var i = 0; i < vrInputs.length; i ++ ) {
var vrInput = vrInputs[ i ];
var state = vrInput.getState();
if ( state.orientation !== null ) {
// if (object.type !== "Object3D") {
// object.quaternion.multiplyQuaternions(object.originalQuaternion, state.orientation);
// } else {
object.quaternion.copy( state.orientation );
// }
}
if ( state.position !== null ) {
object.position.copy( state.position ).multiplyScalar( scope.scale );
}
}
};
this.resetSensor = function () {
for ( var i = 0; i < vrInputs.length; i ++ ) {
var vrInput = vrInputs[ i ];
if ( vrInput.resetSensor !== undefined ) {
vrInput.resetSensor();
} else if ( vrInput.zeroSensor !== undefined ) {
vrInput.zeroSensor();
}
}
};
this.zeroSensor = function () {
THREE.warn( 'THREE.VRControls: .zeroSensor() is now .resetSensor().' );
this.resetSensor();
};
this.dispose = function () {
vrInputs = [];
};
};