All files / src/components/VirtualWalkthrough xr-pointer-ray.ts

0% Statements 0/13
0% Branches 0/4
0% Functions 0/1
0% Lines 0/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                                                           
import { Color, Script, Vec3 } from 'playcanvas';
 
/** Length of the drawn pointer ray, in world meters. */
const RAY_LENGTH = 5;
 
/** Default ray colour (cyan) — bright enough to read against the scene. */
export const DEFAULT_RAY_COLOR = new Color(0.25, 0.8, 1);
 
export class XrPointerRay extends Script {
  static scriptName = 'xrPointerRay';
 
  color = DEFAULT_RAY_COLOR.clone();
 
  private _end = new Vec3();
 
  update() {
    if (this.app.xr?.active !== true) {
      return;
    }
 
    const sources = this.app.xr?.input?.inputSources ?? [];
    for (const source of sources) {
      const origin = source.getOrigin();
      const direction = source.getDirection();
      this._end.copy(direction).mulScalar(RAY_LENGTH).add(origin);
      this.app.drawLine(origin, this._end, this.color, false);
    }
  }
}