@umituz/web-virtual 1.0.0 → 1.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-virtual",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Universal 3D Virtual Environment Engine for Web & React — Offices, Farms, Factories, Schools, Hospitals with 60fps performance, raycasting and interactive entities.",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -4,6 +4,8 @@ import {
4
4
  useRef,
5
5
  useState,
6
6
  useCallback,
7
+ forwardRef,
8
+ useImperativeHandle,
7
9
  } from 'react'
8
10
  import * as THREE from 'three'
9
11
  import type {
@@ -11,7 +13,6 @@ import type {
11
13
  WorldEntity,
12
14
  WorldPreset,
13
15
  PinDisplayMode,
14
- EntitySelectedEvent,
15
16
  } from '../core/types'
16
17
  import { WorldEngine } from '../engine/renderer'
17
18
  import { createStylizedAvatar } from '../entities/people/AvatarBuilder'
@@ -20,6 +21,14 @@ import { WorldControls } from './WorldControls'
20
21
  import { EntityInspector } from './EntityInspector'
21
22
  import type { CameraPresetName } from '../engine/camera'
22
23
 
24
+ export interface VirtualWorldRef {
25
+ focusOnEntity: (entityId: string) => void
26
+ setCameraPreset: (preset: CameraPresetName) => void
27
+ resetCamera: () => void
28
+ setNightMode: (isNight: boolean) => void
29
+ resize: () => void
30
+ }
31
+
23
32
  export interface VirtualWorldProps {
24
33
  world: WorldDefinition
25
34
  preset?: WorldPreset
@@ -36,194 +45,226 @@ export interface VirtualWorldProps {
36
45
  renderCustomInspectorActions?: (entity: WorldEntity) => ReactNode
37
46
  }
38
47
 
39
- export function VirtualWorld({
40
- world,
41
- preset,
42
- selectedEntityId,
43
- onSelectEntity,
44
- onHoverEntity,
45
- defaultPinMode = 'pins',
46
- showControls = true,
47
- showInspector = true,
48
- className = '',
49
- style,
50
- children,
51
- renderCustomInspectorContent,
52
- renderCustomInspectorActions,
53
- }: VirtualWorldProps): ReactNode {
54
- const containerRef = useRef<HTMLDivElement>(null)
55
- const canvasRef = useRef<HTMLCanvasElement>(null)
56
- const engineRef = useRef<WorldEngine | null>(null)
57
- const pinElementsRef = useRef<Map<string, HTMLElement>>(new Map())
58
-
59
- const [nightMode, setNightMode] = useState(false)
60
- const [pinMode, setPinMode] = useState<PinDisplayMode>(defaultPinMode)
61
- const [fps, setFps] = useState<number>(60)
62
- const [internalSelectedEntity, setInternalSelectedEntity] = useState<WorldEntity | null>(null)
63
-
64
- // Determine active selected entity (controlled or uncontrolled)
65
- const activeEntity =
66
- selectedEntityId !== undefined
67
- ? world.entities.find((e) => e.id === selectedEntityId) || null
68
- : internalSelectedEntity
69
-
70
- const handleSelectEntity = useCallback(
71
- (entity: WorldEntity | null) => {
72
- setInternalSelectedEntity(entity)
73
- if (onSelectEntity) {
74
- onSelectEntity(entity)
75
- }
76
- },
77
- [onSelectEntity],
78
- )
79
-
80
- const handleRegisterPinElement = useCallback((entityId: string, el: HTMLElement | null) => {
81
- if (el) {
82
- pinElementsRef.current.set(entityId, el)
83
- } else {
84
- pinElementsRef.current.delete(entityId)
85
- }
86
- }, [])
87
-
88
- // Initialize Three.js Engine
89
- useEffect(() => {
90
- const canvas = canvasRef.current
91
- const container = containerRef.current
92
- if (!canvas || !container) return
93
-
94
- const engine = new WorldEngine({
95
- canvas,
48
+ export const VirtualWorld = forwardRef<VirtualWorldRef, VirtualWorldProps>(
49
+ function VirtualWorld(
50
+ {
96
51
  world,
97
- pinElements: pinElementsRef.current,
98
- onSelectEntity: (evt) => {
99
- handleSelectEntity(evt.entity)
100
- },
101
- onHoverEntity: (entity) => {
102
- if (onHoverEntity) onHoverEntity(entity)
103
- },
104
- onFpsUpdate: (currentFps) => {
105
- setFps(currentFps)
52
+ preset,
53
+ selectedEntityId,
54
+ onSelectEntity,
55
+ onHoverEntity,
56
+ defaultPinMode = 'pins',
57
+ showControls = true,
58
+ showInspector = true,
59
+ className = '',
60
+ style,
61
+ children,
62
+ renderCustomInspectorContent,
63
+ renderCustomInspectorActions,
64
+ },
65
+ ref,
66
+ ): ReactNode {
67
+ const containerRef = useRef<HTMLDivElement>(null)
68
+ const canvasRef = useRef<HTMLCanvasElement>(null)
69
+ const engineRef = useRef<WorldEngine | null>(null)
70
+ const pinElementsRef = useRef<Map<string, HTMLElement>>(new Map())
71
+
72
+ const [nightMode, setNightMode] = useState(false)
73
+ const [pinMode, setPinMode] = useState<PinDisplayMode>(defaultPinMode)
74
+ const [fps, setFps] = useState<number>(60)
75
+ const [internalSelectedEntity, setInternalSelectedEntity] = useState<WorldEntity | null>(null)
76
+
77
+ // Determine active selected entity (controlled or uncontrolled)
78
+ const activeEntity =
79
+ selectedEntityId !== undefined
80
+ ? world.entities.find((e) => e.id === selectedEntityId) || null
81
+ : internalSelectedEntity
82
+
83
+ const handleSelectEntity = useCallback(
84
+ (entity: WorldEntity | null) => {
85
+ setInternalSelectedEntity(entity)
86
+ if (onSelectEntity) {
87
+ onSelectEntity(entity)
88
+ }
106
89
  },
107
- })
108
- engineRef.current = engine
109
-
110
- // 1. Build environment from preset or custom scene builder
111
- const activePreset = preset
112
- if (activePreset && activePreset.buildScene) {
113
- activePreset.buildScene(engine.scene, {
114
- nightMode,
115
- theme: world.theme,
90
+ [onSelectEntity],
91
+ )
92
+
93
+ const handleRegisterPinElement = useCallback((entityId: string, el: HTMLElement | null) => {
94
+ if (el) {
95
+ pinElementsRef.current.set(entityId, el)
96
+ } else {
97
+ pinElementsRef.current.delete(entityId)
98
+ }
99
+ }, [])
100
+
101
+ // Initialize Three.js Engine
102
+ useEffect(() => {
103
+ const canvas = canvasRef.current
104
+ const container = containerRef.current
105
+ if (!canvas || !container) return
106
+
107
+ const engine = new WorldEngine({
108
+ canvas,
109
+ world,
110
+ pinElements: pinElementsRef.current,
111
+ onSelectEntity: (evt) => {
112
+ handleSelectEntity(evt.entity)
113
+ },
114
+ onHoverEntity: (entity) => {
115
+ if (onHoverEntity) onHoverEntity(entity)
116
+ },
117
+ onFpsUpdate: (currentFps) => {
118
+ setFps(currentFps)
119
+ },
120
+ })
121
+ engineRef.current = engine
122
+
123
+ // 1. Build environment from preset or custom scene builder
124
+ const activePreset = preset
125
+ if (activePreset && activePreset.buildScene) {
126
+ activePreset.buildScene(engine.scene, {
127
+ nightMode,
128
+ theme: world.theme,
129
+ })
130
+ }
131
+
132
+ // 2. Build 3D Avatars for people/actor entities
133
+ const avatarGroup = new THREE.Group()
134
+ avatarGroup.name = 'world-avatars'
135
+
136
+ for (const entity of world.entities) {
137
+ if (entity.type === 'person' || entity.type === 'actor') {
138
+ const { group, animate } = createStylizedAvatar(entity, engine.materials)
139
+ avatarGroup.add(group)
140
+ engine.animators.push(animate)
141
+ engine.interactiveObjects.push(group)
142
+ }
143
+ }
144
+ engine.scene.add(avatarGroup)
145
+
146
+ // Resize Observer
147
+ const resizeObserver = new ResizeObserver((entries) => {
148
+ for (const entry of entries) {
149
+ const { width, height } = entry.contentRect
150
+ if (width > 0 && height > 0) {
151
+ engine.resize(width, height)
152
+ }
153
+ }
116
154
  })
117
- }
118
-
119
- // 2. Build 3D Avatars for people/actor entities
120
- const avatarGroup = new THREE.Group()
121
- avatarGroup.name = 'world-avatars'
122
-
123
- for (const entity of world.entities) {
124
- if (entity.type === 'person' || entity.type === 'actor') {
125
- const { group, animate } = createStylizedAvatar(entity, engine.materials)
126
- avatarGroup.add(group)
127
- engine.animators.push(animate)
128
- engine.interactiveObjects.push(group)
155
+ resizeObserver.observe(container)
156
+
157
+ return () => {
158
+ resizeObserver.disconnect()
159
+ engine.dispose()
160
+ engineRef.current = null
161
+ pinElementsRef.current.clear()
129
162
  }
130
- }
131
- engine.scene.add(avatarGroup)
132
-
133
- // Resize Observer
134
- const resizeObserver = new ResizeObserver((entries) => {
135
- for (const entry of entries) {
136
- const { width, height } = entry.contentRect
137
- if (width > 0 && height > 0) {
138
- engine.resize(width, height)
163
+ }, [world, preset, handleSelectEntity, onHoverEntity])
164
+
165
+ // Handle Night Mode Toggle
166
+ const handleToggleNightMode = useCallback(() => {
167
+ setNightMode((prev) => {
168
+ const next = !prev
169
+ if (engineRef.current) {
170
+ engineRef.current.setNightMode(next)
139
171
  }
172
+ return next
173
+ })
174
+ }, [])
175
+
176
+ // Handle Camera Presets
177
+ const handleCameraPreset = useCallback((presetName: CameraPresetName) => {
178
+ if (engineRef.current) {
179
+ engineRef.current.setCameraPreset(presetName)
180
+ }
181
+ }, [])
182
+
183
+ const handleResetCamera = useCallback(() => {
184
+ if (engineRef.current) {
185
+ engineRef.current.resetCamera()
140
186
  }
141
- })
142
- resizeObserver.observe(container)
143
-
144
- return () => {
145
- resizeObserver.disconnect()
146
- engine.dispose()
147
- engineRef.current = null
148
- pinElementsRef.current.clear()
149
- }
150
- }, [world, preset, handleSelectEntity, onHoverEntity])
151
-
152
- // Handle Night Mode Toggle
153
- const handleToggleNightMode = useCallback(() => {
154
- setNightMode((prev) => {
155
- const next = !prev
187
+ }, [])
188
+
189
+ const handleFocusEntity = useCallback((entity: WorldEntity) => {
156
190
  if (engineRef.current) {
157
- engineRef.current.setNightMode(next)
191
+ engineRef.current.focusOnEntity(entity.id)
158
192
  }
159
- return next
160
- })
161
- }, [])
162
-
163
- // Handle Camera Presets
164
- const handleCameraPreset = useCallback((presetName: CameraPresetName) => {
165
- if (engineRef.current) {
166
- engineRef.current.setCameraPreset(presetName)
167
- }
168
- }, [])
169
-
170
- const handleResetCamera = useCallback(() => {
171
- if (engineRef.current) {
172
- engineRef.current.resetCamera()
173
- }
174
- }, [])
175
-
176
- const handleFocusEntity = useCallback((entity: WorldEntity) => {
177
- if (engineRef.current) {
178
- engineRef.current.focusOnEntity(entity.id)
179
- }
180
- }, [])
181
-
182
- return (
183
- <div
184
- ref={containerRef}
185
- className={`wv-container ${className}`}
186
- style={style}
187
- role="region"
188
- aria-label={`${world.name} 3D Sanal Ortamı`}
189
- >
190
- <canvas ref={canvasRef} className="wv-canvas" />
191
-
192
- {/* Floating 60fps GPU Projected Pins */}
193
- <EntityPins
194
- entities={world.entities}
195
- mode={pinMode}
196
- selectedEntityId={activeEntity?.id}
197
- onSelectEntity={handleSelectEntity}
198
- onRegisterElement={handleRegisterPinElement}
199
- />
200
-
201
- {/* Floating View Controls */}
202
- {showControls && (
203
- <WorldControls
204
- nightMode={nightMode}
205
- onToggleNightMode={handleToggleNightMode}
206
- pinMode={pinMode}
207
- onChangePinMode={setPinMode}
208
- onSelectCameraPreset={handleCameraPreset}
209
- onResetCamera={handleResetCamera}
210
- fps={fps}
211
- />
212
- )}
213
-
214
- {/* Slide-out Entity Contextual Inspector */}
215
- {showInspector && activeEntity && (
216
- <EntityInspector
217
- entity={activeEntity}
218
- onClose={() => handleSelectEntity(null)}
219
- onFocusEntity={handleFocusEntity}
220
- renderCustomContent={renderCustomInspectorContent}
221
- renderCustomActions={renderCustomInspectorActions}
193
+ }, [])
194
+
195
+ // Imperative API for parent refs
196
+ useImperativeHandle(
197
+ ref,
198
+ () => ({
199
+ focusOnEntity: (id: string) => {
200
+ if (engineRef.current) engineRef.current.focusOnEntity(id)
201
+ },
202
+ setCameraPreset: (presetName: CameraPresetName) => {
203
+ if (engineRef.current) engineRef.current.setCameraPreset(presetName)
204
+ },
205
+ resetCamera: () => {
206
+ if (engineRef.current) engineRef.current.resetCamera()
207
+ },
208
+ setNightMode: (isNight: boolean) => {
209
+ setNightMode(isNight)
210
+ if (engineRef.current) engineRef.current.setNightMode(isNight)
211
+ },
212
+ resize: () => {
213
+ if (engineRef.current && containerRef.current) {
214
+ const { clientWidth, clientHeight } = containerRef.current
215
+ engineRef.current.resize(clientWidth, clientHeight)
216
+ }
217
+ },
218
+ }),
219
+ [],
220
+ )
221
+
222
+ return (
223
+ <div
224
+ ref={containerRef}
225
+ className={`wv-container ${className}`}
226
+ style={style}
227
+ role="region"
228
+ aria-label={`${world.name} 3D Sanal Ortamı`}
229
+ >
230
+ <canvas ref={canvasRef} className="wv-canvas" />
231
+
232
+ {/* Floating 60fps GPU Projected Pins */}
233
+ <EntityPins
234
+ entities={world.entities}
235
+ mode={pinMode}
236
+ selectedEntityId={activeEntity?.id}
237
+ onSelectEntity={handleSelectEntity}
238
+ onRegisterElement={handleRegisterPinElement}
222
239
  />
223
- )}
224
240
 
225
- {/* Consumer UI Overlays (e.g., bottom dispatch bar, top mode switcher) */}
226
- {children}
227
- </div>
228
- )
229
- }
241
+ {/* Floating View Controls */}
242
+ {showControls && (
243
+ <WorldControls
244
+ nightMode={nightMode}
245
+ onToggleNightMode={handleToggleNightMode}
246
+ pinMode={pinMode}
247
+ onChangePinMode={setPinMode}
248
+ onSelectCameraPreset={handleCameraPreset}
249
+ onResetCamera={handleResetCamera}
250
+ fps={fps}
251
+ />
252
+ )}
253
+
254
+ {/* Slide-out Entity Contextual Inspector */}
255
+ {showInspector && activeEntity && (
256
+ <EntityInspector
257
+ entity={activeEntity}
258
+ onClose={() => handleSelectEntity(null)}
259
+ onFocusEntity={handleFocusEntity}
260
+ renderCustomContent={renderCustomInspectorContent}
261
+ renderCustomActions={renderCustomInspectorActions}
262
+ />
263
+ )}
264
+
265
+ {/* Consumer UI Overlays (e.g., bottom dispatch bar, top mode switcher) */}
266
+ {children}
267
+ </div>
268
+ )
269
+ },
270
+ )
@@ -1,6 +1,6 @@
1
1
  import { describe, it, expect } from 'vitest'
2
2
  import { createEnvironment, validateEnvironment } from './environment'
3
- import { officePreset, farmPreset, factoryPreset, schoolPreset, hospitalPreset } from '../presets'
3
+ import { officePreset, farmPreset, factoryPreset, schoolPreset, hospitalPreset } from '../presets/index'
4
4
 
5
5
  describe('Environment Factory & Presets', () => {
6
6
  it('creates an environment using officePreset', () => {
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // @umituz/web-virtual — Universal 3D Virtual Environment Engine
2
- export * from './core'
3
- export * from './engine'
4
- export * from './entities'
5
- export * from './presets'
6
- export * from './components'
2
+ export * from './core/index'
3
+ export * from './engine/index'
4
+ export * from './entities/index'
5
+ export * from './presets/index'
6
+ export * from './components/index'