WebXRControllerTeleportation.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. // pick grounds that are LOWER only. upper will use parabolic path
  242. let pick = scene.pickWithRay(this._tmpRay, (o) => {
  243. const index = this._floorMeshes.indexOf(o);
  244. if (index === -1) { return false; }
  245. return (this._floorMeshes[index].absolutePosition.y < this._options.xrInput.xrCamera.position.y);
  246. });
  247. if (pick && pick.pickedPoint) {
  248. hitPossible = true;
  249. this.setTargetMeshPosition(pick.pickedPoint);
  250. this.setTargetMeshVisibility(true);
  251. this._showParabolicPath(pick);
  252. } else {
  253. if (this.parabolicRayEnabled) {
  254. // radius compensation according to pointer rotation around X
  255. const xRotation = controllerData.xrController.pointer.rotationQuaternion!.toEulerAngles().x;
  256. const compensation = (1 + ((Math.PI / 2) - Math.abs(xRotation)));
  257. // check parabolic ray
  258. const radius = this.parabolicCheckRadius * compensation;
  259. this._tmpRay.origin.addToRef(this._tmpRay.direction.scale(radius * 2), this._tmpVector);
  260. this._tmpVector.y = this._tmpRay.origin.y;
  261. this._tmpRay.origin.addInPlace(this._tmpRay.direction.scale(radius));
  262. this._tmpVector.subtractToRef(this._tmpRay.origin, this._tmpRay.direction);
  263. this._tmpRay.direction.normalize();
  264. let pick = scene.pickWithRay(this._tmpRay, (o) => {
  265. return this._floorMeshes.indexOf(o) !== -1;
  266. });
  267. if (pick && pick.pickedPoint) {
  268. hitPossible = true;
  269. this.setTargetMeshPosition(pick.pickedPoint);
  270. this.setTargetMeshVisibility(true);
  271. this._showParabolicPath(pick);
  272. }
  273. }
  274. }
  275. // if needed, set visible:
  276. this.setTargetMeshVisibility(hitPossible);
  277. } else {
  278. this.setTargetMeshVisibility(false);
  279. }
  280. } else {
  281. this.setTargetMeshVisibility(false);
  282. }
  283. }
  284. private _currentTeleportationControllerId: string;
  285. private _attachController = (xrController: WebXRController) => {
  286. if (this._controllers[xrController.uniqueId]) {
  287. // already attached
  288. return;
  289. }
  290. this._controllers[xrController.uniqueId] = {
  291. xrController,
  292. teleportationState: {
  293. forward: false,
  294. backwards: false,
  295. rotating: false,
  296. currentRotation: 0,
  297. baseRotation: 0
  298. }
  299. };
  300. const controllerData = this._controllers[xrController.uniqueId];
  301. // motion controller support
  302. xrController.onMotionControllerInitObservable.addOnce(() => {
  303. if (xrController.motionController) {
  304. const movementController = xrController.motionController.getComponentOfType(WebXRControllerComponent.THUMBSTICK) || xrController.motionController.getComponentOfType(WebXRControllerComponent.TOUCHPAD);
  305. if (!movementController || this._options.useMainComponentOnly) {
  306. // use trigger to move on long press
  307. const mainComponent = xrController.motionController.getMainComponent();
  308. if (!mainComponent) {
  309. return;
  310. }
  311. controllerData.onButtonChangedObserver = mainComponent.onButtonStateChanged.add(() => {
  312. // did "pressed" changed?
  313. if (mainComponent.changes.pressed) {
  314. if (mainComponent.changes.pressed.current) {
  315. // simulate "forward" thumbstick push
  316. controllerData.teleportationState.forward = true;
  317. this._currentTeleportationControllerId = controllerData.xrController.uniqueId;
  318. controllerData.teleportationState.baseRotation = this._options.xrInput.xrCamera.rotationQuaternion.toEulerAngles().y;
  319. controllerData.teleportationState.currentRotation = 0;
  320. const timeToSelect = this._options.timeToTeleport || 3000;
  321. let timer = 0;
  322. const observer = this._xrSessionManager.onXRFrameObservable.add(() => {
  323. if (!mainComponent.pressed) {
  324. this._xrSessionManager.onXRFrameObservable.remove(observer);
  325. return;
  326. }
  327. timer += this._xrSessionManager.scene.getEngine().getDeltaTime();
  328. if (timer >= timeToSelect && this._currentTeleportationControllerId === controllerData.xrController.uniqueId && controllerData.teleportationState.forward) {
  329. this._teleportForward(xrController.uniqueId);
  330. }
  331. // failsafe
  332. if (timer >= timeToSelect) {
  333. this._xrSessionManager.onXRFrameObservable.remove(observer);
  334. }
  335. });
  336. } else {
  337. controllerData.teleportationState.forward = false;
  338. this._currentTeleportationControllerId = "";
  339. }
  340. }
  341. });
  342. } else {
  343. controllerData.onButtonChangedObserver = movementController.onButtonStateChanged.add(() => {
  344. if (this._currentTeleportationControllerId === controllerData.xrController.uniqueId && controllerData.teleportationState.forward && !movementController.touched) {
  345. this._teleportForward(xrController.uniqueId);
  346. }
  347. });
  348. // use thumbstick (or touchpad if thumbstick not available)
  349. controllerData.onAxisChangedObserver = movementController.onAxisValueChanged.add((axesData) => {
  350. if (axesData.y <= 0.7 && controllerData.teleportationState.backwards) {
  351. //if (this._currentTeleportationControllerId === controllerData.xrController.uniqueId) {
  352. controllerData.teleportationState.backwards = false;
  353. //this._currentTeleportationControllerId = "";
  354. //}
  355. }
  356. if (axesData.y > 0.7 && !controllerData.teleportationState.forward && this.backwardsMovementEnabled) {
  357. // teleport backwards
  358. if (!controllerData.teleportationState.backwards) {
  359. controllerData.teleportationState.backwards = true;
  360. // teleport backwards ONCE
  361. this._tmpVector.set(0, 0, this.backwardsTeleportationDistance!);
  362. this._tmpVector.rotateByQuaternionToRef(this._options.xrInput.xrCamera.rotationQuaternion!, this._tmpVector);
  363. this._tmpVector.addInPlace(this._options.xrInput.xrCamera.position);
  364. this._options.xrInput.xrCamera.position.subtractToRef(this._tmpVector, this._tmpVector);
  365. this._tmpRay.origin.copyFrom(this._tmpVector);
  366. this._tmpRay.direction.set(0, -1, 0);
  367. let pick = this._xrSessionManager.scene.pickWithRay(this._tmpRay, (o) => {
  368. return this._floorMeshes.indexOf(o) !== -1;
  369. });
  370. // pick must exist, but stay safe
  371. if (pick && pick.pickedPoint) {
  372. // Teleport the users feet to where they targeted
  373. this._options.xrInput.xrCamera.position.addInPlace(pick.pickedPoint);
  374. }
  375. }
  376. }
  377. if (axesData.y < -0.7 && !this._currentTeleportationControllerId && !controllerData.teleportationState.rotating) {
  378. controllerData.teleportationState.forward = true;
  379. this._currentTeleportationControllerId = controllerData.xrController.uniqueId;
  380. controllerData.teleportationState.baseRotation = this._options.xrInput.xrCamera.rotationQuaternion.toEulerAngles().y;
  381. }
  382. if (axesData.x) {
  383. if (!controllerData.teleportationState.forward) {
  384. if (!controllerData.teleportationState.rotating && Math.abs(axesData.x) > 0.7) {
  385. // rotate in the right direction positive is right
  386. controllerData.teleportationState.rotating = true;
  387. const rotation = this.rotationAngle * (axesData.x > 0 ? 1 : -1);
  388. this._options.xrInput.xrCamera.rotationQuaternion.multiplyInPlace(Quaternion.FromEulerAngles(0, rotation, 0));
  389. }
  390. } else {
  391. if (this._currentTeleportationControllerId === controllerData.xrController.uniqueId) {
  392. // set the rotation of the forward movement
  393. if (this.rotationEnabled) {
  394. setTimeout(() => {
  395. controllerData.teleportationState.currentRotation = Math.atan2(axesData.x, -axesData.y);
  396. });
  397. } else {
  398. controllerData.teleportationState.currentRotation = 0;
  399. }
  400. }
  401. }
  402. } else {
  403. controllerData.teleportationState.rotating = false;
  404. }
  405. });
  406. }
  407. }
  408. });
  409. }
  410. private _teleportForward(controllerId: string) {
  411. const controllerData = this._controllers[controllerId];
  412. controllerData.teleportationState.forward = false;
  413. this._currentTeleportationControllerId = "";
  414. // do the movement forward here
  415. if (this._options.teleportationTargetMesh && this._options.teleportationTargetMesh.isVisible) {
  416. const height = this._options.xrInput.xrCamera.realWorldHeight;
  417. this._options.xrInput.xrCamera.position.copyFrom(this._options.teleportationTargetMesh.position);
  418. this._options.xrInput.xrCamera.position.y += height;
  419. this._options.xrInput.xrCamera.rotationQuaternion.multiplyInPlace(Quaternion.FromEulerAngles(0, controllerData.teleportationState.currentRotation, 0));
  420. }
  421. }
  422. private _detachController(xrControllerUniqueId: string) {
  423. const controllerData = this._controllers[xrControllerUniqueId];
  424. if (!controllerData) { return; }
  425. if (controllerData.teleportationComponent) {
  426. if (controllerData.onAxisChangedObserver) {
  427. controllerData.teleportationComponent.onAxisValueChanged.remove(controllerData.onAxisChangedObserver);
  428. }
  429. if (controllerData.onButtonChangedObserver) {
  430. controllerData.teleportationComponent.onButtonStateChanged.remove(controllerData.onButtonChangedObserver);
  431. }
  432. }
  433. // remove from the map
  434. delete this._controllers[xrControllerUniqueId];
  435. }
  436. private createDefaultTargetMesh() {
  437. // set defaults
  438. this._options.defaultTargetMeshOptions = this._options.defaultTargetMeshOptions || {};
  439. const scene = this._xrSessionManager.scene;
  440. let teleportationTarget = GroundBuilder.CreateGround("teleportationTarget", { width: 2, height: 2, subdivisions: 2 }, scene);
  441. teleportationTarget.isPickable = false;
  442. let length = 512;
  443. let dynamicTexture = new DynamicTexture("teleportationPlaneDynamicTexture", length, scene, true);
  444. dynamicTexture.hasAlpha = true;
  445. let context = dynamicTexture.getContext();
  446. let centerX = length / 2;
  447. let centerY = length / 2;
  448. let radius = 200;
  449. context.beginPath();
  450. context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  451. context.fillStyle = this._options.defaultTargetMeshOptions.teleportationFillColor || "#444444";
  452. context.fill();
  453. context.lineWidth = 10;
  454. context.strokeStyle = this._options.defaultTargetMeshOptions.teleportationBorderColor || "#FFFFFF";
  455. context.stroke();
  456. context.closePath();
  457. dynamicTexture.update();
  458. const teleportationCircleMaterial = new StandardMaterial("teleportationPlaneMaterial", scene);
  459. teleportationCircleMaterial.diffuseTexture = dynamicTexture;
  460. teleportationTarget.material = teleportationCircleMaterial;
  461. let torus = TorusBuilder.CreateTorus("torusTeleportation", {
  462. diameter: 0.75,
  463. thickness: 0.1,
  464. tessellation: 20
  465. }, scene);
  466. torus.isPickable = false;
  467. torus.parent = teleportationTarget;
  468. if (!this._options.defaultTargetMeshOptions.disableAnimation) {
  469. let animationInnerCircle = new Animation("animationInnerCircle", "position.y", 30, Animation.ANIMATIONTYPE_FLOAT, Animation.ANIMATIONLOOPMODE_CYCLE);
  470. let keys = [];
  471. keys.push({
  472. frame: 0,
  473. value: 0
  474. });
  475. keys.push({
  476. frame: 30,
  477. value: 0.4
  478. });
  479. keys.push({
  480. frame: 60,
  481. value: 0
  482. });
  483. animationInnerCircle.setKeys(keys);
  484. let easingFunction = new SineEase();
  485. easingFunction.setEasingMode(EasingFunction.EASINGMODE_EASEINOUT);
  486. animationInnerCircle.setEasingFunction(easingFunction);
  487. torus.animations = [];
  488. torus.animations.push(animationInnerCircle);
  489. scene.beginAnimation(torus, 0, 60, true);
  490. }
  491. var cone = CylinderBuilder.CreateCylinder("cone", { diameterTop: 0, tessellation: 4 }, scene);
  492. cone.isPickable = false;
  493. cone.scaling.set(0.5, 0.12, 0.2);
  494. cone.rotate(Axis.X, Math.PI / 2);
  495. cone.position.z = 0.6;
  496. cone.parent = torus;
  497. if (this._options.defaultTargetMeshOptions.torusArrowMaterial) {
  498. torus.material = this._options.defaultTargetMeshOptions.torusArrowMaterial;
  499. cone.material = this._options.defaultTargetMeshOptions.torusArrowMaterial;
  500. } else {
  501. const torusConeMaterial = new StandardMaterial("torusConsMat", scene);
  502. torusConeMaterial.disableLighting = !!this._options.defaultTargetMeshOptions.disableLighting;
  503. if (torusConeMaterial.disableLighting) {
  504. torusConeMaterial.emissiveColor = new Color3(0.3, 0.3, 1.0);
  505. } else {
  506. torusConeMaterial.diffuseColor = new Color3(0.3, 0.3, 1.0);
  507. }
  508. torusConeMaterial.alpha = 0.9;
  509. torus.material = torusConeMaterial;
  510. cone.material = torusConeMaterial;
  511. }
  512. this._options.teleportationTargetMesh = teleportationTarget;
  513. }
  514. private setTargetMeshVisibility(visible: boolean) {
  515. if (!this._options.teleportationTargetMesh) { return; }
  516. if (this._options.teleportationTargetMesh.isVisible === visible) { return; }
  517. this._options.teleportationTargetMesh.isVisible = visible;
  518. this._options.teleportationTargetMesh.getChildren(undefined, false).forEach((m) => { (<any>(m)).isVisible = visible; });
  519. if (!visible) {
  520. if (this._quadraticBezierCurve) {
  521. this._quadraticBezierCurve.dispose();
  522. }
  523. if (this._selectionFeature) {
  524. this._selectionFeature.attach();
  525. }
  526. } else {
  527. if (this._selectionFeature) {
  528. this._selectionFeature.detach();
  529. }
  530. }
  531. }
  532. private setTargetMeshPosition(newPosition: Vector3) {
  533. if (!this._options.teleportationTargetMesh) { return; }
  534. this._options.teleportationTargetMesh.position.copyFrom(newPosition);
  535. this._options.teleportationTargetMesh.position.y += 0.01;
  536. }
  537. private _quadraticBezierCurve: AbstractMesh;
  538. private _showParabolicPath(pickInfo: PickingInfo) {
  539. if (!pickInfo.pickedPoint) { return; }
  540. const controllerData = this._controllers[this._currentTeleportationControllerId];
  541. const quadraticBezierVectors = Curve3.CreateQuadraticBezier(
  542. controllerData.xrController.pointer.absolutePosition,
  543. pickInfo.ray!.origin,
  544. pickInfo.pickedPoint,
  545. 25);
  546. if (this._quadraticBezierCurve) {
  547. this._quadraticBezierCurve.dispose();
  548. }
  549. this._quadraticBezierCurve = LinesBuilder.CreateLines("path line", { points: quadraticBezierVectors.getPoints() });
  550. this._quadraticBezierCurve.isPickable = false;
  551. }
  552. }
  553. WebXRFeaturesManager.AddWebXRFeature(WebXRMotionControllerTeleportation.Name, (xrSessionManager, options) => {
  554. return () => new WebXRMotionControllerTeleportation(xrSessionManager, options);
  555. }, WebXRMotionControllerTeleportation.Version, true);