-
Notifications
You must be signed in to change notification settings - Fork 0
/
Praktikum.html
377 lines (297 loc) · 12.4 KB
/
Praktikum.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
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
<!DOCTYPE html>
<html>
<head>
<title>Praktikumsaufgabe mit WebGL</title>
<link href="Praktikum.css" rel="stylesheet" type="text/css">
</head>
<body>
<script type="text/javascript" src="three.js"></script>
<script type="text/javascript" src="OrbitControls.js"></script>
<script type="text/javascript" src="dat.gui.min.js"></script>
<script type="text/javascript" src="Stats.js"></script>
<script>
/****************************************************************
* Geometric Constants *
* ***************************************************************/
var HEAD_RADIUS = 20;
var HAT_HEIGHT = HEAD_RADIUS * 1.5;
var HAT_BOTTOM_RADIUS = HEAD_RADIUS * 2.5;
var HAT_RADIUS = HEAD_RADIUS*0.75;
var BODY_RADIUS = HEAD_RADIUS + HEAD_RADIUS / 4;
var BUTTON_RADIUS = HEAD_RADIUS / 4;
var EYE_RADIUS = BUTTON_RADIUS / 2;
var MOUTH_RADIUS = HEAD_RADIUS / 12;
var NOSE_RADIUS = BUTTON_RADIUS*0.7;
var NOSE_LENGTH = HEAD_RADIUS*0.8;
var HEAD_TRANSLATION = BODY_RADIUS + HEAD_RADIUS - HEAD_RADIUS / 4;
var container;
var stats;
var controls;
var gui;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer({antialias:true});
var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors} );
var wireframematerial = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors, wireframe:true} );
var mesh;
var wireframeMesh;
/****************************************************************
* Color Geometry which will be merged *
* with each Geometry to colorize them *
* **************************************************************/
var whiteGeometry = new THREE.CircleGeometry(0, 0 , 0 , 0);
var blackGeometry = new THREE.CircleGeometry(0, 0 , 0 , 0);
var orangeGeometry = new THREE.CircleGeometry(0, 0 , 0 , 0);
var colorWhite = new THREE.Face3(0, 1, 1);
var colorBlack = new THREE.Face3(0, 0, 0);
var colorOrange = new THREE.Face3(1, 0, 0);
function initGeometry()
{
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xAAAAAA, 1);
renderer.setFaceCulling(THREE.CullFaceBack);
createSnowman();
camera.position.set(10, 10, 150);
}
/****************************************************************
* Build Snowman *
* **************************************************************/
function createSnowman() {
createBody();
createHead();
createMeshes();
}
/****************************************************************
* Build Meshes *
* **************************************************************/
function createMeshes() {
var whiteMesh = new THREE.Mesh(whiteGeometry, new THREE.MeshBasicMaterial( { color: 0xFFFFFF } ));
var blackMesh = new THREE.Mesh(blackGeometry, new THREE.MeshBasicMaterial( { color: 0x000000 } ));
var orangeMesh = new THREE.Mesh(orangeGeometry, new THREE.MeshBasicMaterial( { color: 0xff0000 } ));
scene.add(whiteMesh);
scene.add(blackMesh);
scene.add(orangeMesh);
}
/****************************************************************
* Build Snowman *
* **************************************************************/
function createBody() {
var snowBodyGeometry = new THREE.SphereGeometry(BODY_RADIUS, 32, 32);
THREE.GeometryUtils.merge(whiteGeometry, snowBodyGeometry);
createButtons();
}
/****************************************************************
* Build Snowman Buttons *
* **************************************************************/
function createButtons() {
var button1 = new THREE.SphereGeometry(BUTTON_RADIUS, 32, 32);
var button2 = new THREE.SphereGeometry(BUTTON_RADIUS, 32, 32);
var button3 = new THREE.SphereGeometry(BUTTON_RADIUS, 32, 32);
button1.applyMatrix(new THREE.Matrix4().scale(new THREE.Vector3(0.5,0.5,0.5)));
button2.applyMatrix(new THREE.Matrix4().scale(new THREE.Vector3(0.5,0.5,0.5)));
button3.applyMatrix(new THREE.Matrix4().scale(new THREE.Vector3(0.5,0.5,0.5)));
button1.applyMatrix(new THREE.Matrix4().makeTranslation(0, -BODY_RADIUS+BUTTON_RADIUS*2, BODY_RADIUS*0.75));
button2.applyMatrix(new THREE.Matrix4().makeTranslation(0, -BODY_RADIUS+BUTTON_RADIUS*2 - BUTTON_RADIUS*1.5, BODY_RADIUS*0.75 - 1.8*BUTTON_RADIUS));
button3.applyMatrix(new THREE.Matrix4().makeTranslation(0, -BODY_RADIUS+BUTTON_RADIUS*2 - BUTTON_RADIUS*1.8, BODY_RADIUS*0.75 - 4*BUTTON_RADIUS));
THREE.GeometryUtils.merge(blackGeometry, button1);
THREE.GeometryUtils.merge(blackGeometry, button2);
THREE.GeometryUtils.merge(blackGeometry, button3);
}
/****************************************************************
* Build Snowman Head *
* **************************************************************/
function createHead() {
var snowHeadGeometry = new THREE.SphereGeometry(HEAD_RADIUS, 32, 32);
snowHeadGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(0.0, 0.0, HEAD_TRANSLATION));
THREE.GeometryUtils.merge(whiteGeometry, snowHeadGeometry);
createEyes();
createNose();
createMouth();
createHat();
}
/****************************************************************
* Build Snowman Eyes *
* **************************************************************/
function createEyes() {
var leftEyeGeometry = new THREE.SphereGeometry(EYE_RADIUS, 32, 32);
var rightEyeGeometry = new THREE.SphereGeometry(EYE_RADIUS, 32, 32);
leftEyeGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(HEAD_RADIUS/2 -2.5, -HEAD_RADIUS+EYE_RADIUS, BODY_RADIUS + HEAD_RADIUS));
rightEyeGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(-HEAD_RADIUS/2 + 2.5, -HEAD_RADIUS+EYE_RADIUS, BODY_RADIUS + HEAD_RADIUS));
THREE.GeometryUtils.merge(blackGeometry, leftEyeGeometry);
THREE.GeometryUtils.merge(blackGeometry, rightEyeGeometry);
}
/****************************************************************
* Build Snowman Nose *
* **************************************************************/
function createNose() {
var noseGeometry = new THREE.CylinderGeometry(NOSE_RADIUS, 0, NOSE_LENGTH, 32, 32, false);
noseGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, -HEAD_RADIUS- NOSE_RADIUS*1.35, HEAD_TRANSLATION));
THREE.GeometryUtils.merge(orangeGeometry, noseGeometry);
}
/****************************************************************
* Build Snowman Mouth *
* **************************************************************/
function createMouth() {
var stone1 = new THREE.SphereGeometry(MOUTH_RADIUS, 32, 32);
var stone2 = new THREE.SphereGeometry(MOUTH_RADIUS, 32, 32);
var stone3 = new THREE.SphereGeometry(MOUTH_RADIUS, 32, 32);
var stone4 = new THREE.SphereGeometry(MOUTH_RADIUS, 32, 32);
var stone5 = new THREE.SphereGeometry(MOUTH_RADIUS, 32, 32);
var dz = HEAD_TRANSLATION;
var stone1Dz = (HEAD_RADIUS - 2*HEAD_RADIUS*0.31);
var stone2Dz = (HEAD_RADIUS - 2*HEAD_RADIUS*0.255);
var stone3Dz = (HEAD_RADIUS - 2*HEAD_RADIUS*0.24);
var stone4Dz = (HEAD_RADIUS - 2*HEAD_RADIUS*0.255);
var stone5Dz = (HEAD_RADIUS - 2*HEAD_RADIUS*0.31);
var radiusSquared = Math.pow(HEAD_RADIUS, 2);
var stone1Dy = Math.sqrt(radiusSquared - Math.pow(stone1Dz, 2)) - MOUTH_RADIUS - MOUTH_RADIUS*0.4;
var stone2Dy = Math.sqrt(radiusSquared - Math.pow(stone2Dz, 2)) - MOUTH_RADIUS + MOUTH_RADIUS*0.4;
var stone3Dy = Math.sqrt(radiusSquared - Math.pow(stone3Dz, 2)) - MOUTH_RADIUS*0.5;
var stone4Dy = Math.sqrt(radiusSquared - Math.pow(stone4Dz, 2)) - MOUTH_RADIUS + MOUTH_RADIUS*0.4;
var stone5Dy = Math.sqrt(radiusSquared - Math.pow(stone5Dz, 2)) - MOUTH_RADIUS - MOUTH_RADIUS*0.4;
var stone1Dx = -HEAD_RADIUS*0.4;
var stone2Dx = -HEAD_RADIUS*0.2;
var stone3Dx = 0;
var stone4Dx = HEAD_RADIUS*0.2;
var stone5Dx = HEAD_RADIUS*0.4;
stone1.applyMatrix(new THREE.Matrix4().makeTranslation(stone1Dx, -stone1Dy, dz - stone1Dz));
stone2.applyMatrix(new THREE.Matrix4().makeTranslation(stone2Dx, -stone2Dy, dz - stone2Dz));
stone3.applyMatrix(new THREE.Matrix4().makeTranslation(stone3Dx, -stone3Dy, dz - stone3Dz));
stone4.applyMatrix(new THREE.Matrix4().makeTranslation(stone4Dx, -stone4Dy, dz - stone4Dz));
stone5.applyMatrix(new THREE.Matrix4().makeTranslation(stone5Dx, -stone5Dy, dz - stone5Dz));
THREE.GeometryUtils.merge(blackGeometry, stone1);
THREE.GeometryUtils.merge(blackGeometry, stone2);
THREE.GeometryUtils.merge(blackGeometry, stone3);
THREE.GeometryUtils.merge(blackGeometry, stone4);
THREE.GeometryUtils.merge(blackGeometry, stone5);
}
/****************************************************************
* Build Snowman Hat *
* **************************************************************/
function createHat() {
var cylinderGeometry = new THREE.CylinderGeometry(HAT_RADIUS, HAT_RADIUS, HAT_HEIGHT, 50, 50, false);
var lowerDisc = new THREE.CircleGeometry(HAT_RADIUS*2, 150, HAT_BOTTOM_RADIUS, HAT_BOTTOM_RADIUS);
var upperDisc = new THREE.CircleGeometry(HAT_RADIUS*2, 150, HAT_BOTTOM_RADIUS, HAT_BOTTOM_RADIUS);
cylinderGeometry.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI/2));
cylinderGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, 0, BODY_RADIUS + 2*HEAD_RADIUS + HEAD_RADIUS/4));
upperDisc.applyMatrix(new THREE.Matrix4().makeTranslation(0.0, 0.0, HEAD_RADIUS*2.75));
lowerDisc.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI));
lowerDisc.applyMatrix(new THREE.Matrix4().makeTranslation(0.0, 0.0, HEAD_RADIUS*2.75));
// Merge geometry
THREE.GeometryUtils.merge(blackGeometry, cylinderGeometry);
THREE.GeometryUtils.merge(blackGeometry, lowerDisc);
THREE.GeometryUtils.merge(blackGeometry, upperDisc);
}
var frameRate = false;
var wireframed = true;
var cullModes = { GL_FRONT:0, GL_BACK:1, GL_FRONT_AND_BACK:2 };
var selectedCullMode = 1;
function initialize()
{
container = document.createElement('div');
document.body.appendChild(container);
container.appendChild(renderer.domElement);
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
stats.domElement.style.visibility = "hidden";
document.body.appendChild(stats.domElement);
initGeometry();
setupControls();
}
var help = function()
{
window.open('Help.html', "_blank");
};
var reset = function()
{
window.location.reload();
};
function updateGui()
{
if (gui != null)
{
gui.destroy();
}
gui = new dat.GUI({width: 520});
var cullmodeChooser = gui.add(this, 'selectedCullMode', cullModes).name("Culling Modus");
var wireframedCheckBox = gui.add(this, 'wireframed').name("Current Backside wireframed");
var frameRateCheckBox = gui.add(this, 'frameRate').name("Frames per Second");
var helpButton = gui.add(this, 'help').name("Hilfe");
var resetButton = gui.add(this, 'reset').name("Reset");
wireframedCheckBox.onFinishChange(function (value)
{
if(value)
scene.add(wireframeMesh);
else
scene.remove(wireframeMesh);
});
cullmodeChooser.onFinishChange(function (value)
{
switch(value)
{
case "0":
{
renderer.setFaceCulling(THREE.CullFaceFront);
break;
}
case "1":
{
renderer.setFaceCulling(THREE.CullFaceBack);
break;
}
case "2":
{
renderer.setFaceCulling(THREE.CullFaceFrontBack);
break;
}
case "3":
{
renderer.setFaceCulling(THREE.CullFaceNone);
break;
}
default:
{
alert("Fehler - cullmodeChooser.onFinishChange(...): invalid value (" + value + ")");
break;
}
}
});
frameRateCheckBox.onChange(function (value)
{
if (frameRate)
{
stats.domElement.style.visibility = "visible";
}
else
{
stats.domElement.style.visibility = "hidden";
}
});
}
function render()
{
requestAnimationFrame(render);
stats.update();
renderer.render(scene, camera);
}
function setupControls()
{
controls = new THREE.OrbitControls(camera, renderer.domElement);
}
initialize();
render();
window.onload = function()
{
updateGui();
};
function resizeWindow()
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', resizeWindow, false);
</script>
</body>
</html>