-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsplat-dialog-box.js
More file actions
441 lines (373 loc) · 13.7 KB
/
Copy pathsplat-dialog-box.js
File metadata and controls
441 lines (373 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
* SplatDialogBox
*
* A dialog box using Gaussian splat-based text for VR commentary.
* Works well with splat rendering pipeline unlike canvas textures.
*/
import * as THREE from "three";
import { textSplats } from "@sparkjsdev/spark";
// Default config
const DEFAULT_CONFIG = {
maxWidth: 6, // Maximum width in world units before wrapping
fontSize: 48, // Base font size for splat text
textColor: 0xffffff, // White text
outlineColor: 0x000000, // Black outline for readability
attributionColor: 0x4fc3f7, // Light blue for attribution
lineSpacing: 1.4, // Line spacing multiplier
padding: 0.2, // Padding around text
outlineOffset: 0.002, // Offset for outline shadow effect
};
// Animation
const FADE_DURATION = 500; // ms
const DISPLAY_DURATION = 10000; // ms (matches ybot.js timing)
/**
* SplatDialogBox class
* Creates a dialog box with splat-based text rendering
*/
export class SplatDialogBox {
constructor(config = {}) {
this.config = { ...DEFAULT_CONFIG, ...config };
this.group = new THREE.Group();
this.group.name = 'splat-dialog-box';
// Text elements
this.textMeshes = []; // Array of line meshes
this.attributionMesh = null;
// State
this.currentText = '';
this.characterName = '';
this.opacity = 0;
this.fadeTimer = null;
this.hideTimer = null;
// Initially hidden
this.group.visible = false;
}
/**
* Position the dialog box relative to a target position (e.g., the screen)
* @param {THREE.Vector3} screenPosition - Position of the screen
* @param {THREE.Quaternion} screenRotation - Optional rotation to match the screen's orientation
* @param {Object} config - Config {offsetX, offsetY, offsetZ, width}
*/
positionRelativeTo(screenPosition, screenRotation = null, config = {}) {
const offsetX = config.offsetX ?? 0;
const offsetY = config.offsetY ?? -1.3; // Default: subtitle position at bottom of screen
const offsetZ = config.offsetZ ?? 0.3; // Slightly forward so text is visible
// Update maxWidth from config if provided
if (config.width) {
this.config.maxWidth = config.width;
}
// Position using configured offsets
this.group.position.set(
screenPosition.x + offsetX,
screenPosition.y + offsetY,
screenPosition.z + offsetZ
);
// Match the screen's rotation (face same direction as the screen)
if (screenRotation) {
this.group.quaternion.copy(screenRotation);
} else {
// Default: face forward along -Z (typical screen orientation)
this.group.rotation.set(0, 0, 0);
}
}
/**
* Show commentary text
* @param {string} text - Commentary text
* @param {string} characterName - Name of the character (e.g., "Y-Bot")
*/
show(text, characterName = "Y-Bot") {
// Clear any existing timers
if (this.fadeTimer) clearInterval(this.fadeTimer);
if (this.hideTimer) clearTimeout(this.hideTimer);
this.currentText = text;
this.characterName = characterName;
// Rebuild the text meshes
this._rebuildText();
this.group.visible = true;
// Fade in
this._fadeIn();
// Schedule fade out
this.hideTimer = setTimeout(() => {
this._fadeOut();
}, DISPLAY_DURATION);
console.log('[SplatDialogBox] Showing:', text);
}
/**
* Hide the dialog box immediately
*/
hide() {
if (this.fadeTimer) clearInterval(this.fadeTimer);
if (this.hideTimer) clearTimeout(this.hideTimer);
this.opacity = 0;
this._updateOpacity();
this.group.visible = false;
console.log('[SplatDialogBox] Hidden');
}
/**
* Check if dialog box is visible
*/
isVisible() {
return this.group.visible && this.opacity > 0;
}
/**
* Rebuild the text meshes with current content
*/
_rebuildText() {
// Clear existing meshes
this._clearMeshes();
// Create quoted text
const quote = `"${this.currentText}"`;
// Simple line wrapping by character count estimate
// We estimate based on maxWidth and fontSize
const lines = this._wrapText(quote);
// Calculate total height for centering
const lineHeight = this.config.fontSize * this.config.lineSpacing * 0.003;
const totalHeight = lines.length * lineHeight;
// Create text splat for each line (with outline for readability)
let y = totalHeight / 2 - lineHeight / 2;
const outlineOffset = this.config.outlineOffset;
for (const line of lines) {
if (line.trim()) {
// Create outline (dark shadow behind text) - 4 directions
const offsets = [
[-outlineOffset, -outlineOffset],
[outlineOffset, -outlineOffset],
[-outlineOffset, outlineOffset],
[outlineOffset, outlineOffset],
];
for (const [ox, oy] of offsets) {
const outline = this._createTextMesh(line, this.config.outlineColor, this.config.fontSize);
outline.position.set(ox, y + oy, -0.001); // Slightly behind
this.group.add(outline);
this.textMeshes.push(outline);
}
// Create main text (on top)
const mesh = this._createTextMesh(line, this.config.textColor, this.config.fontSize);
mesh.position.y = y;
this.group.add(mesh);
this.textMeshes.push(mesh);
}
y -= lineHeight;
}
// Create attribution below the text (with outline)
const attributionText = `— ${this.characterName}`;
// Attribution outline
const attrOffsets = [
[-outlineOffset, -outlineOffset],
[outlineOffset, -outlineOffset],
[-outlineOffset, outlineOffset],
[outlineOffset, outlineOffset],
];
for (const [ox, oy] of attrOffsets) {
const outline = this._createTextMesh(attributionText, this.config.outlineColor, this.config.fontSize * 0.6);
outline.position.set(ox, y - lineHeight * 0.3 + oy, -0.001);
this.group.add(outline);
this.textMeshes.push(outline);
}
// Attribution text
this.attributionMesh = this._createTextMesh(
attributionText,
this.config.attributionColor,
this.config.fontSize * 0.6
);
this.attributionMesh.position.y = y - lineHeight * 0.3;
this.group.add(this.attributionMesh);
// Update opacity
this._updateOpacity();
}
/**
* Create a text splat mesh
* @param {string} text - Text content
* @param {number} color - Color as hex number
* @param {number} fontSize - Font size
* @returns {THREE.Object3D}
*/
_createTextMesh(text, color, fontSize) {
const mesh = textSplats({
text: text,
font: "Georgia", // Serif font to match the italic style
fontSize: fontSize,
color: new THREE.Color(color),
});
// Scale appropriately for world space
// textSplats creates large meshes, scale down
mesh.scale.setScalar(0.25 / 80);
return mesh;
}
/**
* Simple text wrapping based on character count
* @param {string} text - Text to wrap
* @returns {string[]} - Array of lines
*/
_wrapText(text) {
// Estimate chars per line based on maxWidth
// This is approximate - actual width depends on font
const charsPerLine = Math.floor(this.config.maxWidth * 12);
const words = text.split(' ');
const lines = [];
let currentLine = '';
for (const word of words) {
const testLine = currentLine ? `${currentLine} ${word}` : word;
if (testLine.length > charsPerLine && currentLine) {
lines.push(currentLine);
currentLine = word;
} else {
currentLine = testLine;
}
}
if (currentLine) {
lines.push(currentLine);
}
return lines.length > 0 ? lines : [''];
}
/**
* Clear all text meshes
*/
_clearMeshes() {
for (const mesh of this.textMeshes) {
this.group.remove(mesh);
if (mesh.dispose) mesh.dispose();
}
this.textMeshes = [];
if (this.attributionMesh) {
this.group.remove(this.attributionMesh);
if (this.attributionMesh.dispose) this.attributionMesh.dispose();
this.attributionMesh = null;
}
}
/**
* Fade in animation
*/
_fadeIn() {
const startTime = performance.now();
const startOpacity = this.opacity;
this.fadeTimer = setInterval(() => {
const elapsed = performance.now() - startTime;
const progress = Math.min(elapsed / FADE_DURATION, 1);
this.opacity = startOpacity + (1 - startOpacity) * progress;
this._updateOpacity();
if (progress >= 1) {
clearInterval(this.fadeTimer);
this.fadeTimer = null;
}
}, 16);
}
/**
* Fade out animation
*/
_fadeOut() {
const startTime = performance.now();
const startOpacity = this.opacity;
this.fadeTimer = setInterval(() => {
const elapsed = performance.now() - startTime;
const progress = Math.min(elapsed / FADE_DURATION, 1);
this.opacity = startOpacity * (1 - progress);
this._updateOpacity();
if (progress >= 1) {
clearInterval(this.fadeTimer);
this.fadeTimer = null;
this.group.visible = false;
}
}, 16);
}
/**
* Update opacity on all meshes
* Uses SplatMesh's native opacity property for proper Spark rendering
*/
_updateOpacity() {
for (const mesh of this.textMeshes) {
// Use native SplatMesh opacity property
mesh.opacity = this.opacity;
}
if (this.attributionMesh) {
this.attributionMesh.opacity = this.opacity;
}
// Only hide the group when fully transparent (opacity check happens in Spark)
this.group.visible = this.opacity > 0;
}
/**
* Get the group to add to scene
*/
getGroup() {
return this.group;
}
/**
* Clean up resources
*/
dispose() {
if (this.fadeTimer) clearInterval(this.fadeTimer);
if (this.hideTimer) clearTimeout(this.hideTimer);
this._clearMeshes();
}
}
// ============ Singleton API for easy use ============
let splatDialogBox = null;
let sceneRef = null;
/**
* Initialize the splat dialog box system
* @param {THREE.Scene} scene
*/
export function initSplatDialogBox(scene) {
sceneRef = scene;
console.log('[SplatDialogBox] System ready');
}
/**
* Show a splat dialog box with commentary
* @param {string} text - Commentary text
* @param {string} characterName - Character name
* @param {THREE.Vector3} screenPosition - Position to place near
* @param {THREE.Quaternion} screenRotation - Rotation to match
* @param {Object} panelConfig - Config {width, height, offsetY, offsetZ}
*/
export function showSplatCommentary(text, characterName, screenPosition, screenRotation = null, panelConfig = null) {
if (!sceneRef) {
console.warn('[SplatDialogBox] Not initialized - sceneRef is null');
return;
}
console.log('[SplatDialogBox] showSplatCommentary called:', {
text: text?.substring(0, 30) + '...',
characterName,
hasScreenPosition: !!screenPosition,
screenPosition: screenPosition ? `(${screenPosition.x?.toFixed(2)}, ${screenPosition.y?.toFixed(2)}, ${screenPosition.z?.toFixed(2)})` : null,
panelConfig
});
// Create dialog box if needed
if (!splatDialogBox) {
splatDialogBox = new SplatDialogBox(panelConfig || {});
sceneRef.add(splatDialogBox.getGroup());
console.log('[SplatDialogBox] Created and added to scene');
}
// Position relative to screen
if (screenPosition) {
splatDialogBox.positionRelativeTo(screenPosition, screenRotation, panelConfig || {});
console.log('[SplatDialogBox] Positioned at:', splatDialogBox.group.position);
} else {
console.warn('[SplatDialogBox] No screenPosition provided - dialog box not positioned');
}
splatDialogBox.show(text, characterName);
}
/**
* Hide the splat dialog box
*/
export function hideSplatCommentary() {
if (splatDialogBox) {
splatDialogBox.hide();
}
}
/**
* Check if splat dialog box is visible
*/
export function isSplatCommentaryVisible() {
return splatDialogBox?.isVisible() ?? false;
}
/**
* Dispose splat dialog box resources
*/
export function disposeSplatDialogBox() {
if (splatDialogBox) {
if (sceneRef) {
sceneRef.remove(splatDialogBox.getGroup());
}
splatDialogBox.dispose();
splatDialogBox = null;
}
}