69 lines
1.9 KiB
JavaScript
69 lines
1.9 KiB
JavaScript
// Babylon.js 场景管理器
|
|
class BabylonSceneManager {
|
|
constructor(canvasId) {
|
|
this.canvas = document.getElementById(canvasId);
|
|
this.engine = new BABYLON.Engine(this.canvas, true);
|
|
this.scene = null;
|
|
this.camera = null;
|
|
this.onModelLoaded = null;
|
|
this.onBeforeRender = null; // 渲染前回调
|
|
}
|
|
|
|
init() {
|
|
this.scene = new BABYLON.Scene(this.engine);
|
|
this.scene.clearColor = new BABYLON.Color3(0.1, 0.1, 0.1);
|
|
|
|
this.camera = new BABYLON.ArcRotateCamera(
|
|
"camera",
|
|
0,
|
|
Math.PI / 2,
|
|
2,
|
|
new BABYLON.Vector3(0, 1.5, 0),
|
|
this.scene
|
|
);
|
|
this.camera.attachControl(this.canvas, true);
|
|
this.camera.minZ = 0.01;
|
|
this.camera.lowerRadiusLimit = 1;
|
|
this.camera.upperRadiusLimit = 5;
|
|
|
|
this.scene.createDefaultEnvironment();
|
|
|
|
this.engine.runRenderLoop(() => {
|
|
// 在渲染前调用动画更新
|
|
if (this.onBeforeRender) {
|
|
this.onBeforeRender();
|
|
}
|
|
this.scene.render();
|
|
});
|
|
|
|
window.addEventListener('resize', () => {
|
|
this.engine.resize();
|
|
});
|
|
}
|
|
|
|
loadModel(modelPath, onSuccess, onError) {
|
|
BABYLON.SceneLoader.ImportMesh("", "./", modelPath, this.scene,
|
|
(meshes) => {
|
|
this.scene.lights.forEach(light => {
|
|
light.intensity = 50;
|
|
});
|
|
|
|
if (onSuccess) {
|
|
onSuccess(meshes);
|
|
}
|
|
},
|
|
null,
|
|
(scene, message) => {
|
|
if (onError) {
|
|
onError(message);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
// 导出到全局
|
|
if (typeof window !== 'undefined') {
|
|
window.BabylonSceneManager = BabylonSceneManager;
|
|
}
|