62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
|
||
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
|
||
import { Tools } from '@babylonjs/core/Misc/tools';
|
||
import { Monobehiver } from '../base/Monobehiver';
|
||
import { AppConfig } from './AppConfig';
|
||
|
||
/**
|
||
* 相机控制类- 负责创建和控制弧形旋转相机
|
||
*/
|
||
export class AppCamera extends Monobehiver {
|
||
object: ArcRotateCamera | null;
|
||
|
||
constructor(mainApp: any) {
|
||
super(mainApp);
|
||
this.object = null;
|
||
}
|
||
|
||
/** 初始化相机 */
|
||
Awake(): void {
|
||
const scene = this.mainApp.appScene.object;
|
||
const canvas = AppConfig.container;
|
||
if (!scene || !canvas) return;
|
||
|
||
// 创建弧形旋转相机:水平角70度,垂直角85度(接近上帝视角),距离5,目标点(0,2,0)
|
||
this.object = new ArcRotateCamera('Camera', Tools.ToRadians(70), Tools.ToRadians(85), 5, new Vector3(0, 2, 0), scene);
|
||
this.object.attachControl(canvas, true);
|
||
this.object.minZ = 0.01; // 近裁剪面
|
||
// this.object.wheelPrecision =999999; // 滚轮缩放精度
|
||
this.object.panningSensibility = 0;
|
||
|
||
// 限制垂直角范围,实现上帝视角
|
||
// this.object.upperBetaLimit = Tools.ToRadians(60); // 最大垂直角(接近90度,避免万向锁)
|
||
// this.object.lowerBetaLimit = Tools.ToRadians(60); // 最小垂直角
|
||
|
||
this.object.position = new Vector3(-0, 10, 0);
|
||
this.setTarget(0, 2, 0);
|
||
}
|
||
|
||
/** 设置相机目标点 */
|
||
setTarget(x: number, y: number, z: number): void {
|
||
if (this.object) {
|
||
this.object.target = new Vector3(x, y, z);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/** 重置相机到默认位置 */
|
||
reset(): void {
|
||
if (!this.object) return;
|
||
this.object.radius = 5;
|
||
this.object.alpha = Tools.ToRadians(60); // 水平角
|
||
this.object.beta = Tools.ToRadians(60); // 垂直角(上帝视角)
|
||
this.setTarget(0, 2, 0);
|
||
this.object.position = new Vector3(-0, 100, 0);
|
||
}
|
||
|
||
update(): void {
|
||
|
||
}
|
||
}
|