-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscene.js
More file actions
352 lines (307 loc) · 13.7 KB
/
Copy pathscene.js
File metadata and controls
352 lines (307 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
import * as THREE from "three";
import { SplatMesh } from "@sparkjsdev/spark";
import { worldToUniverse } from "./coordinate-transform.js";
import { setupPortalLighting } from "./port.js";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader.js";
// Loaders
const gltfLoader = new GLTFLoader();
const fbxLoader = new FBXLoader();
/**
* ProtoScene - Manages the Three.js scene, camera, renderer, and local frame
*/
export class ProtoScene {
constructor() {
// ========== Scene Setup ==========
this.scene = new THREE.Scene();
// ========== Camera Setup ==========
this.camera = new THREE.PerspectiveCamera(
90,
window.innerWidth / window.innerHeight,
0.1,
1000
);
// ========== Renderer Setup ==========
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(this.renderer.domElement);
// ========== Local Frame Setup ==========
// Local frame for camera (used for movement and teleportation)
this.localFrame = new THREE.Group();
this.scene.add(this.localFrame);
this.localFrame.add(this.camera);
this.localFrame.position.set(0, 2, 0);
// Setup lighting for portal materials
setupPortalLighting(this.scene, this.camera);
}
/**
* Get the scene instance
* @returns {THREE.Scene}
*/
getScene() {
return this.scene;
}
/**
* Get the camera instance
* @returns {THREE.PerspectiveCamera}
*/
getCamera() {
return this.camera;
}
/**
* Get the renderer instance
* @returns {THREE.WebGLRenderer}
*/
getRenderer() {
return this.renderer;
}
/**
* Get the local frame instance
* @returns {THREE.Group}
*/
getLocalFrame() {
return this.localFrame;
}
/**
* Handle window resize - update camera and renderer
*/
handleResize() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
/**
* Load a splat mesh and set its position
* @param {string} url - URL to the splat file
* @param {Array|THREE.Vector3} position - Position [x, y, z] or Vector3
* @param {number} world - World number (0 for root world)
* @param {Array} rotation - Rotation quaternion [x, y, z, w] (default: identity)
* @returns {Promise<SplatMesh>} The loaded mesh
*/
async loadSplatandSetPosition(url, position = [0, 0, 0], world = 0, rotation = [0, 0, 0, 1]) {
console.log("Loading", url);
const absoluteURL = new URL(url, window.location.href).href;
// Pre-check if the URL is accessible (catches 404s early with clear error)
try {
const headResponse = await fetch(absoluteURL, { method: 'HEAD' });
if (!headResponse.ok) {
console.error(`[Scene] Failed to load splat: ${absoluteURL}`);
console.error(`[Scene] HTTP ${headResponse.status}: ${headResponse.statusText}`);
console.error(`[Scene] Check that splatUrl in world.json is correct`);
throw new Error(`Splat file not found: ${absoluteURL} (HTTP ${headResponse.status})`);
}
} catch (fetchError) {
if (fetchError.message.includes('Splat file not found')) {
throw fetchError; // Re-throw our custom error
}
// Network error or CORS - log but continue (SplatMesh might handle it differently)
console.warn(`[Scene] Could not pre-check splat URL (may still load): ${absoluteURL}`, fetchError.message);
}
try {
const mesh = new SplatMesh({ url: absoluteURL, paged: true });
await mesh.initialized;
if (Array.isArray(position)) {
mesh.position.fromArray(position);
} else {
mesh.position.copy(position);
}
mesh.quaternion.fromArray(rotation);
if (world !== 0) {
const universePos = worldToUniverse(mesh.position, world);
mesh.position.copy(universePos);
}
this.scene.add(mesh);
console.log("Loaded", url);
return mesh;
} catch (error) {
console.error(`[Scene] Failed to initialize splat mesh: ${absoluteURL}`);
console.error(`[Scene] Error: ${error.message}`);
console.error(`[Scene] Check that splatUrl in world.json points to a valid .spz file`);
throw error;
}
}
/**
* Load a collision mesh (GLB) and set its position
* @param {string} url - URL to the GLB file
* @param {number} world - World number (0 for root world)
* @param {boolean} visible - Initial visibility (default false)
* @returns {Promise<THREE.Group>} The loaded collision mesh
*/
async loadCollisionMesh(url, world = 0, visible = false, bustCache = true) {
console.log("Loading collision mesh:", url);
let absoluteURL = new URL(url, window.location.href).href;
// Add cache-busting query parameter to force fresh load
if (bustCache) {
const separator = absoluteURL.includes('?') ? '&' : '?';
absoluteURL += `${separator}_t=${Date.now()}`;
}
return new Promise((resolve, reject) => {
gltfLoader.load(
absoluteURL,
(gltf) => {
const collisionMesh = gltf.scene;
// Apply wireframe material to all meshes for debugging visibility
collisionMesh.traverse((child) => {
if (child.isMesh) {
child.material = new THREE.MeshBasicMaterial({
color: 0x00ff00,
wireframe: true,
transparent: true,
opacity: 0.5
});
}
});
// Transform to universe coordinates if not root world
if (world !== 0) {
const universePos = worldToUniverse(collisionMesh.position, world);
collisionMesh.position.copy(universePos);
}
// Set initial visibility
collisionMesh.visible = visible;
this.scene.add(collisionMesh);
// Debug: log mesh info
let meshCount = 0;
collisionMesh.traverse((child) => {
if (child.isMesh) meshCount++;
});
console.log("Loaded collision mesh:", url);
console.log(" Contains", meshCount, "mesh(es)");
console.log(" Position:", collisionMesh.position.toArray());
console.log(" Visible:", collisionMesh.visible);
resolve(collisionMesh);
},
(progress) => {
if (progress.total) {
console.log(`Loading collision mesh: ${(progress.loaded / progress.total * 100).toFixed(0)}%`);
}
},
(error) => {
console.error("Error loading collision mesh:", url, error);
reject(error);
}
);
});
}
/**
* Load a character model (FBX or GLB) with animation
* @param {Object} characterData - Character configuration from world.json
* @param {string} characterData.name - Character name
* @param {string} characterData.model - Model file path
* @param {Array<number>} characterData.position - Position [x, y, z]
* @param {Array<number>} characterData.rotation - Rotation as quaternion [x, y, z, w] or Euler [x, y, z]
* @param {number} characterData.scale - Uniform scale (default 1)
* @param {number} world - World number for position offset
* @param {Function} resolveUrl - URL resolver function
* @returns {Promise<{model: THREE.Group, mixer: THREE.AnimationMixer}>}
*/
async loadCharacter(characterData, world = 0, resolveUrl = null) {
const { name, model, position = [0, 0, 0], rotation = [0, 0, 0, 1], scale = 1 } = characterData;
console.log(`Loading character "${name}":`, model);
// Resolve the URL
let url = model;
if (resolveUrl && !model.startsWith('http')) {
url = resolveUrl(model);
}
const absoluteURL = new URL(url, window.location.href).href;
// Determine loader based on file extension
const ext = model.toLowerCase().split('.').pop();
const isFBX = ext === 'fbx';
return new Promise((resolve, reject) => {
const loader = isFBX ? fbxLoader : gltfLoader;
loader.load(
absoluteURL,
(result) => {
// FBX returns the object directly, GLTF has a .scene property
const characterModel = isFBX ? result : result.scene;
// Set position
characterModel.position.fromArray(position);
// Transform to universe coordinates if not root world
if (world !== 0) {
const universePos = worldToUniverse(characterModel.position, world);
characterModel.position.copy(universePos);
}
// Set rotation (support both quaternion [x,y,z,w] and Euler [x,y,z])
if (rotation.length === 4) {
characterModel.quaternion.fromArray(rotation);
} else if (rotation.length === 3) {
characterModel.rotation.fromArray(rotation);
}
// Set scale
if (typeof scale === 'number') {
characterModel.scale.setScalar(scale);
} else if (Array.isArray(scale)) {
characterModel.scale.fromArray(scale);
}
// Create animation mixer
const mixer = new THREE.AnimationMixer(characterModel);
// Get animations (FBX stores them in result.animations, GLTF in result.animations)
const animations = isFBX ? result.animations : result.animations;
// Play all animations (or just the first one)
if (animations && animations.length > 0) {
console.log(` Found ${animations.length} animation(s) for "${name}"`);
animations.forEach((clip, index) => {
console.log(` - ${clip.name || `Animation ${index}`} (${clip.duration.toFixed(2)}s)`);
const action = mixer.clipAction(clip);
action.play();
});
} else {
console.log(` No animations found for "${name}"`);
}
// Add to scene
this.scene.add(characterModel);
console.log(` Character "${name}" loaded at`, characterModel.position.toArray());
resolve({ model: characterModel, mixer, animations });
},
(progress) => {
if (progress.total) {
const pct = (progress.loaded / progress.total * 100).toFixed(0);
console.log(`Loading character "${name}": ${pct}%`);
}
},
(error) => {
console.error(`Error loading character "${name}":`, error);
reject(error);
}
);
});
}
/**
* Remove a character from the scene
* @param {{model: THREE.Group, mixer: THREE.AnimationMixer}} character
*/
removeCharacter(character) {
if (character.mixer) {
character.mixer.stopAllAction();
}
if (character.model) {
this.scene.remove(character.model);
character.model.traverse((child) => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
if (Array.isArray(child.material)) {
child.material.forEach(m => m.dispose());
} else {
child.material.dispose();
}
}
});
}
}
}
/**
* Load world JSON data from a URL
* @param {string} worldUrl - URL to the world.json file
* @returns {Promise<Object>} World data object
*/
export async function loadWorldJSON(worldUrl) {
console.log("loadWorldJSON:", worldUrl);
// Use cache: 'reload' to force a fresh fetch and bypass browser cache
// This ensures we always get the latest world.json from the server
const response = await fetch(worldUrl, {
cache: 'reload'
});
console.log("response:", response);
const worlddata = await response.json();
console.log("worlddata:", worlddata);
return worlddata;
}