This commit is contained in:
yinsx
2026-01-05 14:04:46 +08:00
parent 525abc9512
commit 6a3509d623
4 changed files with 46 additions and 5 deletions

View File

@ -9,5 +9,10 @@ export const AppConfig = {
container: 'renderDom',
modelUrlList: [] as string[],
success: null as OptionalCallback,
error: null as ErrorCallback
error: null as ErrorCallback,
env: {
hdrPath:"",
intensity: 1,
rotationY: 0
}
};

View File

@ -1,5 +1,6 @@
import { CubeTexture } from '@babylonjs/core/Materials/Textures/cubeTexture';
import { Monobehiver } from '../base/Monobehiver';
import { AppConfig } from './AppConfig';
/**
* 环境管理类- 负责创建和管理HDR环境贴图
@ -14,18 +15,26 @@ export class AppEnv extends Monobehiver {
/** 初始化 - 创建默认HDR环境 */
Awake(): void {
this.createHDR();
this.createHDR(AppConfig.env);
}
/**
* 创建HDR环境贴图
* @param hdrPath HDR文件路径
*/
createHDR(hdrPath = '/hdr/sanGiuseppeBridge.env'): void {
createHDR(options?: { hdrPath?: string; intensity?: number; rotationY?: number }): void {
const hdrPath = options?.hdrPath || AppConfig.env.hdrPath || '/hdr/sanGiuseppeBridge.env';
const intensity = options?.intensity ?? AppConfig.env.intensity ?? 1.5;
const rotationY = options?.rotationY ?? AppConfig.env.rotationY ?? 0;
const scene = this.mainApp.appScene.object;
if (!scene) return;
if (this.object) {
this.object.dispose();
this.object = null;
}
const reflectionTexture = CubeTexture.CreateFromPrefilteredData(hdrPath, scene);
scene.environmentIntensity = 1.5;
reflectionTexture.rotationY = rotationY;
scene.environmentIntensity = intensity;
scene.environmentTexture = reflectionTexture;
this.object = reflectionTexture;
}
@ -48,6 +57,13 @@ export class AppEnv extends Monobehiver {
if (this.object) this.object.rotationY = angle;
}
/**
* 更新环境配置
*/
updateEnvironment(options: { hdrPath?: string; intensity?: number; rotationY?: number }): void {
this.createHDR(options);
}
/** 清理资源 */
clean(): void {
if (this.object) {

View File

@ -47,6 +47,7 @@ export class MainApp {
AppConfig.modelUrlList = config.modelUrlList || [];
AppConfig.success = config.success;
AppConfig.error = config.error;
AppConfig.env = config.env
}
loadModel(): void {

View File

@ -1,4 +1,5 @@
import { MainApp } from './babylonjs/MainApp';
import { AppConfig } from './babylonjs/AppConfig';
import configurator, { ConfiguratorParams } from './components/conf';
import auth from './components/auth';
import { on } from './utils/event';
@ -18,6 +19,11 @@ type InitParams = {
onSuccess?: () => void;
onError?: (error?: unknown) => void;
apiConfig?: ConfiguratorParams;
envConfig?: {
hdrPath?: string;
intensity?: number;
rotationY?: number;
};
};
let mainApp: MainApp | null = null;
@ -43,11 +49,24 @@ const kernel = {
container: params.container || 'renderDom',
modelUrlList: params.modelUrlList || [],
success: params.onSuccess ?? null,
error: params.onError ?? null
error: params.onError ?? null,
envConfig: params.envConfig
});
await mainApp.Awake();
await mainApp.loadModel();
},
/** 更新环境贴图配置(路径/强度/旋转) */
setEnvironment: function (envConfig: { hdrPath?: string; intensity?: number; rotationY?: number }): void {
if (!mainApp) {
console.warn('mainApp is not initialized yet');
return;
}
if (envConfig) {
AppConfig.env = { ...AppConfig.env, ...envConfig };
mainApp.appEnv.updateEnvironment(AppConfig.env);
}
}
};