WebXRControllerTeleportation.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. import { IWebXRFeature, WebXRFeaturesManager, WebXRFeatureName } from '../webXRFeaturesManager';
  2. import { Observer } from '../../../Misc/observable';
  3. import { WebXRSessionManager } from '../webXRSessionManager';
  4. import { Nullable } from '../../../types';
  5. import { WebXRInput } from '../webXRInput';
  6. import { WebXRController } from '../webXRController';
  7. import { WebXRControllerComponent, IWebXRMotionControllerAxesValue } from '../motionController/webXRControllerComponent';
  8. import { AbstractMesh } from '../../../Meshes/abstractMesh';
  9. import { Vector3, Quaternion } from '../../../Maths/math.vector';
  10. import { Ray } from '../../../Culling/ray';
  11. import { Material } from '../../../Materials/material';
  12. import { DynamicTexture } from '../../../Materials/Textures/dynamicTexture';
  13. import { CylinderBuilder } from '../../../Meshes/Builders/cylinderBuilder';
  14. import { SineEase, EasingFunction } from '../../../Animations/easing';
  15. import { Animation } from '../../../Animations/animation';
  16. import { Axis } from '../../../Maths/math.axis';
  17. import { StandardMaterial } from '../../../Materials/standardMaterial';
  18. import { GroundBuilder } from '../../../Meshes/Builders/groundBuilder';
  19. import { TorusBuilder } from '../../../Meshes/Builders/torusBuilder';
  20. import { PickingInfo } from '../../../Collisions/pickingInfo';
  21. import { Curve3 } from '../../../Maths/math.path';
  22. import { LinesBuilder } from '../../../Meshes/Builders/linesBuilder';
  23. import { WebXRAbstractFeature } from './WebXRAbstractFeature';
  24. import { Color3 } from '../../../Maths/math.color';
  25. /**
  26. * The options container for the teleportation module
  27. */
  28. export interface IWebXRTeleportationOptions {
  29. /**
  30. * Babylon XR Input class for controller
  31. */
  32. xrInput: WebXRInput;
  33. /**
  34. * A list of meshes to use as floor meshes.
  35. * Meshes can be added and removed after initializing the feature using the
  36. * addFloorMesh and removeFloorMesh functions
  37. * If empty, rotation will still work
  38. */
  39. floorMeshes?: AbstractMesh[];
  40. /**
  41. * Provide your own teleportation mesh instead of babylon's wonderful doughnut.
  42. * If you want to support rotation, make sure your mesh has a direction indicator.
  43. *
  44. * When left untouched, the default mesh will be initialized.
  45. */
  46. teleportationTargetMesh?: AbstractMesh;
  47. /**
  48. * Values to configure the default target mesh
  49. */
  50. defaultTargetMeshOptions?: {
  51. /**
  52. * Fill color of the teleportation area
  53. */
  54. teleportationFillColor?: string;
  55. /**
  56. * Border color for the teleportation area
  57. */
  58. teleportationBorderColor?: string;
  59. /**
  60. * Disable the mesh's animation sequence
  61. */
  62. disableAnimation?: boolean;
  63. /**
  64. * Disable lighting on the material or the ring and arrow
  65. */
  66. disableLighting?: boolean;
  67. /**
  68. * Override the default material of the torus and arrow
  69. */
  70. torusArrowMaterial?: Material;
  71. };
  72. /**
  73. * Disable using the thumbstick and use the main component (usuallly trigger) on long press.
  74. * This will be automatically true if the controller doesnt have a thumbstick or touchpad.
  75. */
  76. useMainComponentOnly?: boolean;
  77. /**
  78. * If main component is used (no thumbstick), how long should the "long press" take before teleporting
  79. */
  80. timeToTeleport?: number;
  81. }
  82. /**
  83. * This is a teleportation feature to be used with webxr-enabled motion controllers.
  84. * When enabled and attached, the feature will allow a user to move aroundand rotate in the scene using
  85. * the input of the attached controllers.
  86. */
  87. export class WebXRMotionControllerTeleportation extends WebXRAbstractFeature {
  88. /**
  89. * The module's name
  90. */
  91. public static readonly Name = WebXRFeatureName.TELEPORTATION;
  92. /**
  93. * The (Babylon) version of this module.
  94. * This is an integer representing the implementation version.
  95. * This number does not correspond to the webxr specs version
  96. */
  97. public static readonly Version = 1;
  98. /**
  99. * Is rotation enabled when moving forward?
  100. * Disabling this feature will prevent the user from deciding the direction when teleporting
  101. */
  102. public rotationEnabled: boolean = true;
  103. /**
  104. * Should the module support parabolic ray on top of direct ray
  105. * If enabled, the user will be able to point "at the sky" and move according to predefined radius distance
  106. * Very helpful when moving between floors / different heights
  107. */
  108. public parabolicRayEnabled: boolean = true;
  109. /**
  110. * The distance from the user to the inspection point in the direction of the controller
  111. * A higher number will allow the user to move further
  112. * defaults to 5 (meters, in xr units)
  113. */
  114. public parabolicCheckRadius: number = 5;
  115. /**
  116. * How much rotation should be applied when rotating right and left
  117. */
  118. public rotationAngle: number = Math.PI / 8;
  119. /**
  120. * Is movement backwards enabled
  121. */
  122. public backwardsMovementEnabled = true;
  123. /**
  124. * Distance to travel when moving backwards
  125. */
  126. public backwardsTeleportationDistance: number = 0.7;
  127. /**
  128. * Add a new mesh to the floor meshes array
  129. * @param mesh the mesh to use as floor mesh
  130. */
  131. public addFloorMesh(mesh: AbstractMesh) {
  132. this._floorMeshes.push(mesh);
  133. }
  134. /**
  135. * Remove a mesh from the floor meshes array
  136. * @param mesh the mesh to remove
  137. */
  138. public removeFloorMesh(mesh: AbstractMesh) {
  139. const index = this._floorMeshes.indexOf(mesh);
  140. if (index !== -1) {
  141. this._floorMeshes.splice(index, 1);
  142. }
  143. }
  144. /**
  145. * Remove a mesh from the floor meshes array using its name
  146. * @param name the mesh name to remove
  147. */
  148. public removeFloorMeshByName(name: string) {
  149. const mesh = this._xrSessionManager.scene.getMeshByName(name);
  150. if (mesh) {
  151. this.removeFloorMesh(mesh);
  152. }
  153. }
  154. private _tmpRay = new Ray(new Vector3(), new Vector3());
  155. private _tmpVector = new Vector3();
  156. private _floorMeshes: AbstractMesh[];
  157. private _controllers: {
  158. [controllerUniqueId: string]: {
  159. xrController: WebXRController;
  160. teleportationComponent?: WebXRControllerComponent;
  161. teleportationState: {
  162. forward: boolean;
  163. backwards: boolean;
  164. currentRotation: number;
  165. baseRotation: number;
  166. rotating: boolean;
  167. }
  168. onAxisChangedObserver?: Nullable<Observer<IWebXRMotionControllerAxesValue>>;
  169. onButtonChangedObserver?: Nullable<Observer<WebXRControllerComponent>>;
  170. };
  171. } = {};
  172. /**
  173. * constructs a new anchor system
  174. * @param _xrSessionManager an instance of WebXRSessionManager
  175. * @param _options configuration object for this feature
  176. */
  177. constructor(_xrSessionManager: WebXRSessionManager, private _options: IWebXRTeleportationOptions) {
  178. super(_xrSessionManager);
  179. // create default mesh if not provided
  180. if (!this._options.teleportationTargetMesh) {
  181. this.createDefaultTargetMesh();
  182. }
  183. this._floorMeshes = this._options.floorMeshes || [];
  184. this.setTargetMeshVisibility(false);
  185. }
  186. private _selectionFeature: IWebXRFeature;
  187. /**
  188. * This function sets a selection feature that will be disabled when
  189. * the forward ray is shown and will be reattached when hidden.
  190. * This is used to remove the selection rays when moving.
  191. * @param selectionFeature the feature to disable when forward movement is enabled
  192. */
  193. public setSelectionFeature(selectionFeature: IWebXRFeature) {
  194. this._selectionFeature = selectionFeature;
  195. }
  196. public attach(): boolean {
  197. if (!super.attach()) {
  198. return false;
  199. }
  200. this._options.xrInput.controllers.forEach(this._attachController);
  201. this._addNewAttachObserver(this._options.xrInput.onControllerAddedObservable, this._attachController);
  202. this._addNewAttachObserver(this._options.xrInput.onControllerRemovedObservable, (controller) => {
  203. // REMOVE the controller
  204. this._detachController(controller.uniqueId);
  205. });
  206. return true;
  207. }
  208. public detach(): boolean {
  209. if (!super.detach()) {
  210. return false;
  211. }
  212. Object.keys(this._controllers).forEach((controllerId) => {
  213. this._detachController(controllerId);
  214. });
  215. this.setTargetMeshVisibility(false);
  216. return true;
  217. }
  218. public dispose(): void {
  219. super.dispose();
  220. this._options.teleportationTargetMesh && this._options.teleportationTargetMesh.dispose(false, true);
  221. }
  222. protected _onXRFrame(_xrFrame: XRFrame) {
  223. const frame = this._xrSessionManager.currentFrame;
  224. const scene = this._xrSessionManager.scene;
  225. if (!this.attach || !frame) { return; }
  226. // render target if needed
  227. const targetMesh = this._options.teleportationTargetMesh;
  228. if (this._currentTeleportationControllerId) {
  229. if (!targetMesh) {
  230. return;
  231. }
  232. targetMesh.rotationQuaternion = targetMesh.rotationQuaternion || new Quaternion();
  233. const controllerData = this._controllers[this._currentTeleportationControllerId];
  234. if (controllerData.teleportationState.forward) {
  235. // set the rotation
  236. Quaternion.RotationYawPitchRollToRef(controllerData.teleportationState.currentRotation + controllerData.teleportationState.baseRotation, 0, 0, targetMesh.rotationQuaternion);
  237. // set the ray and position
  238. let hitPossible = false;
  239. // first check if direct ray possible
  240. controllerData.xrController.getWorldPointerRayToRef(this._tmpRay);
  241. let pick = scene.pickWithRay(this._tmpRay, (o) => {
  242. return this._floorMeshes.indexOf(o) !== -1;
  243. });
  244. if (pick && pick.pickedPoint) {
  245. hitPossible = true;
  246. this.setTargetMeshPosition(pick.pickedPoint);
  247. this.setTargetMeshVisibility(true);
  248. this.showParabolicPath(pick);
  249. } else {
  250. if (this.parabolicRayEnabled) {
  251. // check parabolic ray
  252. const radius = this.parabolicCheckRadius;
  253. this._tmpRay.origin.addToRef(this._tmpRay.direction.scale(radius * 2), this._tmpVector);
  254. this._tmpVector.y = this._tmpRay.origin.y;
  255. this._tmpRay.origin.addInPlace(this._tmpRay.direction.scale(radius));
  256. this._tmpVector.subtractToRef(this._tmpRay.origin, this._tmpRay.direction);
  257. this._tmpRay.direction.normalize();
  258. let pick = scene.pickWithRay(this._tmpRay, (o) => {
  259. return this._floorMeshes.indexOf(o) !== -1;
  260. });
  261. if (pick && pick.pickedPoint) {
  262. hitPossible = true;
  263. this.setTargetMeshPosition(pick.pickedPoint);
  264. this.setTargetMeshVisibility(true);
  265. this.showParabolicPath(pick);
  266. }
  267. }
  268. }
  269. // if needed, set visible:
  270. this.setTargetMeshVisibility(hitPossible);
  271. } else {
  272. this.setTargetMeshVisibility(false);
  273. }
  274. } else {
  275. this.setTargetMeshVisibility(false);
  276. }
  277. }
  278. private _currentTeleportationControllerId: string;
  279. private _attachController = (xrController: WebXRController) => {
  280. if (this._controllers[xrController.uniqueId]) {
  281. // already attached
  282. return;
  283. }
  284. this._controllers[xrController.uniqueId] = {
  285. xrController,
  286. teleportationState: {
  287. forward: false,
  288. backwards: false,
  289. rotating: false,
  290. currentRotation: 0,
  291. baseRotation: 0
  292. }
  293. };
  294. const controllerData = this._controllers[xrController.uniqueId];
  295. // motion controller support
  296. xrController.onMotionControllerProfileLoaded.addOnce(() => {
  297. if (xrController.motionController) {
  298. const movementController = xrController.motionController.getComponentOfType(WebXRControllerComponent.THUMBSTICK) || xrController.motionController.getComponentOfType(WebXRControllerComponent.TOUCHPAD);
  299. if (!movementController || this._options.useMainComponentOnly) {
  300. // use trigger to move on long press
  301. const mainComponent = xrController.motionController.getMainComponent();
  302. if (!mainComponent) {
  303. return;
  304. }
  305. controllerData.onButtonChangedObserver = mainComponent.onButtonStateChanged.add(() => {
  306. // did "pressed" changed?
  307. if (mainComponent.changes.pressed) {
  308. if (mainComponent.changes.pressed.current) {
  309. // simulate "forward" thumbstick push
  310. controllerData.teleportationState.forward = true;
  311. this._currentTeleportationControllerId = controllerData.xrController.uniqueId;
  312. controllerData.teleportationState.baseRotation = this._options.xrInput.xrCamera.rotationQuaternion.toEulerAngles().y;
  313. controllerData.teleportationState.currentRotation = 0;
  314. const timeToSelect = this._options.timeToTeleport || 3000;
  315. let timer = 0;
  316. const observer = this._xrSessionManager.onXRFrameObservable.add(() => {
  317. if (!mainComponent.pressed) {
  318. this._xrSessionManager.onXRFrameObservable.remove(observer);
  319. return;
  320. }
  321. timer += this._xrSessionManager.scene.getEngine().getDeltaTime();
  322. if (timer >= timeToSelect && this._currentTeleportationControllerId === controllerData.xrController.uniqueId && controllerData.teleportationState.forward) {
  323. this._teleportForward(xrController.uniqueId);
  324. }
  325. // failsafe
  326. if (timer >= timeToSelect) {
  327. this._xrSessionManager.onXRFrameObservable.remove(observer);
  328. }
  329. });
  330. } else {
  331. controllerData.teleportationState.forward = false;
  332. this._currentTeleportationControllerId = "";
  333. }
  334. }
  335. });
  336. } else {
  337. controllerData.onButtonChangedObserver = movementController.onButtonStateChanged.add(() => {
  338. if (this._currentTeleportationControllerId === controllerData.xrController.uniqueId && controllerData.teleportationState.forward && !movementController.touched) {
  339. this._teleportForward(xrController.uniqueId);
  340. }
  341. });
  342. // use thumbstick (or touchpad if thumbstick not available)
  343. controllerData.onAxisChangedObserver = movementController.onAxisValueChanged.add((axesData) => {
  344. if (axesData.y <= 0.7 && controllerData.teleportationState.backwards) {
  345. //if (this._currentTeleportationControllerId === controllerData.xrController.uniqueId) {
  346. controllerData.teleportationState.backwards = false;
  347. //this._currentTeleportationControllerId = "";
  348. //}
  349. }
  350. if (axesData.y > 0.7 && !controllerData.teleportationState.forward && this.backwardsMovementEnabled) {
  351. // teleport backwards
  352. if (!controllerData.teleportationState.backwards) {
  353. controllerData.teleportationState.backwards = true;
  354. // teleport backwards ONCE
  355. this._tmpVector.set(0, 0, this.backwardsTeleportationDistance!);
  356. this._tmpVector.rotateByQuaternionToRef(this._options.xrInput.xrCamera.rotationQuaternion!, this._tmpVector);
  357. this._tmpVector.addInPlace(this._options.xrInput.xrCamera.position);
  358. this._options.xrInput.xrCamera.position.subtractToRef(this._tmpVector, this._tmpVector);
  359. this._tmpRay.origin.copyFrom(this._tmpVector);
  360. this._tmpRay.direction.set(0, -1, 0);
  361. let pick = this._xrSessionManager.scene.pickWithRay(this._tmpRay, (o) => {
  362. return this._floorMeshes.indexOf(o) !== -1;
  363. });
  364. // pick must exist, but stay safe
  365. if (pick && pick.pickedPoint) {
  366. // Teleport the users feet to where they targeted
  367. this._options.xrInput.xrCamera.position.addInPlace(pick.pickedPoint);
  368. }
  369. }
  370. }
  371. if (axesData.y < -0.7 && !this._currentTeleportationControllerId && !controllerData.teleportationState.rotating) {
  372. controllerData.teleportationState.forward = true;
  373. this._currentTeleportationControllerId = controllerData.xrController.uniqueId;
  374. controllerData.teleportationState.baseRotation = this._options.xrInput.xrCamera.rotationQuaternion.toEulerAngles().y;
  375. }
  376. if (axesData.x) {
  377. if (!controllerData.teleportationState.forward) {
  378. if (!controllerData.teleportationState.rotating && Math.abs(axesData.x) > 0.7) {
  379. // rotate in the right direction positive is right
  380. controllerData.teleportationState.rotating = true;
  381. const rotation = this.rotationAngle * (axesData.x > 0 ? 1 : -1);
  382. this._options.xrInput.xrCamera.rotationQuaternion.multiplyInPlace(Quaternion.FromEulerAngles(0, rotation, 0));
  383. }
  384. } else {
  385. if (this._currentTeleportationControllerId === controllerData.xrController.uniqueId) {
  386. // set the rotation of the forward movement
  387. if (this.rotationEnabled) {
  388. setTimeout(() => {
  389. controllerData.teleportationState.currentRotation = Math.atan2(axesData.x, -axesData.y);
  390. });
  391. } else {
  392. controllerData.teleportationState.currentRotation = 0;
  393. }
  394. }
  395. }
  396. } else {
  397. controllerData.teleportationState.rotating = false;
  398. }
  399. });
  400. }
  401. }
  402. });
  403. }
  404. private _teleportForward(controllerId: string) {
  405. const controllerData = this._controllers[controllerId];
  406. controllerData.teleportationState.forward = false;
  407. this._currentTeleportationControllerId = "";
  408. // do the movement forward here
  409. if (this._options.teleportationTargetMesh && this._options.teleportationTargetMesh.isVisible) {
  410. const height = this._options.xrInput.xrCamera.position.y - this._options.teleportationTargetMesh.position.y;
  411. this._options.xrInput.xrCamera.position.copyFrom(this._options.teleportationTargetMesh.position);
  412. this._options.xrInput.xrCamera.position.y += height;
  413. this._options.xrInput.xrCamera.rotationQuaternion.multiplyInPlace(Quaternion.FromEulerAngles(0, controllerData.teleportationState.currentRotation, 0));
  414. }
  415. }
  416. private _detachController(xrControllerUniqueId: string) {
  417. const controllerData = this._controllers[xrControllerUniqueId];
  418. if (!controllerData) { return; }
  419. if (controllerData.teleportationComponent) {
  420. if (controllerData.onAxisChangedObserver) {
  421. controllerData.teleportationComponent.onAxisValueChanged.remove(controllerData.onAxisChangedObserver);
  422. }
  423. if (controllerData.onButtonChangedObserver) {
  424. controllerData.teleportationComponent.onButtonStateChanged.remove(controllerData.onButtonChangedObserver);
  425. }
  426. }
  427. // remove from the map
  428. delete this._controllers[xrControllerUniqueId];
  429. }
  430. private createDefaultTargetMesh() {
  431. // set defaults
  432. this._options.defaultTargetMeshOptions = this._options.defaultTargetMeshOptions || {};
  433. const scene = this._xrSessionManager.scene;
  434. let teleportationTarget = GroundBuilder.CreateGround("teleportationTarget", { width: 2, height: 2, subdivisions: 2 }, scene);
  435. teleportationTarget.isPickable = false;
  436. let length = 512;
  437. let dynamicTexture = new DynamicTexture("teleportationPlaneDynamicTexture", length, scene, true);
  438. dynamicTexture.hasAlpha = true;
  439. let context = dynamicTexture.getContext();
  440. let centerX = length / 2;
  441. let centerY = length / 2;
  442. let radius = 200;
  443. context.beginPath();
  444. context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  445. context.fillStyle = this._options.defaultTargetMeshOptions.teleportationFillColor || "#444444";
  446. context.fill();
  447. context.lineWidth = 10;
  448. context.strokeStyle = this._options.defaultTargetMeshOptions.teleportationBorderColor || "#FFFFFF";
  449. context.stroke();
  450. context.closePath();
  451. dynamicTexture.update();
  452. const teleportationCircleMaterial = new StandardMaterial("teleportationPlaneMaterial", scene);
  453. teleportationCircleMaterial.diffuseTexture = dynamicTexture;
  454. teleportationTarget.material = teleportationCircleMaterial;
  455. let torus = TorusBuilder.CreateTorus("torusTeleportation", {
  456. diameter: 0.75,
  457. thickness: 0.1,
  458. tessellation: 20
  459. }, scene);
  460. torus.isPickable = false;
  461. torus.parent = teleportationTarget;
  462. if (!this._options.defaultTargetMeshOptions.disableAnimation) {
  463. let animationInnerCircle = new Animation("animationInnerCircle", "position.y", 30, Animation.ANIMATIONTYPE_FLOAT, Animation.ANIMATIONLOOPMODE_CYCLE);
  464. let keys = [];
  465. keys.push({
  466. frame: 0,
  467. value: 0
  468. });
  469. keys.push({
  470. frame: 30,
  471. value: 0.4
  472. });
  473. keys.push({
  474. frame: 60,
  475. value: 0
  476. });
  477. animationInnerCircle.setKeys(keys);
  478. let easingFunction = new SineEase();
  479. easingFunction.setEasingMode(EasingFunction.EASINGMODE_EASEINOUT);
  480. animationInnerCircle.setEasingFunction(easingFunction);
  481. torus.animations = [];
  482. torus.animations.push(animationInnerCircle);
  483. scene.beginAnimation(torus, 0, 60, true);
  484. }
  485. var cone = CylinderBuilder.CreateCylinder("cone", { diameterTop: 0, tessellation: 4 }, scene);
  486. cone.isPickable = false;
  487. cone.scaling.set(0.5, 0.12, 0.2);
  488. cone.rotate(Axis.X, Math.PI / 2);
  489. cone.position.z = 0.6;
  490. cone.parent = torus;
  491. if (this._options.defaultTargetMeshOptions.torusArrowMaterial) {
  492. torus.material = this._options.defaultTargetMeshOptions.torusArrowMaterial;
  493. cone.material = this._options.defaultTargetMeshOptions.torusArrowMaterial;
  494. } else {
  495. const torusConeMaterial = new StandardMaterial("torusConsMat", scene);
  496. torusConeMaterial.disableLighting = !!this._options.defaultTargetMeshOptions.disableLighting;
  497. if (torusConeMaterial.disableLighting) {
  498. torusConeMaterial.emissiveColor = new Color3(0.3, 0.3, 1.0);
  499. } else {
  500. torusConeMaterial.diffuseColor = new Color3(0.3, 0.3, 1.0);
  501. }
  502. torusConeMaterial.alpha = 0.9;
  503. torus.material = torusConeMaterial;
  504. cone.material = torusConeMaterial;
  505. }
  506. this._options.teleportationTargetMesh = teleportationTarget;
  507. }
  508. private setTargetMeshVisibility(visible: boolean) {
  509. if (!this._options.teleportationTargetMesh) { return; }
  510. if (this._options.teleportationTargetMesh.isVisible === visible) { return; }
  511. this._options.teleportationTargetMesh.isVisible = visible;
  512. this._options.teleportationTargetMesh.getChildren(undefined, false).forEach((m) => { (<any>(m)).isVisible = visible; });
  513. if (!visible) {
  514. if (this._quadraticBezierCurve) {
  515. this._quadraticBezierCurve.dispose();
  516. }
  517. if (this._selectionFeature) {
  518. this._selectionFeature.attach();
  519. }
  520. } else {
  521. if (this._selectionFeature) {
  522. this._selectionFeature.detach();
  523. }
  524. }
  525. }
  526. private setTargetMeshPosition(newPosition: Vector3) {
  527. if (!this._options.teleportationTargetMesh) { return; }
  528. this._options.teleportationTargetMesh.position.copyFrom(newPosition);
  529. this._options.teleportationTargetMesh.position.y += 0.01;
  530. }
  531. private _quadraticBezierCurve: AbstractMesh;
  532. private showParabolicPath(pickInfo: PickingInfo) {
  533. if (!pickInfo.pickedPoint) { return; }
  534. const controllerData = this._controllers[this._currentTeleportationControllerId];
  535. const quadraticBezierVectors = Curve3.CreateQuadraticBezier(
  536. controllerData.xrController.pointer.absolutePosition,
  537. pickInfo.ray!.origin,
  538. pickInfo.pickedPoint,
  539. 25);
  540. if (this._quadraticBezierCurve) {
  541. this._quadraticBezierCurve.dispose();
  542. }
  543. this._quadraticBezierCurve = LinesBuilder.CreateLines("path line", { points: quadraticBezierVectors.getPoints() });
  544. this._quadraticBezierCurve.isPickable = false;
  545. }
  546. }
  547. WebXRFeaturesManager.AddWebXRFeature(WebXRMotionControllerTeleportation.Name, (xrSessionManager, options) => {
  548. return () => new WebXRMotionControllerTeleportation(xrSessionManager, options);
  549. }, WebXRMotionControllerTeleportation.Version, true);