-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdirect-model-environment.js
More file actions
817 lines (662 loc) · 29.9 KB
/
direct-model-environment.js
File metadata and controls
817 lines (662 loc) · 29.9 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { LANES, COLORS, SPAWN_CONFIG } from './constants.js';
// Verify DRACO loader is imported
// DRACOLoader imported for model compression support
export class DirectModelEnvironment {
constructor(scene) {
this.scene = scene;
this.ground = null;
this.activeBuildings = [];
this.streetDecorations = [];
this.lastTreeSpawnZ = -100; // Track last tree spawn position
this.gameController = null;
// Performance tracking - initialize immediately
this.performanceData = {
frameDrops: 0,
lastFrameTime: performance.now(),
spawnDelay: 800
};
// Road dimensions
this.roadWidth = 8;
this.sidewalkWidth = 3;
// Model templates
this.gltfLoader = new GLTFLoader();
// Setup DRACO loader for compressed GLB files
const dracoLoader = new DRACOLoader();
// Try both local and CDN paths for maximum compatibility
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.6/');
dracoLoader.setDecoderConfig({ type: 'js' });
this.gltfLoader.setDRACOLoader(dracoLoader);
// DRACO loader configured and attached to GLTFLoader
this.buildingTemplates = {}; // Store loaded building models
this.buildingPool = new Map(); // Object pool for building instances
this.treeTemplate = null;
this.currentBuildingIndex = 0;
this.modelsLoaded = false;
// Building file names categorized by complexity for LOD
this.buildingFiles = [
'001.glb', '002.glb', '006.glb', '007.glb', '008.glb',
'009.glb', '0010.glb', '0011.glb', '0012.glb'
];
// LOD categorization: simpler models for distance rendering
this.lodCategories = {
detailed: ['001.glb', '002.glb', '006.glb'], // Complex models for close viewing
medium: ['007.glb', '008.glb', '009.glb'], // Medium complexity for mid-distance
simple: ['0010.glb', '0011.glb', '0012.glb'] // Simpler models for far distance
};
this.setupScene();
this.setupLighting();
this.createOptimizedRoad();
// Load the specific models (restore original working system)
this.loadSpecificModels();
}
setGameController(gameController) {
this.gameController = gameController;
}
setupScene() {
this.scene.background = new THREE.Color(0xFFDAB9);
this.scene.fog = new THREE.Fog(0xFFDAB9, 40, 150);
}
setupLighting() {
const ambientLight = new THREE.AmbientLight(0xFFE5B4, 0.8);
this.scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xFFFFE0, 0.6);
directionalLight.position.set(10, 15, 10);
directionalLight.castShadow = false;
this.scene.add(directionalLight);
// Direct model environment setup complete
}
async startProgressiveLoading() {
// Progressive loading: Essential models first, then detailed models
// Starting progressive loading
// Phase 1: Load essential simple models first for immediate gameplay
if (window.gameLoadingManager) {
window.gameLoadingManager.updateProgress(1, 'Loading essential models...');
}
try {
// Load simple models first (can start playing with these)
const essentialModels = this.lodCategories.simple;
const essentialCount = await this.loadModelBatch(essentialModels, 'Essential');
// Verify we have models loaded before proceeding
if (essentialCount > 0) {
// Create a basic scene so the game can start
this.modelsLoaded = true; // Mark as loaded so game can start
this.createInitialScene();
// Game ready with essential models
} else {
// Fall back to creating a fallback scene
// No essential models loaded, using fallback
this.createFallbackScene();
this.modelsLoaded = true;
}
if (window.gameLoadingManager) {
window.gameLoadingManager.updateProgress(3, 'Game ready! Loading enhanced models...');
}
// Phase 2: Load remaining models in background
setTimeout(() => this.loadRemainingModels(), 100);
} catch (error) {
// Error in progressive loading, using fallback
this.createFallbackScene();
}
}
async loadRemainingModels() {
// Load medium and detailed models in background
// Loading remaining models in background
try {
// Load medium complexity models
const mediumModels = this.lodCategories.medium;
await this.loadModelBatch(mediumModels, 'Medium');
// Load detailed models
const detailedModels = this.lodCategories.detailed;
await this.loadModelBatch(detailedModels, 'Detailed');
// Load tree model
await this.loadTreeModel();
// All models loaded - enhanced graphics available
if (window.gameLoadingManager) {
window.gameLoadingManager.updateProgress(5, 'All models loaded!');
}
} catch (error) {
// Error loading remaining models
}
}
async loadModelBatch(modelFiles, batchName) {
// Loading batch of models
const promises = modelFiles.map(async (filename) => {
try {
let gltf;
// Check if asset is preloaded first
if (window.assetPreloader && window.assetPreloader.getLoadedAsset(`building_${filename}`)) {
gltf = window.assetPreloader.getLoadedAsset(`building_${filename}`);
// Using preloaded building model
} else {
// Fallback to loading if not preloaded
gltf = await this.gltfLoader.loadAsync(`./assets/city/model/${filename}`);
}
this.buildingTemplates[filename] = gltf.scene.clone();
this.enhanceBuilding(this.buildingTemplates[filename], filename);
// Model loaded successfully
return filename;
} catch (error) {
// Failed to load model, using fallback
return null;
}
});
const results = await Promise.all(promises);
const successCount = results.filter(r => r !== null).length;
// Model batch loading complete
return successCount;
}
async loadTreeModel() {
try {
let gltf;
// Check if asset is preloaded first
if (window.assetPreloader && window.assetPreloader.getLoadedAsset('tree_main')) {
gltf = window.assetPreloader.getLoadedAsset('tree_main');
// Using preloaded tree model
} else {
// Fallback to loading
gltf = await this.gltfLoader.loadAsync('./assets/city/model/Palm_Tree.glb');
}
this.treeTemplate = gltf.scene.clone();
this.enhanceTree(this.treeTemplate);
// Tree model loaded successfully
} catch (error) {
// Failed to load tree model
}
}
async loadSpecificModels() {
// Loading specific building and tree models
// Update loading progress
if (window.gameLoadingManager) {
window.gameLoadingManager.updateProgress(1, 'Loading building models...');
}
try {
// Load all building models with progress tracking
const buildingPromises = this.buildingFiles.map(async (filename, index) => {
try {
const gltf = await this.gltfLoader.loadAsync(`./assets/city/model/${filename}`);
this.buildingTemplates[filename] = gltf.scene.clone();
this.enhanceBuilding(this.buildingTemplates[filename], filename);
// Building loaded successfully
// Update progress for each building loaded
if (window.gameLoadingManager) {
const progress = 1 + (index + 1) / this.buildingFiles.length * 2; // Buildings take 2 steps (1-3)
window.gameLoadingManager.updateProgress(progress, `Loading models... (${index + 1}/${this.buildingFiles.length})`);
}
return filename;
} catch (error) {
// Failed to load building model
return null;
}
});
// Load tree model
if (window.gameLoadingManager) {
window.gameLoadingManager.updateProgress(3, 'Loading tree models...');
}
const treePromise = this.gltfLoader.loadAsync('./assets/city/model/Palm_Tree.glb')
.then(gltf => {
this.treeTemplate = gltf.scene.clone();
this.enhanceTree(this.treeTemplate);
// Tree model loaded successfully
return 'tree';
})
.catch(error => {
// Failed to load tree model
return null;
});
// Wait for all models to load
const results = await Promise.all([...buildingPromises, treePromise]);
const successfulBuildings = results.filter(r => r && r !== 'tree');
// Building and tree models loaded successfully
if (window.gameLoadingManager) {
window.gameLoadingManager.updateProgress(4, 'Creating urban scene...');
}
this.modelsLoaded = true;
// Create initial urban scene
this.createInitialScene();
// Scene creation complete
if (window.gameLoadingManager) {
window.gameLoadingManager.updateProgress(5, 'Ready to play!');
}
} catch (error) {
// Error loading models
this.createFallbackScene();
// Still complete loading even if models failed
if (window.gameLoadingManager) {
window.gameLoadingManager.updateProgress(5, 'Ready to play!');
}
}
}
enhanceBuilding(building, filename) {
// Apply African colors to buildings
const africanColors = [
0xFF6347, // Terracotta
0x0047AB, // Bold blue
0xFFD700, // Golden yellow
0x663399, // Royal purple
0x228B22, // Forest green
0xDC143C // Crimson red
];
// Different color for each building type
const buildingIndex = this.buildingFiles.indexOf(filename);
const color = africanColors[buildingIndex % africanColors.length];
let meshCount = 0;
building.traverse((child) => {
if (child.isMesh) {
// Performance optimization: only nearby buildings cast shadows
child.castShadow = false; // Disabled for performance
child.receiveShadow = false; // Also disable receive shadows for better performance
// Apply African color theme based on building type
if (child.material && meshCount % 4 === 0) { // Every 4th mesh gets color
child.material = child.material.clone();
child.material.color = new THREE.Color(color);
child.material.color.multiplyScalar(0.8 + Math.random() * 0.2); // Slight variation
// Performance: reduce material complexity
child.material.roughness = 0.8;
child.material.metalness = 0.0;
}
meshCount++;
}
});
// Enhanced building with African color theme
}
selectLODModel(distanceFromCamera) {
// Select appropriate building category based on distance
const category = distanceFromCamera < 60 ? 'detailed'
: distanceFromCamera < 120 ? 'medium'
: 'simple';
const modelsInCategory = this.lodCategories[category];
const modelIndex = this.currentBuildingIndex % modelsInCategory.length;
return modelsInCategory[modelIndex];
}
enhanceTree(tree) {
// Enhance tree - Enable shadows and keep original materials
tree.traverse((child) => {
if (child.isMesh) {
// ENABLE SHADOWS for better realism
child.castShadow = true;
child.receiveShadow = true;
if (child.material) {
child.material = child.material.clone();
child.material.roughness = 0.8;
child.material.metalness = 0.1;
}
}
});
}
createOptimizedRoad() {
const canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 256;
const context = canvas.getContext('2d');
// Road base
context.fillStyle = '#404040';
context.fillRect(0, 0, canvas.width, canvas.height);
// Road texture
context.fillStyle = '#4A4A4A';
for (let i = 0; i < 50; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const size = Math.random() * 3 + 1;
context.fillRect(x, y, size, size);
}
// Lane markings for 3 lanes
context.fillStyle = '#FFFFFF';
for (let i = 0; i < canvas.height; i += 20) {
context.fillRect(canvas.width / 3 - 1, i, 2, 10);
context.fillRect((canvas.width * 2 / 3) - 1, i, 2, 10);
}
// Road edges
context.fillStyle = '#FFDD00';
context.fillRect(0, 0, 3, canvas.height);
context.fillRect(canvas.width - 3, 0, 3, canvas.height);
const roadTexture = new THREE.CanvasTexture(canvas);
roadTexture.wrapS = THREE.RepeatWrapping;
roadTexture.wrapT = THREE.RepeatWrapping;
roadTexture.repeat.set(1, 150);
const roadGeometry = new THREE.PlaneGeometry(this.roadWidth, 1500);
const roadMaterial = new THREE.MeshStandardMaterial({
map: roadTexture,
side: THREE.DoubleSide,
roughness: 0.8,
metalness: 0.0
});
this.road = new THREE.Mesh(roadGeometry, roadMaterial);
this.road.rotation.x = -Math.PI / 2;
this.road.receiveShadow = true;
this.scene.add(this.road);
this.createSidewalks();
this.ground = this.road;
// Road created with clear 3-lane markings
}
createSidewalks() {
const sidewalkGeometry = new THREE.PlaneGeometry(this.sidewalkWidth, 1500);
const sidewalkMaterial = new THREE.MeshStandardMaterial({
color: 0xB0B0B0,
roughness: 0.9
});
this.leftSidewalk = new THREE.Mesh(sidewalkGeometry, sidewalkMaterial);
this.leftSidewalk.rotation.x = -Math.PI / 2;
this.leftSidewalk.position.x = -(this.roadWidth / 2 + this.sidewalkWidth / 2);
this.leftSidewalk.position.y = 0.02;
this.leftSidewalk.receiveShadow = true;
this.scene.add(this.leftSidewalk);
this.rightSidewalk = new THREE.Mesh(sidewalkGeometry, sidewalkMaterial.clone());
this.rightSidewalk.rotation.x = -Math.PI / 2;
this.rightSidewalk.position.x = (this.roadWidth / 2 + this.sidewalkWidth / 2);
this.rightSidewalk.position.y = 0.02;
this.rightSidewalk.receiveShadow = true;
this.scene.add(this.rightSidewalk);
}
createInitialScene() {
if (!this.modelsLoaded) {
// Models not loaded yet, waiting
setTimeout(() => this.createInitialScene(), 1000);
return;
}
// Creating consistent dense urban scene with loaded models
// Create BALANCED initial buildings for both sides with LOD
for (let i = 0; i < 30; i++) { // Increased for better coverage
const z = -20 - (i * 12); // Every 12 units for denser coverage
// Ensure BOTH sides get buildings consistently, using LOD based on distance
this.spawnSpecificBuilding(z, 'left', 0); // Camera starts at Z=0
this.spawnSpecificBuilding(z - 6, 'right', 0); // Slight offset for variety
}
// Trees enabled - spaced out for individual placement
for (let i = 0; i < 10; i++) {
this.spawnSpecificTree(-20 - (i * 60));
}
}
spawnSpecificBuilding(zPosition, forceSide = null, cameraZ = 0) {
const loadedBuildings = Object.keys(this.buildingTemplates);
if (loadedBuildings.length === 0) {
// No buildings loaded, creating fallback
this.createFallbackBuilding(zPosition);
return;
}
let buildingKey;
// Only use LOD system if models are fully loaded
if (this.modelsLoaded) {
// Calculate distance from camera for LOD selection
const distanceFromCamera = Math.abs(zPosition - cameraZ);
buildingKey = this.selectLODModel(distanceFromCamera);
} else {
// Fallback to original cycling method while models are loading
buildingKey = loadedBuildings[this.currentBuildingIndex % loadedBuildings.length];
}
this.currentBuildingIndex++;
// Try to get from pool first, then clone from template
const template = this.buildingTemplates[buildingKey];
const building = this.getBuildingFromPool(buildingKey) ?? template?.clone();
if (!building) {
this.createFallbackBuilding(zPosition, forceSide);
return;
}
// Slightly larger scale for better visibility while maintaining performance
const scale = 0.006 + Math.random() * 0.006; // 0.006 to 0.012 scale (better visibility)
building.scale.setScalar(scale);
// Calculate building dimensions and position with safety margin
const bbox = new THREE.Box3().setFromObject(building);
const size = bbox.getSize(new THREE.Vector3());
const groundY = -bbox.min.y;
const side = forceSide || (Math.random() < 0.5 ? 'left' : 'right');
const halfWidth = size.x / 2 + 2; // 2 units safety margin
const xOffset = side === 'left' ? -6 - halfWidth : 6 + halfWidth;
building.position.set(xOffset, groundY, zPosition);
building.rotation.y = (Math.random() - 0.5) * 0.3;
// Building positioned with calculated offset and width
this.scene.add(building);
this.activeBuildings.push({
object: building,
zPosition: zPosition,
type: buildingKey,
scale: scale
});
// Removed detailed logging for performance
}
spawnSpecificTree(zPosition) {
if (!this.treeTemplate) {
// Tree template not loaded
return;
}
// UNIFORM SCALING - Perfect size (User defined)
const tree = this.treeTemplate.clone();
// Scale: 1.4 to 2.4
const scale = 1.4 + Math.random() * 1.0;
tree.scale.setScalar(scale);
const bbox = new THREE.Box3().setFromObject(tree);
const groundY = -bbox.min.y;
const isEvenZ = Math.floor(Math.abs(zPosition / 100)) % 2 === 0;
// ORIGINAL PLACEMENT: +/- 5.5
// We are intentionally IGNORING width checks (scale-to-fit disabled)
// The canopy WILL overhang the road, but it's high enough to run under.
const xOffset = isEvenZ ? -5.5 : 5.5;
tree.position.set(xOffset, groundY, zPosition);
this.scene.add(tree);
this.streetDecorations.push({
object: tree,
zPosition: zPosition
});
// Tree spawned with forced large scale and original placement
}
createFallbackBuilding(zPosition, forceSide = null) {
// Simple fallback if models fail to load
const height = 3 + Math.random() * 3; // Smaller fallback buildings
const width = 2 + Math.random() * 1.5;
const depth = 2 + Math.random() * 1.5;
const geometry = new THREE.BoxGeometry(width, height, depth);
const material = new THREE.MeshStandardMaterial({
color: [0xFF6347, 0x0047AB, 0xFFD700, 0x663399, 0x228B22][Math.floor(Math.random() * 5)],
roughness: 0.7
});
const building = new THREE.Mesh(geometry, material);
building.position.y = height / 2;
building.castShadow = true;
building.receiveShadow = true;
// Use forced side or default behavior
const side = forceSide || (Math.random() < 0.5 ? 'left' : 'right');
let xOffset;
if (side === 'left') {
xOffset = -10 - Math.random() * 3; // X = -10 to -13 (consistent with GLB)
xOffset -= (width / 2); // Account for building width
} else {
xOffset = 10 + Math.random() * 3; // X = +10 to +13 (consistent with GLB)
xOffset += (width / 2); // Account for building width
}
// Safety check
if (xOffset > -6 && xOffset < 6) {
xOffset = side === 'left' ? -12 : 12;
}
building.position.x = xOffset;
building.position.z = zPosition;
this.scene.add(building);
this.activeBuildings.push({
object: building,
zPosition: zPosition,
type: 'Fallback',
scale: 1.0
});
// Fallback building created with calculated dimensions
}
createFallbackScene() {
// Creating consistent dense fallback urban scene
for (let i = 0; i < 30; i++) { // Same high density for fallback
const z = -20 - (i * 12); // Same close spacing as GLB buildings
this.createFallbackBuilding(z, 'left');
this.createFallbackBuilding(z - 6, 'right'); // Ensure both sides
}
}
startSpawning() {
this.spawnElements();
}
spawnElements() {
if (!this.gameController || !this.gameController.isGameActive()) {
return;
}
const playerZ = this.gameController.getPlayerPosition().z;
const gameSpeed = this.gameController.getGameSpeed();
const spawnZ = playerZ - 120; // Spawn ahead for coverage
// Batch spawn buildings for better performance
const buildingsToSpawn = [
{ z: spawnZ, side: 'left' },
{ z: spawnZ - 15, side: 'left' },
{ z: spawnZ - 8, side: 'right' },
{ z: spawnZ - 23, side: 'right' }
];
// Batch spawn in single operation with camera position for LOD
this.batchSpawnBuildings(buildingsToSpawn, playerZ);
// Trees enabled - DISTANCE BASED SPAWNING for consistency at high speeds
// Check distance since last spawn
if (Math.abs(spawnZ - this.lastTreeSpawnZ) > (40 + Math.random() * 40)) {
this.spawnSpecificTree(spawnZ - 30);
this.lastTreeSpawnZ = spawnZ;
}
// Adaptive spawn timing based on game speed and performance
const adaptiveDelay = this.calculateAdaptiveSpawnDelay(gameSpeed);
setTimeout(() => {
this.spawnElements();
}, adaptiveDelay);
}
updateModels(gameSpeed, cameraZ) {
// More aggressive building culling for better performance
for (let i = this.activeBuildings.length - 1; i >= 0; i--) {
const building = this.activeBuildings[i];
building.object.position.z += gameSpeed;
building.zPosition += gameSpeed;
// Balanced despawning - not too aggressive to maintain density
if (building.zPosition > cameraZ + 30) { // Optimized: Reduced from 70 to 30 to save draw calls
this.scene.remove(building.object);
this.returnBuildingToPool(building.object, building.type);
this.activeBuildings.splice(i, 1);
} else {
// Far culling: Hide buildings that are too far ahead (beyond fog)
// Fog ends at 150, so anything beyond 160 is invisible anyway
const distanceAhead = Math.abs(building.zPosition - cameraZ);
if (distanceAhead > 160) {
building.object.visible = false;
} else {
building.object.visible = true;
}
}
}
// Update trees with aggressive culling
for (let i = this.streetDecorations.length - 1; i >= 0; i--) {
const decoration = this.streetDecorations[i];
decoration.object.position.z += gameSpeed;
decoration.zPosition += gameSpeed;
if (decoration.zPosition > cameraZ + 25) { // Very aggressive culling for trees
this.scene.remove(decoration.object);
this.streetDecorations.splice(i, 1);
}
}
}
updateGround(cameraZ) {
const groundZ = cameraZ - 750 + 75; // Calculate common Z position
if (this.road) {
this.road.position.z = groundZ;
}
// Update sidewalks to follow camera - fixes disappearing sidewalks at high speeds
if (this.leftSidewalk) {
this.leftSidewalk.position.z = groundZ;
}
if (this.rightSidewalk) {
this.rightSidewalk.position.z = groundZ;
}
}
reset() {
this.activeBuildings.forEach(building => this.scene.remove(building.object));
this.activeBuildings = [];
this.streetDecorations.forEach(decoration => this.scene.remove(decoration.object));
this.streetDecorations = [];
this.lastTreeSpawnZ = -100; // Reset spawn tracker
this.currentBuildingIndex = 0;
if (this.modelsLoaded) {
this.createInitialScene();
}
}
getGround() {
return this.road;
}
getBuildingFromPool(buildingKey) {
if (!this.buildingPool.has(buildingKey)) {
this.buildingPool.set(buildingKey, []);
}
const pool = this.buildingPool.get(buildingKey);
return pool.length > 0 ? pool.pop() : null;
}
returnBuildingToPool(building, buildingKey) {
// Reset building state for reuse
building.position.set(0, 0, 0);
building.rotation.set(0, 0, 0);
building.scale.setScalar(1);
if (!this.buildingPool.has(buildingKey)) {
this.buildingPool.set(buildingKey, []);
}
const pool = this.buildingPool.get(buildingKey);
// Limit pool size to prevent memory bloat
if (pool.length < 10) {
pool.push(building);
}
}
batchSpawnBuildings(buildingsToSpawn, cameraZ = 0) {
const loadedBuildings = Object.keys(this.buildingTemplates);
if (loadedBuildings.length === 0) return;
// Process all buildings in a single batch to minimize DOM manipulation
buildingsToSpawn.forEach(({ z, side }) => {
let buildingKey;
// Only use LOD system if models are fully loaded
if (this.modelsLoaded) {
// Calculate distance from camera for LOD selection
const distanceFromCamera = Math.abs(z - cameraZ);
buildingKey = this.selectLODModel(distanceFromCamera);
} else {
// Fallback to original cycling method while models are loading
buildingKey = loadedBuildings[this.currentBuildingIndex % loadedBuildings.length];
}
this.currentBuildingIndex++;
const batchTemplate = this.buildingTemplates[buildingKey];
const building = this.getBuildingFromPool(buildingKey) ?? batchTemplate?.clone();
if (!building) return; // Skip this building
const scale = 0.006 + Math.random() * 0.006;
building.scale.setScalar(scale);
// Calculate building width after scaling — measure directly, no clone needed
const bbox = new THREE.Box3().setFromObject(building);
const buildingWidth = bbox.getSize(new THREE.Vector3()).x;
const halfWidth = buildingWidth / 2 + 2; // 2 units safety margin
const xOffset = side === 'left' ? -6 - halfWidth : 6 + halfWidth;
building.position.set(xOffset, 0.1, z);
building.rotation.y = (Math.random() - 0.5) * 0.3;
this.scene.add(building);
this.activeBuildings.push({
object: building,
zPosition: z,
type: buildingKey,
scale: scale
});
});
}
calculateAdaptiveSpawnDelay(gameSpeed = 0.13) {
const currentTime = performance.now();
const frameTime = currentTime - this.performanceData.lastFrameTime;
this.performanceData.lastFrameTime = currentTime;
// Base spawn delay, adjusted for game speed to maintain density
let baseDelay = 800; // Base delay in ms
// Speed compensation: faster game = faster spawning to maintain visual density
const speedMultiplier = Math.max(0.3, 1 / (gameSpeed * 5)); // Inverse relationship with speed
let speedAdjustedDelay = baseDelay * speedMultiplier;
// Performance compensation: bad performance = slower spawning
if (frameTime > 20) {
this.performanceData.spawnDelay = Math.min(this.performanceData.spawnDelay + 100, 1500);
} else if (frameTime < 12) { // If performance is good, allow faster spawning
this.performanceData.spawnDelay = Math.max(this.performanceData.spawnDelay - 50, 200);
}
// Combine speed adjustment with performance adjustment
const finalDelay = Math.min(speedAdjustedDelay, this.performanceData.spawnDelay);
return Math.max(200, finalDelay); // Minimum 200ms to prevent overload
}
updateBuildings(gameSpeed, cameraZ) {
this.updateModels(gameSpeed, cameraZ);
}
}