UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

37 lines (36 loc) 1.26 kB
"use strict"; import { EventDispatcher, Ray } from "three"; import { BASE_XR_SESSION_EVENT_NAMES } from "./Common"; import { removeFromParent } from "../../engine/poly/PolyOnObjectsAddRemoveHooksController"; export function webXRControllerName(controllerIndex) { return `webXRController-${controllerIndex}`; } export class CoreWebXRControllerContainer extends EventDispatcher { constructor(scene, renderer, index) { super(); this.scene = scene; this.renderer = renderer; this.index = index; this.ray = new Ray(); this.controller = this.renderer.xr.getController(this.index); for (const eventName of BASE_XR_SESSION_EVENT_NAMES) { const event = { type: eventName, controllerContainer: this }; this.controller.addEventListener(eventName, (controllerEvent) => { this.dispatchEvent(event); }); } this.controller.name = webXRControllerName(this.index); } initialize(camera) { if (camera == null) { removeFromParent(this.scene, this.controller); return; } const cameraParent = camera.parent; if (cameraParent) { cameraParent.add(this.controller); } else { console.warn("camera has no parent, webXR controls cannot be added to the scene"); } } }