@umituz/web-virtual 1.1.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -8
- package/package.json +1 -1
- package/src/components/DigitalHQView.tsx +91 -52
- package/src/components/EntityInspector.tsx +8 -8
- package/src/components/VirtualCommandBar.tsx +10 -10
- package/src/components/VirtualEntityDrawer.tsx +15 -15
- package/src/components/VirtualReportModal.tsx +7 -6
- package/src/components/VirtualRoster.tsx +12 -11
- package/src/components/VirtualWorld.tsx +170 -35
- package/src/components/WorldControls.tsx +12 -12
- package/src/core/environment.test.ts +100 -16
- package/src/core/environment.ts +93 -7
- package/src/core/types.ts +53 -2
- package/src/engine/renderer.ts +58 -2
- package/src/entities/people/AvatarBuilder.test.ts +75 -0
- package/src/entities/people/AvatarBuilder.ts +23 -3
- package/src/presets/factoryPreset.ts +13 -44
- package/src/presets/farmPreset.ts +13 -44
- package/src/presets/hospitalPreset.ts +12 -33
- package/src/presets/officePreset.ts +65 -356
- package/src/presets/schoolPreset.ts +12 -33
- package/src/styles/virtual.css +215 -0
|
@@ -39,7 +39,7 @@ export function VirtualCommandBar({
|
|
|
39
39
|
defaultOpen = false,
|
|
40
40
|
isOpen: externalIsOpen,
|
|
41
41
|
onToggleOpen,
|
|
42
|
-
placeholder = "
|
|
42
|
+
placeholder = "Assign a mission to the general manager or a specialist... (Enter to send)",
|
|
43
43
|
className = '',
|
|
44
44
|
}: VirtualCommandBarProps): ReactNode {
|
|
45
45
|
const [internalIsOpen, setInternalIsOpen] = useState(defaultOpen)
|
|
@@ -56,10 +56,10 @@ export function VirtualCommandBar({
|
|
|
56
56
|
type="button"
|
|
57
57
|
onClick={() => handleToggle(true)}
|
|
58
58
|
className="wv-command-collapsed-btn"
|
|
59
|
-
aria-label="
|
|
59
|
+
aria-label="Open mission panel"
|
|
60
60
|
>
|
|
61
61
|
<Send size={14} color="#3b82f6" />
|
|
62
|
-
<span>⚡
|
|
62
|
+
<span>⚡ Mission Panel ▲</span>
|
|
63
63
|
</button>
|
|
64
64
|
)
|
|
65
65
|
}
|
|
@@ -86,9 +86,9 @@ export function VirtualCommandBar({
|
|
|
86
86
|
type="button"
|
|
87
87
|
onClick={() => handleToggle(false)}
|
|
88
88
|
className="wv-collapse-btn"
|
|
89
|
-
title="
|
|
89
|
+
title="Collapse mission panel"
|
|
90
90
|
>
|
|
91
|
-
|
|
91
|
+
Hide ▼
|
|
92
92
|
</button>
|
|
93
93
|
</div>
|
|
94
94
|
)}
|
|
@@ -102,9 +102,9 @@ export function VirtualCommandBar({
|
|
|
102
102
|
onChange={(e) => onChangeTarget(e.target.value)}
|
|
103
103
|
className="wv-target-select"
|
|
104
104
|
>
|
|
105
|
-
<option value="auto">👑
|
|
105
|
+
<option value="auto">👑 General Manager (auto-delegate)</option>
|
|
106
106
|
{entities.length > 0 && (
|
|
107
|
-
<optgroup label="
|
|
107
|
+
<optgroup label="Office desks / staff">
|
|
108
108
|
{entities.map((en) => (
|
|
109
109
|
<option key={en.id} value={en.id}>
|
|
110
110
|
{en.pinNumber ? `#${en.pinNumber} ` : ''}
|
|
@@ -138,10 +138,10 @@ export function VirtualCommandBar({
|
|
|
138
138
|
type="button"
|
|
139
139
|
onClick={onToggleDemoMode}
|
|
140
140
|
className={`wv-demo-toggle-btn ${isDemoMode ? 'is-demo' : 'is-ai'}`}
|
|
141
|
-
title="
|
|
141
|
+
title="Execution mode"
|
|
142
142
|
>
|
|
143
143
|
{isDemoMode ? <Sparkles size={14} /> : <Bot size={14} />}
|
|
144
|
-
<span className="wv-demo-label">{isDemoMode ? '
|
|
144
|
+
<span className="wv-demo-label">{isDemoMode ? 'Fast demo' : 'Real AI'}</span>
|
|
145
145
|
</button>
|
|
146
146
|
)}
|
|
147
147
|
|
|
@@ -157,7 +157,7 @@ export function VirtualCommandBar({
|
|
|
157
157
|
) : (
|
|
158
158
|
<>
|
|
159
159
|
<Send size={14} style={{ marginRight: 6 }} />
|
|
160
|
-
<span>
|
|
160
|
+
<span>Launch</span>
|
|
161
161
|
</>
|
|
162
162
|
)}
|
|
163
163
|
</button>
|
|
@@ -64,14 +64,14 @@ export function VirtualEntityDrawer({
|
|
|
64
64
|
type="button"
|
|
65
65
|
onClick={onClose}
|
|
66
66
|
className="wv-drawer-close-btn"
|
|
67
|
-
title="
|
|
67
|
+
title="Close (Esc)"
|
|
68
68
|
>
|
|
69
69
|
<X size={16} />
|
|
70
70
|
</button>
|
|
71
71
|
|
|
72
72
|
<div className="wv-drawer-meta-tags">
|
|
73
73
|
{entity.pinNumber && (
|
|
74
|
-
<span className="wv-drawer-pin-tag">
|
|
74
|
+
<span className="wv-drawer-pin-tag">Desk #{entity.pinNumber}</span>
|
|
75
75
|
)}
|
|
76
76
|
{entity.department && (
|
|
77
77
|
<span className="wv-drawer-dept-tag">{entity.department}</span>
|
|
@@ -86,13 +86,13 @@ export function VirtualEntityDrawer({
|
|
|
86
86
|
<div className="wv-drawer-body">
|
|
87
87
|
{/* Status Row */}
|
|
88
88
|
<div className="wv-drawer-status-card">
|
|
89
|
-
<span className="wv-drawer-status-label"
|
|
89
|
+
<span className="wv-drawer-status-label">Work status</span>
|
|
90
90
|
<span className={`wv-drawer-status-chip ${isRunning ? 'is-running' : isAwaiting ? 'is-awaiting' : 'is-idle'}`}>
|
|
91
91
|
{isRunning
|
|
92
|
-
? '⚡
|
|
92
|
+
? '⚡ Working on a mission'
|
|
93
93
|
: isAwaiting
|
|
94
|
-
? '⏳
|
|
95
|
-
: '✓
|
|
94
|
+
? '⏳ Awaiting approval'
|
|
95
|
+
: '✓ Idle / ready for a mission'}
|
|
96
96
|
</span>
|
|
97
97
|
</div>
|
|
98
98
|
|
|
@@ -101,19 +101,19 @@ export function VirtualEntityDrawer({
|
|
|
101
101
|
<div className="wv-drawer-metrics-grid">
|
|
102
102
|
<div className="wv-drawer-metric-cell">
|
|
103
103
|
<div className="wv-metric-val">{metrics.totalRuns}</div>
|
|
104
|
-
<div className="wv-metric-lbl">
|
|
104
|
+
<div className="wv-metric-lbl">Total missions</div>
|
|
105
105
|
</div>
|
|
106
106
|
<div className="wv-drawer-metric-cell">
|
|
107
107
|
<div className="wv-metric-val is-success">
|
|
108
108
|
{metrics.successRate !== undefined ? `${Math.round(metrics.successRate)}%` : '100%'}
|
|
109
109
|
</div>
|
|
110
|
-
<div className="wv-metric-lbl">
|
|
110
|
+
<div className="wv-metric-lbl">Success rate</div>
|
|
111
111
|
</div>
|
|
112
112
|
<div className="wv-drawer-metric-cell">
|
|
113
113
|
<div className="wv-metric-val">
|
|
114
114
|
${metrics.costUsd !== undefined ? metrics.costUsd.toFixed(3) : '0.000'}
|
|
115
115
|
</div>
|
|
116
|
-
<div className="wv-metric-lbl">
|
|
116
|
+
<div className="wv-metric-lbl">Cost (last 24h)</div>
|
|
117
117
|
</div>
|
|
118
118
|
</div>
|
|
119
119
|
)}
|
|
@@ -122,7 +122,7 @@ export function VirtualEntityDrawer({
|
|
|
122
122
|
{latestTask ? (
|
|
123
123
|
<div className="wv-drawer-task-section">
|
|
124
124
|
<div className="wv-task-header">
|
|
125
|
-
<span className="wv-task-title">
|
|
125
|
+
<span className="wv-task-title">Latest / active mission</span>
|
|
126
126
|
{latestTask.createdAt && (
|
|
127
127
|
<span className="wv-task-time">
|
|
128
128
|
{new Date(latestTask.createdAt).toLocaleTimeString([], {
|
|
@@ -137,7 +137,7 @@ export function VirtualEntityDrawer({
|
|
|
137
137
|
<p className="wv-task-prompt">{latestTask.prompt}</p>
|
|
138
138
|
{latestTask.summary && (
|
|
139
139
|
<div className="wv-task-summary">
|
|
140
|
-
<strong
|
|
140
|
+
<strong>Summary: </strong>
|
|
141
141
|
{latestTask.summary}
|
|
142
142
|
</div>
|
|
143
143
|
)}
|
|
@@ -148,7 +148,7 @@ export function VirtualEntityDrawer({
|
|
|
148
148
|
className="wv-view-report-btn"
|
|
149
149
|
>
|
|
150
150
|
<FileText size={13} style={{ marginRight: 6 }} />
|
|
151
|
-
<span
|
|
151
|
+
<span>View output report</span>
|
|
152
152
|
</button>
|
|
153
153
|
)}
|
|
154
154
|
</div>
|
|
@@ -156,14 +156,14 @@ export function VirtualEntityDrawer({
|
|
|
156
156
|
) : (
|
|
157
157
|
<div className="wv-drawer-empty-task">
|
|
158
158
|
<Clock size={20} className="wv-empty-icon" />
|
|
159
|
-
<p>
|
|
159
|
+
<p>No mission has been run at this desk yet.</p>
|
|
160
160
|
</div>
|
|
161
161
|
)}
|
|
162
162
|
|
|
163
163
|
{/* Direct Dispatch to this Entity */}
|
|
164
164
|
{onDispatch && onChangeDispatchPrompt && (
|
|
165
165
|
<div className="wv-drawer-dispatch-section">
|
|
166
|
-
<span className="wv-dispatch-title">
|
|
166
|
+
<span className="wv-dispatch-title">Assign a mission to this desk</span>
|
|
167
167
|
<div className="wv-dispatch-row">
|
|
168
168
|
<input
|
|
169
169
|
type="text"
|
|
@@ -175,7 +175,7 @@ export function VirtualEntityDrawer({
|
|
|
175
175
|
onDispatch()
|
|
176
176
|
}
|
|
177
177
|
}}
|
|
178
|
-
placeholder="
|
|
178
|
+
placeholder="e.g. Draft the new campaign copy..."
|
|
179
179
|
className="wv-dispatch-input"
|
|
180
180
|
/>
|
|
181
181
|
<button
|
|
@@ -17,7 +17,7 @@ export interface VirtualReportModalProps {
|
|
|
17
17
|
export function VirtualReportModal({
|
|
18
18
|
report,
|
|
19
19
|
onClose,
|
|
20
|
-
title = '
|
|
20
|
+
title = 'Mission & output report',
|
|
21
21
|
}: VirtualReportModalProps): ReactNode {
|
|
22
22
|
useEffect(() => {
|
|
23
23
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
@@ -42,7 +42,7 @@ export function VirtualReportModal({
|
|
|
42
42
|
type="button"
|
|
43
43
|
onClick={onClose}
|
|
44
44
|
className="wv-modal-close-btn"
|
|
45
|
-
title="
|
|
45
|
+
title="Close"
|
|
46
46
|
>
|
|
47
47
|
<X size={16} />
|
|
48
48
|
</button>
|
|
@@ -51,14 +51,15 @@ export function VirtualReportModal({
|
|
|
51
51
|
{/* Modal Body */}
|
|
52
52
|
<div className="wv-modal-body">
|
|
53
53
|
<div className="wv-modal-prompt-box">
|
|
54
|
-
<span className="wv-modal-section-label"
|
|
54
|
+
<span className="wv-modal-section-label">Request / mission brief:</span>
|
|
55
55
|
<p className="wv-modal-prompt-text">{report.prompt}</p>
|
|
56
56
|
</div>
|
|
57
57
|
|
|
58
58
|
<div className="wv-modal-output-section">
|
|
59
|
-
<span className="wv-modal-section-label"
|
|
59
|
+
<span className="wv-modal-section-label">Generated output / report:</span>
|
|
60
60
|
<div className="wv-modal-output-box">
|
|
61
|
-
{report.summary ||
|
|
61
|
+
{report.summary ||
|
|
62
|
+
'Mission completed successfully. Outputs have been routed to the operations pool.'}
|
|
62
63
|
</div>
|
|
63
64
|
</div>
|
|
64
65
|
</div>
|
|
@@ -70,7 +71,7 @@ export function VirtualReportModal({
|
|
|
70
71
|
onClick={onClose}
|
|
71
72
|
className="wv-modal-btn"
|
|
72
73
|
>
|
|
73
|
-
|
|
74
|
+
Close
|
|
74
75
|
</button>
|
|
75
76
|
</div>
|
|
76
77
|
</div>
|
|
@@ -10,8 +10,7 @@ import {
|
|
|
10
10
|
Globe,
|
|
11
11
|
Shield,
|
|
12
12
|
} from 'lucide-react'
|
|
13
|
-
import type { WorldEntity } from '../core/types'
|
|
14
|
-
import type { DepartmentInfo } from '../presets/officePreset'
|
|
13
|
+
import type { WorldEntity, DepartmentInfo } from '../core/types'
|
|
15
14
|
|
|
16
15
|
export interface VirtualRosterProps {
|
|
17
16
|
entities: WorldEntity[]
|
|
@@ -37,7 +36,7 @@ export function VirtualRoster({
|
|
|
37
36
|
onSearchChange,
|
|
38
37
|
selectedCategory,
|
|
39
38
|
onSelectCategory,
|
|
40
|
-
title = '
|
|
39
|
+
title = 'Office roster',
|
|
41
40
|
className = '',
|
|
42
41
|
}: VirtualRosterProps): ReactNode {
|
|
43
42
|
// Filter entities based on search and category
|
|
@@ -47,9 +46,9 @@ export function VirtualRoster({
|
|
|
47
46
|
if (searchQuery && !nameMatch && !roleMatch) return false
|
|
48
47
|
|
|
49
48
|
if (selectedCategory !== 'all') {
|
|
50
|
-
const dept = departments.find((d) => d.slug === selectedCategory)
|
|
49
|
+
const dept = departments.find((d) => (d.slug ?? d.id) === selectedCategory)
|
|
51
50
|
if (dept) {
|
|
52
|
-
if (!dept.slotIds.includes(en.id)) return false
|
|
51
|
+
if (!(dept.slotIds ?? []).includes(en.id)) return false
|
|
53
52
|
} else if (en.department !== selectedCategory && en.zoneId !== selectedCategory) {
|
|
54
53
|
return false
|
|
55
54
|
}
|
|
@@ -85,14 +84,16 @@ export function VirtualRoster({
|
|
|
85
84
|
</div>
|
|
86
85
|
<div>
|
|
87
86
|
<h3 className="wv-roster-heading">{title}</h3>
|
|
88
|
-
<p className="wv-roster-subheading">
|
|
87
|
+
<p className="wv-roster-subheading">
|
|
88
|
+
{entities.length} {entities.length === 1 ? 'member' : 'members'} at their desks
|
|
89
|
+
</p>
|
|
89
90
|
</div>
|
|
90
91
|
</div>
|
|
91
92
|
<button
|
|
92
93
|
type="button"
|
|
93
94
|
onClick={onClose}
|
|
94
95
|
className="wv-roster-close-btn"
|
|
95
|
-
title="
|
|
96
|
+
title="Hide roster panel"
|
|
96
97
|
>
|
|
97
98
|
<X size={16} />
|
|
98
99
|
</button>
|
|
@@ -105,7 +106,7 @@ export function VirtualRoster({
|
|
|
105
106
|
type="text"
|
|
106
107
|
value={searchQuery}
|
|
107
108
|
onChange={(e) => onSearchChange(e.target.value)}
|
|
108
|
-
placeholder="
|
|
109
|
+
placeholder="Search by name or role..."
|
|
109
110
|
className="wv-roster-search-input"
|
|
110
111
|
/>
|
|
111
112
|
</div>
|
|
@@ -118,13 +119,13 @@ export function VirtualRoster({
|
|
|
118
119
|
onClick={() => onSelectCategory('all')}
|
|
119
120
|
className={`wv-roster-pill ${selectedCategory === 'all' ? 'is-active' : ''}`}
|
|
120
121
|
>
|
|
121
|
-
|
|
122
|
+
All
|
|
122
123
|
</button>
|
|
123
124
|
{departments.map((dept) => (
|
|
124
125
|
<button
|
|
125
126
|
key={dept.id}
|
|
126
127
|
type="button"
|
|
127
|
-
onClick={() => onSelectCategory(dept.slug)}
|
|
128
|
+
onClick={() => onSelectCategory(dept.slug ?? dept.id)}
|
|
128
129
|
className={`wv-roster-pill ${selectedCategory === dept.slug ? 'is-active' : ''}`}
|
|
129
130
|
>
|
|
130
131
|
{dept.name.split(' ')[0]}
|
|
@@ -169,7 +170,7 @@ export function VirtualRoster({
|
|
|
169
170
|
{en.pinNumber && <span className="wv-roster-pin">#{en.pinNumber}</span>}
|
|
170
171
|
<h4 className="wv-roster-name">{en.name}</h4>
|
|
171
172
|
</div>
|
|
172
|
-
<p className="wv-roster-role">{en.role || en.description || '
|
|
173
|
+
<p className="wv-roster-role">{en.role || en.description || 'Staff member'}</p>
|
|
173
174
|
</div>
|
|
174
175
|
</div>
|
|
175
176
|
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
forwardRef,
|
|
8
8
|
useImperativeHandle,
|
|
9
9
|
} from 'react'
|
|
10
|
+
import { MonitorOff, RotateCw } from 'lucide-react'
|
|
10
11
|
import * as THREE from 'three'
|
|
11
12
|
import type {
|
|
12
13
|
WorldDefinition,
|
|
@@ -15,7 +16,7 @@ import type {
|
|
|
15
16
|
PinDisplayMode,
|
|
16
17
|
} from '../core/types'
|
|
17
18
|
import { WorldEngine } from '../engine/renderer'
|
|
18
|
-
import { createStylizedAvatar } from '../entities/people/AvatarBuilder'
|
|
19
|
+
import { createStylizedAvatar, type CreatedAvatar } from '../entities/people/AvatarBuilder'
|
|
19
20
|
import { EntityPins } from './EntityPins'
|
|
20
21
|
import { WorldControls } from './WorldControls'
|
|
21
22
|
import { EntityInspector } from './EntityInspector'
|
|
@@ -77,13 +78,33 @@ export const VirtualWorld = forwardRef<VirtualWorldRef, VirtualWorldProps>(
|
|
|
77
78
|
const engineRef = useRef<WorldEngine | null>(null)
|
|
78
79
|
const pinElementsRef = useRef<Map<string, HTMLElement>>(new Map())
|
|
79
80
|
|
|
81
|
+
// Hot-sync registry: avatars are created once per entity and updated in place.
|
|
82
|
+
const avatarRegistryRef = useRef<Map<string, CreatedAvatar>>(new Map())
|
|
83
|
+
const avatarGroupRef = useRef<THREE.Group | null>(null)
|
|
84
|
+
const staticInteractivesRef = useRef<THREE.Object3D[]>([])
|
|
85
|
+
|
|
86
|
+
// Latest props/refs readable by stable engine callbacks (prevents engine re-init churn)
|
|
87
|
+
const worldRef = useRef(world)
|
|
88
|
+
const nightModeRef = useRef(false)
|
|
89
|
+
const callbacksRef = useRef({
|
|
90
|
+
onSelectEntity: (_entity: WorldEntity | null) => {},
|
|
91
|
+
onHoverEntity: (_entity: WorldEntity | null) => {},
|
|
92
|
+
})
|
|
93
|
+
|
|
80
94
|
const [internalNightMode, setInternalNightMode] = useState(false)
|
|
81
95
|
const [internalPinMode, setInternalPinMode] = useState<PinDisplayMode>(defaultPinMode)
|
|
82
96
|
const [fps, setFps] = useState<number>(60)
|
|
83
97
|
const [internalSelectedEntity, setInternalSelectedEntity] = useState<WorldEntity | null>(null)
|
|
84
98
|
|
|
99
|
+
// WebGL resilience state
|
|
100
|
+
const [webglFailed, setWebglFailed] = useState(false)
|
|
101
|
+
const [contextLost, setContextLost] = useState(false)
|
|
102
|
+
const [engineEpoch, setEngineEpoch] = useState(0)
|
|
103
|
+
|
|
85
104
|
const nightMode = externalNightMode !== undefined ? externalNightMode : internalNightMode
|
|
86
105
|
const pinMode = externalPinMode !== undefined ? externalPinMode : internalPinMode
|
|
106
|
+
nightModeRef.current = nightMode
|
|
107
|
+
worldRef.current = world
|
|
87
108
|
|
|
88
109
|
// Determine active selected entity (controlled or uncontrolled)
|
|
89
110
|
const activeEntity =
|
|
@@ -101,6 +122,14 @@ export const VirtualWorld = forwardRef<VirtualWorldRef, VirtualWorldProps>(
|
|
|
101
122
|
[onSelectEntity],
|
|
102
123
|
)
|
|
103
124
|
|
|
125
|
+
// Keep stable engine callbacks pointing at the latest handler identities
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
callbacksRef.current = {
|
|
128
|
+
onSelectEntity: handleSelectEntity,
|
|
129
|
+
onHoverEntity: onHoverEntity || (() => {}),
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
|
|
104
133
|
const handleRegisterPinElement = useCallback((entityId: string, el: HTMLElement | null) => {
|
|
105
134
|
if (el) {
|
|
106
135
|
pinElementsRef.current.set(entityId, el)
|
|
@@ -109,59 +138,63 @@ export const VirtualWorld = forwardRef<VirtualWorldRef, VirtualWorldProps>(
|
|
|
109
138
|
}
|
|
110
139
|
}, [])
|
|
111
140
|
|
|
112
|
-
//
|
|
141
|
+
// ============================================================
|
|
142
|
+
// ENGINE LIFECYCLE — created once per preset (NOT per world data
|
|
143
|
+
// change). Scene, lighting, camera and GLB models survive
|
|
144
|
+
// entity/polling updates; only avatars are diffed below.
|
|
145
|
+
// ============================================================
|
|
113
146
|
useEffect(() => {
|
|
114
147
|
const canvas = canvasRef.current
|
|
115
148
|
const container = containerRef.current
|
|
116
149
|
if (!canvas || !container) return
|
|
117
150
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
})
|
|
151
|
+
let engine: WorldEngine
|
|
152
|
+
try {
|
|
153
|
+
engine = new WorldEngine({
|
|
154
|
+
canvas,
|
|
155
|
+
world: worldRef.current,
|
|
156
|
+
pinElements: pinElementsRef.current,
|
|
157
|
+
onSelectEntity: (evt) => callbacksRef.current.onSelectEntity(evt.entity),
|
|
158
|
+
onHoverEntity: (entity) => callbacksRef.current.onHoverEntity(entity),
|
|
159
|
+
onFpsUpdate: (currentFps) => {
|
|
160
|
+
setFps(currentFps)
|
|
161
|
+
},
|
|
162
|
+
onContextLost: () => setContextLost(true),
|
|
163
|
+
})
|
|
164
|
+
} catch (err) {
|
|
165
|
+
console.error('[web-virtual] WebGL is unavailable, rendering fallback UI:', err)
|
|
166
|
+
setWebglFailed(true)
|
|
167
|
+
return
|
|
168
|
+
}
|
|
169
|
+
|
|
132
170
|
engineRef.current = engine
|
|
171
|
+
setWebglFailed(false)
|
|
172
|
+
setContextLost(false)
|
|
133
173
|
|
|
134
174
|
// 1. Build environment from preset or custom scene builder
|
|
135
175
|
const activePreset = preset
|
|
136
176
|
if (activePreset && activePreset.buildScene) {
|
|
137
177
|
activePreset.buildScene(engine.scene, {
|
|
138
|
-
nightMode,
|
|
139
|
-
theme:
|
|
178
|
+
nightMode: nightModeRef.current,
|
|
179
|
+
theme: worldRef.current.theme,
|
|
140
180
|
})
|
|
141
181
|
}
|
|
142
182
|
|
|
143
|
-
// 2.
|
|
183
|
+
// 2. Avatar container — populated by the entity sync effect below
|
|
144
184
|
const avatarGroup = new THREE.Group()
|
|
145
185
|
avatarGroup.name = 'world-avatars'
|
|
146
|
-
|
|
147
|
-
for (const entity of world.entities) {
|
|
148
|
-
if (entity.type === 'person' || entity.type === 'actor') {
|
|
149
|
-
const { group, animate } = createStylizedAvatar(entity, engine.materials)
|
|
150
|
-
avatarGroup.add(group)
|
|
151
|
-
engine.animators.push(animate)
|
|
152
|
-
engine.interactiveObjects.push(group)
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
186
|
engine.scene.add(avatarGroup)
|
|
187
|
+
avatarGroupRef.current = avatarGroup
|
|
156
188
|
|
|
157
|
-
// 3.
|
|
189
|
+
// 3. Collect static interactive objects (workstations, chairs, monitors)
|
|
190
|
+
const staticInteractives: THREE.Object3D[] = []
|
|
158
191
|
engine.scene.traverse((obj) => {
|
|
159
192
|
if (obj.userData && obj.userData.entityId) {
|
|
160
|
-
|
|
161
|
-
engine.interactiveObjects.push(obj)
|
|
162
|
-
}
|
|
193
|
+
staticInteractives.push(obj)
|
|
163
194
|
}
|
|
164
195
|
})
|
|
196
|
+
staticInteractivesRef.current = staticInteractives
|
|
197
|
+
engine.interactiveObjects = [...staticInteractives]
|
|
165
198
|
|
|
166
199
|
// Resize Observer
|
|
167
200
|
const resizeObserver = new ResizeObserver((entries) => {
|
|
@@ -176,11 +209,61 @@ export const VirtualWorld = forwardRef<VirtualWorldRef, VirtualWorldProps>(
|
|
|
176
209
|
|
|
177
210
|
return () => {
|
|
178
211
|
resizeObserver.disconnect()
|
|
212
|
+
avatarRegistryRef.current.clear()
|
|
213
|
+
avatarGroupRef.current = null
|
|
214
|
+
staticInteractivesRef.current = []
|
|
179
215
|
engine.dispose()
|
|
180
216
|
engineRef.current = null
|
|
181
|
-
pinElementsRef.current.clear()
|
|
182
217
|
}
|
|
183
|
-
}, [
|
|
218
|
+
}, [preset, engineEpoch])
|
|
219
|
+
|
|
220
|
+
// ============================================================
|
|
221
|
+
// ENTITY HOT-SYNC — diff world.entities against the avatar
|
|
222
|
+
// registry: add new avatars, update changed ones in place
|
|
223
|
+
// (status halo, animation flags, transform), remove stale ones.
|
|
224
|
+
// No scene rebuild, no GLB reload, no camera reset.
|
|
225
|
+
// ============================================================
|
|
226
|
+
useEffect(() => {
|
|
227
|
+
const engine = engineRef.current
|
|
228
|
+
const avatarGroup = avatarGroupRef.current
|
|
229
|
+
if (!engine || !avatarGroup) return
|
|
230
|
+
|
|
231
|
+
// Always point the engine (raycaster/projector/focus) at the latest data
|
|
232
|
+
engine.updateWorld(world)
|
|
233
|
+
|
|
234
|
+
const registry = avatarRegistryRef.current
|
|
235
|
+
const seen = new Set<string>()
|
|
236
|
+
|
|
237
|
+
for (const entity of world.entities) {
|
|
238
|
+
if (entity.type !== 'person' && entity.type !== 'actor') continue
|
|
239
|
+
seen.add(entity.id)
|
|
240
|
+
|
|
241
|
+
const existing = registry.get(entity.id)
|
|
242
|
+
if (existing) {
|
|
243
|
+
existing.update(entity)
|
|
244
|
+
continue
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const created = createStylizedAvatar(entity, engine.materials)
|
|
248
|
+
registry.set(entity.id, created)
|
|
249
|
+
avatarGroup.add(created.group)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Remove avatars for entities that no longer exist
|
|
253
|
+
for (const [id, created] of registry) {
|
|
254
|
+
if (!seen.has(id)) {
|
|
255
|
+
created.group.removeFromParent()
|
|
256
|
+
registry.delete(id)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Rebuild animator + interactive lists from the registry
|
|
261
|
+
engine.animators = Array.from(registry.values()).map((a) => a.animate)
|
|
262
|
+
engine.interactiveObjects = [
|
|
263
|
+
...staticInteractivesRef.current,
|
|
264
|
+
...Array.from(registry.values()).map((a) => a.group),
|
|
265
|
+
]
|
|
266
|
+
}, [world, preset, engineEpoch])
|
|
184
267
|
|
|
185
268
|
// Sync Night Mode with engine
|
|
186
269
|
useEffect(() => {
|
|
@@ -252,16 +335,68 @@ export const VirtualWorld = forwardRef<VirtualWorldRef, VirtualWorldProps>(
|
|
|
252
335
|
[],
|
|
253
336
|
)
|
|
254
337
|
|
|
338
|
+
const handleRestoreContext = useCallback(() => {
|
|
339
|
+
// Rebuild the engine from scratch (fresh WebGL context, fresh scene)
|
|
340
|
+
setContextLost(false)
|
|
341
|
+
setEngineEpoch((epoch) => epoch + 1)
|
|
342
|
+
}, [])
|
|
343
|
+
|
|
344
|
+
// Graceful fallback when WebGL is not available at all
|
|
345
|
+
if (webglFailed) {
|
|
346
|
+
return (
|
|
347
|
+
<div
|
|
348
|
+
ref={containerRef}
|
|
349
|
+
className={`wv-container wv-webgl-fallback ${className}`}
|
|
350
|
+
style={style}
|
|
351
|
+
role="region"
|
|
352
|
+
aria-label={`${world.name} 3D environment`}
|
|
353
|
+
>
|
|
354
|
+
<div className="wv-fallback-card">
|
|
355
|
+
<MonitorOff size={28} />
|
|
356
|
+
<h3>3D view unavailable</h3>
|
|
357
|
+
<p>
|
|
358
|
+
Your browser or device does not support WebGL, which is required for the 3D
|
|
359
|
+
environment. Entity data remains available below.
|
|
360
|
+
</p>
|
|
361
|
+
<ul className="wv-fallback-entity-list">
|
|
362
|
+
{world.entities.slice(0, 12).map((entity) => (
|
|
363
|
+
<li key={entity.id}>
|
|
364
|
+
<span className={`wv-status-pip ${entity.status || 'idle'}`} />
|
|
365
|
+
{entity.pinNumber ? `#${entity.pinNumber} ` : ''}
|
|
366
|
+
{entity.name}
|
|
367
|
+
</li>
|
|
368
|
+
))}
|
|
369
|
+
</ul>
|
|
370
|
+
</div>
|
|
371
|
+
{children}
|
|
372
|
+
</div>
|
|
373
|
+
)
|
|
374
|
+
}
|
|
375
|
+
|
|
255
376
|
return (
|
|
256
377
|
<div
|
|
257
378
|
ref={containerRef}
|
|
258
379
|
className={`wv-container ${nightMode ? 'is-night' : ''} ${className}`}
|
|
259
380
|
style={style}
|
|
260
381
|
role="region"
|
|
261
|
-
aria-label={`${world.name} 3D
|
|
382
|
+
aria-label={`${world.name} 3D environment`}
|
|
262
383
|
>
|
|
263
384
|
<canvas ref={canvasRef} className="wv-canvas" />
|
|
264
385
|
|
|
386
|
+
{/* WebGL context lost overlay (e.g. GPU driver reset) */}
|
|
387
|
+
{contextLost && (
|
|
388
|
+
<div className="wv-context-lost-overlay">
|
|
389
|
+
<div className="wv-fallback-card">
|
|
390
|
+
<RotateCw size={26} />
|
|
391
|
+
<h3>3D view interrupted</h3>
|
|
392
|
+
<p>The graphics context was lost (usually a GPU or tab reset). Restore it below.</p>
|
|
393
|
+
<button type="button" className="wv-fallback-btn" onClick={handleRestoreContext}>
|
|
394
|
+
Restore 3D view
|
|
395
|
+
</button>
|
|
396
|
+
</div>
|
|
397
|
+
</div>
|
|
398
|
+
)}
|
|
399
|
+
|
|
265
400
|
{/* Floating 60fps GPU Projected Pins */}
|
|
266
401
|
<EntityPins
|
|
267
402
|
entities={world.entities}
|