Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics Fundamentals / Practicum Homework / a09.html
blob: c74dfc62f34879c234b136288e82d6699fcbbddf (plain) (blame)
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
<html>
	<head>
		<style>
			body { margin: 0; }
			canvas { width: 100%; height: 100% }
		</style>
	</head>
	<body>
		<script src="three.min.js"></script>
		<script src="CSG.js"></script>
		<script>
			var renderer = new THREE.WebGLRenderer();
			renderer.setSize( window.innerWidth, window.innerHeight );
			renderer.setAnimationLoop( drawFrame );
			renderer.shadowMap.enabled = true;
			renderer.shadowMap.type = THREE.PCFSoftShadowMap;
			document.body.appendChild( renderer.domElement );

			var scene = new THREE.Scene();
			
			var aspect = window.innerWidth/window.innerHeight;
			var camera = new THREE.PerspectiveCamera( 30, aspect );
			// camera.position.set( 0, 1200, 0 );
			// camera.lookAt( new THREE.Vector3(0, 0, -1) );
			var CAM_HEIGHT = 10;
			var cameraPos = new THREE.Vector3();
			camera.position.set(0, CAM_HEIGHT, 0);
			var cameraLook = new THREE.Vector3(0, CAM_HEIGHT, 1);
			camera.lookAt(cameraLook);



			var groundShape = new THREE.Shape();
			groundShape.moveTo(-31, 142);
			groundShape.lineTo(8, 142);
			groundShape.lineTo(38, 164);
			groundShape.lineTo(65, 151);
			groundShape.lineTo(103, 204);
			groundShape.lineTo(152, 214);
			groundShape.lineTo(154, 201);
			groundShape.lineTo(189, 152);
			groundShape.lineTo(208, 123);
			groundShape.lineTo(162, 111);
			groundShape.lineTo(168, 91);
			groundShape.lineTo(136, 90);
			groundShape.lineTo(154, 30);
			groundShape.lineTo(146, 6);
			groundShape.lineTo(197, -38);
			groundShape.lineTo(194, -95);
			groundShape.lineTo(175, -74);
			groundShape.lineTo(142, -88);
			groundShape.lineTo(150, -112);
			groundShape.lineTo(138, -121);
			groundShape.lineTo(132, -142);
			groundShape.lineTo(150, -152);
			groundShape.lineTo(52, -246);
			groundShape.lineTo(-11, -226);
			groundShape.lineTo(-86, -276);
			groundShape.lineTo(-100, -314);
			groundShape.lineTo(-163, -286);
			groundShape.lineTo(-116, -242);
			groundShape.lineTo(-129, -213);
			groundShape.lineTo(-70, -132);
			groundShape.lineTo(-56, -136);
			groundShape.lineTo(-26, -93);
			groundShape.lineTo(-55, -87);
			groundShape.lineTo(-80, -62);
			groundShape.lineTo(-141, -92);
			groundShape.lineTo(-162, -64);
			groundShape.lineTo(-106, -20);
			groundShape.lineTo(-142, 5);
			groundShape.lineTo(-236, -8);
			groundShape.lineTo(-252, 146);
			groundShape.lineTo(-240, 169);
			groundShape.lineTo(-106, 266);
			groundShape.lineTo(-112, 162);
			groundShape.lineTo(-64, 175);
			groundShape.lineTo(-62, 201);
			groundShape.lineTo(-24, 197);

			var extrudeSettings = { depth: 3, bevelEnabled: true, bevelSegments: 2, steps: 2, bevelSize: 0.1, bevelThickness: 0.1 };
			var ground = new THREE.Mesh(new THREE.ExtrudeGeometry( groundShape, extrudeSettings ),
			                            new THREE.MeshPhongMaterial({ color: 'green' }));
			ground.rotation.x = -Math.PI / 2;
			ground.receiveShadow = true;

			var uni = new THREE.Mesh(new THREE.BoxGeometry(28, 10, 20),
			                         new THREE.MeshPhongMaterial({ color: 'gray' }));
			uni.position.set(42, 5, -111);
			uni.rotation.y = Math.PI / 4;
			uni.castShadow = true;
			scene.add(uni);

			var mosque = new THREE.Mesh(new THREE.BoxGeometry(10, 12, 10),
			                            new THREE.MeshPhongMaterial({ color: 0xdec687 }));
			mosque.position.set(-181, 6, -30);
			mosque.castShadow = true;
			scene.add(mosque);

			var founders = new THREE.Mesh(new THREE.BoxGeometry(30, 20, 30),
			                              new THREE.MeshPhongMaterial({ color: 0xcdc1c1 }));
			founders.position.set(-101, 10, 47);
			founders.rotation.y = Math.PI / 4;
			founders.castShadow = true;
			scene.add(founders);

			var lake = new THREE.Mesh(new THREE.BoxGeometry(40, 3, 50),
			                          new THREE.MeshPhongMaterial({ color: 0x9cc0f9 }));
			lake.position.set(66, 2, 159);
			lake.rotation.y = -Math.PI / 4;
			scene.add(lake);



			scene.add(ground);

			var light = new THREE.PointLight(0xffffff, 0.5);
			light.position.set(0,200,0);
			light.castShadow = true;
			light.target = ground;
			scene.add(light);

			var ambient = new THREE.AmbientLight(0xffffff, 0.25);
			scene.add(ambient);


			var frame = 0;

			const FRAMES_PER_MOVE = 400;
			const FRAMES_ROTATE = 100;
			var alpha = 0;
			var update = true;
			var toLerpPos = null;
			var toLerpDir = null;
			var tempLook = new THREE.Vector3();
			var positions = [
				[82, CAM_HEIGHT, -90], [42, CAM_HEIGHT, -111],
				[-126, CAM_HEIGHT, -42], [-181, CAM_HEIGHT, -30],
				[-48, CAM_HEIGHT, 26], [-101, CAM_HEIGHT, 47],
				[21, CAM_HEIGHT, 107], [66, CAM_HEIGHT / 2, 159],
			];
			positions = positions.map(x => new THREE.Vector3(...x));
			function drawFrame()
			{
				if (positions.length > 0 || alpha > 0) {
					if (alpha < 0.1 && update) {
						cameraPos = camera.position.clone();
						toLerpPos = positions.shift();
						toLerpDir = positions.shift();
						update = false;
					}
					else if (alpha >= 1.2 && !update) {
						update = true;
					}

					if (alpha >= 1.0) {
						camera.lookAt(tempLook.lerpVectors(toLerpDir, positions[1], (alpha - 1.0) * 5));
					}
					else {
						camera.lookAt(toLerpDir);
						camera.position.lerpVectors(cameraPos, toLerpPos, alpha);
					}
				}
				alpha = (frame % (FRAMES_PER_MOVE + FRAMES_ROTATE)) / FRAMES_PER_MOVE;

				renderer.render( scene, camera );
				frame++;
			}
		</script>
	</body>
</html>