Update index.html
Browse files- index.html +176 -87
index.html
CHANGED
@@ -3,11 +3,12 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>Three.js Flight Simulator</title>
|
7 |
<style>
|
8 |
-
body { margin: 0; background-color: #
|
9 |
canvas { display: block; }
|
10 |
-
#controls { position: absolute; top: 10px; left: 10px; background: rgba(255,255,255,0.7); padding: 10px; font-family: monospace; }
|
|
|
11 |
</style>
|
12 |
</head>
|
13 |
<body>
|
@@ -16,86 +17,144 @@
|
|
16 |
<b>Flight Simulator Controls:</b><br>
|
17 |
W / β : Pitch Up (Climb)<br>
|
18 |
S / β : Pitch Down (Dive)<br>
|
19 |
-
A / β : Yaw Left (Turn Left)<br>
|
20 |
-
D / β : Yaw Right (Turn Right)<br>
|
21 |
-
Q / E : Roll Left
|
22 |
-
ββ Speed up (
|
23 |
-
ββ Slow down (
|
24 |
-
Mouse Look (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
</div>
|
26 |
|
27 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
28 |
<script>
|
29 |
-
//
|
30 |
let scene = new THREE.Scene();
|
31 |
-
scene.background = new THREE.Color(0x87CEEB); // Sky blue
|
32 |
-
|
33 |
-
// Camera = our eyes in the plane
|
34 |
-
let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
35 |
|
36 |
-
//
|
37 |
-
let
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
//
|
42 |
-
let groundGeom = new THREE.PlaneGeometry(
|
43 |
-
let
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
let ground = new THREE.Mesh(groundGeom, groundMat);
|
45 |
-
ground.rotation.x = -Math.PI / 2;
|
46 |
-
ground.
|
47 |
scene.add(ground);
|
48 |
|
49 |
-
//
|
50 |
-
let planeGeom = new THREE.
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
-
|
58 |
-
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
]);
|
64 |
-
let
|
65 |
-
|
66 |
-
let
|
67 |
-
let
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
scene.add(ambientLight);
|
76 |
let dirLight = new THREE.DirectionalLight(0xFFFFFF, 0.8);
|
77 |
-
dirLight.position.set(
|
|
|
|
|
|
|
|
|
|
|
78 |
scene.add(dirLight);
|
79 |
|
80 |
-
//
|
81 |
-
let
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
let maxSpeed = 0.2;
|
86 |
-
let minSpeed = 0.01;
|
87 |
|
88 |
-
//
|
89 |
-
let
|
90 |
-
let
|
91 |
-
let
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
//
|
94 |
let keys = {
|
95 |
w: false, s: false, a: false, d: false, q: false, e: false
|
96 |
};
|
97 |
-
|
98 |
-
// Handle key presses
|
99 |
document.addEventListener('keydown', (e) => {
|
100 |
switch (e.key) {
|
101 |
case 'w': case 'ArrowUp': keys.w = true; break;
|
@@ -117,7 +176,10 @@ document.addEventListener('keyup', (e) => {
|
|
117 |
}
|
118 |
});
|
119 |
|
120 |
-
//
|
|
|
|
|
|
|
121 |
document.addEventListener('mousedown', (e) => {
|
122 |
mouseDown = true;
|
123 |
lastMouseX = e.clientX;
|
@@ -130,43 +192,70 @@ document.addEventListener('mousemove', (e) => {
|
|
130 |
let dy = e.clientY - lastMouseY;
|
131 |
yaw -= dx * sensitivity;
|
132 |
pitch -= dy * sensitivity;
|
133 |
-
pitch = Math.max(-Math.PI/2, Math.min(Math.PI/2, pitch));
|
134 |
lastMouseX = e.clientX;
|
135 |
lastMouseY = e.clientY;
|
136 |
}
|
137 |
});
|
138 |
|
139 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
function animate() {
|
141 |
requestAnimationFrame(animate);
|
142 |
|
143 |
-
//
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
if (
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
// Limit angles
|
156 |
-
pitch = Math.max(-Math.PI/
|
157 |
roll = Math.max(-Math.PI/4, Math.min(Math.PI/4, roll));
|
158 |
|
159 |
-
//
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
plane.rotation.x = pitch;
|
165 |
-
plane.rotation.y = yaw;
|
166 |
|
167 |
-
//
|
168 |
-
|
169 |
|
|
|
170 |
renderer.render(scene, camera);
|
171 |
}
|
172 |
animate();
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Enhanced Three.js Flight Simulator</title>
|
7 |
<style>
|
8 |
+
body { margin: 0; background-color: #000; overflow: hidden; }
|
9 |
canvas { display: block; }
|
10 |
+
#controls { position: absolute; top: 10px; left: 10px; background: rgba(255,255,255,0.7); padding: 10px; font-family: monospace; font-size: 14px; }
|
11 |
+
#instruments { position: absolute; top: 10px; right: 10px; background: rgba(0,0,0,0.7); color: #0F0; padding: 10px; font-family: monospace; font-size: 14px; }
|
12 |
</style>
|
13 |
</head>
|
14 |
<body>
|
|
|
17 |
<b>Flight Simulator Controls:</b><br>
|
18 |
W / β : Pitch Up (Climb)<br>
|
19 |
S / β : Pitch Down (Dive)<br>
|
20 |
+
A / β : Yaw Left (Turn Left, but use roll for real turns)<br>
|
21 |
+
D / β : Yaw Right (Turn Right, but use roll for real turns)<br>
|
22 |
+
Q / E : Roll Left/Right (Bank for turns)<br>
|
23 |
+
ββ Speed up (gradually)<br>
|
24 |
+
ββ Slow down (gradually)<br>
|
25 |
+
Mouse Look (drag to change view direction)
|
26 |
+
</div>
|
27 |
+
|
28 |
+
<div id="instruments">
|
29 |
+
<b>Flight Instruments:</b><br>
|
30 |
+
Alt: <span id="altimeter">0 ft</span><br>
|
31 |
+
Airspeed: <span id="airspeed">0 kts</span><br>
|
32 |
+
Heading: <span id="heading">0Β°</span>
|
33 |
</div>
|
34 |
|
35 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
36 |
<script>
|
37 |
+
// Scene
|
38 |
let scene = new THREE.Scene();
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
// Skybox (gradient sky)
|
41 |
+
let skyGeom = new THREE.SphereGeometry(500, 32, 32);
|
42 |
+
let skyMat = new THREE.ShaderMaterial({
|
43 |
+
vertexShader: `
|
44 |
+
varying vec3 vWorldPosition;
|
45 |
+
void main() {
|
46 |
+
vec4 worldPosition = modelMatrix * vec4(position, 1.0);
|
47 |
+
vWorldPosition = worldPosition.xyz;
|
48 |
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
49 |
+
}
|
50 |
+
`,
|
51 |
+
fragmentShader: `
|
52 |
+
varying vec3 vWorldPosition;
|
53 |
+
void main() {
|
54 |
+
float heightFactor = (vWorldPosition.y + 250.0) / 500.0;
|
55 |
+
heightFactor = clamp(heightFactor, 0.0, 1.0);
|
56 |
+
vec3 topColor = vec3(0.1, 0.4, 0.8); // Light blue
|
57 |
+
vec3 bottomColor = vec3(0.8, 0.6, 0.2); // Light brown
|
58 |
+
gl_FragColor = vec4(mix(bottomColor, topColor, heightFactor), 1.0);
|
59 |
+
}
|
60 |
+
`,
|
61 |
+
side: THREE.BackSide
|
62 |
+
});
|
63 |
+
let sky = new THREE.Mesh(skyGeom, skyMat);
|
64 |
+
scene.add(sky);
|
65 |
|
66 |
+
// Ground (simple hills)
|
67 |
+
let groundGeom = new THREE.PlaneGeometry(200, 200, 64, 64);
|
68 |
+
for (let i = 0; i < groundGeom.attributes.position.count; i++) {
|
69 |
+
let x = groundGeom.attributes.position.getX(i);
|
70 |
+
let z = groundGeom.attributes.position.getZ(i);
|
71 |
+
let y = Math.sin(x * 0.1) * Math.cos(z * 0.1) * 5.0; // Simple hills
|
72 |
+
groundGeom.attributes.position.setY(i, y);
|
73 |
+
}
|
74 |
+
let groundMat = new THREE.MeshLambertMaterial({ color: 0x228B22 }); // Forest green
|
75 |
let ground = new THREE.Mesh(groundGeom, groundMat);
|
76 |
+
ground.rotation.x = -Math.PI / 2;
|
77 |
+
ground.receiveShadow = true;
|
78 |
scene.add(ground);
|
79 |
|
80 |
+
// Airplane (slightly nicer mesh)
|
81 |
+
let planeGeom = new THREE.Group();
|
82 |
+
let fuselageGeom = new THREE.BoxGeometry(2, 0.4, 6);
|
83 |
+
let fuselageMat = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
|
84 |
+
let fuselage = new THREE.Mesh(fuselageGeom, fuselageMat);
|
85 |
+
fuselage.castShadow = true;
|
86 |
+
let wingGeom = new THREE.BufferGeometry();
|
87 |
+
const wingVertices = new Float32Array([
|
88 |
+
-2, 0, 1, 2, 0, 1, 2, 0, -2,
|
89 |
+
-2, 0, 1, 2, 0, -2, -2, 0, -2,
|
90 |
+
]);
|
91 |
+
let posAttr = new THREE.BufferAttribute(wingVertices, 3);
|
92 |
+
wingGeom.setAttribute('position', posAttr);
|
93 |
+
let wingMat = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
|
94 |
+
let wingLeft = new THREE.Mesh(wingGeom, wingMat);
|
95 |
+
wingLeft.position.x = -0.5;
|
96 |
+
wingLeft.castShadow = true;
|
97 |
+
let wingRight = wingLeft.clone();
|
98 |
+
wingRight.position.x = 0.5;
|
99 |
+
let tailGeom = new THREE.BufferGeometry();
|
100 |
+
const tailVertices = new Float32Array([
|
101 |
+
0, 0.5, -3, -0.5, 0, -3, 0.5, 0, -3
|
102 |
]);
|
103 |
+
let tailAttr = new THREE.BufferAttribute(tailVertices, 3);
|
104 |
+
tailGeom.setAttribute('position', tailAttr);
|
105 |
+
let tailMat = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
|
106 |
+
let tail = new THREE.Mesh(tailGeom, tailMat);
|
107 |
+
tail.castShadow = true;
|
108 |
+
planeGeom.add(fuselage);
|
109 |
+
planeGeom.add(wingLeft);
|
110 |
+
planeGeom.add(wingRight);
|
111 |
+
planeGeom.add(tail);
|
112 |
+
scene.add(planeGeom);
|
113 |
+
|
114 |
+
// Camera
|
115 |
+
let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
116 |
+
planeGeom.add(camera);
|
117 |
+
camera.position.y = 1.5;
|
118 |
+
camera.position.z = 2;
|
119 |
+
|
120 |
+
// Lighting
|
121 |
+
let ambientLight = new THREE.AmbientLight(0x333333);
|
122 |
scene.add(ambientLight);
|
123 |
let dirLight = new THREE.DirectionalLight(0xFFFFFF, 0.8);
|
124 |
+
dirLight.position.set(10, 20, 10);
|
125 |
+
dirLight.castShadow = true;
|
126 |
+
dirLight.shadow.mapSize.width = 2048;
|
127 |
+
dirLight.shadow.mapSize.height = 2048;
|
128 |
+
dirLight.shadow.camera.near = 1;
|
129 |
+
dirLight.shadow.camera.far = 100;
|
130 |
scene.add(dirLight);
|
131 |
|
132 |
+
// Renderer
|
133 |
+
let renderer = new THREE.WebGLRenderer({ antialias: true });
|
134 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
135 |
+
renderer.shadowMap.enabled = true;
|
136 |
+
document.body.appendChild(renderer.domElement);
|
|
|
|
|
137 |
|
138 |
+
// Flight dynamics
|
139 |
+
let pitch = 0; // radians
|
140 |
+
let yaw = 0;
|
141 |
+
let roll = 0;
|
142 |
+
let velocity = new THREE.Vector3(0, 0, 0); // m/s
|
143 |
+
let airspeed = 0; // knots
|
144 |
+
let altitude = 0; // meters
|
145 |
+
let heading = 0; // degrees
|
146 |
+
let liftForce = 0;
|
147 |
+
let dragForce = 0;
|
148 |
+
let gravity = new THREE.Vector3(0, -9.81, 0); // m/sΒ²
|
149 |
+
let mass = 1000; // kg (just a number)
|
150 |
+
let wingLiftCoefficient = 10.0; // Made-up lift coeff
|
151 |
+
let dragCoefficient = 0.5; // Parasitic drag
|
152 |
+
let maxStallAngle = Math.PI / 6; // 30 degrees
|
153 |
|
154 |
+
// Controls state
|
155 |
let keys = {
|
156 |
w: false, s: false, a: false, d: false, q: false, e: false
|
157 |
};
|
|
|
|
|
158 |
document.addEventListener('keydown', (e) => {
|
159 |
switch (e.key) {
|
160 |
case 'w': case 'ArrowUp': keys.w = true; break;
|
|
|
176 |
}
|
177 |
});
|
178 |
|
179 |
+
// Mouse look
|
180 |
+
let mouseDown = false;
|
181 |
+
let lastMouseX, lastMouseY;
|
182 |
+
let sensitivity = 0.005;
|
183 |
document.addEventListener('mousedown', (e) => {
|
184 |
mouseDown = true;
|
185 |
lastMouseX = e.clientX;
|
|
|
192 |
let dy = e.clientY - lastMouseY;
|
193 |
yaw -= dx * sensitivity;
|
194 |
pitch -= dy * sensitivity;
|
195 |
+
pitch = Math.max(-Math.PI/2, Math.min(Math.PI/2, pitch));
|
196 |
lastMouseX = e.clientX;
|
197 |
lastMouseY = e.clientY;
|
198 |
}
|
199 |
});
|
200 |
|
201 |
+
// Update instruments display
|
202 |
+
function updateInstruments() {
|
203 |
+
document.getElementById('altimeter').innerText = Math.round(altitude * 3.28084) + ' ft'; // meters to feet
|
204 |
+
document.getElementById('airspeed').innerText = Math.round(airspeed) + ' kts';
|
205 |
+
document.getElementById('heading').innerText = Math.round(THREE.Math.radToDeg(yaw)) % 360 + 'Β°';
|
206 |
+
}
|
207 |
+
|
208 |
+
// Main loop
|
209 |
function animate() {
|
210 |
requestAnimationFrame(animate);
|
211 |
|
212 |
+
// Aerodynamics
|
213 |
+
let velocityDir = new THREE.Vector3(0, 0, -1); // Plane's forward in local space
|
214 |
+
velocityDir.applyQuaternion(planeGeom.quaternion).normalize();
|
215 |
+
airspeed = velocity.length() * 1.94384; // m/s to knots (approx)
|
216 |
+
let angleOfAttack = Math.acos(velocityDir.dot(new THREE.Vector3(0, 1, 0).applyQuaternion(planeGeom.quaternion)));
|
217 |
+
liftForce = wingLiftCoefficient * airspeed * airspeed * Math.sin(angleOfAttack);
|
218 |
+
if (angleOfAttack > maxStallAngle) liftForce *= (1 - (angleOfAttack - maxStallAngle) / (Math.PI / 2 - maxStallAngle)); // Stall
|
219 |
+
dragForce = dragCoefficient * airspeed * airspeed;
|
220 |
+
|
221 |
+
// Forces
|
222 |
+
let lift = new THREE.Vector3(0, liftForce, 0).applyQuaternion(planeGeom.quaternion); // Lift always perpendicular to wings
|
223 |
+
let drag = velocityDir.clone().multiplyScalar(-dragForce);
|
224 |
+
let totalForce = new THREE.Vector3().add(gravity).add(lift).add(drag).divideScalar(mass);
|
225 |
+
|
226 |
+
// Update velocity & position (Verlet-ish integration)
|
227 |
+
velocity.add(totalForce.multiplyScalar(1/60)); // dt ~= 1/60s
|
228 |
+
planeGeom.position.add(velocity.clone().multiplyScalar(1/60));
|
229 |
+
|
230 |
+
// Collision with ground (very crude)
|
231 |
+
altitude = planeGeom.position.y - groundGeom.attributes.position.getY(0); // approx
|
232 |
+
if (altitude < 0) {
|
233 |
+
planeGeom.position.y += -altitude; // push back up
|
234 |
+
velocity.y = Math.max(0, velocity.y * 0.8); // dampen bounce
|
235 |
+
}
|
236 |
+
|
237 |
+
// Controls
|
238 |
+
if (keys.w) pitch -= 0.005;
|
239 |
+
if (keys.s) pitch += 0.005;
|
240 |
+
if (keys.a) yaw -= 0.01;
|
241 |
+
if (keys.d) yaw += 0.01;
|
242 |
+
if (keys.q) roll -= 0.02;
|
243 |
+
if (keys.e) roll += 0.02;
|
244 |
|
245 |
// Limit angles
|
246 |
+
pitch = Math.max(-Math.PI/3, Math.min(Math.PI/4, pitch)); // less extreme pitch
|
247 |
roll = Math.max(-Math.PI/4, Math.min(Math.PI/4, roll));
|
248 |
|
249 |
+
// Apply rotations (order matters)
|
250 |
+
planeGeom.rotation.order = 'ZXY'; // Roll, Pitch, Yaw (aviation standard)
|
251 |
+
planeGeom.rotation.z = roll;
|
252 |
+
planeGeom.rotation.x = pitch;
|
253 |
+
planeGeom.rotation.y = yaw;
|
|
|
|
|
254 |
|
255 |
+
// Update camera direction (following plane's rotation)
|
256 |
+
heading = THREE.Math.radToDeg(yaw) % 360;
|
257 |
|
258 |
+
updateInstruments();
|
259 |
renderer.render(scene, camera);
|
260 |
}
|
261 |
animate();
|