Files
zhengte.babylonjs-sdk/src/babylonjs/AppPositionGizmo.ts
2026-04-30 14:46:01 +08:00

81 lines
2.2 KiB
TypeScript

import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
import { PositionGizmo } from '@babylonjs/core/Gizmos/positionGizmo';
import { UtilityLayerRenderer } from '@babylonjs/core/Rendering/utilityLayerRenderer';
import { MainApp } from './MainApp';
import { Monobehiver } from '../base/Monobehiver';
export class AppPositionGizmo extends Monobehiver {
private utilityLayer: UtilityLayerRenderer | null = null;
private gizmo: PositionGizmo | null = null;
private enabled = true;
private rotationEnabled = false;
private scaleEnabled = false;
constructor(mainApp: MainApp) {
super(mainApp);
}
Awake(): void {
const scene = this.mainApp.appScene.object;
if (!scene) return;
this.utilityLayer = new UtilityLayerRenderer(scene);
this.gizmo = new PositionGizmo(this.utilityLayer);
this.gizmo.updateGizmoRotationToMatchAttachedMesh = false;
this.gizmo.updateGizmoPositionToMatchAttachedMesh = true;
}
setEnabled(enabled: boolean): void {
this.enabled = enabled;
if (!enabled) {
this.detach();
}
}
configure(options?: { position?: boolean; rotation?: boolean; scale?: boolean }): void {
if (!options) return;
if (typeof options.position === 'boolean') {
this.setEnabled(options.position);
}
if (typeof options.rotation === 'boolean') {
this.rotationEnabled = options.rotation;
}
if (typeof options.scale === 'boolean') {
this.scaleEnabled = options.scale;
}
}
toggle(): void {
this.setEnabled(!this.enabled);
}
attach(mesh: AbstractMesh | null): void {
if (!this.enabled || !this.gizmo) return;
this.gizmo.attachedMesh = mesh;
}
detach(): void {
if (this.gizmo) {
this.gizmo.attachedMesh = null;
}
}
isEnabled(): boolean {
return this.enabled;
}
getAttachedMesh(): AbstractMesh | null {
return this.gizmo?.attachedMesh ?? null;
}
dispose(): void {
this.gizmo?.dispose();
this.utilityLayer?.dispose();
this.gizmo = null;
this.utilityLayer = null;
}
}