From 6a3509d6232384c520cdc089ab90f66b4394a2d1 Mon Sep 17 00:00:00 2001 From: yinsx Date: Mon, 5 Jan 2026 14:04:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/babylonjs/AppConfig.ts | 7 ++++++- src/babylonjs/AppEnv.ts | 22 +++++++++++++++++++--- src/babylonjs/MainApp.ts | 1 + src/main.ts | 21 ++++++++++++++++++++- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/babylonjs/AppConfig.ts b/src/babylonjs/AppConfig.ts index 5fb24c7..25d7fb9 100644 --- a/src/babylonjs/AppConfig.ts +++ b/src/babylonjs/AppConfig.ts @@ -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 + } }; diff --git a/src/babylonjs/AppEnv.ts b/src/babylonjs/AppEnv.ts index 1049de2..3257a6b 100644 --- a/src/babylonjs/AppEnv.ts +++ b/src/babylonjs/AppEnv.ts @@ -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) { diff --git a/src/babylonjs/MainApp.ts b/src/babylonjs/MainApp.ts index 888cf0f..8b6670a 100644 --- a/src/babylonjs/MainApp.ts +++ b/src/babylonjs/MainApp.ts @@ -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 { diff --git a/src/main.ts b/src/main.ts index 80ec0b0..cca828f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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); + } } };