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,21 @@ module.exports.Component = registerComponent('look-controls', {
2323 } ,
2424
2525 init : function ( ) {
26+ var self = this ;
2627 var sceneEl = this . el . sceneEl ;
27-
2828 this . previousHMDPosition = new THREE . Vector3 ( ) ;
2929 this . hmdQuaternion = new THREE . Quaternion ( ) ;
3030 this . hmdEuler = new THREE . Euler ( ) ;
3131 this . position = new THREE . Vector3 ( ) ;
32+ this . polyfillObject = new THREE . Object3D ( ) ;
33+ this . polyfillControls = new PolyfillControls ( this . polyfillObject ) ;
3234 this . rotation = { } ;
3335 this . deltaRotation = { } ;
34-
3536 this . setupMouseControls ( ) ;
36- this . setupHMDControls ( ) ;
3737 this . bindMethods ( ) ;
38-
39- // Reset previous HMD position when we exit VR.
40- sceneEl . addEventListener ( 'exit-vr' , this . onExitVR ) ;
38+ sceneEl . addEventListener ( 'enter-vr' , function ( ) {
39+ sceneEl . renderer . vr . setPoseTarget ( self . el . object3D ) ;
40+ } ) ;
4141 } ,
4242
4343 update : function ( oldData ) {
@@ -57,22 +57,9 @@ module.exports.Component = registerComponent('look-controls', {
5757
5858 tick : function ( t ) {
5959 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 ( ) ;
60+ if ( ! data . enabled || this . el . sceneEl . is ( 'vr-mode' ) ) { return ; }
6561 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 ;
62+ this . updateOrientation ( ) ;
7663 } ,
7764
7865 play : function ( ) {
@@ -108,16 +95,6 @@ module.exports.Component = registerComponent('look-controls', {
10895 this . yawObject . add ( this . pitchObject ) ;
10996 } ,
11097
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-
12198 /**
12299 * Add mouse and touch event listeners to canvas.
123100 */
@@ -161,71 +138,46 @@ module.exports.Component = registerComponent('look-controls', {
161138 canvasEl . removeEventListener ( 'touchstart' , this . onTouchStart ) ;
162139 canvasEl . removeEventListener ( 'touchmove' , this . onTouchMove ) ;
163140 canvasEl . removeEventListener ( 'touchend' , this . onTouchEnd ) ;
141+
142+ // sceneEl events.
143+ sceneEl . removeEventListener ( 'exit-vr' , this . onExitVR ) ;
144+ sceneEl . removeEventListener ( 'loaded' , this . wrapGetCamera ) ;
164145 } ,
165146
166147 /**
167148 * Update orientation for mobile, mouse drag, and headset.
168149 * Mouse-drag only enabled if HMD is not active.
169150 */
170151 updateOrientation : function ( ) {
171- var currentRotation ;
172- var deltaRotation = this . deltaRotation ;
173152 var hmdEuler = this . hmdEuler ;
174- var hmdQuaternion = this . hmdQuaternion ;
175153 var pitchObject = this . pitchObject ;
176154 var yawObject = this . yawObject ;
177155 var sceneEl = this . el . sceneEl ;
178156 var rotation = this . rotation ;
179-
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 {
157+ var object3D = this . el . object3D ;
158+ if ( sceneEl . is ( 'vr-mode' ) ) {
159+ // Calculate HMD quaternion.
160+ hmdEuler . setFromQuaternion ( object3D . quaternion , 'YXZ' ) ;
203161 // Mouse rotation ignored with an active headset. Use headset rotation.
204162 rotation . x = radToDeg ( hmdEuler . x ) ;
205163 rotation . y = radToDeg ( hmdEuler . y ) ;
206164 rotation . z = radToDeg ( hmdEuler . z ) ;
165+ } else {
166+ // Calculate polyfilled HMD quaternion.
167+ this . polyfillControls . update ( ) ;
168+ hmdEuler . setFromQuaternion ( this . polyfillObject . quaternion , 'YXZ' ) ;
169+ // On mobile, do camera rotation with touch events and sensors.
170+ rotation . x = radToDeg ( hmdEuler . x ) + radToDeg ( pitchObject . rotation . x ) ;
171+ rotation . y = radToDeg ( hmdEuler . y ) + radToDeg ( yawObject . rotation . y ) ;
172+ rotation . z = 0 ;
207173 }
208174
209175 this . el . setAttribute ( 'rotation' , rotation ) ;
210176 } ,
211177
212178 /**
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 ;
224- } ,
225-
226- /**
227- * Handle positional tracking.
228- */
179+ * Handle positional tracking.
180+ */
229181 updatePosition : function ( ) {
230182 var el = this . el ;
231183 var currentHMDPosition ;
@@ -238,26 +190,37 @@ module.exports.Component = registerComponent('look-controls', {
238190
239191 // Calculate change in position.
240192 currentHMDPosition = this . calculateHMDPosition ( ) ;
241-
242193 currentPosition = el . getAttribute ( 'position' ) ;
243194
244195 position . copy ( currentPosition ) . sub ( previousHMDPosition ) . add ( currentHMDPosition ) ;
245196 el . setAttribute ( 'position' , position ) ;
246197 previousHMDPosition . copy ( currentHMDPosition ) ;
247198 } ,
248199
249- /**
250- * Get headset position from VRControls.
251- */
252200 calculateHMDPosition : ( function ( ) {
253201 var position = new THREE . Vector3 ( ) ;
254202 return function ( ) {
255- this . dolly . updateMatrix ( ) ;
256- position . setFromMatrixPosition ( this . dolly . matrix ) ;
203+ var object3D = this . el . object3D ;
204+ object3D . updateMatrix ( ) ;
205+ position . setFromMatrixPosition ( object3D . matrix ) ;
257206 return position ;
258207 } ;
259208 } ) ( ) ,
260209
210+ /**
211+ * Calculate delta rotation for mouse-drag and touch-drag.
212+ */
213+ calculateDeltaRotation : function ( ) {
214+ var currentRotationX = radToDeg ( this . pitchObject . rotation . x ) ;
215+ var currentRotationY = radToDeg ( this . yawObject . rotation . y ) ;
216+ this . deltaRotation . x = currentRotationX - ( this . previousRotationX || 0 ) ;
217+ this . deltaRotation . y = currentRotationY - ( this . previousRotationY || 0 ) ;
218+ // Store current rotation for next tick.
219+ this . previousRotationX = currentRotationX ;
220+ this . previousRotationY = currentRotationY ;
221+ return this . deltaRotation ;
222+ } ,
223+
261224 /**
262225 * Translate mouse drag into rotation.
263226 *
@@ -377,7 +340,3 @@ module.exports.Component = registerComponent('look-controls', {
377340 disableGrabCursor ( ) ;
378341 }
379342} ) ;
380-
381- function isNullVector ( vector ) {
382- return vector . x === 0 && vector . y === 0 && vector . z === 0 ;
383- }
0 commit comments