浏览代码

better version of forward ray

Raanan Weber 5 年之前
父节点
当前提交
2eb051a937
共有 1 个文件被更改,包括 8 次插入6 次删除
  1. 8 6
      src/XR/webXRInputSource.ts

+ 8 - 6
src/XR/webXRInputSource.ts

@@ -37,7 +37,6 @@ export interface IWebXRControllerOptions {
  * Represents an XR controller
  * Represents an XR controller
  */
  */
 export class WebXRInputSource {
 export class WebXRInputSource {
-    private _tmpQuaternion = new Quaternion();
     private _tmpVector = new Vector3();
     private _tmpVector = new Vector3();
     private _uniqueId: string;
     private _uniqueId: string;
 
 
@@ -94,6 +93,8 @@ export class WebXRInputSource {
             this.grip.rotationQuaternion = new Quaternion();
             this.grip.rotationQuaternion = new Quaternion();
         }
         }
 
 
+        this._tmpVector.set(0, 0, (this._scene.useRightHandedSystem ? -1.0 : 1.0));
+
         // for now only load motion controllers if gamepad object available
         // for now only load motion controllers if gamepad object available
         if (this.inputSource.gamepad) {
         if (this.inputSource.gamepad) {
             WebXRMotionControllerManager.GetMotionControllerWithXRInput(inputSource, _scene, this._options.forceControllerProfile).then((motionController) => {
             WebXRMotionControllerManager.GetMotionControllerWithXRInput(inputSource, _scene, this._options.forceControllerProfile).then((motionController) => {
@@ -149,12 +150,13 @@ export class WebXRInputSource {
      */
      */
     public getWorldPointerRayToRef(result: Ray, gripIfAvailable: boolean = false) {
     public getWorldPointerRayToRef(result: Ray, gripIfAvailable: boolean = false) {
         const object = gripIfAvailable && this.grip ? this.grip : this.pointer;
         const object = gripIfAvailable && this.grip ? this.grip : this.pointer;
-        let worldMatrix = object.computeWorldMatrix();
-        worldMatrix.decompose(undefined, this._tmpQuaternion, undefined);
-        this._tmpVector.set(0, 0, (this._scene.useRightHandedSystem ? -1.0 : 1.0));
-        this._tmpVector.rotateByQuaternionToRef(this._tmpQuaternion, this._tmpVector);
+        Vector3.TransformNormalToRef(
+            this._tmpVector,
+            object.getWorldMatrix(),
+            result.direction
+        );
+        result.direction.normalize();
         result.origin.copyFrom(object.absolutePosition);
         result.origin.copyFrom(object.absolutePosition);
-        result.direction.copyFrom(this._tmpVector);
         result.length = 1000;
         result.length = 1000;
     }
     }