234 lines
8.0 KiB
TypeScript
234 lines
8.0 KiB
TypeScript
"use client"
|
|
|
|
/**
|
|
* Scene Manager Component
|
|
* Orchestrates all advanced Three.js systems
|
|
*/
|
|
|
|
import { useEffect, useRef } from "react"
|
|
import { useThree, useFrame } from "@react-three/fiber"
|
|
import * as THREE from "three"
|
|
import { PhysicsEngine } from "@/lib/three/physics"
|
|
import { PerformanceMonitor } from "@/lib/three/performance"
|
|
import { ModelLoader } from "@/lib/three/loaders"
|
|
import { MaterialManager } from "@/lib/three/materials"
|
|
import { ShaderManager } from "@/lib/three/shaders"
|
|
import { VolumetricLightManager } from "@/lib/three/volumetric-lighting"
|
|
import { WeatherSystem } from "@/lib/three/weather-system"
|
|
import { ReflectionProbeManager } from "@/lib/three/advanced-materials-ext"
|
|
import { SceneGraphOptimizer } from "@/lib/three/scene-optimization"
|
|
import { HDREnvironmentManager } from "@/lib/three/hdr-pipeline"
|
|
import { AmbientOcclusionBaker } from "@/lib/three/baked-ao"
|
|
|
|
interface SceneManagerProps {
|
|
enablePhysics?: boolean
|
|
enablePerformanceMonitoring?: boolean
|
|
enableVolumetricLighting?: boolean
|
|
enableWeather?: boolean
|
|
enableReflectionProbes?: boolean
|
|
enableSceneOptimization?: boolean
|
|
enableHDR?: boolean
|
|
enableAOBaking?: boolean
|
|
roomDimensions?: { width: number; length: number; height: number }
|
|
onSystemsReady?: (systems: any) => void
|
|
}
|
|
|
|
export function SceneManager({
|
|
enablePhysics = true,
|
|
enablePerformanceMonitoring = true,
|
|
enableVolumetricLighting = true,
|
|
enableWeather = false,
|
|
enableReflectionProbes = true,
|
|
enableSceneOptimization = true,
|
|
enableHDR = false,
|
|
enableAOBaking = false,
|
|
roomDimensions,
|
|
onSystemsReady
|
|
}: SceneManagerProps) {
|
|
|
|
const { scene, camera, gl, viewport, size, events, xr } = useThree()
|
|
|
|
// System refs
|
|
const physicsRef = useRef<PhysicsEngine>(null as unknown as PhysicsEngine)
|
|
const performanceRef = useRef<PerformanceMonitor>(null as unknown as PerformanceMonitor)
|
|
const modelLoaderRef = useRef<ModelLoader>(null as unknown as ModelLoader)
|
|
const materialManagerRef = useRef<MaterialManager>(null as unknown as MaterialManager)
|
|
const shaderManagerRef = useRef<ShaderManager>(null as unknown as ShaderManager)
|
|
const volumetricLightingRef = useRef<VolumetricLightManager>(null as unknown as VolumetricLightManager)
|
|
const weatherRef = useRef<WeatherSystem>(null as unknown as WeatherSystem)
|
|
const reflectionProbesRef = useRef<ReflectionProbeManager>(null as unknown as ReflectionProbeManager)
|
|
const sceneOptimizerRef = useRef<SceneGraphOptimizer>(null as unknown as SceneGraphOptimizer)
|
|
const hdrManagerRef = useRef<HDREnvironmentManager>(null as unknown as HDREnvironmentManager)
|
|
const aoBakerRef = useRef<AmbientOcclusionBaker>(null as unknown as AmbientOcclusionBaker)
|
|
// Initialize all systems
|
|
useEffect(() => {
|
|
console.log('[SceneManager] Initializing advanced systems...')
|
|
// Core systems (always initialize)
|
|
modelLoaderRef.current = ModelLoader.getInstance()
|
|
materialManagerRef.current = MaterialManager.getInstance()
|
|
shaderManagerRef.current = ShaderManager.getInstance()
|
|
|
|
// Optional systems
|
|
if (enablePhysics) {
|
|
physicsRef.current = PhysicsEngine.getInstance()
|
|
physicsRef.current.setGravity(0, -9.81, 0)
|
|
physicsRef.current.setEnabled(true)
|
|
console.log('[SceneManager] Physics engine initialized')
|
|
}
|
|
|
|
if (enablePerformanceMonitoring) {
|
|
performanceRef.current = PerformanceMonitor.getInstance()
|
|
console.log('[SceneManager] Performance monitor initialized')
|
|
}
|
|
|
|
if (enableVolumetricLighting) {
|
|
volumetricLightingRef.current = new VolumetricLightManager(scene, camera, gl)
|
|
console.log('[SceneManager] Volumetric lighting initialized')
|
|
}
|
|
|
|
if (enableWeather) {
|
|
weatherRef.current = new WeatherSystem(scene)
|
|
weatherRef.current.setWeather({ type: 'clear', intensity: 0 }, 0)
|
|
console.log('[SceneManager] Weather system initialized')
|
|
}
|
|
|
|
if (enableReflectionProbes) {
|
|
reflectionProbesRef.current = new ReflectionProbeManager(scene, gl)
|
|
|
|
// Add main probe at scene center
|
|
if (roomDimensions) {
|
|
reflectionProbesRef.current.addProbe(
|
|
'main',
|
|
new THREE.Vector3(0, roomDimensions.height / 2, 0),
|
|
Math.max(roomDimensions.width, roomDimensions.length, roomDimensions.height),
|
|
512,
|
|
1.0
|
|
)
|
|
}
|
|
console.log('[SceneManager] Reflection probes initialized')
|
|
}
|
|
|
|
if (enableSceneOptimization) {
|
|
sceneOptimizerRef.current = new SceneGraphOptimizer(scene)
|
|
sceneOptimizerRef.current.setFrustumCulling(true)
|
|
sceneOptimizerRef.current.setDistanceCulling(true, 100)
|
|
console.log('[SceneManager] Scene optimizer initialized')
|
|
}
|
|
|
|
if (enableHDR) {
|
|
hdrManagerRef.current = new HDREnvironmentManager(scene, gl)
|
|
console.log('[SceneManager] HDR manager initialized')
|
|
}
|
|
|
|
if (enableAOBaking) {
|
|
aoBakerRef.current = new AmbientOcclusionBaker(scene, gl)
|
|
console.log('[SceneManager] AO baker initialized')
|
|
}
|
|
|
|
// Notify parent component
|
|
if (onSystemsReady) {
|
|
onSystemsReady({
|
|
physics: physicsRef.current,
|
|
performance: performanceRef.current,
|
|
modelLoader: modelLoaderRef.current,
|
|
materialManager: materialManagerRef.current,
|
|
shaderManager: shaderManagerRef.current,
|
|
volumetricLighting: volumetricLightingRef.current,
|
|
weather: weatherRef.current,
|
|
reflectionProbes: reflectionProbesRef.current,
|
|
sceneOptimizer: sceneOptimizerRef.current,
|
|
hdrManager: hdrManagerRef.current,
|
|
aoBaker: aoBakerRef.current,
|
|
viewport,
|
|
size,
|
|
events,
|
|
xr,
|
|
})
|
|
}
|
|
|
|
console.log('[SceneManager] All systems initialized successfully')
|
|
|
|
// Cleanup
|
|
return () => {
|
|
console.log('[SceneManager] Disposing systems...')
|
|
volumetricLightingRef.current?.dispose()
|
|
weatherRef.current?.dispose()
|
|
reflectionProbesRef.current?.dispose()
|
|
hdrManagerRef.current?.dispose()
|
|
aoBakerRef.current?.dispose()
|
|
sceneOptimizerRef.current?.buildOctree()
|
|
physicsRef.current?.setEnabled(false)
|
|
performanceRef.current?.reset()
|
|
modelLoaderRef.current?.dispose()
|
|
materialManagerRef.current?.disposeAll()
|
|
shaderManagerRef.current?.disposeAll()
|
|
}
|
|
}, [
|
|
scene,
|
|
camera,
|
|
gl,
|
|
enablePhysics,
|
|
enablePerformanceMonitoring,
|
|
enableVolumetricLighting,
|
|
enableWeather,
|
|
enableReflectionProbes,
|
|
enableSceneOptimization,
|
|
enableHDR,
|
|
enableAOBaking,
|
|
roomDimensions,
|
|
onSystemsReady,
|
|
viewport,
|
|
size,
|
|
events,
|
|
xr,
|
|
])
|
|
|
|
// Update all systems in render loop
|
|
useFrame((state, delta) => {
|
|
// Update physics
|
|
if (physicsRef.current && enablePhysics) {
|
|
physicsRef.current.update(delta)
|
|
console.log('[SceneManager] Physics updated')
|
|
}
|
|
|
|
// Update performance monitor
|
|
if (performanceRef.current && enablePerformanceMonitoring) {
|
|
performanceRef.current.updateRendererMetrics(gl)
|
|
console.log('[SceneManager] Performance monitor updated')
|
|
}
|
|
|
|
// Update volumetric lighting
|
|
if (volumetricLightingRef.current && enableVolumetricLighting) {
|
|
volumetricLightingRef.current.update(delta)
|
|
console.log('[SceneManager] Volumetric lighting updated')
|
|
}
|
|
|
|
// Update weather
|
|
if (weatherRef.current && enableWeather) {
|
|
weatherRef.current.update(delta)
|
|
console.log('[SceneManager] Weather updated')
|
|
}
|
|
|
|
// Update reflection probes (less frequently)
|
|
if (reflectionProbesRef.current && enableReflectionProbes) {
|
|
reflectionProbesRef.current.update(delta)
|
|
console.log('[SceneManager] Reflection probes updated')
|
|
}
|
|
|
|
// Update scene optimization
|
|
if (sceneOptimizerRef.current && enableSceneOptimization) {
|
|
sceneOptimizerRef.current.optimize(camera)
|
|
console.log('[SceneManager] Scene optimizer updated')
|
|
}
|
|
|
|
// Update shader animations
|
|
if (shaderManagerRef.current) {
|
|
// Would animate shader uniforms here
|
|
console.log('[SceneManager] Shader manager updated')
|
|
}
|
|
})
|
|
|
|
return null // This is a logic-only component
|
|
}
|
|
|