forked from aframevr/aframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVRControls.js
173 lines (94 loc) · 2.77 KB
/
VRControls.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
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
/**
* @author dmarcos / https://github.com/dmarcos
* @author mrdoob / http://mrdoob.com
*/
THREE.VRControls = function ( object, onError ) {
var scope = this;
var vrDisplay, vrDisplays;
var standingMatrix = new THREE.Matrix4();
var frameData = null;
if ( 'VRFrameData' in window ) {
frameData = new VRFrameData();
}
function gotVRDisplays( displays ) {
vrDisplays = displays;
if ( displays.length > 0 ) {
vrDisplay = displays[ 0 ];
} else {
if ( onError ) onError( 'VR input not available.' );
}
}
if ( navigator.getVRDisplays ) {
navigator.getVRDisplays().then( gotVRDisplays ).catch ( function () {
console.warn( 'THREE.VRControls: Unable to get VR Displays' );
} );
}
// 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;
// If true will use "standing space" coordinate system where y=0 is the
// floor and x=0, z=0 is the center of the room.
this.standing = false;
// Distance from the users eyes to the floor in meters. Used when
// standing=true but the VRDisplay doesn't provide stageParameters.
this.userHeight = 1.6;
this.getVRDisplay = function () {
return vrDisplay;
};
this.setVRDisplay = function ( value ) {
vrDisplay = value;
};
this.getVRDisplays = function () {
console.warn( 'THREE.VRControls: getVRDisplays() is being deprecated.' );
return vrDisplays;
};
this.getStandingMatrix = function () {
return standingMatrix;
};
this.update = function () {
if ( vrDisplay ) {
var pose;
if ( vrDisplay.getFrameData ) {
vrDisplay.getFrameData( frameData );
pose = frameData.pose;
} else if ( vrDisplay.getPose ) {
pose = vrDisplay.getPose();
}
if ( pose.orientation !== null ) {
object.quaternion.fromArray( pose.orientation );
}
if ( pose.position !== null ) {
object.position.fromArray( pose.position );
} else {
object.position.set( 0, 0, 0 );
}
if ( this.standing ) {
if ( vrDisplay.stageParameters ) {
object.updateMatrix();
standingMatrix.fromArray( vrDisplay.stageParameters.sittingToStandingTransform );
object.applyMatrix( standingMatrix );
} else {
object.position.setY( object.position.y + this.userHeight );
}
}
object.position.multiplyScalar( scope.scale );
}
};
this.resetPose = function () {
if ( vrDisplay ) {
vrDisplay.resetPose();
}
};
this.resetSensor = function () {
console.warn( 'THREE.VRControls: .resetSensor() is now .resetPose().' );
this.resetPose();
};
this.zeroSensor = function () {
console.warn( 'THREE.VRControls: .zeroSensor() is now .resetPose().' );
this.resetPose();
};
this.dispose = function () {
vrDisplay = null;
};
};