Definition: State
State 类型定义如下(继承自 Pose):
import * as THREE from "three";
import { Mode } from "@realsee/five";
interface Pose {
/** 相机偏航角 (水平方向),单位:弧度 */
longitude: number;
/** 相机俯仰角 (垂直方向),单位:弧度 */
latitude: number;
/** 相机垂直视场角,单位:度 */
fov: number;
/** 相机看向的目标中心点坐标 */
offset: THREE.Vector3;
/** 相机距离目标中心点的距离 */
distance: number;
}
interface State extends Pose {
/** 显示模式 */
mode: Mode;
/** 当前点位的唯一标识 (workCode) */
workCode: string;
/** 当前点位在列表中的索引 */
panoIndex: number;
}
offset 为中心、distance 为半径的球面上。offset 的距离。distance 通常为 0,即相机位置与 offset 重合(第一人称视角)。offset 通常位于模型中心,distance 大于模型包围盒半径(第三人称上帝视角)。setState 或用户交互结束时,此值即为最终结果。通常用于 UI 同步(如高亮当前模式按钮)。currentState 会不断变化直至追赶上 state。通常用于需要实时反馈的场景(如罗盘旋转)。使用 setState 更新状态:
// 切换到户型图模式,并旋转相机到 45 度
five.setState({
mode: 'Floorplan',
longitude: Math.PI / 4
});
获取当前状态并基于此修改:
const state = five.state;
// 在当前状态基础上,向右旋转 90 度,并略微拉远距离
five.setState({
longitude: state.longitude + Math.PI / 2,
distance: state.distance + 2.0
});
longitude 和 latitude 使用弧度,而 fov 使用角度(度)。offset 必须是 THREE.Vector3 实例。如果在 React 等框架中使用纯对象(Plain Object),Five 内部会自动转换,但在 TypeScript 中需注意类型匹配。setState 触发的相机运动是异步动画过程。如果需要立即无动画跳转,可传递第二个参数 immediately: true。State 中的 offset 是 THREE.Vector3 实例,不能直接 JSON.stringify。保存时需手动提取 { x, y, z },恢复时需重新构造 new THREE.Vector3(x, y, z)。// 保存
const snapshot = {
...five.state,
offset: { x: five.state.offset.x, y: five.state.offset.y, z: five.state.offset.z }
};
localStorage.setItem('fiveState', JSON.stringify(snapshot));
// 恢复
const saved = JSON.parse(localStorage.getItem('fiveState')!);
five.setState({
...saved,
offset: new THREE.Vector3(saved.offset.x, saved.offset.y, saved.offset.z)
}, true);
setState / getCurrentState 的宿主。tags: [状态保存, 状态恢复, 场景还原, 快照, camera, state, pose, interaction, save, restore, snapshot]