@umituz/web-virtual 0.0.0-stage → 1.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.
@@ -0,0 +1,111 @@
1
+ import * as THREE from 'three'
2
+ import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
3
+ import type { CameraConfiguration, Vector3Tuple } from '../core/types'
4
+
5
+ export type CameraPresetName = 'isometric' | 'top' | 'front' | 'side'
6
+
7
+ export class WorldCamera {
8
+ public camera: THREE.PerspectiveCamera
9
+ public controls: OrbitControls
10
+
11
+ private targetPosition = new THREE.Vector3()
12
+ private targetLookAt = new THREE.Vector3()
13
+ private isTransitioning = false
14
+ private transitionSpeed = 0.08
15
+
16
+ constructor(
17
+ canvas: HTMLCanvasElement,
18
+ aspect: number,
19
+ private config: CameraConfiguration,
20
+ ) {
21
+ this.camera = new THREE.PerspectiveCamera(
22
+ config.fov || 40,
23
+ aspect,
24
+ config.near || 0.1,
25
+ config.far || 250,
26
+ )
27
+
28
+ const pos = config.position || [26, 24, 26]
29
+ this.camera.position.set(pos[0], pos[1], pos[2])
30
+
31
+ this.controls = new OrbitControls(this.camera, canvas)
32
+ this.controls.enableDamping = config.enableDamping ?? true
33
+ this.controls.dampingFactor = config.dampingFactor ?? 0.05
34
+ this.controls.maxPolarAngle = config.maxPolarAngle ?? Math.PI / 2 - 0.05
35
+ this.controls.minDistance = config.minDistance ?? 8
36
+ this.controls.maxDistance = config.maxDistance ?? 65
37
+
38
+ const tgt = config.target || [0, 0, 0]
39
+ this.controls.target.set(tgt[0], tgt[1], tgt[2])
40
+ this.controls.update()
41
+ }
42
+
43
+ updateAspect(aspect: number): void {
44
+ this.camera.aspect = aspect
45
+ this.camera.updateProjectionMatrix()
46
+ }
47
+
48
+ update(): void {
49
+ if (this.isTransitioning) {
50
+ this.camera.position.lerp(this.targetPosition, this.transitionSpeed)
51
+ this.controls.target.lerp(this.targetLookAt, this.transitionSpeed)
52
+
53
+ if (
54
+ this.camera.position.distanceTo(this.targetPosition) < 0.1 &&
55
+ this.controls.target.distanceTo(this.targetLookAt) < 0.1
56
+ ) {
57
+ this.camera.position.copy(this.targetPosition)
58
+ this.controls.target.copy(this.targetLookAt)
59
+ this.isTransitioning = false
60
+ }
61
+ }
62
+
63
+ this.controls.update()
64
+ }
65
+
66
+ focusOn(position: Vector3Tuple, distance = 8): void {
67
+ const tgt = new THREE.Vector3(position[0], position[1], position[2])
68
+ const offset = new THREE.Vector3(distance * 0.7, distance * 0.7, distance * 0.7)
69
+
70
+ this.targetLookAt.copy(tgt)
71
+ this.targetPosition.copy(tgt).add(offset)
72
+ this.isTransitioning = true
73
+ }
74
+
75
+ setPreset(preset: CameraPresetName): void {
76
+ const tgt = new THREE.Vector3(0, 0, 0)
77
+ let pos = new THREE.Vector3(26, 24, 26)
78
+
79
+ switch (preset) {
80
+ case 'isometric':
81
+ pos = new THREE.Vector3(26, 24, 26)
82
+ break
83
+ case 'top':
84
+ pos = new THREE.Vector3(0, 36, 0.01)
85
+ break
86
+ case 'front':
87
+ pos = new THREE.Vector3(0, 15, 30)
88
+ break
89
+ case 'side':
90
+ pos = new THREE.Vector3(32, 16, 0)
91
+ break
92
+ }
93
+
94
+ this.targetLookAt.copy(tgt)
95
+ this.targetPosition.copy(pos)
96
+ this.isTransitioning = true
97
+ }
98
+
99
+ reset(): void {
100
+ const defaultPos = this.config.position || [26, 24, 26]
101
+ const defaultTgt = this.config.target || [0, 0, 0]
102
+
103
+ this.targetPosition.set(defaultPos[0], defaultPos[1], defaultPos[2])
104
+ this.targetLookAt.set(defaultTgt[0], defaultTgt[1], defaultTgt[2])
105
+ this.isTransitioning = true
106
+ }
107
+
108
+ dispose(): void {
109
+ this.controls.dispose()
110
+ }
111
+ }
@@ -0,0 +1,6 @@
1
+ export * from './materials'
2
+ export * from './lighting'
3
+ export * from './camera'
4
+ export * from './raycaster'
5
+ export * from './performance'
6
+ export * from './renderer'
@@ -0,0 +1,81 @@
1
+ import * as THREE from 'three'
2
+ import type { LightingConfiguration } from '../core/types'
3
+
4
+ export class WorldLighting {
5
+ private ambientLight: THREE.AmbientLight
6
+ private mainSun: THREE.DirectionalLight
7
+ private fillLight: THREE.DirectionalLight
8
+ private nightLight: THREE.DirectionalLight
9
+ private hemisphereLight: THREE.HemisphereLight
10
+
11
+ constructor(
12
+ private scene: THREE.Scene,
13
+ private config: LightingConfiguration = {},
14
+ ) {
15
+ const ambientColor = new THREE.Color(config.ambientColor || '#ffffff')
16
+ this.ambientLight = new THREE.AmbientLight(ambientColor, config.ambientIntensity ?? 0.85)
17
+ this.scene.add(this.ambientLight)
18
+
19
+ this.hemisphereLight = new THREE.HemisphereLight(0xffffff, 0x444455, 0.4)
20
+ this.hemisphereLight.position.set(0, 50, 0)
21
+ this.scene.add(this.hemisphereLight)
22
+
23
+ const sunColor = new THREE.Color(config.sunColor || '#fff5ea')
24
+ this.mainSun = new THREE.DirectionalLight(sunColor, config.sunIntensity ?? 1.1)
25
+ const sunPos = config.sunPosition || [20, 30, 15]
26
+ this.mainSun.position.set(sunPos[0], sunPos[1], sunPos[2])
27
+
28
+ if (config.shadows !== false) {
29
+ this.mainSun.castShadow = true
30
+ this.mainSun.shadow.mapSize.width = 2048
31
+ this.mainSun.shadow.mapSize.height = 2048
32
+ this.mainSun.shadow.camera.near = 0.5
33
+ this.mainSun.shadow.camera.far = 100
34
+ this.mainSun.shadow.camera.left = -30
35
+ this.mainSun.shadow.camera.right = 30
36
+ this.mainSun.shadow.camera.top = 30
37
+ this.mainSun.shadow.camera.bottom = -30
38
+ this.mainSun.shadow.bias = -0.0005
39
+ }
40
+ this.scene.add(this.mainSun)
41
+
42
+ this.fillLight = new THREE.DirectionalLight(0xdbeafe, 0.4)
43
+ this.fillLight.position.set(-20, 18, -15)
44
+ this.scene.add(this.fillLight)
45
+
46
+ this.nightLight = new THREE.DirectionalLight(
47
+ new THREE.Color(config.nightSunColor || '#60a5fa'),
48
+ 0,
49
+ )
50
+ this.nightLight.position.set(-15, 25, 20)
51
+ this.scene.add(this.nightLight)
52
+ }
53
+
54
+ setNightMode(isNight: boolean): void {
55
+ if (isNight) {
56
+ this.ambientLight.intensity = this.config.nightAmbientIntensity ?? 0.35
57
+ this.ambientLight.color.set(this.config.nightAmbientColor || '#1e293b')
58
+ this.mainSun.intensity = 0.15
59
+ this.mainSun.color.set('#3b82f6')
60
+ this.fillLight.intensity = 0.1
61
+ this.nightLight.intensity = this.config.nightSunIntensity ?? 0.5
62
+ this.hemisphereLight.intensity = 0.15
63
+ } else {
64
+ this.ambientLight.intensity = this.config.ambientIntensity ?? 0.85
65
+ this.ambientLight.color.set(this.config.ambientColor || '#ffffff')
66
+ this.mainSun.intensity = this.config.sunIntensity ?? 1.1
67
+ this.mainSun.color.set(this.config.sunColor || '#fff5ea')
68
+ this.fillLight.intensity = 0.4
69
+ this.nightLight.intensity = 0
70
+ this.hemisphereLight.intensity = 0.4
71
+ }
72
+ }
73
+
74
+ dispose(): void {
75
+ this.scene.remove(this.ambientLight)
76
+ this.scene.remove(this.mainSun)
77
+ this.scene.remove(this.fillLight)
78
+ this.scene.remove(this.nightLight)
79
+ this.scene.remove(this.hemisphereLight)
80
+ }
81
+ }
@@ -0,0 +1,215 @@
1
+ import * as THREE from 'three'
2
+ import type { WorldTheme } from '../core/types'
3
+
4
+ export class MaterialPalette {
5
+ private materials: Map<string, THREE.Material> = new Map()
6
+
7
+ getMaterial<T extends THREE.Material>(key: string, factory: () => T): T {
8
+ if (!this.materials.has(key)) {
9
+ this.materials.set(key, factory())
10
+ }
11
+ return this.materials.get(key) as T
12
+ }
13
+
14
+ // Floors
15
+ getTileFloor(color = '#e2e8f0'): THREE.MeshStandardMaterial {
16
+ return this.getMaterial(`floor:tile:${color}`, () => {
17
+ return new THREE.MeshStandardMaterial({
18
+ color: new THREE.Color(color),
19
+ roughness: 0.35,
20
+ metalness: 0.05,
21
+ })
22
+ })
23
+ }
24
+
25
+ getWoodParquet(color = '#b48a58'): THREE.MeshStandardMaterial {
26
+ return this.getMaterial(`floor:wood:${color}`, () => {
27
+ return new THREE.MeshStandardMaterial({
28
+ color: new THREE.Color(color),
29
+ roughness: 0.45,
30
+ metalness: 0.1,
31
+ })
32
+ })
33
+ }
34
+
35
+ getDarkSlate(color = '#1e293b'): THREE.MeshStandardMaterial {
36
+ return this.getMaterial(`floor:slate:${color}`, () => {
37
+ return new THREE.MeshStandardMaterial({
38
+ color: new THREE.Color(color),
39
+ roughness: 0.5,
40
+ metalness: 0.15,
41
+ })
42
+ })
43
+ }
44
+
45
+ // Walls & Partitions
46
+ getWallMaterial(color = '#f8fafc'): THREE.MeshStandardMaterial {
47
+ return this.getMaterial(`wall:${color}`, () => {
48
+ return new THREE.MeshStandardMaterial({
49
+ color: new THREE.Color(color),
50
+ roughness: 0.85,
51
+ metalness: 0.02,
52
+ })
53
+ })
54
+ }
55
+
56
+ getFrameMaterial(color = '#0f172a'): THREE.MeshStandardMaterial {
57
+ return this.getMaterial(`frame:${color}`, () => {
58
+ return new THREE.MeshStandardMaterial({
59
+ color: new THREE.Color(color),
60
+ roughness: 0.3,
61
+ metalness: 0.7,
62
+ })
63
+ })
64
+ }
65
+
66
+ getGlassMaterial(color = '#93c5fd', opacity = 0.28): THREE.MeshPhysicalMaterial {
67
+ return this.getMaterial(`glass:${color}:${opacity}`, () => {
68
+ return new THREE.MeshPhysicalMaterial({
69
+ color: new THREE.Color(color),
70
+ transparent: true,
71
+ opacity,
72
+ roughness: 0.08,
73
+ metalness: 0.1,
74
+ transmission: 0.85,
75
+ ior: 1.5,
76
+ reflectivity: 0.7,
77
+ depthWrite: false,
78
+ })
79
+ })
80
+ }
81
+
82
+ // Furniture & Props
83
+ getDeskTopMaterial(color = '#ffffff'): THREE.MeshStandardMaterial {
84
+ return this.getMaterial(`furniture:desktop:${color}`, () => {
85
+ return new THREE.MeshStandardMaterial({
86
+ color: new THREE.Color(color),
87
+ roughness: 0.4,
88
+ metalness: 0.1,
89
+ })
90
+ })
91
+ }
92
+
93
+ getExecutiveWoodMaterial(): THREE.MeshStandardMaterial {
94
+ return this.getMaterial('furniture:executive_wood', () => {
95
+ return new THREE.MeshStandardMaterial({
96
+ color: new THREE.Color('#382212'),
97
+ roughness: 0.3,
98
+ metalness: 0.15,
99
+ })
100
+ })
101
+ }
102
+
103
+ getLeatherMaterial(color = '#1e293b'): THREE.MeshStandardMaterial {
104
+ return this.getMaterial(`furniture:leather:${color}`, () => {
105
+ return new THREE.MeshStandardMaterial({
106
+ color: new THREE.Color(color),
107
+ roughness: 0.6,
108
+ metalness: 0.05,
109
+ })
110
+ })
111
+ }
112
+
113
+ getMetalLegMaterial(): THREE.MeshStandardMaterial {
114
+ return this.getMaterial('furniture:metal_legs', () => {
115
+ return new THREE.MeshStandardMaterial({
116
+ color: new THREE.Color('#334155'),
117
+ roughness: 0.2,
118
+ metalness: 0.85,
119
+ })
120
+ })
121
+ }
122
+
123
+ getMonitorBezelMaterial(): THREE.MeshStandardMaterial {
124
+ return this.getMaterial('tech:bezel', () => {
125
+ return new THREE.MeshStandardMaterial({
126
+ color: new THREE.Color('#090d16'),
127
+ roughness: 0.25,
128
+ metalness: 0.75,
129
+ })
130
+ })
131
+ }
132
+
133
+ getMonitorScreenMaterial(active = true, color = '#38bdf8'): THREE.MeshBasicMaterial {
134
+ return this.getMaterial(`tech:screen:${active}:${color}`, () => {
135
+ return new THREE.MeshBasicMaterial({
136
+ color: new THREE.Color(active ? color : '#0f172a'),
137
+ })
138
+ })
139
+ }
140
+
141
+ getPlantLeavesMaterial(): THREE.MeshStandardMaterial {
142
+ return this.getMaterial('nature:ficus_leaves', () => {
143
+ return new THREE.MeshStandardMaterial({
144
+ color: new THREE.Color('#15803d'),
145
+ roughness: 0.5,
146
+ metalness: 0.05,
147
+ })
148
+ })
149
+ }
150
+
151
+ getTerracottaPotMaterial(): THREE.MeshStandardMaterial {
152
+ return this.getMaterial('nature:pot_terracotta', () => {
153
+ return new THREE.MeshStandardMaterial({
154
+ color: new THREE.Color('#c2410c'),
155
+ roughness: 0.8,
156
+ metalness: 0.02,
157
+ })
158
+ })
159
+ }
160
+
161
+ // Avatars / People
162
+ getSkinMaterial(color = '#fbcfe8'): THREE.MeshStandardMaterial {
163
+ return this.getMaterial(`person:skin:${color}`, () => {
164
+ return new THREE.MeshStandardMaterial({
165
+ color: new THREE.Color(color),
166
+ roughness: 0.6,
167
+ metalness: 0.05,
168
+ })
169
+ })
170
+ }
171
+
172
+ getHairMaterial(color = '#1e1b4b'): THREE.MeshStandardMaterial {
173
+ return this.getMaterial(`person:hair:${color}`, () => {
174
+ return new THREE.MeshStandardMaterial({
175
+ color: new THREE.Color(color),
176
+ roughness: 0.7,
177
+ metalness: 0.05,
178
+ })
179
+ })
180
+ }
181
+
182
+ getClothingMaterial(color = '#2563eb'): THREE.MeshStandardMaterial {
183
+ return this.getMaterial(`person:clothes:${color}`, () => {
184
+ return new THREE.MeshStandardMaterial({
185
+ color: new THREE.Color(color),
186
+ roughness: 0.65,
187
+ metalness: 0.08,
188
+ })
189
+ })
190
+ }
191
+
192
+ // Status Glow Ring
193
+ getStatusGlow(status: string): THREE.MeshBasicMaterial {
194
+ let color = '#22c55e'
195
+ if (status === 'running' || status === 'working') color = '#3b82f6'
196
+ else if (status === 'awaiting_approval') color = '#f59e0b'
197
+ else if (status === 'failed') color = '#ef4444'
198
+ else if (status === 'offline') color = '#64748b'
199
+
200
+ return this.getMaterial(`status:glow:${color}`, () => {
201
+ return new THREE.MeshBasicMaterial({
202
+ color: new THREE.Color(color),
203
+ transparent: true,
204
+ opacity: 0.8,
205
+ })
206
+ })
207
+ }
208
+
209
+ dispose(): void {
210
+ for (const mat of this.materials.values()) {
211
+ mat.dispose()
212
+ }
213
+ this.materials.clear()
214
+ }
215
+ }
@@ -0,0 +1,79 @@
1
+ import * as THREE from 'three'
2
+ import type { WorldEntity } from '../core/types'
3
+
4
+ export class PerformanceManager {
5
+ private lastTime = performance.now()
6
+ private frameCount = 0
7
+ private fps = 60
8
+ private adaptiveRatio = 1.0
9
+
10
+ update(): number {
11
+ this.frameCount++
12
+ const now = performance.now()
13
+ const delta = now - this.lastTime
14
+
15
+ if (delta >= 1000) {
16
+ this.fps = Math.round((this.frameCount * 1000) / delta)
17
+ this.frameCount = 0
18
+ this.lastTime = now
19
+
20
+ // Adaptive quality adjustment
21
+ if (this.fps < 30 && this.adaptiveRatio > 0.75) {
22
+ this.adaptiveRatio = Math.max(0.75, this.adaptiveRatio - 0.1)
23
+ } else if (this.fps > 55 && this.adaptiveRatio < 1.0) {
24
+ this.adaptiveRatio = Math.min(1.0, this.adaptiveRatio + 0.05)
25
+ }
26
+ }
27
+
28
+ return this.fps
29
+ }
30
+
31
+ getFps(): number {
32
+ return this.fps
33
+ }
34
+
35
+ getAdaptiveRatio(): number {
36
+ return this.adaptiveRatio
37
+ }
38
+ }
39
+
40
+ export class DirectDOMProjector {
41
+ private tempVec = new THREE.Vector3()
42
+
43
+ projectEntities(
44
+ entities: WorldEntity[],
45
+ camera: THREE.Camera,
46
+ width: number,
47
+ height: number,
48
+ elementMap: Map<string, HTMLElement>,
49
+ ): void {
50
+ const halfW = width / 2
51
+ const halfH = height / 2
52
+
53
+ for (const entity of entities) {
54
+ const el = elementMap.get(entity.id)
55
+ if (!el) continue
56
+
57
+ // Head / top of desk elevation offset (+ 1.5 units)
58
+ this.tempVec.set(
59
+ entity.position[0],
60
+ entity.position[1] + 1.45,
61
+ entity.position[2],
62
+ )
63
+
64
+ this.tempVec.project(camera)
65
+
66
+ // Check if behind camera
67
+ if (this.tempVec.z > 1.0) {
68
+ el.style.display = 'none'
69
+ continue
70
+ }
71
+
72
+ el.style.display = ''
73
+ const screenX = this.tempVec.x * halfW + halfW
74
+ const screenY = -(this.tempVec.y * halfH) + halfH
75
+
76
+ el.style.transform = `translate3d(${screenX}px, ${screenY}px, 0)`
77
+ }
78
+ }
79
+ }
@@ -0,0 +1,51 @@
1
+ import * as THREE from 'three'
2
+ import type { WorldEntity } from '../core/types'
3
+
4
+ export interface RaycastHit {
5
+ entityId: string
6
+ point: THREE.Vector3
7
+ distance: number
8
+ }
9
+
10
+ export class WorldRaycaster {
11
+ private raycaster = new THREE.Raycaster()
12
+ private mouse = new THREE.Vector2()
13
+
14
+ constructor() {
15
+ this.raycaster.params.Points = { threshold: 0.2 }
16
+ }
17
+
18
+ intersect(
19
+ clientX: number,
20
+ clientY: number,
21
+ rect: DOMRect,
22
+ camera: THREE.Camera,
23
+ interactiveObjects: THREE.Object3D[],
24
+ ): RaycastHit | null {
25
+ if (rect.width === 0 || rect.height === 0) return null
26
+
27
+ this.mouse.x = ((clientX - rect.left) / rect.width) * 2 - 1
28
+ this.mouse.y = -((clientY - rect.top) / rect.height) * 2 + 1
29
+
30
+ this.raycaster.setFromCamera(this.mouse, camera)
31
+ const intersects = this.raycaster.intersectObjects(interactiveObjects, true)
32
+
33
+ if (intersects.length === 0) return null
34
+
35
+ for (const hit of intersects) {
36
+ let curr: THREE.Object3D | null = hit.object
37
+ while (curr) {
38
+ if (curr.userData && curr.userData.entityId) {
39
+ return {
40
+ entityId: curr.userData.entityId,
41
+ point: hit.point,
42
+ distance: hit.distance,
43
+ }
44
+ }
45
+ curr = curr.parent
46
+ }
47
+ }
48
+
49
+ return null
50
+ }
51
+ }