-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.html
334 lines (283 loc) · 8.45 KB
/
index.html
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
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My-Threejs</title>
<link rel="stylesheet" type="text/css" href="./demo/css/style.css" />
<script type="text/javascript" src="libs/three.js"></script>
<script type="text/javascript" src="libs/OrbitControls.js"></script>
<script type="text/javascript" src="libs/Detector.js"></script>
<script type="text/javascript" src="libs/stats.js"></script>
<script type="text/javascript" src="libs/tween.js"></script>
<script type="text/javascript" src="libs/dat.gui.js"></script>
<style>
body {
background: #9fdaff
}
</style>
</head>
<body>
<div id="zk-world" class="zk-world"></div>
<script type="text/javascript">
if (!Detector.webgl) Detector.addGetWebGLMessage();
//COLORS
var Colors = {
red: 0xf25346,
white: 0xd8d0d1,
brown: 0x59332e,
pink: 0xF5986E,
brownDark: 0x23190f,
blue: 0x006585
};
// THREEJS RELATED VARIABLES
var scene,
camera,
renderer,
container;
//SCREEN & MOUSE VARIABLES
var HEIGHT,
WIDTH,
mousePos = {
x: 0,
y: 0
};
//INIT THREE JS, SCREEN AND MOUSE EVENTS
function createScene() {
HEIGHT = window.innerHeight;
WIDTH = window.innerWidth;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(60, WIDTH / HEIGHT, 1, 10000);
scene.fog = new THREE.Fog(0x9de4ff, 100, 950);
camera.position.x = 0;
camera.position.z = 300;
camera.position.y = 100;
// camera.position.set( 10, 400, 200 );
// camera.lookAt(scene.position);
renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true
});
renderer.setSize(WIDTH, HEIGHT);
renderer.shadowMap.enabled = true;
container = document.getElementById('zk-world');
container.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
}
// HANDLE SCREEN EVENTS
function onWindowResize() {
HEIGHT = window.innerHeight;
WIDTH = window.innerWidth;
renderer.setSize(WIDTH, HEIGHT);
camera.aspect = WIDTH / HEIGHT;
camera.updateProjectionMatrix();
}
// LIGHTS
var ambientLight,
hemisphereLight,
shadowLight;
function createLights() {
hemisphereLight = new THREE.HemisphereLight(0xaaaaaa, 0x000000, .9)
shadowLight = new THREE.DirectionalLight(0xffffff, .9);
shadowLight.position.set(150, 350, 350);
shadowLight.castShadow = true;
shadowLight.shadow.camera.left = -400;
shadowLight.shadow.camera.right = 400;
shadowLight.shadow.camera.top = 400;
shadowLight.shadow.camera.bottom = -400;
shadowLight.shadow.camera.near = 1;
shadowLight.shadow.camera.far = 1000;
shadowLight.shadow.mapSize.width = 2048;
shadowLight.shadow.mapSize.height = 2048;
scene.add(hemisphereLight);
scene.add(shadowLight);
}
var AirPlane = function() {
this.mesh = new THREE.Object3D();
this.mesh.name = "airPlane";
// Create the cabin
var geomCockpit = new THREE.BoxGeometry(60, 50, 50, 1, 1, 1);
var matCockpit = new THREE.MeshPhongMaterial({
color: Colors.red,
// flatShading: THREE.FlatflatShading
});
var cockpit = new THREE.Mesh(geomCockpit, matCockpit);
cockpit.castShadow = true;
cockpit.receiveShadow = true;
this.mesh.add(cockpit);
// Create Engine
var geomEngine = new THREE.BoxGeometry(20, 50, 50, 1, 1, 1);
var matEngine = new THREE.MeshPhongMaterial({
color: Colors.white,
// flatShading: THREE.FlatflatShading
});
var engine = new THREE.Mesh(geomEngine, matEngine);
engine.position.x = 40;
engine.castShadow = true;
engine.receiveShadow = true;
this.mesh.add(engine);
// Create Tailplane
var geomTailPlane = new THREE.BoxGeometry(15, 20, 5, 1, 1, 1);
var matTailPlane = new THREE.MeshPhongMaterial({
color: Colors.red,
// flatShading: THREE.FlatflatShading
});
var tailPlane = new THREE.Mesh(geomTailPlane, matTailPlane);
tailPlane.position.set(-35, 25, 0);
tailPlane.castShadow = true;
tailPlane.receiveShadow = true;
this.mesh.add(tailPlane);
// Create Wing
var geomSideWing = new THREE.BoxGeometry(40, 8, 150, 1, 1, 1);
var matSideWing = new THREE.MeshPhongMaterial({
color: Colors.red,
// flatShading: THREE.FlatflatShading
});
var sideWing = new THREE.Mesh(geomSideWing, matSideWing);
sideWing.position.set(0, 0, 0);
sideWing.castShadow = true;
sideWing.receiveShadow = true;
this.mesh.add(sideWing);
// Propeller
var geomPropeller = new THREE.BoxGeometry(20, 10, 10, 1, 1, 1);
var matPropeller = new THREE.MeshPhongMaterial({
color: Colors.brown,
// flatShading: THREE.FlatflatShading
});
this.propeller = new THREE.Mesh(geomPropeller, matPropeller);
this.propeller.castShadow = true;
this.propeller.receiveShadow = true;
// Blades
var geomBlade = new THREE.BoxGeometry(1, 100, 20, 1, 1, 1);
var matBlade = new THREE.MeshPhongMaterial({
color: Colors.brownDark,
// flatShading: THREE.FlatflatShading
});
var blade = new THREE.Mesh(geomBlade, matBlade);
blade.position.set(8, 0, 0);
blade.castShadow = true;
blade.receiveShadow = true;
this.propeller.add(blade);
this.propeller.position.set(50, 0, 0);
this.mesh.add(this.propeller);
};
Sky = function() {
this.mesh = new THREE.Object3D();
this.nClouds = 20;
this.clouds = [];
var stepAngle = Math.PI * 2 / this.nClouds;
for (var i = 0; i < this.nClouds; i++) {
var c = new Cloud();
this.clouds.push(c);
var a = stepAngle * i;
var h = 750 + Math.random() * 200;
c.mesh.position.y = Math.sin(a) * h;
c.mesh.position.x = Math.cos(a) * h;
c.mesh.position.z = -400 - Math.random() * 400;
c.mesh.rotation.z = a + Math.PI / 2;
var s = 1 + Math.random() * 2;
c.mesh.scale.set(s, s, s);
this.mesh.add(c.mesh);
}
}
Sea = function() {
var geom = new THREE.CylinderGeometry(600, 600, 800, 40, 10);
geom.applyMatrix(new THREE.Matrix4().makeRotationX(-Math.PI / 2));
var mat = new THREE.MeshPhongMaterial({
color: Colors.blue,
transparent: true,
opacity: .6,
// flatShading: THREE.FlatflatShading
});
this.mesh = new THREE.Mesh(geom, mat);
this.mesh.receiveShadow = true;
}
Cloud = function() {
this.mesh = new THREE.Object3D();
this.mesh.name = "cloud";
var geom = new THREE.CubeGeometry(20, 20, 20);
var mat = new THREE.MeshPhongMaterial({
color: Colors.white
});
var nBlocs = 3 + Math.floor(Math.random() * 3);
for (var i = 0; i < nBlocs; i++) {
var m = new THREE.Mesh(geom.clone(), mat);
m.position.x = i * 15;
m.position.y = Math.random() * 10;
m.position.z = Math.random() * 10;
m.rotation.z = Math.random() * Math.PI * 2;
m.rotation.y = Math.random() * Math.PI * 2;
var s = .1 + Math.random() * .9;
m.scale.set(s, s, s);
m.castShadow = true;
m.receiveShadow = true;
this.mesh.add(m);
}
}
// 3D Models
var sea;
var airplane;
function createPlane() {
airplane = new AirPlane();
airplane.mesh.scale.set(.25, .25, .25);
airplane.mesh.position.y = 100;
scene.add(airplane.mesh);
}
function createSea() {
sea = new Sea();
sea.mesh.position.y = -600;
scene.add(sea.mesh);
}
function createSky() {
sky = new Sky();
sky.mesh.position.y = -600;
scene.add(sky.mesh);
}
function loop() {
updatePlane();
sea.mesh.rotation.z += .005;
sky.mesh.rotation.z += .01;
renderer.render(scene, camera);
requestAnimationFrame(loop);
}
function updatePlane() {
var targetY = normalize(mousePos.y, -.75, .75, 25, 175);
var targetX = normalize(mousePos.x, -.75, .75, -100, 100);
airplane.mesh.position.y = targetY;
airplane.mesh.position.x = targetX;
airplane.propeller.rotation.x += 0.3;
}
function normalize(v, vmin, vmax, tmin, tmax) {
var nv = Math.max(Math.min(v, vmax), vmin);
var dv = vmax - vmin;
var pc = (nv - vmin) / dv;
var dt = tmax - tmin;
var tv = tmin + (pc * dt);
return tv;
}
function init(event) {
document.addEventListener('mousemove', handleMouseMove, false);
createScene();
createLights();
createPlane();
createSea();
createSky();
loop();
}
// HANDLE MOUSE EVENTS
var mousePos = {
x: 0,
y: 0
};
function handleMouseMove(event) {
var tx = -1 + (event.clientX / WIDTH) * 2;
var ty = 1 - (event.clientY / HEIGHT) * 2;
mousePos = {
x: tx,
y: ty
};
}
window.addEventListener('load', init, false);
</script>
</body>
</html>