11var registerComponent = require ( '../core/component' ) . registerComponent ;
22var THREE = require ( '../lib/three' ) ;
3- var DEFAULT_CAMERA_HEIGHT = require ( '../constants' ) . DEFAULT_CAMERA_HEIGHT ;
43var bind = require ( '../utils/bind' ) ;
4+ var PolyfillControls = require ( '../utils' ) . device . PolyfillControls ;
55
66// To avoid recalculation at every mouse movement tick
77var GRABBING_CLASS = 'a-grabbing' ;
@@ -23,21 +23,16 @@ module.exports.Component = registerComponent('look-controls', {
2323 } ,
2424
2525 init : function ( ) {
26- var sceneEl = this . el . sceneEl ;
27-
2826 this . previousHMDPosition = new THREE . Vector3 ( ) ;
2927 this . hmdQuaternion = new THREE . Quaternion ( ) ;
3028 this . hmdEuler = new THREE . Euler ( ) ;
3129 this . position = new THREE . Vector3 ( ) ;
30+ this . polyfillObject = new THREE . Object3D ( ) ;
31+ this . polyfillControls = new PolyfillControls ( this . polyfillObject ) ;
3232 this . rotation = { } ;
3333 this . deltaRotation = { } ;
34-
3534 this . setupMouseControls ( ) ;
36- this . setupHMDControls ( ) ;
3735 this . bindMethods ( ) ;
38-
39- // Reset previous HMD position when we exit VR.
40- sceneEl . addEventListener ( 'exit-vr' , this . onExitVR ) ;
4136 } ,
4237
4338 update : function ( oldData ) {
@@ -57,22 +52,9 @@ module.exports.Component = registerComponent('look-controls', {
5752
5853 tick : function ( t ) {
5954 var data = this . data ;
60- if ( ! data . enabled ) { return ; }
61- this . controls . standing = data . standing ;
62- this . controls . userHeight = this . getUserHeight ( ) ;
63- this . controls . update ( ) ;
64- this . updateOrientation ( ) ;
55+ if ( ! data . enabled || this . el . sceneEl . is ( 'vr-mode' ) ) { return ; }
6556 this . updatePosition ( ) ;
66- } ,
67-
68- /**
69- * Return user height to use for standing poses, where a device doesn't provide an offset.
70- */
71- getUserHeight : function ( ) {
72- var el = this . el ;
73- return el . hasAttribute ( 'camera' )
74- ? el . getAttribute ( 'camera' ) . userHeight
75- : DEFAULT_CAMERA_HEIGHT ;
57+ this . updateOrientation ( ) ;
7658 } ,
7759
7860 play : function ( ) {
@@ -108,16 +90,6 @@ module.exports.Component = registerComponent('look-controls', {
10890 this . yawObject . add ( this . pitchObject ) ;
10991 } ,
11092
111- /**
112- * Set up VR controls that will copy data to the dolly.
113- */
114- setupHMDControls : function ( ) {
115- this . dolly = new THREE . Object3D ( ) ;
116- this . euler = new THREE . Euler ( ) ;
117- this . controls = new THREE . VRControls ( this . dolly ) ;
118- this . controls . userHeight = 0.0 ;
119- } ,
120-
12193 /**
12294 * Add mouse and touch event listeners to canvas.
12395 */
@@ -140,6 +112,9 @@ module.exports.Component = registerComponent('look-controls', {
140112 canvasEl . addEventListener ( 'touchstart' , this . onTouchStart ) ;
141113 window . addEventListener ( 'touchmove' , this . onTouchMove ) ;
142114 window . addEventListener ( 'touchend' , this . onTouchEnd ) ;
115+
116+ // sceneEl events.
117+ sceneEl . addEventListener ( 'exit-vr' , this . onExitVR ) ;
143118 } ,
144119
145120 /**
@@ -161,71 +136,39 @@ module.exports.Component = registerComponent('look-controls', {
161136 canvasEl . removeEventListener ( 'touchstart' , this . onTouchStart ) ;
162137 canvasEl . removeEventListener ( 'touchmove' , this . onTouchMove ) ;
163138 canvasEl . removeEventListener ( 'touchend' , this . onTouchEnd ) ;
139+
140+ // sceneEl events.
141+ sceneEl . removeEventListener ( 'exit-vr' , this . onExitVR ) ;
164142 } ,
165143
166144 /**
167145 * Update orientation for mobile, mouse drag, and headset.
168146 * Mouse-drag only enabled if HMD is not active.
169147 */
170148 updateOrientation : function ( ) {
171- var currentRotation ;
172- var deltaRotation = this . deltaRotation ;
173149 var hmdEuler = this . hmdEuler ;
174- var hmdQuaternion = this . hmdQuaternion ;
175150 var pitchObject = this . pitchObject ;
176151 var yawObject = this . yawObject ;
177152 var sceneEl = this . el . sceneEl ;
178153 var rotation = this . rotation ;
179154
180- // Calculate HMD quaternion.
181- hmdQuaternion = hmdQuaternion . copy ( this . dolly . quaternion ) ;
182- hmdEuler . setFromQuaternion ( hmdQuaternion , 'YXZ' ) ;
183-
184- if ( sceneEl . isMobile ) {
185- // On mobile, do camera rotation with touch events and sensors.
186- rotation . x = radToDeg ( hmdEuler . x ) + radToDeg ( pitchObject . rotation . x ) ;
187- rotation . y = radToDeg ( hmdEuler . y ) + radToDeg ( yawObject . rotation . y ) ;
188- rotation . z = radToDeg ( hmdEuler . z ) ;
189- } else if ( ! sceneEl . is ( 'vr-mode' ) || isNullVector ( hmdEuler ) || ! this . data . hmdEnabled ) {
190- // Mouse drag if WebVR not active (not connected, no incoming sensor data).
191- currentRotation = this . el . getAttribute ( 'rotation' ) ;
192- this . calculateDeltaRotation ( ) ;
193- if ( this . data . reverseMouseDrag ) {
194- rotation . x = currentRotation . x - deltaRotation . x ;
195- rotation . y = currentRotation . y - deltaRotation . y ;
196- rotation . z = currentRotation . z ;
197- } else {
198- rotation . x = currentRotation . x + deltaRotation . x ;
199- rotation . y = currentRotation . y + deltaRotation . y ;
200- rotation . z = currentRotation . z ;
201- }
202- } else {
203- // Mouse rotation ignored with an active headset. Use headset rotation.
204- rotation . x = radToDeg ( hmdEuler . x ) ;
205- rotation . y = radToDeg ( hmdEuler . y ) ;
206- rotation . z = radToDeg ( hmdEuler . z ) ;
207- }
155+ // In VR mode THREE is in charge of updating the camera rotation.
156+ if ( sceneEl . is ( 'vr-mode' ) ) { return ; }
208157
209- this . el . setAttribute ( 'rotation' , rotation ) ;
210- } ,
158+ // Calculate polyfilled HMD quaternion.
159+ this . polyfillControls . update ( ) ;
160+ hmdEuler . setFromQuaternion ( this . polyfillObject . quaternion , 'YXZ' ) ;
161+ // On mobile, do camera rotation with touch events and sensors.
162+ rotation . x = radToDeg ( hmdEuler . x ) + radToDeg ( pitchObject . rotation . x ) ;
163+ rotation . y = radToDeg ( hmdEuler . y ) + radToDeg ( yawObject . rotation . y ) ;
164+ rotation . z = 0 ;
211165
212- /**
213- * Calculate delta rotation for mouse-drag and touch-drag.
214- */
215- calculateDeltaRotation : function ( ) {
216- var currentRotationX = radToDeg ( this . pitchObject . rotation . x ) ;
217- var currentRotationY = radToDeg ( this . yawObject . rotation . y ) ;
218- this . deltaRotation . x = currentRotationX - ( this . previousRotationX || 0 ) ;
219- this . deltaRotation . y = currentRotationY - ( this . previousRotationY || 0 ) ;
220- // Store current rotation for next tick.
221- this . previousRotationX = currentRotationX ;
222- this . previousRotationY = currentRotationY ;
223- return this . deltaRotation ;
166+ this . el . setAttribute ( 'rotation' , rotation ) ;
224167 } ,
225168
226169 /**
227- * Handle positional tracking.
228- */
170+ * Handle positional tracking.
171+ */
229172 updatePosition : function ( ) {
230173 var el = this . el ;
231174 var currentHMDPosition ;
@@ -238,26 +181,37 @@ module.exports.Component = registerComponent('look-controls', {
238181
239182 // Calculate change in position.
240183 currentHMDPosition = this . calculateHMDPosition ( ) ;
241-
242184 currentPosition = el . getAttribute ( 'position' ) ;
243185
244186 position . copy ( currentPosition ) . sub ( previousHMDPosition ) . add ( currentHMDPosition ) ;
245187 el . setAttribute ( 'position' , position ) ;
246188 previousHMDPosition . copy ( currentHMDPosition ) ;
247189 } ,
248190
249- /**
250- * Get headset position from VRControls.
251- */
252191 calculateHMDPosition : ( function ( ) {
253192 var position = new THREE . Vector3 ( ) ;
254193 return function ( ) {
255- this . dolly . updateMatrix ( ) ;
256- position . setFromMatrixPosition ( this . dolly . matrix ) ;
194+ var object3D = this . el . object3D ;
195+ object3D . updateMatrix ( ) ;
196+ position . setFromMatrixPosition ( object3D . matrix ) ;
257197 return position ;
258198 } ;
259199 } ) ( ) ,
260200
201+ /**
202+ * Calculate delta rotation for mouse-drag and touch-drag.
203+ */
204+ calculateDeltaRotation : function ( ) {
205+ var currentRotationX = radToDeg ( this . pitchObject . rotation . x ) ;
206+ var currentRotationY = radToDeg ( this . yawObject . rotation . y ) ;
207+ this . deltaRotation . x = currentRotationX - ( this . previousRotationX || 0 ) ;
208+ this . deltaRotation . y = currentRotationY - ( this . previousRotationY || 0 ) ;
209+ // Store current rotation for next tick.
210+ this . previousRotationX = currentRotationX ;
211+ this . previousRotationY = currentRotationY ;
212+ return this . deltaRotation ;
213+ } ,
214+
261215 /**
262216 * Translate mouse drag into rotation.
263217 *
@@ -377,7 +331,3 @@ module.exports.Component = registerComponent('look-controls', {
377331 disableGrabCursor ( ) ;
378332 }
379333} ) ;
380-
381- function isNullVector ( vector ) {
382- return vector . x === 0 && vector . y === 0 && vector . z === 0 ;
383- }
0 commit comments