Home / Modules / Camera / FirstPersonCameraRig

FirstPersonCameraRig

Pure logic

Follows a target eye position and current forward direction to produce actor-locked first-person view motion.

CategoryCamera
Dependency tierPure logic
Related modulesnone
Demo scenecamera-gallery · Camera rigs ↓
Take just this module
modules/camera/FirstPersonCameraRig.js

Copy it together with its internal dependencies, preserving the relative directory structure.

Camera rigs

three

Auto-driving showcase — no controls needed.

Source

import { BaseCameraRig, CAMERA_HEIGHT_SOURCES, CAMERA_ROTATION_MODES } from './BaseCameraRig.js';
import { toVec3 } from '../math/Vector3Utils.js';
import { DEFAULT_WORLD_BASIS } from '../math/WorldBasis.js';

export class FirstPersonCameraRig extends BaseCameraRig {
  constructor({
    eyeHeight = 1.72,
    lookDistance = 1,
    heightVectorSource = CAMERA_HEIGHT_SOURCES.frameUp,
    basis = DEFAULT_WORLD_BASIS,
  }) {
    super({ basis, rotationMode: CAMERA_ROTATION_MODES.lookAt });
    this.eyeHeight = eyeHeight;
    this.lookDistance = lookDistance;
    this.heightVectorSource = heightVectorSource;
  }

  step({
    targetPosition,
    targetFrame,
    camera = null,
  }) {
    const frame = this.resolveTargetFrame(targetFrame);
    const heightVector = this.vectorFromSource(this.heightVectorSource, frame);
    const position = toVec3(targetPosition)
      .addScaledVector(heightVector, this.eyeHeight);

    this.setLookAtPose({
      position,
      lookAt: position.clone().addScaledVector(frame.forward, this.lookDistance),
      up: frame.up,
    });

    const pose = this.getPose();
    this.applyToCamera(camera, pose);
    return pose;
  }
}