首页 / 模块目录 / 相机机位 / FirstPersonCameraRig

FirstPersonCameraRig

纯逻辑

跟随目标眼位与当前前向,产生锁定 actor 的第一人称视角。

类别相机机位
依赖档位纯逻辑
相关模块
演示场camera-gallery · 相机机位 ↓
只取这个模块
modules/camera/FirstPersonCameraRig.js

连同其内部依赖一并复制,保留相对目录结构。

相机机位

three

自动行驶展示,无需操作。

源码

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;
  }
}