提交 bff1076e 编写于 作者: M Mugen87

WebXRManager: Introduce WebXRController.

上级 d52b0925
import { Group } from '../../objects/Group';
export class WebXRController {
constructor();
getTargetRaySpace(): Group;
getGripSpace(): Group;
dispatchEvent( event: object ): this;
disconnect( inputSource: object ): this;
update( inputSource: object, frame: object, referenceSpace: string ): this;
}
import { Group } from '../../objects/Group.js';
/**
* @author Mugen87 / https://github.com/Mugen87
*/
function WebXRController() {
this._targetRay = null;
this._grip = null;
}
Object.assign( WebXRController.prototype, {
constructor: WebXRController,
getTargetRaySpace: function () {
if ( this._targetRay === null ) {
this._targetRay = new Group();
this._targetRay.matrixAutoUpdate = false;
this._targetRay.visible = false;
}
return this._targetRay;
},
getGripSpace: function () {
if ( this._grip === null ) {
this._grip = new Group();
this._grip.matrixAutoUpdate = false;
this._grip.visible = false;
}
return this._grip;
},
dispatchEvent: function ( event ) {
if ( this._targetRay !== null ) {
this._targetRay.dispatchEvent( event );
}
if ( this._grip !== null ) {
this._grip.dispatchEvent( event );
}
return this;
},
disconnect: function ( inputSource ) {
this.dispatchEvent( { type: 'disconnected', data: inputSource } );
if ( this._targetRay !== null ) {
this._targetRay.visible = false;
}
if ( this._grip !== null ) {
this._grip.visible = false;
}
return this;
},
update: function ( inputSource, frame, referenceSpace ) {
var inputPose = null;
var gripPose = null;
var targetRay = this._targetRay;
var grip = this._grip;
if ( inputSource ) {
if ( targetRay !== null ) {
inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
if ( inputPose !== null ) {
targetRay.matrix.fromArray( inputPose.transform.matrix );
targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale );
}
}
if ( grip !== null && inputSource.gripSpace ) {
gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
if ( gripPose !== null ) {
grip.matrix.fromArray( gripPose.transform.matrix );
grip.matrix.decompose( grip.position, grip.rotation, grip.scale );
}
}
}
if ( targetRay !== null ) {
targetRay.visible = ( inputPose !== null );
}
if ( grip !== null ) {
grip.visible = ( gripPose !== null );
}
return this;
}
} );
export { WebXRController };
......@@ -4,11 +4,11 @@
import { ArrayCamera } from '../../cameras/ArrayCamera.js';
import { EventDispatcher } from '../../core/EventDispatcher.js';
import { Group } from '../../objects/Group.js';
import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
import { Vector3 } from '../../math/Vector3.js';
import { Vector4 } from '../../math/Vector4.js';
import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
import { WebXRController } from './WebXRController.js';
function WebXRManager( renderer, gl ) {
......@@ -55,20 +55,12 @@ function WebXRManager( renderer, gl ) {
if ( controller === undefined ) {
controller = {};
controller = new WebXRController();
controllers[ id ] = controller;
}
if ( controller.targetRay === undefined ) {
controller.targetRay = new Group();
controller.targetRay.matrixAutoUpdate = false;
controller.targetRay.visible = false;
}
return controller.targetRay;
return controller.getTargetRaySpace();
};
......@@ -78,20 +70,12 @@ function WebXRManager( renderer, gl ) {
if ( controller === undefined ) {
controller = {};
controller = new WebXRController();
controllers[ id ] = controller;
}
if ( controller.grip === undefined ) {
controller.grip = new Group();
controller.grip.matrixAutoUpdate = false;
controller.grip.visible = false;
}
return controller.grip;
return controller.getGripSpace();
};
......@@ -103,17 +87,7 @@ function WebXRManager( renderer, gl ) {
if ( controller ) {
if ( controller.targetRay ) {
controller.targetRay.dispatchEvent( { type: event.type } );
}
if ( controller.grip ) {
controller.grip.dispatchEvent( { type: event.type } );
}
controller.dispatchEvent( { type: event.type } );
}
......@@ -123,19 +97,7 @@ function WebXRManager( renderer, gl ) {
inputSourcesMap.forEach( function ( controller, inputSource ) {
if ( controller.targetRay ) {
controller.targetRay.dispatchEvent( { type: 'disconnected', data: inputSource } );
controller.targetRay.visible = false;
}
if ( controller.grip ) {
controller.grip.dispatchEvent( { type: 'disconnected', data: inputSource } );
controller.grip.visible = false;
}
controller.disconnect( inputSource );
} );
......@@ -257,18 +219,7 @@ function WebXRManager( renderer, gl ) {
if ( controller ) {
if ( controller.targetRay ) {
controller.targetRay.dispatchEvent( { type: 'disconnected', data: inputSource } );
}
if ( controller.grip ) {
controller.grip.dispatchEvent( { type: 'disconnected', data: inputSource } );
}
controller.dispatchEvent( { type: 'disconnected', data: inputSource } );
inputSourcesMap.delete( inputSource );
}
......@@ -284,17 +235,7 @@ function WebXRManager( renderer, gl ) {
if ( controller ) {
if ( controller.targetRay ) {
controller.targetRay.dispatchEvent( { type: 'connected', data: inputSource } );
}
if ( controller.grip ) {
controller.grip.dispatchEvent( { type: 'connected', data: inputSource } );
}
controller.dispatchEvent( { type: 'connected', data: inputSource } );
}
......@@ -470,53 +411,9 @@ function WebXRManager( renderer, gl ) {
for ( var i = 0; i < controllers.length; i ++ ) {
var controller = controllers[ i ];
var inputSource = inputSources[ i ];
var inputPose = null;
var gripPose = null;
if ( inputSource ) {
if ( controller.targetRay ) {
inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
if ( inputPose !== null ) {
controller.targetRay.matrix.fromArray( inputPose.transform.matrix );
controller.targetRay.matrix.decompose( controller.targetRay.position, controller.targetRay.rotation, controller.targetRay.scale );
}
}
if ( controller.grip && inputSource.gripSpace ) {
gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
if ( gripPose !== null ) {
controller.grip.matrix.fromArray( gripPose.transform.matrix );
controller.grip.matrix.decompose( controller.grip.position, controller.grip.rotation, controller.grip.scale );
}
}
}
if ( controller.targetRay ) {
controller.targetRay.visible = inputPose !== null;
}
if ( controller.grip ) {
controller.grip.visible = gripPose !== null;
}
controller.update( inputSource, frame, referenceSpace );
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册