akhaliq HF Staff commited on
Commit
107476b
·
verified ·
1 Parent(s): 638c90a

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +144 -156
index.html CHANGED
@@ -1,165 +1,153 @@
1
  <!DOCTYPE html>
2
- <html lang="en">
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
- const scene = new THREE.Scene();
17
- const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
18
- const renderer = new THREE.WebGLRenderer();
19
- renderer.setSize(window.innerWidth, window.innerHeight);
20
- document.body.appendChild(renderer.domElement);
21
-
22
- // CAMERA initial position
23
- camera.position.set(0, 5, 10);
24
-
25
- // RESIZE HANDLING
26
- window.addEventListener('resize', () => {
27
- camera.aspect = window.innerWidth / window.innerHeight;
28
- camera.updateProjectionMatrix();
29
  renderer.setSize(window.innerWidth, window.innerHeight);
30
- });
31
-
32
- // GROUND (Road)
33
- const roadGeom = new THREE.PlaneGeometry(100, 4);
34
- const roadMat = new THREE.MeshBasicMaterial({ color: 0x444444 });
35
- const road = new THREE.Mesh(roadGeom, roadMat);
36
- road.rotation.x = -Math.PI / 2;
37
- road.position.z = -2;
38
- scene.add(road);
39
-
40
- // ROAD dashed line
41
- const dashedLineGeom = new THREE.Geometry();
42
- for (let i = -50; i < 50; i += 4) {
43
- const point = new THREE.Vector3(i, 0.1, -2);
44
- dashedLineGeom.vertices.push(point);
45
- }
46
- const dashedLineMat = new THREE.LineDashedMaterial({
47
- color: 0xffffff,
48
- linewidth: 1,
49
- scale: 1,
50
- dashSize: 1,
51
- gapSize: 1
52
- });
53
- const dashedLine = new THREE.Line(dashedLineGeom, dashedLineMat);
54
- dashedLine.computeLineDistances();
55
- scene.add(dashedLine);
56
-
57
- // MOUNTAINS (just 3 big blocks)
58
- const mountainGeom = new THREE.BoxGeometry(20, 10, 20);
59
- const mountainMat = new THREE.MeshLambertMaterial({ color: 0x888888 });
60
- for (let i = -1; i <= 1; i++) {
61
- const mountain = new THREE.Mesh(mountainGeom, mountainMat);
62
- mountain.position.set(i * 25, -5, -50);
63
- scene.add(mountain);
64
- }
65
-
66
- // CLOUDS
67
- function makeCloud(x, z) {
68
- const puffGeom = new THREE.SphereGeometry(2, 6, 6);
69
- const puffMat = new THREE.MeshBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.7 });
70
- const cloud = new THREE.Group();
71
- for (let i = 0; i < 3; i++) {
72
- const puff = new THREE.Mesh(puffGeom, puffMat);
73
- puff.position.set(i * 2 - 2, Math.random() * 2, Math.random() * 2 - 1);
74
- cloud.add(puff);
75
- }
76
- cloud.position.set(x, 15, z);
77
- scene.add(cloud);
78
- return cloud;
79
- }
80
- const clouds = [];
81
- for (let i = 0; i < 10; i++) {
82
- clouds.push(makeCloud(Math.random() * 100 - 50, Math.random() * 100 - 50));
83
- }
84
-
85
- // TREES (simple green cones)
86
- function makeTree(x, z) {
87
- const treeGeom = new THREE.ConeGeometry(1, 3);
88
- const treeMat = new THREE.MeshLambertMaterial({ color: 0x22aa22 });
89
- const tree = new THREE.Mesh(treeGeom, treeMat);
90
- tree.position.set(x, 1, z);
91
- scene.add(tree);
92
- }
93
- for (let i = -40; i <= 40; i += 8) {
94
- makeTree(i + Math.random() * 4 - 2, -4 + Math.random() * 2 - 1);
95
- }
96
-
97
- // CAR
98
- const carGeom = new THREE.BoxGeometry(3, 1.5, 1.5);
99
- const carMat = new THREE.MeshLambertMaterial({ color: 0xff0000 });
100
- const car = new THREE.Mesh(carGeom, carMat);
101
- car.position.set(0, 1, 0);
102
- scene.add(car);
103
- let carSpeed = 0;
104
- let carDir = 0; // radians
105
- const carVel = new THREE.Vector3();
106
-
107
- // TRAIN (just a silly little train going in circles)
108
- const trainGeom = new THREE.BoxGeometry(2, 1, 1);
109
- const trainMat = new THREE.MeshLambertMaterial({ color: 0x0000ff });
110
- const train = new THREE.Mesh(trainGeom, trainMat);
111
- train.position.set(20, 1, -4);
112
- scene.add(train);
113
- let trainAngle = 0;
114
-
115
- // LIGHTING
116
- const ambientLight = new THREE.AmbientLight(0x333333);
117
- scene.add(ambientLight);
118
- const dirLight = new THREE.DirectionalLight(0xffffff, 1);
119
- dirLight.position.set(0, 10, 5);
120
- scene.add(dirLight);
121
-
122
- // KEYBOARD CONTROLS
123
- document.addEventListener('keydown', (e) => {
124
- switch(e.key) {
125
- case 'ArrowUp': case 'w': carSpeed = Math.min(carSpeed + 0.2, 0.5); break;
126
- case 'ArrowDown': case 's': carSpeed = Math.max(carSpeed - 0.2, -0.5); break;
127
- case 'ArrowLeft': case 'a': carDir -= 0.1; break;
128
- case 'ArrowRight': case 'd': carDir += 0.1; break;
129
- }
130
- });
131
-
132
- // MAIN LOOP
133
- function animate() {
134
- requestAnimationFrame(animate);
135
-
136
- // Move CAR
137
- carVel.x = Math.sin(carDir) * carSpeed;
138
- carVel.z = Math.cos(carDir) * carSpeed;
139
- car.position.x += carVel.x;
140
- car.position.z += carVel.z;
141
- carSpeed *= 0.98; // damping
142
- // keep car on road bounds
143
- if (car.position.x < -45) car.position.x = -45;
144
- if (car.position.x > 45) car.position.x = 45;
145
- // Follow car with camera (smoothly)
146
- camera.position.x += (car.position.x - camera.position.x) * 0.05;
147
- camera.position.z += (car.position.z + 10 - camera.position.z) * 0.05;
148
-
149
- // Move TRAIN in circle
150
- trainAngle += 0.01;
151
- train.position.x = Math.sin(trainAngle) * 20;
152
- train.position.z = Math.cos(trainAngle) * 20 - 4;
153
-
154
- // Move CLOUDS slowly
155
- clouds.forEach(cloud => {
156
- cloud.position.x += 0.01;
157
- if (cloud.position.x > 50) cloud.position.x = -50;
158
  });
159
 
160
- renderer.render(scene, camera);
161
- }
162
- animate();
163
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  </body>
165
  </html>
 
1
  <!DOCTYPE html>
2
+ <html>
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <style>body { margin: 0; background-color: #87CEEB; }</style>
 
 
 
 
 
6
  </head>
7
  <body>
8
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
9
+ <script>
10
+ // SETTINGS
11
+ var planeSpeed = 0.1;
12
+ var planeRollSpeed = 0.05;
13
+ var planePitchSpeed = 0.05;
14
+ var maxAltitude = 1000;
15
+ var maxSpeed = 5;
16
+
17
+ // SETUP SCENE, CAMERA, RENDERER
18
+ var scene = new THREE.Scene();
19
+ var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000);
20
+ var renderer = new THREE.WebGLRenderer({ antialias: true });
 
 
 
21
  renderer.setSize(window.innerWidth, window.innerHeight);
22
+ document.body.appendChild(renderer.domElement);
23
+
24
+ // GENERATE SKYBOX (simple gradient)
25
+ var vertexShader = `
26
+ varying vec3 vWorldPosition;
27
+ void main() {
28
+ vec4 worldPosition = modelMatrix * vec4(position, 1.0);
29
+ vWorldPosition = worldPosition.xyz;
30
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
31
+ }
32
+ `;
33
+ var fragmentShader = `
34
+ uniform vec3 topColor;
35
+ uniform vec3 bottomColor;
36
+ uniform float offset;
37
+ uniform float exponent;
38
+ varying vec3 vWorldPosition;
39
+ void main() {
40
+ float h = normalize(vWorldPosition + offset).y;
41
+ gl_FragColor = vec4(mix(bottomColor, topColor, pow(max(h, 0.0), exponent)), 1.0);
42
+ }
43
+ `;
44
+ var skyUniforms = {
45
+ topColor: { value: new THREE.Color(0x87CEEB) }, // light blue
46
+ bottomColor: { value: new THREE.Color(0xFFFFFF) }, // white
47
+ offset: { value: 33 },
48
+ exponent: { value: 0.6 }
49
+ };
50
+ var skyGeo = new THREE.SphereGeometry(5000, 32, 15);
51
+ var skyMat = new THREE.ShaderMaterial({
52
+ vertexShader, fragmentShader, uniforms: skyUniforms, side: THREE.BackSide
53
+ });
54
+ var sky = new THREE.Mesh(skyGeo, skyMat);
55
+ scene.add(sky);
56
+
57
+ // GENERATE GROUND (simple plane)
58
+ var groundGeo = new THREE.PlaneGeometry(10000, 10000, 10, 10);
59
+ var groundMat = new THREE.MeshLambertMaterial({ color: 0x32CD32 }); // lime green
60
+ var ground = new THREE.Mesh(groundGeo, groundMat);
61
+ ground.rotation.x = -Math.PI / 2;
62
+ scene.add(ground);
63
+
64
+ // GENERATE AIRPLANE (simple mesh)
65
+ var planeGeo = new THREE.Group();
66
+ var fuselageGeo = new THREE.CylinderGeometry(1, 2, 10, 20);
67
+ var fuselageMat = new THREE.MeshLambertMaterial({ color: 0xFFFFFF }); // white
68
+ var fuselage = new THREE.Mesh(fuselageGeo, fuselageMat);
69
+ planeGeo.add(fuselage);
70
+ var wingGeo = new THREE.BoxGeometry(10, 1, 1);
71
+ var wingMat = new THREE.MeshLambertMaterial({ color: 0xFF0000 }); // red
72
+ var leftWing = new THREE.Mesh(wingGeo, wingMat);
73
+ leftWing.position.x = -5;
74
+ planeGeo.add(leftWing);
75
+ var rightWing = new THREE.Mesh(wingGeo, wingMat);
76
+ rightWing.position.x = 5;
77
+ planeGeo.add(rightWing);
78
+ var tailGeo = new THREE.ConeGeometry(1, 2, 10);
79
+ var tailMat = new THREE.MeshLambertMaterial({ color: 0x0000FF }); // blue
80
+ var tail = new THREE.Mesh(tailGeo, tailMat);
81
+ tail.position.z = -5;
82
+ planeGeo.add(tail);
83
+ scene.add(planeGeo);
84
+ var plane = planeGeo; // we'll move this
85
+
86
+ // ADD LIGHTING
87
+ var sun = new THREE.DirectionalLight(0xFFFFFF, 1);
88
+ sun.position.set(100, 100, 100);
89
+ scene.add(sun);
90
+ var ambient = new THREE.AmbientLight(0x444444);
91
+ scene.add(ambient);
92
+
93
+ // CAMERA ATTACH TO PLANE
94
+ plane.add(camera);
95
+ camera.position.set(0, 5, -15); // behind the cockpit
96
+ camera.lookAt(plane.position);
97
+
98
+ // USER INPUT (keyboard)
99
+ var keys = {
100
+ w: false, s: false, a: false, d: false,
101
+ q: false, e: false, r: false, f: false
102
+ };
103
+ document.addEventListener('keydown', (e) => {
104
+ if (e.key in keys) keys[e.key] = true;
105
+ });
106
+ document.addEventListener('keyup', (e) => {
107
+ if (e.key in keys) keys[e.key] = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  });
109
 
110
+ // MAIN LOOP
111
+ var velocity = new THREE.Vector3();
112
+ var direction = new THREE.Vector3();
113
+ var roll = 0, pitch = 0, altitude = 0, speed = 0;
114
+ function animate() {
115
+ requestAnimationFrame(animate);
116
+
117
+ // PLANE CONTROLS
118
+ if (keys.w) speed = Math.min(maxSpeed, speed + 0.01);
119
+ if (keys.s) speed = Math.max(0, speed - 0.01);
120
+ if (keys.a) roll -= planeRollSpeed;
121
+ if (keys.d) roll += planeRollSpeed;
122
+ if (keys.q) pitch -= planePitchSpeed;
123
+ if (keys.e) pitch += planePitchSpeed;
124
+ if (keys.r) { /* reset */ roll = pitch = 0; speed = 1; }
125
+ if (keys.f) { /* full throttle */ speed = maxSpeed; }
126
+
127
+ // PHYSICS SIM (very basic)
128
+ plane.rotation.z = roll;
129
+ plane.rotation.x = pitch;
130
+ direction.set(0, 0, -1).applyEuler(plane.rotation);
131
+ velocity.add(direction.multiplyScalar(speed * planeSpeed));
132
+ plane.position.add(velocity);
133
+ altitude = plane.position.y;
134
+ if (altitude < 0) {
135
+ plane.position.y = 0; // don't go underground
136
+ velocity.y = 0;
137
+ }
138
+ if (altitude > maxAltitude) {
139
+ plane.position.y = maxAltitude; // don't go too high
140
+ velocity.y = 0;
141
+ }
142
+ velocity.multiplyScalar(0.99); // friction
143
+
144
+ // CAMERA FOLLOW
145
+ camera.lookAt(plane.position);
146
+
147
+ // RENDER
148
+ renderer.render(scene, camera);
149
+ }
150
+ animate();
151
+ </script>
152
  </body>
153
  </html>