-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyramids Simulation.html
More file actions
349 lines (324 loc) · 16.9 KB
/
Pyramids Simulation.html
File metadata and controls
349 lines (324 loc) · 16.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
<html>
<head>
<title>Pyramids Simulation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel = "icon" type = "image/png" href = "icon.png">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<script src="build/three.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/controls/dat.gui.min.js"></script>
<script src="js/OBJLoader.js"></script>
<div id="myModal">
<div id="modal-content">
<img src="icon.png" width="40%" height="20%">
<p><b>
Welcome to Pyramids Simulation V2<br><br><hr><br>
**Brief about the pyramids**<br><br><br>
The Egyptian pyramids are ancient pyramid-shaped masonry structures located in Egypt<br><br>
They were built as tombs for the country's pharaohs<br><br>
The Pyramid of Khufu at Giza is the largest Egyptian pyramid<br><br>
It is the only one of the Seven Wonders of the Ancient World still in existence<br><br>
The middle sized is named Khafre<br><br>
The smalest sized is named Menkaure<br><br>
<hr><br>**Controls**<br><br><br>
Mouse scroll to zoom in and out<br><br>
Mouse left click hold + mouse movement to free rotate<br><br>
<hr><br>
Hope you enjoy it
<br><br>
</b></p>
<button id="closebtn" style="margin-left: 7.5vw;" onclick="modalclose()">
<b>Proceed</b></button><button id="chngbtn"><b>Changelog</b></button>
</div>
<div id="modal2-content">
<p><b>
**CHANGELOG**<br>
<hr><br>**V1**<br><br><br>
Added sphere<br><br>
Added plane<br><br>
Added cylinders<br><br>
Edited cylinders to shape pyramids<br><br>
Added camera<br><br>
Edited camera limits<br><br>
Added textures<br><br>
Added the welcome message and controls<br><br>
<hr><br>**V2**<br><br><br>
Added brief about the pyramids<br><br>
Added changelog<br><br>
Added the sun & shadows<br><br>
Added sphinx<br><br>
Added the objects (2 camels & a horse)<br><br>
Added movement to a camel<br><br>
Changed the sky texture<br><br>
Added camera reset button<br><br>
Added day control button<br><br>
Added the moon<br><br>
<hr><br>
</b></p>
<button id="bckbtn"><b>Back</b></button><button id="closebtn" style="margin-right: 7vw;" onclick="modalclose()"><b>Proceed</b></button>
</div>
</div>
<script>
//displaying the modal
window.onload = function() {
document.getElementById("myModal").style.display = "block";
}
//switching between the modal pages
document.getElementById("chngbtn").onclick = function() {
document.getElementById("modal-content").style.display = "none";
document.getElementById("modal2-content").style.display = "block";
}
//switching between the modal pages
document.getElementById("bckbtn").onclick = function() {
document.getElementById("modal-content").style.display = "block";
document.getElementById("modal2-content").style.display = "none";
}
//closing the modal and setting the controls
function modalclose(){
control = new THREE.OrbitControls( camera );
control.maxPolarAngle = Math.PI/2;
control.maxDistance = 900;
control.enableKeys = false;
document.getElementById("modal-content").style.display = "none";
document.getElementById("modal2-content").style.display = "none";
document.getElementById("myModal").style.display = "none";
};
var camera, control, scene, renderer, light ,light2 , light3;
var lightBool = false; //boolean for 'toggle light'
var lightVal = 0; //value for light change
var loaded = false; //boolean for camel movement
var moveVal = 0.1; //value for camel movement
var cameldegree = 50; //value for camel rotation
init();
animate();
function init() {
//creating the scene and the camera
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 70, window.innerWidth/window.innerHeight, 1, 10000 );
camera.position.set( -600, 70, 500);
//adding the sphere(sky) and setting its texture
var sphereGeo = new THREE.SphereBufferGeometry( 1000, 32, 32 );
var skytexture = new THREE.TextureLoader().load( 'textures/sky.jpg' );
var skymaterial = new THREE.MeshPhongMaterial( { map: skytexture } );
var containerMesh = new THREE.Mesh( sphereGeo, skymaterial );
skymaterial.side = THREE.BackSide;
containerMesh.position.x = 0;
containerMesh.position.y = 0;
containerMesh.position.z = 0;
scene.add( containerMesh );
//adding the plane(floor) and setting its texture
var planeGeo = new THREE.BoxBufferGeometry( 2000, 0, 2000 );
var sandtexture = new THREE.TextureLoader().load( 'textures/sand.jpg' );
var sandmaterial = new THREE.MeshPhongMaterial( { map: sandtexture } );
var planeMesh = new THREE.Mesh( planeGeo, sandmaterial );
planeMesh.receiveShadow = true;
planeMesh.position.y = -107;
containerMesh.add( planeMesh );
//applying texture for the pyramids
var texture = new THREE.TextureLoader().load( 'textures/pyramids.jpg' );
var material = new THREE.MeshPhongMaterial( { map: texture } ); //phong to receive/cast shadows
/* The Biggest Pyramid */
var firstPyramidGeo = new THREE.CylinderBufferGeometry( 0, 210, 210, 4 );
var firstPyramidMesh = new THREE.Mesh( firstPyramidGeo, material );
firstPyramidMesh.castShadow = true;
firstPyramidMesh.receiveShadow = true;
firstPyramidMesh.position.y = -1 ;
containerMesh.add( firstPyramidMesh );
/* The Smallest Pyramid */
var secondPyramidGeo = new THREE.CylinderBufferGeometry( 0, 130, 130, 4 );
var secondPyramidMesh = new THREE.Mesh( secondPyramidGeo, material );
secondPyramidMesh.position.x += 280 ;
secondPyramidMesh.position.y -= 41 ;
secondPyramidMesh.position.z += 120;
secondPyramidMesh.castShadow = true;
secondPyramidMesh.receiveShadow = true;
containerMesh.add( secondPyramidMesh );
/* The Middle Pyramid */
var thirdPyramidGeo = new THREE.CylinderBufferGeometry( 0, 150, 150, 4 );
var thirdPyramidMesh = new THREE.Mesh( thirdPyramidGeo, material );
thirdPyramidMesh.position.x -= 320 ;
thirdPyramidMesh.position.y -= 32 ;
thirdPyramidMesh.position.z -= 80;
thirdPyramidMesh.castShadow = true;
thirdPyramidMesh.receiveShadow = true;
containerMesh.add( thirdPyramidMesh );
//creating the upper control box options
var options = {
'Reset Camera': function() {
camera.position.set( -600, 70, 500);
control = new THREE.OrbitControls( camera );
},
'Toggle Light': function() {
lightVal = lightVal == 0 ? 1 : 0;
}
};
//adding options to the upper control box
var gui = new dat.GUI();
gui.add(options, 'Reset Camera');
gui.add(options, 'Toggle Light');
//adding the objects and textures loaders
var textureLoader = new THREE.TextureLoader();
var objLoader = new THREE.OBJLoader();
//adding sphinx
var sphinxtexture = textureLoader.load( 'textures/sphinx.jpg' );
var sphinxmaterial = new THREE.MeshPhongMaterial( { map: sphinxtexture } ); //phong to receive/cast shadows
objLoader.load('models/sphinx.obj',function(sp){
sp.traverse(function(node){
if (node.isMesh) {node.material=sphinxmaterial};
node.castShadow = true;
node.receiveShadow = true;
});
sp.position.x = -550 ;
sp.position.y = -110 ;
sp.position.z = -220 ;
sp.rotation.x = THREE.Math.degToRad(270);
sp.rotation.z = THREE.Math.degToRad(-45);
sp.scale.x = 0.055;
sp.scale.y = 0.055;
sp.scale.z = 0.055;
containerMesh.add( sp );
});
//adding the moving camel
var camelSkin1 = textureLoader.load( 'textures/camel1.jpg' );
var camelmaterial = new THREE.MeshPhongMaterial( { map: camelSkin1 } ); //phong to receive/cast shadows
objLoader.load('models/camel.obj',function(c1){
c1.traverse(function(node){
if (node.isMesh) {node.material=camelmaterial};
node.castShadow = true;
node.receiveShadow = true;
loaded = true;
});
c1.position.x += 180 ;
c1.position.y -= 105 ;
c1.position.z += 200;
c1.rotation.y -= THREE.Math.degToRad(115);
c1.scale.x += 23;
c1.scale.y += 23;
c1.scale.z += 23;
containerMesh.add( c1 );
});
//adding the second camel with different object and texture
var camelSkin2 = textureLoader.load( 'textures/camel2.jpg' );
var camelmaterial2 = new THREE.MeshPhongMaterial( { map: camelSkin2 } ); //phong to receive/cast shadows
objLoader.load('models/camel2.obj',function(c2){
c2.traverse(function(node){
if (node.isMesh) {node.material=camelmaterial2};
node.castShadow = true;
node.receiveShadow = true;
});
c2.position.x -= 380 ;
c2.position.y -= 107 ;
c2.position.z += 130;
c2.rotation.y = THREE.Math.degToRad(cameldegree);
c2.scale.x += 10;
c2.scale.y += 10;
c2.scale.z += 10;
containerMesh.add( c2 );
});
//adding the horse
var horseSkin = textureLoader.load( 'textures/horse.jpg' );
var horsematerial = new THREE.MeshPhongMaterial( { map: horseSkin } ); //phong to receive/cast shadows
objLoader.load('models/horse.obj',function(h1){
h1.traverse(function(node){
if (node.isMesh) {node.material=horsematerial};
node.castShadow = true;
node.receiveShadow = true;
});
h1.position.x -= 250 ;
h1.position.y -= 115 ;
h1.position.z += 400;
h1.rotation.y -= THREE.Math.degToRad(87);
h1.scale.x += 11;
h1.scale.y += 11;
h1.scale.z += 11;
containerMesh.add( h1 );
});
//adding the first light to cast shadows
light = new THREE.DirectionalLight(0xffffff, 0.8);
light.castShadow = true;
light.shadow.camera.near = 1;
light.shadow.camera.far = 10000;
light.shadow.camera.left = -500;
light.shadow.camera.bottom = -500;
light.shadow.camera.right = 300;
light.shadow.camera.top = 600;
light.shadow.mapSize.width = 2048;
light.shadow.mapSize.height = 2048;
light.position.set(670,670,670);
containerMesh.add( light );
//adding the second light to light up everything
light2 = new THREE.PointLight( 0xffffff, 1);
light2.castShadow = true;
light2.position.set( 0, -900, 0 );
containerMesh.add( light2 );
//adding the thrid light to simulate moon
light3 = new THREE.DirectionalLight(0xffffff, 0);
light3.position.set(-450,-500,670);
light3.add( new THREE.Mesh(
new THREE.SphereBufferGeometry( 50, 20 , 20 ),
new THREE.MeshBasicMaterial({ color: 0xcccccc })
));
containerMesh.add( light3 );
//creating a pivot
pivot = new THREE.Group();
//moving the pivot back to world origin
pivot.position.set( 0.0, 0.0, 0 );
//adding the pivot to the center of the sphere
containerMesh.add( pivot );
pivot.add( light );
pivot.add( light3 );
light2.layers.set(1);
containerMesh.layers.set(1);
//setting the renderer
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
document.body.appendChild( renderer.domElement );
}
function animate() {
//start the camel movement after loading and rotate after reaching a certain position
if (loaded == true) {
if (scene.children[0].children[6].position.z >= 450 || scene.children[0].children[6].position.z <= 0) {
cameldegree+=180;
moveVal = -moveVal;
scene.children[0].children[6].rotation.y = THREE.Math.degToRad(cameldegree) ;
}
scene.children[0].children[6].position.z += moveVal;
scene.children[0].children[6].position.x += moveVal;
}
//making it night
if (lightBool == false) {
light2.intensity -= 0.0035 * lightVal;
light.intensity -= 0.003 * lightVal;
light3.intensity += 0.0055 * lightVal;
pivot.rotation.z += 0.008 * lightVal;
if (light2.intensity <= 0){
lightBool = true;
light2.intensity = -1.7;
light.intensity = -0.7;
}
}
//making it day
if (lightBool == true) {
light2.intensity += 0.0055 * lightVal;
light.intensity += 0.003 * lightVal;
light3.intensity -= 0.0035 * lightVal;
pivot.rotation.z += 0.008 * lightVal;
if (light2.intensity >= 1){
lightBool = false;
}
}
camera.layers.set(0);
renderer.render(scene, camera);
renderer.autoClear = false;
camera.layers.set(1);
renderer.render(scene, camera);
requestAnimationFrame( animate );
}
</script>
</body>
</html>