Files
bim_engine/src/managers/engine-manager.ts

99 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-12-04 18:41:11 +08:00
import { Engine, type EngineOptions, type ModelLoadOptions } from '../components/engine';
/**
* 3D
* Engine BimEngine API
* initialize()
*/
export class EngineManager {
/** 3D 引擎挂载的父容器 */
private container: HTMLElement;
/** 3D 引擎组件实例 */
private engine: Engine | null = null;
/**
*
* @param container 3D
*/
constructor(container: HTMLElement) {
this.container = container;
}
/**
* 3D
* @param options 使
* @returns
*/
public initialize(options?: Omit<EngineOptions, 'container'>): boolean {
// 如果已经初始化,先销毁旧的实例
if (this.engine && this.engine.isInitialized()) {
console.warn('[EngineManager] 3D Engine already initialized. Destroying old instance...');
this.engine.destroy();
this.engine = null;
}
try {
// 创建 Engine 组件实例
// options 中的配置会自动复制给 createEngine 使用
this.engine = new Engine({
container: this.container,
...options, // 合并配置选项
});
// 调用组件的 init 方法初始化引擎
this.engine.init();
return this.engine.isInitialized();
} catch (error) {
console.error('[EngineManager] Failed to initialize 3D engine:', error);
this.engine = null;
return false;
}
}
/**
* 3D
*/
public isInitialized(): boolean {
return this.engine !== null && this.engine.isInitialized();
}
/**
* 3D
* @param url URL
* @param options
*/
public loadModel(url: string, options?: ModelLoadOptions): void {
if (!this.engine || !this.engine.isInitialized()) {
console.error('[EngineManager] 3D Engine not initialized. Please call initialize() first.');
return;
}
this.engine.loadModel(url, options);
}
/**
* 3D
* API
*/
public getEngine(): any {
if (!this.engine) {
console.warn('[EngineManager] 3D Engine not initialized.');
return null;
}
return this.engine.getEngine();
}
/**
* 3D
*/
public destroy(): void {
if (this.engine) {
this.engine.destroy();
this.engine = null;
}
}
}