How to use the troika-core.utils.createClassExtender function in troika-core

To help you get started, we’ve selected a few troika-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github protectwise / troika / packages / troika-xr / src / facade / XRCameraFacade.js View on Github external
import { Matrix4, PerspectiveCamera, Vector4, Vector3, Quaternion } from 'three'
import { PerspectiveCamera3DFacade } from 'troika-3d'
import { utils } from 'troika-core'

const tempVec3 = new Vector3()
const tempQuat = new Quaternion()
const dummyObj = {}
const tempMat4 = new Matrix4()

function extendAsXRCamera(BaseCameraFacadeClass) {
  return doExtendAsXRCamera(BaseCameraFacadeClass || PerspectiveCamera3DFacade)
}

const doExtendAsXRCamera = utils.createClassExtender('xrCamera', function(BaseCameraFacadeClass) {
  return class XRCameraFacade extends BaseCameraFacadeClass {
    constructor(parent) {
      super(parent)

      // Required props
      this.xrSession = null
      this.xrReferenceSpace = null

      // This will behave like an ArrayCamera with a sub-camera for each view.
      // The individual view cameras will be created as needed based on the xrFrame's pose views.
      const mainCam = this.threeObject
      mainCam.isArrayCamera = true
      mainCam.cameras = []

      // Expose the camera's configured position/orientation as an offset XRReferenceSpace.
      this.offsetReferenceSpace = null
github protectwise / troika / packages / troika-xr / src / webvr-deprecated / facade / VrCamera.js View on Github external
* 2) It's sometimes useful for code elsewhere in the scene to be aware of the pose
 *    transformation. For example, `Camera3DFacade.getFrustum()` is able to match the
 *    user's actual posed view, which it wouldn't be able to otherwise.
 */

import { Matrix4, PerspectiveCamera, Quaternion, Ray, Raycaster, Vector2, Vector3, Vector4 } from 'three'
import { utils } from 'troika-core'

const tempMat4 = new Matrix4()
const tempQuat = new Quaternion()
const tempVec2 = new Vector2()
const tempVec3 = new Vector3()
const tempRaycaster = new Raycaster()


export const extendAsVrCamera = utils.createClassExtender('vrCamera', function(BaseCamFacadeClass) {
  class VrCamera extends BaseCamFacadeClass {
    constructor(parent) {
      super(parent)

      // Make it behave like an ArrayCamera with left/right eye cameras
      const leftCam = new PerspectiveCamera()
      leftCam.viewport = new Vector4()

      const rightCam = new PerspectiveCamera()
      rightCam.viewport = new Vector4()

      const mainCam = this.threeObject
      mainCam.isArrayCamera = true
      mainCam.cameras = [leftCam, rightCam]

      // Match the ThreeJS convention of letting objects use layer masks 1 and 2 to