'use client' import React, { useEffect, useRef, useState } from 'react' import { Engine, Scene, Vector3, HemisphericLight, ArcRotateCamera, Color3, Color4, AbstractMesh, Nullable, HighlightLayer, Mesh, InstancedMesh, Animation, CubicEase, EasingFunction, ImportMeshAsync, PointerEventTypes, PointerInfo, Matrix, } from '@babylonjs/core' import '@babylonjs/loaders' import SceneToolbar from './SceneToolbar'; import LoadingSpinner from '../ui/LoadingSpinner' export interface ModelViewerProps { modelPath: string onSelectModel: (path: string) => void; onModelLoaded?: (modelData: { meshes: AbstractMesh[] boundingBox: { min: { x: number; y: number; z: number } max: { x: number; y: number; z: number } } }) => void onError?: (error: string) => void activeMenu?: string | null focusSensorId?: string | null renderOverlay?: (params: { anchor: { left: number; top: number } | null; info?: { name?: string; sensorId?: string } | null }) => React.ReactNode isSensorSelectionEnabled?: boolean onSensorPick?: (sensorId: string | null) => void highlightAllSensors?: boolean } const ModelViewer: React.FC = ({ modelPath, onSelectModel, onModelLoaded, onError, focusSensorId, renderOverlay, isSensorSelectionEnabled, onSensorPick, highlightAllSensors, }) => { const canvasRef = useRef(null) const engineRef = useRef>(null) const sceneRef = useRef>(null) const [isLoading, setIsLoading] = useState(false) const [loadingProgress, setLoadingProgress] = useState(0) const [showModel, setShowModel] = useState(false) const isInitializedRef = useRef(false) const isDisposedRef = useRef(false) const importedMeshesRef = useRef([]) const highlightLayerRef = useRef(null) const highlightedMeshesRef = useRef([]) const chosenMeshRef = useRef(null) const [overlayPos, setOverlayPos] = useState<{ left: number; top: number } | null>(null) const [overlayData, setOverlayData] = useState<{ name?: string; sensorId?: string } | null>(null) const [modelReady, setModelReady] = useState(false) const [panActive, setPanActive] = useState(false); const handlePan = () => setPanActive(!panActive); useEffect(() => { const scene = sceneRef.current; const camera = scene?.activeCamera as ArcRotateCamera; const canvas = canvasRef.current; if (!scene || !camera || !canvas) { return; } let observer: any = null; if (panActive) { camera.detachControl(); observer = scene.onPointerObservable.add((pointerInfo: PointerInfo) => { const evt = pointerInfo.event; if (evt.buttons === 1) { camera.inertialPanningX -= evt.movementX / camera.panningSensibility; camera.inertialPanningY += evt.movementY / camera.panningSensibility; } else if (evt.buttons === 2) { camera.inertialAlphaOffset -= evt.movementX / camera.angularSensibilityX; camera.inertialBetaOffset -= evt.movementY / camera.angularSensibilityY; } }, PointerEventTypes.POINTERMOVE); } else { camera.detachControl(); camera.attachControl(canvas, true); } return () => { if (observer) { scene.onPointerObservable.remove(observer); } if (!camera.isDisposed() && !camera.inputs.attachedToElement) { camera.attachControl(canvas, true); } }; }, [panActive, sceneRef, canvasRef]); const handleZoomIn = () => { const camera = sceneRef.current?.activeCamera as ArcRotateCamera if (camera) { sceneRef.current?.stopAnimation(camera) const ease = new CubicEase() ease.setEasingMode(EasingFunction.EASINGMODE_EASEOUT) const frameRate = 60 const durationMs = 300 const totalFrames = Math.round((durationMs / 1000) * frameRate) const currentRadius = camera.radius const targetRadius = Math.max(camera.lowerRadiusLimit ?? 0.1, currentRadius * 0.8) Animation.CreateAndStartAnimation( 'zoomIn', camera, 'radius', frameRate, totalFrames, currentRadius, targetRadius, Animation.ANIMATIONLOOPMODE_CONSTANT, ease ) } } const handleZoomOut = () => { const camera = sceneRef.current?.activeCamera as ArcRotateCamera if (camera) { sceneRef.current?.stopAnimation(camera) const ease = new CubicEase() ease.setEasingMode(EasingFunction.EASINGMODE_EASEOUT) const frameRate = 60 const durationMs = 300 const totalFrames = Math.round((durationMs / 1000) * frameRate) const currentRadius = camera.radius const targetRadius = Math.min(camera.upperRadiusLimit ?? Infinity, currentRadius * 1.2) Animation.CreateAndStartAnimation( 'zoomOut', camera, 'radius', frameRate, totalFrames, currentRadius, targetRadius, Animation.ANIMATIONLOOPMODE_CONSTANT, ease ) } } const handleTopView = () => { const camera = sceneRef.current?.activeCamera as ArcRotateCamera; if (camera) { sceneRef.current?.stopAnimation(camera); const ease = new CubicEase(); ease.setEasingMode(EasingFunction.EASINGMODE_EASEOUT); const frameRate = 60; const durationMs = 500; const totalFrames = Math.round((durationMs / 1000) * frameRate); Animation.CreateAndStartAnimation( 'topViewAlpha', camera, 'alpha', frameRate, totalFrames, camera.alpha, Math.PI / 2, Animation.ANIMATIONLOOPMODE_CONSTANT, ease ); Animation.CreateAndStartAnimation( 'topViewBeta', camera, 'beta', frameRate, totalFrames, camera.beta, 0, Animation.ANIMATIONLOOPMODE_CONSTANT, ease ); } }; useEffect(() => { isDisposedRef.current = false isInitializedRef.current = false return () => { isDisposedRef.current = true } }, []) useEffect(() => { if (!canvasRef.current || isInitializedRef.current) return const canvas = canvasRef.current const engine = new Engine(canvas, true, { stencil: true }) engineRef.current = engine engine.runRenderLoop(() => { if (!isDisposedRef.current && sceneRef.current) { sceneRef.current.render() } }) const scene = new Scene(engine) sceneRef.current = scene scene.clearColor = new Color4(0.1, 0.1, 0.15, 1) const camera = new ArcRotateCamera('camera', 0, Math.PI / 3, 20, Vector3.Zero(), scene) camera.attachControl(canvas, true) camera.lowerRadiusLimit = 2 camera.upperRadiusLimit = 200 camera.wheelDeltaPercentage = 0.01 camera.panningSensibility = 50 camera.angularSensibilityX = 1000 camera.angularSensibilityY = 1000 const ambientLight = new HemisphericLight('ambientLight', new Vector3(0, 1, 0), scene) ambientLight.intensity = 0.4 ambientLight.diffuse = new Color3(0.7, 0.7, 0.8) ambientLight.specular = new Color3(0.2, 0.2, 0.3) ambientLight.groundColor = new Color3(0.3, 0.3, 0.4) const keyLight = new HemisphericLight('keyLight', new Vector3(1, 1, 0), scene) keyLight.intensity = 0.6 keyLight.diffuse = new Color3(1, 1, 0.9) keyLight.specular = new Color3(1, 1, 0.9) const fillLight = new HemisphericLight('fillLight', new Vector3(-1, 0.5, -1), scene) fillLight.intensity = 0.3 fillLight.diffuse = new Color3(0.8, 0.8, 1) const hl = new HighlightLayer('highlight-layer', scene) highlightLayerRef.current = hl const handleResize = () => { if (!isDisposedRef.current) { engine.resize() } } window.addEventListener('resize', handleResize) isInitializedRef.current = true return () => { isDisposedRef.current = true isInitializedRef.current = false window.removeEventListener('resize', handleResize) highlightLayerRef.current?.dispose() highlightLayerRef.current = null if (engineRef.current) { engineRef.current.dispose() engineRef.current = null } sceneRef.current = null } }, []) useEffect(() => { if (!isInitializedRef.current || isDisposedRef.current) { return } if (!modelPath || modelPath.trim() === '') { console.warn('[ModelViewer] No model path provided') // Не вызываем onError для пустого пути - это нормальное состояние при инициализации setIsLoading(false) return } const loadModel = async () => { if (!sceneRef.current || isDisposedRef.current) { return } const currentModelPath = modelPath; console.log('[ModelViewer] Starting model load:', currentModelPath); setIsLoading(true) setLoadingProgress(0) setShowModel(false) setModelReady(false) setPanActive(false) const oldMeshes = sceneRef.current.meshes.slice(); const activeCameraId = sceneRef.current.activeCamera?.uniqueId; console.log('[ModelViewer] Cleaning up old meshes. Total:', oldMeshes.length); oldMeshes.forEach(m => { if (m.uniqueId !== activeCameraId) { m.dispose(); } }); console.log('[ModelViewer] Loading GLTF model:', currentModelPath) // UI элемент загрузчика (есть эффект замедленности) const progressInterval = setInterval(() => { setLoadingProgress(prev => { if (prev >= 90) { clearInterval(progressInterval) return 90 } return prev + Math.random() * 15 }) }, 100) try { console.log('[ModelViewer] Calling ImportMeshAsync with path:', currentModelPath); // Проверим доступность файла через fetch try { const testResponse = await fetch(currentModelPath, { method: 'HEAD' }); console.log('[ModelViewer] File availability check:', { url: currentModelPath, status: testResponse.status, statusText: testResponse.statusText, ok: testResponse.ok }); } catch (fetchError) { console.error('[ModelViewer] File fetch error:', fetchError); } const result = await ImportMeshAsync(currentModelPath, sceneRef.current) console.log('[ModelViewer] ImportMeshAsync completed successfully'); console.log('[ModelViewer] Import result:', { meshesCount: result.meshes.length, particleSystemsCount: result.particleSystems.length, skeletonsCount: result.skeletons.length, animationGroupsCount: result.animationGroups.length }); if (isDisposedRef.current || modelPath !== currentModelPath) { console.log('[ModelViewer] Model loading aborted - model changed during load') clearInterval(progressInterval) setIsLoading(false) return; } importedMeshesRef.current = result.meshes clearInterval(progressInterval) setLoadingProgress(100) console.log('[ModelViewer] GLTF Model loaded successfully!', result) if (result.meshes.length > 0) { const boundingBox = result.meshes[0].getHierarchyBoundingVectors() const size = boundingBox.max.subtract(boundingBox.min) const maxDimension = Math.max(size.x, size.y, size.z) const camera = sceneRef.current!.activeCamera as ArcRotateCamera camera.radius = maxDimension * 2 camera.target = result.meshes[0].position importedMeshesRef.current = result.meshes setModelReady(true) onModelLoaded?.({ meshes: result.meshes, boundingBox: { min: boundingBox.min, max: boundingBox.max } }) // Плавное появление модели setTimeout(() => { if (!isDisposedRef.current && modelPath === currentModelPath) { setShowModel(true) setIsLoading(false) } else { console.log('Model display aborted - model changed during animation') } }, 500) } else { console.warn('No meshes found in model') onError?.('В модели не найдена геометрия') setIsLoading(false) } } catch (error) { clearInterval(progressInterval) if (!isDisposedRef.current && modelPath === currentModelPath) { console.error('Error loading GLTF model:', error) const errorMessage = error instanceof Error ? error.message : String(error) onError?.(`Ошибка загрузки модели: ${errorMessage}`) } else { console.log('Error occurred but loading was aborted - model changed') } setIsLoading(false) } } // Загрузка модлеи начинается после появления спиннера requestIdleCallback(() => loadModel(), { timeout: 50 }) }, [modelPath, onError, onModelLoaded]) useEffect(() => { console.log('[ModelViewer] highlightAllSensors effect triggered:', { highlightAllSensors, modelReady, sceneReady: !!sceneRef.current }) if (!sceneRef.current || isDisposedRef.current || !modelReady) return // Если включено выделение всех сенсоров - выделяем их все if (highlightAllSensors) { console.log('[ModelViewer] Calling highlightAllSensorMeshes()') highlightAllSensorMeshes() return } const sensorId = (focusSensorId ?? '').trim() if (!sensorId) { console.log('[ModelViewer] Focus cleared (no Sensor_ID provided)') for (const m of highlightedMeshesRef.current) { m.renderingGroupId = 0 } highlightedMeshesRef.current = [] highlightLayerRef.current?.removeAllMeshes() chosenMeshRef.current = null setOverlayPos(null) setOverlayData(null) return } const allMeshes = importedMeshesRef.current || [] if (allMeshes.length === 0) { console.warn('[ModelViewer] No meshes available for sensor matching') for (const m of highlightedMeshesRef.current) { m.renderingGroupId = 0 } highlightedMeshesRef.current = [] highlightLayerRef.current?.removeAllMeshes() chosenMeshRef.current = null setOverlayPos(null) setOverlayData(null) return } const sensorMeshes = allMeshes.filter((m: any) => { try { return ((m.id ?? '').includes('IfcSensor') || (m.name ?? '').includes('IfcSensor')) } catch (error) { console.warn('[ModelViewer] Error filtering sensor mesh:', error) return false } }) const chosen = sensorMeshes.find((m: any) => { try { const meta: any = (m as any)?.metadata const extras: any = meta?.gltf?.extras ?? meta?.extras ?? (m as any)?.extras const sid = extras?.Sensor_ID ?? extras?.sensor_id ?? extras?.SERIAL_NUMBER ?? extras?.serial_number ?? extras?.detector_id if (sid != null) { return String(sid).trim() === sensorId } const monitoringSensorInstance = extras?.bonsaiPset_ARBM_PSet_MonitoringSensor_Instance if (monitoringSensorInstance && typeof monitoringSensorInstance === 'string') { try { const parsedInstance = JSON.parse(monitoringSensorInstance) const instanceSensorId = parsedInstance?.Sensor_ID if (instanceSensorId != null) { return String(instanceSensorId).trim() === sensorId } } catch (parseError) { console.warn('[ModelViewer] Error parsing MonitoringSensor_Instance JSON:', parseError) } } return false } catch (error) { console.warn('[ModelViewer] Error matching sensor mesh:', error) return false } }) console.log('[ModelViewer] Sensor focus', { requested: sensorId, totalImportedMeshes: allMeshes.length, totalSensorMeshes: sensorMeshes.length, chosen: chosen ? { id: chosen.id, name: chosen.name, uniqueId: chosen.uniqueId, parent: chosen.parent?.name } : null, source: 'result.meshes', }) const scene = sceneRef.current! if (chosen) { try { const camera = scene.activeCamera as ArcRotateCamera const bbox = (typeof chosen.getHierarchyBoundingVectors === 'function') ? chosen.getHierarchyBoundingVectors() : { min: chosen.getBoundingInfo().boundingBox.minimumWorld, max: chosen.getBoundingInfo().boundingBox.maximumWorld } const center = bbox.min.add(bbox.max).scale(0.5) const size = bbox.max.subtract(bbox.min) const maxDimension = Math.max(size.x, size.y, size.z) const targetRadius = Math.max(camera.lowerRadiusLimit ?? 2, maxDimension * 1.5) scene.stopAnimation(camera) const ease = new CubicEase() ease.setEasingMode(EasingFunction.EASINGMODE_EASEINOUT) const frameRate = 60 const durationMs = 600 const totalFrames = Math.round((durationMs / 1000) * frameRate) Animation.CreateAndStartAnimation('camTarget', camera, 'target', frameRate, totalFrames, camera.target.clone(), center.clone(), Animation.ANIMATIONLOOPMODE_CONSTANT, ease) Animation.CreateAndStartAnimation('camRadius', camera, 'radius', frameRate, totalFrames, camera.radius, targetRadius, Animation.ANIMATIONLOOPMODE_CONSTANT, ease) const hl = highlightLayerRef.current if (hl) { // Переключаем группу рендеринга для предыдущего выделенного меша for (const m of highlightedMeshesRef.current) { m.renderingGroupId = 0 } highlightedMeshesRef.current = [] hl.removeAllMeshes() if (chosen instanceof Mesh) { chosen.renderingGroupId = 1 highlightedMeshesRef.current.push(chosen) hl.addMesh(chosen, new Color3(1, 1, 0)) } else if (chosen instanceof InstancedMesh) { // Сохраняем исходный меш для инстанса chosen.sourceMesh.renderingGroupId = 1 highlightedMeshesRef.current.push(chosen.sourceMesh) hl.addMesh(chosen.sourceMesh, new Color3(1, 1, 0)) } else { const children = typeof (chosen as any)?.getChildMeshes === 'function' ? (chosen as any).getChildMeshes() : [] for (const cm of children) { if (cm instanceof Mesh) { cm.renderingGroupId = 1 highlightedMeshesRef.current.push(cm) hl.addMesh(cm, new Color3(1, 1, 0)) } } } } chosenMeshRef.current = chosen setOverlayData({ name: chosen.name, sensorId }) } catch (error) { console.error('[ModelViewer] Error focusing on sensor mesh:', error) for (const m of highlightedMeshesRef.current) { m.renderingGroupId = 0 } highlightedMeshesRef.current = [] highlightLayerRef.current?.removeAllMeshes() chosenMeshRef.current = null setOverlayPos(null) setOverlayData(null) } } else { for (const m of highlightedMeshesRef.current) { m.renderingGroupId = 0 } highlightedMeshesRef.current = [] highlightLayerRef.current?.removeAllMeshes() chosenMeshRef.current = null setOverlayPos(null) setOverlayData(null) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [focusSensorId, modelReady, highlightAllSensors]) // Помощь: извлечь Sensor_ID из метаданных меша (совпадающая логика с фокусом) const getSensorIdFromMesh = React.useCallback((m: AbstractMesh | null): string | null => { if (!m) return null try { const meta: any = (m as any)?.metadata const extras: any = meta?.gltf?.extras ?? meta?.extras ?? (m as any)?.extras const sid = extras?.Sensor_ID ?? extras?.sensor_id ?? extras?.SERIAL_NUMBER ?? extras?.serial_number ?? extras?.detector_id if (sid != null) return String(sid).trim() const monitoringSensorInstance = extras?.bonsaiPset_ARBM_PSet_MonitoringSensor_Instance if (monitoringSensorInstance && typeof monitoringSensorInstance === 'string') { try { const parsedInstance = JSON.parse(monitoringSensorInstance) const instanceSensorId = parsedInstance?.Sensor_ID if (instanceSensorId != null) return String(instanceSensorId).trim() } catch (parseError) { console.warn('[ModelViewer] Error parsing MonitoringSensor_Instance JSON in pick:', parseError) } } } catch { } return null }, []) // Функция для выделения всех сенсоров на модели const highlightAllSensorMeshes = React.useCallback(() => { console.log('[ModelViewer] highlightAllSensorMeshes called') const scene = sceneRef.current if (!scene || !highlightLayerRef.current) { console.log('[ModelViewer] Cannot highlight - scene or highlightLayer not ready:', { sceneReady: !!scene, highlightLayerReady: !!highlightLayerRef.current }) return } const allMeshes = importedMeshesRef.current || [] // Use the same logic as getSensorIdFromMesh to identify sensor meshes const sensorMeshes = allMeshes.filter((m: any) => { try { const sensorId = getSensorIdFromMesh(m) if (sensorId) { console.log(`[ModelViewer] Found sensor mesh: ${m.name} (id: ${m.id}, sensorId: ${sensorId})`) return true } return false } catch (error) { console.warn('[ModelViewer] Error filtering sensor mesh:', error) return false } }) console.log(`[ModelViewer] Found ${sensorMeshes.length} sensor meshes out of ${allMeshes.length} total meshes`) if (sensorMeshes.length === 0) { console.log('[ModelViewer] No sensor meshes found to highlight') return } // Clear previous highlights for (const m of highlightedMeshesRef.current) { m.renderingGroupId = 0 } highlightedMeshesRef.current = [] highlightLayerRef.current?.removeAllMeshes() // Highlight all sensor meshes sensorMeshes.forEach((mesh: any) => { try { if (mesh instanceof Mesh) { mesh.renderingGroupId = 1 highlightedMeshesRef.current.push(mesh) highlightLayerRef.current?.addMesh(mesh, new Color3(1, 1, 0)) } else if (mesh instanceof InstancedMesh) { mesh.sourceMesh.renderingGroupId = 1 highlightedMeshesRef.current.push(mesh.sourceMesh) highlightLayerRef.current?.addMesh(mesh.sourceMesh, new Color3(1, 1, 0)) } else { const children = typeof mesh.getChildMeshes === 'function' ? mesh.getChildMeshes() : [] for (const cm of children) { if (cm instanceof Mesh) { cm.renderingGroupId = 1 highlightedMeshesRef.current.push(cm) highlightLayerRef.current?.addMesh(cm, new Color3(1, 1, 0)) } } } } catch (error) { console.warn('[ModelViewer] Error highlighting sensor mesh:', error) } }) console.log(`[ModelViewer] Successfully highlighted ${highlightedMeshesRef.current.length} sensor meshes`) }, [getSensorIdFromMesh]) // Включение выбора на основе взаимодействия с моделью только при готовности модели и включении выбора сенсоров useEffect(() => { const scene = sceneRef.current if (!scene || !modelReady || !isSensorSelectionEnabled) return const pickObserver = scene.onPointerObservable.add((pointerInfo: PointerInfo) => { if (pointerInfo.type !== PointerEventTypes.POINTERPICK) return const pick = pointerInfo.pickInfo if (!pick || !pick.hit) { onSensorPick?.(null) return } const pickedMesh = pick.pickedMesh const sensorId = getSensorIdFromMesh(pickedMesh) if (sensorId) { onSensorPick?.(sensorId) } else { onSensorPick?.(null) } }) return () => { scene.onPointerObservable.remove(pickObserver) } }, [modelReady, isSensorSelectionEnabled, onSensorPick, getSensorIdFromMesh]) // Расчет позиции оверлея const computeOverlayPosition = React.useCallback((mesh: AbstractMesh | null) => { if (!sceneRef.current || !mesh) return null const scene = sceneRef.current try { const bbox = (typeof mesh.getHierarchyBoundingVectors === 'function') ? mesh.getHierarchyBoundingVectors() : { min: mesh.getBoundingInfo().boundingBox.minimumWorld, max: mesh.getBoundingInfo().boundingBox.maximumWorld } const center = bbox.min.add(bbox.max).scale(0.5) const viewport = scene.activeCamera?.viewport.toGlobal(engineRef.current!.getRenderWidth(), engineRef.current!.getRenderHeight()) if (!viewport) return null const projected = Vector3.Project(center, Matrix.Identity(), scene.getTransformMatrix(), viewport) if (!projected) return null return { left: projected.x, top: projected.y } } catch (error) { console.error('[ModelViewer] Error computing overlay position:', error) return null } }, []) // Позиция оверлея изначально useEffect(() => { if (!chosenMeshRef.current || !overlayData) return const pos = computeOverlayPosition(chosenMeshRef.current) setOverlayPos(pos) }, [overlayData, computeOverlayPosition]) // Позиция оверлея при движущейся камере useEffect(() => { if (!sceneRef.current || !chosenMeshRef.current || !overlayData) return const scene = sceneRef.current const updateOverlayPosition = () => { const pos = computeOverlayPosition(chosenMeshRef.current) setOverlayPos(pos) } scene.registerBeforeRender(updateOverlayPosition) return () => scene.unregisterBeforeRender(updateOverlayPosition) }, [overlayData, computeOverlayPosition]) return (
{!modelPath ? (
3D модель не выбрана
Выберите модель в панели «Зоны мониторинга», чтобы начать просмотр
Если список пуст, добавьте файлы в каталог assets/big-models или проверьте API
) : ( <> {isLoading && (
)} {!modelReady && !isLoading && (
3D модель не загружена
Модель не готова к отображению
)} )} {renderOverlay && overlayPos && overlayData ? renderOverlay({ anchor: overlayPos, info: overlayData }) : null }
) } export default ModelViewer