-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProceduralCity.html
More file actions
1009 lines (802 loc) · 30.5 KB
/
Copy pathProceduralCity.html
File metadata and controls
1009 lines (802 loc) · 30.5 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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>Procedural City</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<link type="text/css" rel="stylesheet" href="Instructions.css">
</head>
<body>
<div id="blocker">
<div id="instructions">
<span style="font-size:60px">Welcome to Our Small City Generator</span>
<br /><br />
<span style="font-size:25px">--Instructions--</span>
<br />
<span style="font-size:20px">
When in sky view:<br/>
Change City= R<br/>
Change Camera Angles= 1,2,3,4,5<br/>
Go to First Person With F <br/>
</span>
<span style="font-size:20px">
When in first person:<br/>
Move= W,A,S,D<br/>
Look: MOUSE<br/>
Go back to SkyView With T<br/>
</span>
<span style="font-size:25px">Click to Continue</span>
</div>
</div>
<script src='build/perlin.js'></script>
<script type="module">
import * as THREE from './build/three.module.js';
import { PointerLockControls } from './build/PointerLockControls.js';
//initializes the global properties of three and the scene
let camera, scene, renderer, controls, blocker, instructions;
let viewType = "skyview";
let objects = [];
let moveForward = false;
let moveBackward = false;
let moveLeft = false;
let moveRight = false;
let canJump = false;
let prevTime = performance.now();
let velocity = new THREE.Vector3();
let direction = new THREE.Vector3();
let vertex = new THREE.Vector3();
let color = new THREE.Color();
//initializes the global properties of the city
let gridSize = 15;
let roadWidth = 20;
let maximumTreeDensity = 70;
let chunkSize = 150;
let chunkMargin = 10;
let minBuildingHeight = 50;
let maxBuildingHeight = 400;
const maxBuildingHeightRandomness = 15;
const tallBuildingAvgStop = 40;
function getSceneWidth() {
return chunkSize * gridSize;
}
function getSceneLength() {
return chunkSize * gridSize;
}
//controls how many small buildings per chunk
const subChunks = 2;
//largest gap possible between buildings
const maxBuildingGap = 20;
//setting the height range of our objects
const groundHeight = 30;
const curbHeight = 1;
const minTreeHeight = 4;
const maxTreeHeight = 10;
//global booleans to tell weather its a building chunk or another chunk
let groundMap;
let buildingMap;
//controls how random each chunk is in deciding weather its a ground or building chunk
const groundThreshold = 0.85;
const parkThreshold = 0.2;
function getRandomIntBetween(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generateLighting() {
// Creates lights with first parameter of color and last being intensity
let hemisphereLight = new THREE.HemisphereLight('lightblue', 'lightblue', 0.4);
let shadowLight = new THREE.DirectionalLight(0x000000, 0.25);
shadowLight.position.set(getSceneWidth() / 2, 800, getSceneLength() / 2);
let backLight = new THREE.DirectionalLight('lightblue', 0.1);
backLight.position.set(-120, 180, 60);
scene.add(backLight, shadowLight, hemisphereLight);
}
//normalizes our random numbers so they are between 0 and 1
function normalizeArray(array) {
let minValue = Math.min.apply(Math, array);
let maxValue = Math.max.apply(Math, array);
//makes the coords between 0 and 1
return array.map(function (value) {
return (value - minValue) / (maxValue - minValue);
});
}
//takes the 1-d array and makes it 2d for our chunk positions
function generate2DArray(array, numberOfColumns) {
let temp = array.slice(0);
let results = [];
while (temp.length) {
results.push(temp.splice(0, numberOfColumns));
}
return results;
}
// generates the base chunks
function generatePreceduralMaps() {
noise.seed(Math.random());
//generates the perlin noise randomness variables
let generalNoiseFrequency = 15;
let groundNoiseFrequency = 8;
let generalNoiseDistribution = [];
let groundNoiseDistribution = [];
//loops through and creates the distribution of noise across the chunk
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
generalNoiseDistribution.push(Math.abs(noise.perlin2(i / generalNoiseFrequency, j / generalNoiseFrequency))),
groundNoiseDistribution.push(Math.abs(noise.perlin2(i / generalNoiseFrequency, j / groundNoiseFrequency)));
}
}
//normalizes noise points between 0 and 1
let normalizedDistribution = normalizeArray(generalNoiseDistribution);
//a map that holds weather its a ground or building chunk
let groundDistributionMap = normalizedDistribution.map(function (arrayValue) {
return arrayValue <= groundThreshold ? true : false;
});
//transforms the map to 2d points
groundMap = generate2DArray(groundDistributionMap, gridSize);
// normalizes ground points
let normalizedGroundDistribution = normalizeArray(groundNoiseDistribution);
// Map our noises to an array holding binary values which indicate whether it's a building or a park block
let buildingDistributionMap = normalizedGroundDistribution.map(function (
arrayValue,
index
) {
return !!(groundDistributionMap[index] && arrayValue > parkThreshold);
});
// making the distribution of buildings a 2D point instead of a single value
buildingMap = generate2DArray(buildingDistributionMap, gridSize);
}
//creates the snow texture
const snowTexture = new THREE.TextureLoader().load( 'textures/snow.jpg' );
// Create a mesh we're going to use to model our snow elements
function getSnowMesh(boxParams, position) {
let material = new THREE.MeshPhongMaterial({ map : snowTexture });
// creates box geometry for the snow
let geometry = new THREE.BoxGeometry(
boxParams.width,
boxParams.height,
boxParams.depth
);
// Generate and return the mesh
let mesh = new THREE.Mesh(geometry, material);
mesh.position.set(position.x, position.y, position.z);
mesh.receiveShadow = false;
mesh.castShadow = false;
return mesh;
}
//takes a params we created for box and returns there mesh with a optional shadow
function createBoxMesh(boxParams, position, color, shadowOn) {
//makes shadowOn defaulted to true if its not passed in
if (typeof shadowOn === "undefined") shadowOn = true;
let material = new THREE.MeshLambertMaterial({
color: color
});
let geometry = new THREE.BoxGeometry(
boxParams.width,
boxParams.height,
boxParams.depth
);
let mesh = new THREE.Mesh(geometry, material);
mesh.position.set(position.x, position.y, position.z);
mesh.receiveShadow = true;
mesh.castShadow = shadowOn;
return mesh;
}
//takes all the meshes and merges them into a single mesh
function createSingleMesh(meshList) {
let geometry = new THREE.Geometry();
// Merge all of the meshes:
for (let i = 0; i < meshList.length; i++) {
meshList[i].updateMatrix();
geometry.merge(meshList[i].geometry, meshList[i].matrix);
}
//creates mesh
let mergedMesh = new THREE.Mesh(geometry, meshList[0].material);
mergedMesh.castShadow = true;
mergedMesh.receiveShadow = true;
return mergedMesh;
}
//functions to create proper cordinates for the scene
function getSceneXCoordinate(x) {
return x * chunkSize + chunkSize / 2 - getSceneWidth() / 2;
}
function getSceneZCoordinate(z) {
return z * chunkSize + chunkSize / 2 - getSceneLength() / 2;
}
//functions to check if its a ground or building chunk and returns its cords
function isGroundChunk(x, z) {
return groundMap[x][z];
}
function isBuildingChunk(x, z) {
return buildingMap[x][z];
}
// generates the each object on the chunk including making each type of non building chunk
function generateCityChunkObjects() {
let streetHeight = 2 * curbHeight;
// makes the base color of our streets a dark brown color
let baseColor = 0x736b5c;
let baseGeometryParams = { width: getSceneWidth(), height: groundHeight, depth: getSceneLength() };
let basePosition = { x: 0, y: -(groundHeight / 2) - streetHeight, z: 0 };
let baseMesh = createBoxMesh(baseGeometryParams, basePosition, baseColor);
// Initialize the snow mesh parameters and creates the snow mesh
let snowGeometryParams = { width: getSceneWidth() - 2, height: 0, depth: getSceneLength() - 2 };
let snowPosition = { x: 0, y: -streetHeight, z: 0 };
let snow = getSnowMesh(snowGeometryParams, snowPosition);
//determines where the streets should go
let groundMeshList = [];
let streetMeshList = [];
for (let i = 0; i < groundMap.length; i++) {
for (let j = 0; j < groundMap[0].length; j++) {
if (isGroundChunk(i, j)) {
let x = getSceneXCoordinate(i);
let z = getSceneZCoordinate(j);
groundMeshList.push(
createBoxMesh(
// Geometry parameters
{ width: chunkSize, height: 0, depth: chunkSize },
// Positional parameters
{ x: x, y: -streetHeight, z: z
}, // Mesh color
0x736b5c
));
streetMeshList.push(
createBoxMesh(
{ width: chunkSize, height: streetHeight, depth: chunkSize },
{ x: x, y: -streetHeight / 2, z: z
}, // adds the color to the street
0x999999
));
}
}
}
// puts the street and ground together in the mesh
if (streetMeshList.length) scene.add(createSingleMesh(streetMeshList));
if (groundMeshList.length) scene.add(createSingleMesh(groundMeshList));
//adds snow chunks to the screen
scene.add(baseMesh, snow);
}
//makes the ground chunks
function generateGroundChunk() {
// loops through the grid to check for ground chunks
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
if (isGroundChunk(i, j)) {
//converts cords so it can be displayed in the scene
let x = getSceneXCoordinate(i);
let z = getSceneZCoordinate(j);
// assigns the curb width based off the
// randomness of the road and chunk next to it
let curbWidth = chunkSize - roadWidth;
//checks if chunk is for building or not
if (isBuildingChunk(i, j)) {
let buildingCurbMesh = createBoxMesh(
// Geometry parameters
{ width: curbWidth, height: curbHeight, depth: curbWidth
},
// Positional parameters
{ x: x, y: curbHeight / 2, z: z
});
scene.add(buildingCurbMesh);
//calculates random height and number buildings in the chunk
let buildingHeight = getRandomIntBetween(
minBuildingHeight,
maxBuildingHeight
);
let buildingWidth = curbWidth - chunkMargin * 2;
let buildingGeometryParameters = {
width: buildingWidth,
height: buildingHeight,
depth: buildingWidth
};
let buildingPosition = { x: x, z: z };
generateBuildingChunk(
buildingGeometryParameters,
buildingPosition,
subChunks,
[] );
//else make a park or parking lot if its not a building chunk
} else {
//random chance for a park or parking lot
let parkRand = Math.random() * 10;
if (parkRand <= 5) {
//creates park
let parkMesh = createBoxMesh(
// Geometry parameters
{ width: curbWidth, height: curbHeight, depth: curbWidth},
// Positional parameters
{ x: x, y: curbHeight / 2, z: z},
//sets the park to the color green
0x81a377
);
scene.add(parkMesh);
let treeWidth = curbWidth - chunkMargin * 2;
generateTrees(x, z, treeWidth);
//creates parking lot
} else {
// parking textures
let parkingTexture = new THREE.TextureLoader().load( 'textures/parking1.jpg');
parkingTexture.repeat.set(3, 5);
parkingTexture.wrapS = THREE.RepeatWrapping;
parkingTexture.wrapT = THREE.RepeatWrapping;
let parkingMaterial = new THREE.MeshLambertMaterial({map: parkingTexture});
let parkingGeometry = new THREE.BoxGeometry(curbWidth, curbHeight, curbWidth);
let parkingMesh = new THREE.Mesh(parkingGeometry, parkingMaterial);
parkingMesh.position.set(x, curbHeight / 2, z);
scene.add(parkingMesh);
}
}
}
}
}
}
// makes a tree
let Tree = function (x, z) {
// holds the different part of the tree
this.components = [];
// Generate a random height for our tree
let treeHeight = getRandomIntBetween(minTreeHeight, maxTreeHeight);
let treeMaterial = new THREE.MeshLambertMaterial({color: 0x736b5c});
let branchColors = [0x306030, 0xff7f50, 0xc30b4e,0xffa631, 0xfffafa, 0xf8f8ff];
let colorRand = Math.floor(Math.random() * branchColors.length);
let branchMaterial = new THREE.MeshLambertMaterial({color: branchColors[colorRand]});
// Create a the mesh for the trunk
let trunkGeometry = new THREE.BoxGeometry(2, treeHeight, 2);
let trunkMesh = new THREE.Mesh(trunkGeometry, treeMaterial);
trunkMesh.position.set(x, treeHeight / 2 + curbHeight, z);
// Create a the mesh for branch type 1
let branchGeometry1 = new THREE.BoxGeometry(8, maxTreeHeight * 1.5, 5);
let branchMesh1 = new THREE.Mesh(branchGeometry1, branchMaterial);
branchMesh1.position.set(x, treeHeight + curbHeight + 5, z);
// Create a the mesh for branch type 2
let branchGeometry2 = new THREE.DodecahedronGeometry( 6 );
let branchMesh2 = new THREE.Mesh(branchGeometry2, branchMaterial);
branchMesh2.position.set(x, treeHeight+5, z);
// Create a the mesh for branch type 3
let branchGeometry3 = new THREE.ConeGeometry( 5, maxTreeHeight* 1.5);
let branchMesh3 = new THREE.Mesh(branchGeometry3, branchMaterial);
branchMesh3.position.set(x, treeHeight+5, z);
// Create a the mesh for branch type 4
let branchGeometry4 = new THREE.CylinderGeometry( 5, 5, maxTreeHeight* 1.5);
let branchMesh4 = new THREE.Mesh(branchGeometry4, branchMaterial);
branchMesh4.position.set(x, treeHeight+5, z);
//an array to randomly swap between tree types
let branchMeshes = [branchMesh1,branchMesh2,branchMesh3,branchMesh4];
// Rotate the tree in a random direction
branchMeshes[treeRand].rotation.y = Math.random();
// Add the branch and trunk to the tree components list
this.components.push(branchMeshes[treeRand], trunkMesh);
// makes the tree a single mesh based on its components
this.getMergedMesh = function () {
return createSingleMesh(this.components);
};
};
//adds the trees to the scene
function generateTrees(x, z, parkSize) {
let trees = [];
//adds a random number of trees to the chunk
let numberOfTrees = getRandomIntBetween(0, maximumTreeDensity);
for (let i = 0; i < numberOfTrees; i++) {
//random position for the trees
let tree_x_coord = getRandomIntBetween(x - parkSize / 2, x + parkSize / 2);
let tree_z_coord = getRandomIntBetween(z - parkSize / 2, z + parkSize / 2);
let tree = new Tree(tree_x_coord, tree_z_coord);
trees.push(tree.getMergedMesh());
}
// adds the chunk of trees to the scene
if (trees.length) scene.add(createSingleMesh(trees));
}
//building textures
let buildingTextures = [11];
buildingTextures[0] = new THREE.TextureLoader().load( 'textures/building0.jpg' );
buildingTextures[0].repeat.set(8, 8);
buildingTextures[0].wrapS = THREE.RepeatWrapping;
buildingTextures[0].wrapT = THREE.RepeatWrapping;
for(let i = 1; i < 10; i++ ){
buildingTextures[i] = new THREE.TextureLoader().load( 'textures/building' + i + '.jpg');
}
//the number that chooses a random texture from the array and random shape
let buildingRand = Math.floor(Math.random() * buildingTextures.length);
let shapeRand = Math.floor(Math.random() * 10);
let treeRand = Math.floor(Math.random() * 4);
// Create a mesh we're going to use for our buildings
function getBuildingMesh(boxGeometryParameters, position) {
let buildingSideMaterial = new THREE.MeshLambertMaterial({
map: buildingTextures[buildingRand] // random building texture
});
//maps texture on sides of the building
buildingSideMaterial.map.needsUpdate = true;
let buildingTopMaterial = new THREE.MeshLambertMaterial({
color: color
});
// Create a box geometry for box shaping buildings
let boxGeometry = new THREE.BoxGeometry(
boxGeometryParameters.width,
boxGeometryParameters.height,
boxGeometryParameters.depth
);
// Set the building materials for each box geometry building side
let boxMaterials = [
buildingSideMaterial,
buildingSideMaterial,
buildingTopMaterial,
buildingTopMaterial,
buildingSideMaterial,
buildingSideMaterial
];
// Create a cylinder geometry for rounded buildings
let roundGeometry = new THREE.CylinderGeometry(
boxGeometryParameters.width/2.2,
boxGeometryParameters.width/2.2,
boxGeometryParameters.height
);
// Set the building materials for each cylinder building side
let roundMaterials = [
buildingSideMaterial, // Left side
buildingTopMaterial, // Top side
];
// Create a cone geometry for cone shaped buildings
let coneGeometry = new THREE.ConeGeometry(
boxGeometryParameters.width/2.2,
boxGeometryParameters.height
);
// Set the building materials for each cone building side
let coneMaterials = [
buildingSideMaterial, // Other sides
buildingTopMaterial, // Top/Bottom side
];
let shapeGeometries = [boxGeometry,roundGeometry,coneGeometry];
let shapeMaterials = [boxMaterials,roundMaterials,coneMaterials];
//chooses a random shape for the building
let mesh;
// 50 percent of buildings are box geometry
if (shapeRand <= 5){
mesh = new THREE.Mesh(shapeGeometries[0],shapeMaterials[0]);
}
// 30 percent of buildings are cylinder geometry
else if (shapeRand > 5 && shapeRand <= 8){
mesh = new THREE.Mesh(shapeGeometries[1],shapeMaterials[1]);
}
// 20 percent of buildings are cone geometry
else if (shapeRand > 8){
mesh = new THREE.Mesh(shapeGeometries[2],shapeMaterials[2]);
}
mesh.position.set(position.x, position.y, position.z);
mesh.receiveShadow = true;
mesh.castShadow = true;
return mesh;
}
// creates buildings
let Building = function (geometryParameters, position) {
// holds each part of a building
this.components = [];
let buildingMesh = getBuildingMesh(geometryParameters, position);
this.components.push(buildingMesh);
this.getMergedMesh = function () {
return createSingleMesh(this.components);
};
};
//determines if the building is tall or short
function isTall(height) {
return Math.round(height / maxBuildingHeight * 100) >= tallBuildingAvgStop;
}
//makes building chunk
function generateBuildingChunk(
geometryParameters,
position,
numOfDivisions,
buildings
) {
// determines how many buildings are in chunk based on size of buildings
if (isTall(geometryParameters.height) || numOfDivisions < 1) {
// Generate a randomized range of heights for the buildings
let maxHeightDeviation = getRandomIntBetween(0, maxBuildingHeightRandomness);
// Generate a random building height falling within our range
let buildingHeight = getRandomIntBetween(
geometryParameters.height - maxHeightDeviation,
geometryParameters.height + maxHeightDeviation
);
let buildingGeometryParameters = {
width: geometryParameters.width,
height: buildingHeight,
depth: geometryParameters.depth
};
let buildingPosition = {
x: position.x,
y: buildingGeometryParameters.height / 2 + curbHeight,
z: position.z
};
//adds building to chunk
let building = new Building(buildingGeometryParameters, buildingPosition);
buildings.push(building.getMergedMesh());
// Calculate the amount of buildings we've already generated
let totalBuildingsBuilt = buildings.length;
//sees how many more buildings need to be created
let totalBuildingsToBuild = Math.pow(2, subChunks);
//stops making buildings if there is not more and adds the chunk to the scene
if (
totalBuildingsBuilt >= totalBuildingsToBuild ||
isTall(buildingGeometryParameters.height)
) {
scene.add(createSingleMesh(buildings));
}
} else {
//if more buildings need to be made it makes a smaller chunk an does the same process
let sliceDeviation = Math.abs(
getRandomIntBetween(0, maxBuildingGap)
);
//ensures we are still within the chunk and creates a new depth for the smaller chunk
if (geometryParameters.width <= geometryParameters.depth) {
let depth1 =
Math.abs(geometryParameters.depth / 2 - sliceDeviation) -
chunkMargin / 2;
let depth2 =
Math.abs(-(geometryParameters.depth / 2) - sliceDeviation) -
chunkMargin / 2;
// then makes new z coordinates for the smaller chunk
let z1 =
position.z +
sliceDeviation / 2 +
geometryParameters.depth / 4 +
chunkMargin / 4;
let z2 =
position.z +
sliceDeviation / 2 -
geometryParameters.depth / 4 -
chunkMargin / 4;
//now recursively make the buildings in the smaller chunk
generateBuildingChunk(
//building mesh properties
{ width: geometryParameters.width, height: geometryParameters.height, depth: depth1 },
{ x: position.x, z: z1 },
//lowers the amount of sub chunks need be made
numOfDivisions - 1,
buildings
);
//calls function again on second z positions
generateBuildingChunk(
{ width: geometryParameters.width, height: geometryParameters.height, depth: depth2 },
{ x: position.x, z: z2 },
numOfDivisions - 1,
buildings
);
} else {
//make more sub chunks based off other possible depth values
let width1 =
Math.abs(geometryParameters.width / 2 - sliceDeviation) -
chunkMargin / 2;
let width2 =
Math.abs(-(geometryParameters.width / 2) - sliceDeviation) -
chunkMargin / 2;
// new position
let x1 =
position.x +
sliceDeviation / 2 +
geometryParameters.width / 4 +
chunkMargin / 4;
let x2 =
position.x +
sliceDeviation / 2 -
geometryParameters.width / 4 -
chunkMargin / 4;
//does recursion again
generateBuildingChunk(
{ width: width1, height: geometryParameters.height, depth: geometryParameters.depth },
{ x: x1, z: position.z },
numOfDivisions - 1,
buildings
);
generateBuildingChunk(
{ width: width2, height: geometryParameters.height, depth: geometryParameters.depth },
{ x: x2, z: position.z },
numOfDivisions - 1,
buildings
);
}
}
}
// Function called on window resize events.
function onWindowResize() {
renderer.setSize(window.innerHeight, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
// Generate the building scene and renderer
function generateScene() {
scene = new THREE.Scene();
if (viewType === "firstperson") {
scene.fog = new THREE.Fog( 'skyblue', 0, 1000 );
}
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true, premultipliedAlpha: false });
renderer.shadowMapEnabled = true;
//sets the type of the shadowType
renderer.shadowMapType = THREE.PCFSoftShadowMap;
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
if (viewType === "skyview") {
gridSize = 10;
skyview();
}else {
gridSize = 25;
firstPerson();
}
//calculates for when the window gets resized
window.addEventListener("resize", onWindowResize, false);
}
let render = function () {
requestAnimationFrame(render);
//sets up pointer lock controls
if (viewType === "firstperson") { //turns firstPerson view on
checkCameraBounds();
let time = performance.now();
let delta = ( time - prevTime ) / 1000;
velocity.x -= velocity.x * 8.0 * delta;
velocity.z -= velocity.z * 8.0 * delta;
velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
direction.z = Number( moveForward ) - Number( moveBackward );
direction.x = Number( moveRight ) - Number( moveLeft );
direction.normalize(); // this ensures consistent movements in all directions
if ( moveForward || moveBackward ) velocity.z -= direction.z * 400.0 * delta;
if ( moveLeft || moveRight ) velocity.x -= direction.x * 400.0 * delta;
controls.moveRight( - velocity.x * delta );
controls.moveForward( - velocity.z * delta );
controls.getObject().position.y += ( velocity.y * delta );
if ( controls.getObject().position.y < 10 ) {
velocity.y = 0;
controls.getObject().position.y = 10;
canJump = true;
}
prevTime = time;
}
renderer.render(scene, camera);
};
// Ensures the explorer stays within the bounds of the city
function checkCameraBounds() {
let maxBound = getSceneWidth()/2;
let minBound = (getSceneWidth()*-1)/2;
if (camera.position.x > maxBound) {
camera.position.x = maxBound;
}
if (camera.position.x < minBound) {
camera.position.x = minBound;
}
if (camera.position.z > maxBound) {
camera.position.z = maxBound;
}
if (camera.position.z < minBound) {
camera.position.z = minBound;
}
}
// resets the scene
function reset() {
controls.unlock();
let canvas = document.getElementsByTagName("CANVAS")[0];
document.body.removeChild(canvas);
init();
}
//initilizes the new reset scene
function init() {
generateScene();
generateLighting();
generatePreceduralMaps();
generateCityChunkObjects();
generateGroundChunk();
}
init();
render();
function skyview() {//sets up the sky texture
let maxBound = getSceneWidth()/2;
let minBound = (getSceneWidth()*-1)/2;
// Create a camera and set its position in the world space.
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 4000);
camera.position.set(minBound, maxBound-100, minBound);
camera.lookAt(new THREE.Vector3(0, 0, 0));
// Use orbit controls, which allow the camera to orbit around a target.
controls = new PointerLockControls( camera, document.body );
blocker = document.getElementById( 'blocker' );
instructions = document.getElementById( 'instructions' );
instructions.addEventListener( 'click', function () {
controls.lock();
}, false );
controls.addEventListener( 'lock', function () {
instructions.style.display = 'none';
blocker.style.display = 'none';
} );
controls.addEventListener( 'unlock', function () {
blocker.style.display = 'block';
instructions.style.display = '';
} );
//adds controls to sky view
window.onkeyup = function(event)
{
switch(event.key)
{
case 'f':
viewType = "firstperson";
reset();
break;
case 'r':
viewType = "skyview";
buildingRand = Math.floor(Math.random() * buildingTextures.length);
shapeRand = Math.floor(Math.random() * 10);
treeRand = Math.floor(Math.random() * 4);
reset();
case '1':
camera.position.set(minBound, maxBound-100, minBound);
camera.lookAt(new THREE.Vector3(0, 0, 0));
break;
case '2':
camera.position.set(0, maxBound, 0);
break;
case '3':
camera.position.set(maxBound, maxBound-100, maxBound);
camera.lookAt(new THREE.Vector3(0, 0, 0));
break;
case '4':
camera.position.set(maxBound, maxBound-100, 0);
camera.lookAt(new THREE.Vector3(0, 0, 0));
break;
case '5':
camera.position.set(0, maxBound-100, maxBound);
camera.lookAt(new THREE.Vector3(0, 0, 0));
break;
}
}
}
// Adds first person view and its event listeners
function firstPerson() {
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 4000 );
controls = new PointerLockControls( camera, document.body );
window.onkeydown = function(event)
{
switch(event.key)
{
case 'ArrowUp':
case 'w':
moveForward = true;
break;
case 'ArrowLeft':
case 'a':
moveLeft = true;
break;
case 'ArrowDown':
case 's':
moveBackward = true;
break;
case 'ArrowRight':
case 'd':
moveRight = true;
break;
case ' ': // space
if ( canJump === true ) velocity.y += 200;
canJump = false;
break;
case 't':
viewType = "skyview";
reset();
}
};
window.onkeyup = function(event)
{
switch(event.key)
{
case 'ArrowUp':
case 'w':
moveForward = false;
break;
case 'ArrowLeft':
case 'a':
moveLeft = false;
break;
case 'ArrowDown':
case 's':
moveBackward = false;
break;
case 'ArrowRight':