forked from Oli97/jjko-flight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcockpit3d_kurve.html
250 lines (228 loc) · 8.21 KB
/
cockpit3d_kurve.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
<html>
<head>
<title>Cockpit — Studienprojekt</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<body>
<div class="container">
<div id="container"><br /><br /><br /><br /><br />Generating world...</div>
<canvas id="text"></canvas>
</div>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
position: relative;
}
#text {
position: absolute;
left: 0px;
top: 0px;
width: 100vw;
height: 100vh;
margin: 0px;
display: block;
z-index: 10;
}
#container {
width: 100vw;
height: 100vh;
margin: 0px;
display: block;
}
</style>
<script type="text/javascript" src="glMatrix-0.9.5.min.js"></script>
<script type="text/javascript" src="webgl-utils.js"></script>
<script type="text/javascript" src="three.js-master/build/three.js"></script>
<script src="three.js-master/examples/js/ImprovedNoise.js"></script>
<script src="three.js-master/examples/js/Detector.js"></script>
<script src="three.js-master/examples/js/libs/stats.min.js"></script>
<!--script src="three.js-master/examples/js/controls/FirstPersonControls.js"></script-->
<script src="autopilot_kurve.js"></script>
<script language="javascript" type="text/javascript" src="2d.js"></script>
<script type="text/javascript">
if ( ! Detector.webgl ) {
Detector.addGetWebGLMessage();
document.getElementById( 'container' ).innerHTML = "";
}
var container, stats;
var camera, controls, scene, renderer;
var mesh, texture, data;
var altx = 0, altz = 0, vertices;
var worldWidth = 256, worldDepth = 256,
worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2;
var clock = new THREE.Clock();
var reload = false;
var weite = 125000, breite = 125000;
init();
animate();
function init() {
container = document.getElementById( "container" );
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 100000 );/*Kamera erstellen mit perpektivischen Eigenschaften*/
scene = new THREE.Scene();
var path = "three.js-master/examples/textures/cube/skybox/";
var urls = [
path + "px.jpg", path + "nx.jpg",
path + "py.jpg", path + "ny.jpg",
path + "pz.jpg", path + "nz.jpg"
];
textureCube = new THREE.CubeTextureLoader().load( urls );
scene.background = textureCube;
controls = new THREE.FirstPersonControls( camera );/*Steuerung für die Kamera erstellen*/
controls.movementSpeed = 0;/*Bewegungsgeschwindigkeit der Kamera*/
controls.lookSpeed = 0.1;/*Drehgeschwindigkeit*/
data = generateHeight( worldWidth, worldDepth );/*Erstellt natürliche Höhendaten für die 3D-Welt*/
camera.position.y = 8000;/*Startposition der Kamera*/
var geometry = new THREE.PlaneBufferGeometry( weite, breite, worldWidth - 1, worldDepth - 1 ); /*Erste zwei Variablen bestimmen größe der 3D-Welt und die anderen Zwei bestimmen größe der einzelnen Segmente*/
geometry.rotateX( - Math.PI / 2 );/*Rotation um die x-Achse*/
vertices = geometry.attributes.position.array;/*Positionskoordinaten werden erstellt*/
for ( var i = 0, j = 0, l = vertices.length; i < l; i ++, j += 3 ) {
vertices[ j + 1 ] = data[ i ] * 100;/*y Koordinaten werden übernommen,Faktor beeinflusst Höhe der Hügel*/
}
texture = new THREE.CanvasTexture( generateTexture( data, worldWidth, worldDepth ) );/*Texturen erstellen*/
texture.wrapS = THREE.ClampToEdgeWrapping;/*Kanten "festklammern"*/
texture.wrapT = THREE.ClampToEdgeWrapping;
mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );/*Vermischen von Geometrie und Textur*/
scene.add( mesh );
renderer = new THREE.WebGLRenderer();/*zeichnet die 3D Welt*/
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.innerHTML = "";
container.appendChild( renderer.domElement );/*canvas wodrauf der renderer zeichnet*/
stats = new Stats();
//container.appendChild( stats.dom );/*Gibt daten oben links an*/
//
window.addEventListener( 'resize', onWindowResize, false );/*fenster anpassen*/
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
controls.handleResize();
}
function generateHeight( width, height ) {
var size = width * height, data = new Uint8Array( size ),
perlin = new ImprovedNoise(), quality = 1, z = Math.random() * 100;
for ( var j = 0; j < 4; j ++ ) {
for ( var i = 0; i < size; i ++ ) {
var x = i % width, y = ~~ ( i / width );//%=modulo,~~ eliminiert Nachkommastellen
data[ i ] += Math.abs( perlin.noise( x / quality, y / quality, z ) * quality * 1.75 );
}
quality *= 5;
}
return data;
}
function generateHeight2( width, height ) {
var size = width * height, data = new Uint8Array( size );
for ( var i = 0; i < size; i ++ ) {
var j = i % width;
if((180<j && j<200&&i>180*width&&i<200*width)||(8<j && j<28&&i>180*width&&i<200*width)){
data[i] = 20;
}else{
if((130<j && j<140&&i>110*width&&i<120*width)||(150<j && j<170&&i>135*width&&i<140*width)){
data[i] = 30;
}else{
if((145<j && j<155&&i>105*width&&i<115*width)||(80<j && j<90&&i>140*width&&i<150*width)){
data[i] = 100;
}else{
if((0<j && j<10&&i>10*width&&i<20*width)||(192<j && j<202&&i>10*width&&i<20*width)){
data[i] = 200;
}else{
data[ i ] =0;
}
}
}
}
}
return data;
}
function generateTexture( data, width, height ) {
var canvas, canvasScaled, context, image, imageData,
level, diff, vector3, sun, shade;
vector3 = new THREE.Vector3( 0, 0, 0 );
sun = new THREE.Vector3( 1, 1, 1 );
sun.normalize();
canvas = document.createElement( 'canvas' );
canvas.width = width;
canvas.height = height;
context = canvas.getContext( '2d' );
context.fillStyle = 'grey';
context.fillRect( 0, 0, width, height );
image = context.getImageData( 0, 0, canvas.width, canvas.height );
imageData = image.data;
for ( var i = 0, j = 0, l = imageData.length; i < l; i += 4, j ++ ) {
vector3.x = data[ j - 2 ] - data[ j + 2 ];
vector3.y = 2;
vector3.z = data[ j - width * 2 ] - data[ j + width * 2 ];
vector3.normalize();
shade = vector3.dot( sun );
imageData[ i ] = ( 96 + shade * 128 ) * ( 0.5 + data[ j ] * 0.007 );
imageData[ i + 1 ] = ( 32 + shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
imageData[ i + 2 ] = ( shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
}
context.putImageData( image, 0, 0 );
// Scaled 4x
canvasScaled = document.createElement( 'canvas' );
canvasScaled.width = width * 4;
canvasScaled.height = height * 4;
context = canvasScaled.getContext( '2d' );
context.scale( 4, 4 );
context.drawImage( canvas, 0, 0 );
image = context.getImageData( 0, 0, canvasScaled.width, canvasScaled.height );
imageData = image.data;
for ( var i = 0, l = imageData.length; i < l; i += 4 ) {
var v = ~~ ( Math.random() * 5 );
imageData[ i ] += v;
imageData[ i + 1 ] += v;
imageData[ i + 2 ] += v;
}
context.putImageData( image, 0, 0 );
return canvasScaled;
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
reset();
absturz();
}
function render() {
controls.update( clock.getDelta() );
camera.rotation.x=controls.b()* Math.PI / 180;
camera.rotation.y=(-90-controls.a())* Math.PI / 180;
camera.rotation.z=-controls.c()* Math.PI / 180;
renderer.render( scene, camera );
}
function reset() {
if(Math.abs(camera.position.x-altx)>weite/2||Math.abs(camera.position.z-altz)>breite/2){/*reset*/
scene.translateX(camera.position.x-altx);
scene.translateZ(camera.position.z-altz);
altx = camera.position.x;
altz = camera.position.z;
}
}
function absturz() {
for ( var i = 0, l = vertices.length; i < l; i += 3 ) {
if(!reload&&Math.abs(camera.position.x-altx-vertices[i])<1000&&Math.abs(camera.position.z-altz-vertices[i+2])<1000&&vertices[i+1]>camera.position.y){
if (confirm('Sie sind abgestuerzt!')) {
location.reload();
reload = true;
}
}
}
if(!reload&&isNaN(camera.position.x)){
if (confirm('Systemfehler!')) {
location.reload();
reload = true;
}
}
}
</script>
</head>
</body>
</html>