akhaliq HF Staff commited on
Commit
b79e6f7
·
verified ·
1 Parent(s): c1f40ba

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +145 -180
index.html CHANGED
@@ -3,205 +3,170 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>3D Car Simulator with Three.js</title>
7
  <style>
8
  body { margin: 0; background-color: #87CEEB; overflow: hidden; }
9
  canvas { display: block; }
 
10
  </style>
11
  </head>
12
  <body>
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
14
  <script>
15
- // SETUP SCENE, CAMERA, RENDERER
16
  let scene = new THREE.Scene();
 
 
 
17
  let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
 
 
18
  let renderer = new THREE.WebGLRenderer();
19
  renderer.setSize(window.innerWidth, window.innerHeight);
20
  document.body.appendChild(renderer.domElement);
21
 
22
- // SKYBOX / BACKGROUND (just a simple gradient sky, no cube maps today)
23
- let skyGeom = new THREE.SphereGeometry(500, 32, 32);
24
- let skyMat = new THREE.MeshBasicMaterial({
25
- map: new THREE.CanvasTexture((function(){
26
- let c = document.createElement('canvas'), ctx = c.getContext('2d');
27
- c.width = c.height = 256;
28
- let grd = ctx.createLinearGradient(0, 0, 0, 256);
29
- grd.addColorStop(0, '#87CEEB'); // light sky blue top
30
- grd.addColorStop(1, '#4682B4'); // steel blue bottom
31
- ctx.fillStyle = grd;
32
- ctx.fillRect(0, 0, 256, 256);
33
- return c;
34
- })()),
35
- side: THREE.BackSide
36
- });
37
- let sky = new THREE.Mesh(skyGeom, skyMat);
38
- scene.add(sky);
39
-
40
- // CLOUDS (just a few puffy billow-y things using transparent textures)
41
- function makeCloud(x, y, z) {
42
- let cloudGeo = new THREE.SphereGeometry(10 + Math.random() * 5, 16, 16);
43
- let cloudMat = new THREE.MeshBasicMaterial({
44
- color: 0xffffff,
45
- opacity: Math.random() * 0.3 + 0.2,
46
- transparent: true,
47
- map: new THREE.CanvasTexture((function(){
48
- let c = document.createElement('canvas'), ctx = c.getContext('2d');
49
- c.width = c.height = 64;
50
- ctx.fillStyle = 'white';
51
- ctx.beginPath(); ctx.arc(32, 32, 28, 0, Math.PI * 2); ctx.fill();
52
- // make edges puffy/feathered
53
- let gd = ctx.createRadialGradient(32, 32, 10, 32, 32, 28);
54
- gd.addColorStop(0, 'rgba(255,255,255,1)');
55
- gd.addColorStop(0.7, 'rgba(255,255,255,0.3)');
56
- gd.addColorStop(1, 'rgba(255,255,255,0)');
57
- ctx.fillStyle = gd;
58
- ctx.fillRect(0, 0, 64, 64);
59
- return c;
60
- })())
61
- });
62
- let cloud = new THREE.Mesh(cloudGeo, cloudMat);
63
- cloud.position.set(x, y, z);
64
- scene.add(cloud);
65
- return cloud;
66
- }
67
- let clouds = [];
68
- for (let i = 0; i < 10; i++) clouds.push(makeCloud(Math.random() * 200 - 100, 30 + Math.random() * 10, Math.random() * 200 - 100));
69
-
70
- // MOUNTAINS (simple low-poly with vertex colors)
71
- let mountainGeom = new THREE.BufferGeometry();
72
- let mVerts = new Float32Array([
73
- 0, 0, 0,
74
- 50, 0, 0,
75
- 25, 40, 0,
76
-
77
- 50, 0, 0,
78
- 100, 0, 0,
79
- 75, 40, 0,
80
-
81
- 0, 0, 0,
82
- 0, 0, 50,
83
- 25, 40, 25,
84
-
85
- 100, 0, 0,
86
- 100, 0, 50,
87
- 75, 40, 25,
88
- ]);
89
- let mColors = new Float32Array([
90
- 0.5, 0.4, 0.3, // brown base
91
- 0.5, 0.4, 0.3,
92
- 0.7, 0.6, 0.5, // beige peak
93
-
94
- 0.5, 0.4, 0.3,
95
- 0.5, 0.4, 0.3,
96
- 0.7, 0.6, 0.5,
97
-
98
- 0.5, 0.4, 0.3,
99
- 0.5, 0.4, 0.3,
100
- 0.7, 0.6, 0.5,
101
-
102
- 0.5, 0.4, 0.3,
103
- 0.5, 0.4, 0.3,
104
- 0.7, 0.6, 0.5,
105
  ]);
106
- mountainGeom.setAttribute('position', new THREE.BufferAttribute(mVerts, 3));
107
- mountainGeom.setAttribute('color', new THREE.BufferAttribute(mColors, 3));
108
- let mountainMat = new THREE.MeshBasicMaterial({ vertexColors: true });
109
- let mountains = new THREE.Mesh(mountainGeom, mountainMat);
110
- mountains.scale.set(4, 4, 4); // bigger
111
- mountains.position.z = -100;
112
- scene.add(mountains);
113
-
114
- // ROAD (just a long thin plane)
115
- let roadGeom = new THREE.PlaneGeometry(200, 10);
116
- let roadMat = new THREE.MeshBasicMaterial({ color: 0x444444 });
117
- let road = new THREE.Mesh(roadGeom, roadMat);
118
- road.rotation.x = -Math.PI / 2; // flat on ground
119
- road.position.y = -0.1; // under car wheels
120
- scene.add(road);
121
-
122
- // TREES (simple green cones)
123
- function makeTree(x, z) {
124
- let treeGeo = new THREE.ConeGeometry(4, 10);
125
- let treeMat = new THREE.MeshBasicMaterial({ color: 0x228B22 }); // forestgreen
126
- let tree = new THREE.Mesh(treeGeo, treeMat);
127
- tree.position.set(x, 5, z); // half-height up so it sits on ground
128
- scene.add(tree);
129
- // quick 'n' dirty trunk
130
- let trunkGeo = new THREE.CylinderGeometry(1, 1, 2);
131
- let trunkMat = new THREE.MeshBasicMaterial({ color: 0x964B00 }); // brown
132
- let trunk = new THREE.Mesh(trunkGeo, trunkMat);
133
- trunk.position.set(x, 1, z);
134
- scene.add(trunk);
135
- }
136
- for (let i = -5; i <= 5; i++) {
137
- makeTree(i * 15, -50); // left side trees
138
- makeTree(i * 15, 50); // right side trees
139
- }
140
 
141
- // CAR (just a box with wheels)
142
- let carBodyGeom = new THREE.BoxGeometry(10, 4, 6);
143
- let carBodyMat = new THREE.MeshBasicMaterial({ color: 0xff0000 }); // red
144
- let car = new THREE.Mesh(carBodyGeom, carBodyMat);
145
- car.position.y = 2; // up off ground
146
-
147
- let wheelGeo = new THREE.CylinderGeometry(1.5, 1.5, 1);
148
- let wheelMat = new THREE.MeshBasicMaterial({ color: 0x333333 });
149
- function makeWheel(x, z) {
150
- let wheel = new THREE.Mesh(wheelGeo, wheelMat);
151
- wheel.rotation.x = Math.PI / 2; // roll it
152
- wheel.position.set(x, 0.5, z);
153
- car.add(wheel);
154
- }
155
- makeWheel(-4, -2); makeWheel( 4, -2); // front wheels
156
- makeWheel(-4, 2); makeWheel( 4, 2); // rear wheels
157
- scene.add(car);
158
-
159
- // TRAIN (just a long box with wheels, moving in circle)
160
- let trainBodyGeom = new THREE.BoxGeometry(30, 4, 6);
161
- let trainBodyMat = new THREE.MeshBasicMaterial({ color: 0x0000ff }); // blue
162
- let train = new THREE.Mesh(trainBodyGeom, trainBodyMat);
163
- train.position.y = 2;
164
- train.position.x = 80; // start off to side
165
- let trainWheelGeo = new THREE.CylinderGeometry(2, 2, 1.5);
166
- let trainWheelMat = new THREE.MeshBasicMaterial({ color: 0x333333 });
167
- function makeTrainWheel(x, z) {
168
- let wheel = new THREE.Mesh(trainWheelGeo, trainWheelMat);
169
- wheel.rotation.x = Math.PI / 2;
170
- wheel.position.set(x, 0.5, z);
171
- train.add(wheel);
172
- }
173
- makeTrainWheel(-12, -2.5); makeTrainWheel( 12, -2.5);
174
- makeTrainWheel(-12, 2.5); makeTrainWheel( 12, 2.5);
175
- scene.add(train);
176
-
177
- // ANIMATION / PHYSICS
178
- let carSpeed = 0.5;
179
- let trainAngle = 0;
180
- let clock = new THREE.Clock();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  function animate() {
182
  requestAnimationFrame(animate);
 
 
 
 
 
 
 
 
183
 
184
- // move car forward
185
- car.position.z += carSpeed;
186
- if (car.position.z > 100) car.position.z = -100; // loop road
187
-
188
- // move train in circle (easy parametric equation)
189
- trainAngle += 0.01;
190
- train.position.x = Math.cos(trainAngle) * 80;
191
- train.position.z = Math.sin(trainAngle) * 80;
192
- train.rotation.y = trainAngle + Math.PI / 2; // face direction of travel
193
-
194
- // bob clouds gently up/down
195
- clouds.forEach(cloud => {
196
- cloud.position.y = 30 + Math.sin(clock.getElapsedTime() + cloud.position.x * 0.01) * 2;
197
- });
198
-
199
- // keep camera just behind car
200
- camera.position.x = car.position.x;
201
- camera.position.z = car.position.z - 15;
202
- camera.position.y = 8; // nice view height
203
- camera.lookAt(car.position.x, 2, car.position.z); // look at car
204
-
205
  renderer.render(scene, camera);
206
  }
207
  animate();
 
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: #87CEEB; 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; }
11
  </style>
12
  </head>
13
  <body>
14
+
15
+ <div id="controls">
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 / Right (Bank)<br>
22
+ ↑↑ Speed up (W pressed faster)<br>
23
+ ↓↓ Slow down (S pressed slower)<br>
24
+ Mouse Look (move mouse to look around)
25
+ </div>
26
+
27
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
28
  <script>
29
+ // Our scene
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
+ // Renderer
37
  let renderer = new THREE.WebGLRenderer();
38
  renderer.setSize(window.innerWidth, window.innerHeight);
39
  document.body.appendChild(renderer.domElement);
40
 
41
+ // Simple ground (big plane)
42
+ let groundGeom = new THREE.PlaneGeometry(100, 100);
43
+ let groundMat = new THREE.MeshLambertMaterial({ color: 0x32CD32 }); // Lime green
44
+ let ground = new THREE.Mesh(groundGeom, groundMat);
45
+ ground.rotation.x = -Math.PI / 2; // Lay it flat
46
+ ground.position.y = -2; // A bit below zero
47
+ scene.add(ground);
48
+
49
+ // Simple airplane (just for show, a box + 2 triangles for "wings")
50
+ let planeGeom = new THREE.BufferGeometry();
51
+ const planeVertices = new Float32Array([
52
+ // Fuselage (box)
53
+ -1, -0.2, 1, 1, -0.2, 1, 1, 0.2, 1, -1, 0.2, 1, // front face
54
+ -1, -0.2, -1, 1, -0.2, -1, 1, 0.2, -1, -1, 0.2, -1, // back face
55
+ -1, -0.2, -1, -1, -0.2, 1, -1, 0.2, 1, -1, 0.2, -1, // left face
56
+ 1, -0.2, -1, 1, -0.2, 1, 1, 0.2, 1, 1, 0.2, -1, // right face
57
+ -1, -0.2, -1, 1, -0.2, -1, 1, -0.2, 1, -1, -0.2, 1, // bottom face
58
+ -1, 0.2, -1, 1, 0.2, -1, 1, 0.2, 1, -1, 0.2, 1, // top face
59
+ // Left wing (triangle)
60
+ -1, 0, 1, -2, 0, 0.5, -1, 0, -1,
61
+ // Right wing (triangle)
62
+ 1, 0, 1, 2, 0, 0.5, 1, 0, -1,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  ]);
64
+ let posAttr = new THREE.BufferAttribute(planeVertices, 3);
65
+ planeGeom.setAttribute('position', posAttr);
66
+ let planeMat = new THREE.MeshBasicMaterial({ color: 0xFFFFFF }); // White plane
67
+ let plane = new THREE.Mesh(planeGeom, planeMat);
68
+ scene.add(plane);
69
+ camera.position.y = 2; // Sit in the "cockpit"
70
+ camera.position.z = 5; // A bit forward
71
+ plane.add(camera); // Camera is now child of plane, so it moves with it
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
+ // Lighting (2 lights, ambient + directional)
74
+ let ambientLight = new THREE.AmbientLight(0x444444);
75
+ scene.add(ambientLight);
76
+ let dirLight = new THREE.DirectionalLight(0xFFFFFF, 0.8);
77
+ dirLight.position.set(5, 10, 5);
78
+ scene.add(dirLight);
79
+
80
+ // Flight dynamics variables
81
+ let pitch = 0; // Up/down nose
82
+ let yaw = 0; // Left/right turn
83
+ let roll = 0; // Left/right tilt (banking)
84
+ let speed = 0.05; // Forward movement speed
85
+ let maxSpeed = 0.2;
86
+ let minSpeed = 0.01;
87
+
88
+ // Mouse look variables
89
+ let mouseDown = false;
90
+ let lastMouseX, lastMouseY;
91
+ let sensitivity = 0.005;
92
+
93
+ // Key controls state
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;
102
+ case 's': case 'ArrowDown': keys.s = true; break;
103
+ case 'a': case 'ArrowLeft': keys.a = true; break;
104
+ case 'd': case 'ArrowRight': keys.d = true; break;
105
+ case 'q': keys.q = true; break;
106
+ case 'e': keys.e = true; break;
107
+ }
108
+ });
109
+ document.addEventListener('keyup', (e) => {
110
+ switch (e.key) {
111
+ case 'w': case 'ArrowUp': keys.w = false; break;
112
+ case 's': case 'ArrowDown': keys.s = false; break;
113
+ case 'a': case 'ArrowLeft': keys.a = false; break;
114
+ case 'd': case 'ArrowRight': keys.d = false; break;
115
+ case 'q': keys.q = false; break;
116
+ case 'e': keys.e = false; break;
117
+ }
118
+ });
119
+
120
+ // Handle mouse for looking around
121
+ document.addEventListener('mousedown', (e) => {
122
+ mouseDown = true;
123
+ lastMouseX = e.clientX;
124
+ lastMouseY = e.clientY;
125
+ });
126
+ document.addEventListener('mouseup', () => mouseDown = false);
127
+ document.addEventListener('mousemove', (e) => {
128
+ if (mouseDown) {
129
+ let dx = e.clientX - lastMouseX;
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)); // limit straight up/down
134
+ lastMouseX = e.clientX;
135
+ lastMouseY = e.clientY;
136
+ }
137
+ });
138
+
139
+ // Main animation loop
140
  function animate() {
141
  requestAnimationFrame(animate);
142
+
143
+ // Controls logic
144
+ if (keys.w) pitch -= 0.01; // Nose down to climb
145
+ if (keys.s) pitch += 0.01; // Nose up to dive
146
+ if (keys.a) yaw -= 0.02; // Turn left
147
+ if (keys.d) yaw += 0.02; // Turn right
148
+ if (keys.q) roll -= 0.02; // Roll left
149
+ if (keys.e) roll += 0.02; // Roll right
150
 
151
+ // Speed adjustment (very basic)
152
+ if (keys.w && speed < maxSpeed) speed *= 1.01; // Accelerate
153
+ if (keys.s && speed > minSpeed) speed *= 0.99; // Brake
154
+
155
+ // Limit angles
156
+ pitch = Math.max(-Math.PI/2, Math.min(Math.PI/2, pitch));
157
+ roll = Math.max(-Math.PI/4, Math.min(Math.PI/4, roll));
158
+
159
+ // Move plane forward (always toward its blue axis, which is -Z in its local space)
160
+ plane.translateZ(-speed); // Negative because Three.js uses right-hand coord system
161
+
162
+ // Apply rotations (order matters: roll, pitch, yaw - like aircraft conventions)
163
+ plane.rotation.z = roll;
164
+ plane.rotation.x = pitch;
165
+ plane.rotation.y = yaw;
166
+
167
+ // Just for visual niceness: move the directional light with camera a bit
168
+ dirLight.position.copy(camera.getWorldPosition(new THREE.Vector3()).add(new THREE.Vector3(5, 10, 5)));
169
+
 
 
170
  renderer.render(scene, camera);
171
  }
172
  animate();