Update index.html
Browse files- index.html +183 -114
index.html
CHANGED
@@ -1,141 +1,210 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
-
<html>
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
-
<
|
|
|
6 |
<style>
|
7 |
-
body { margin: 0; background-color: #87CEEB;
|
8 |
canvas { display: block; }
|
9 |
</style>
|
10 |
</head>
|
11 |
<body>
|
12 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
13 |
<script>
|
14 |
-
//
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
19 |
document.body.appendChild(renderer.domElement);
|
20 |
|
21 |
-
//
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
scene.add(
|
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 |
-
// MAIN
|
98 |
-
function animate(
|
99 |
requestAnimationFrame(animate);
|
100 |
-
|
101 |
-
getControlState(); // read keyboard
|
102 |
-
|
103 |
-
// ROTATE PLANE (pitch, yaw from controls)
|
104 |
-
planeRot.x += pitchRate * 0.01; // integrate pitch
|
105 |
-
planeRot.y += yawRate * 0.01; // integrate yaw
|
106 |
-
planeRot.z += rollRate * 0.005; // a bit of roll (less intuitive)
|
107 |
-
plane.rotation.setFromEuler(planeRot);
|
108 |
-
|
109 |
-
// FORWARD VELOCITY (based on throttle & direction)
|
110 |
-
var forward = new THREE.Vector3(0, 0, 1);
|
111 |
-
forward.applyEuler(planeRot); // where are we pointing?
|
112 |
-
var desiredVel = forward.multiplyScalar(throttle * maxSpeed);
|
113 |
-
|
114 |
-
// LIFT FORCE (magic makes plane want to go up when moving fast)
|
115 |
-
var lift = forward.clone().multiplyScalar(liftCoeff * throttle * maxSpeed);
|
116 |
-
lift.y = Math.abs(lift.z); // up vector scales with forward speed
|
117 |
-
|
118 |
-
// SUM FORCES (thrust, lift, gravity)
|
119 |
-
var accel = desiredVel.clone().sub(planeVel).multiplyScalar(0.05); // gentle accel
|
120 |
-
accel.add(lift.multiplyScalar(0.01)); // tiny bit of lift helps
|
121 |
-
accel.add(gravity); // always pulled down
|
122 |
-
|
123 |
-
// INTEGRATE VELOCITY & POSITION
|
124 |
-
planeVel.add(accel);
|
125 |
-
planePos.add(planeVel.multiplyScalar(0.016)); // assume ~60fps = 0.016 s/step
|
126 |
-
|
127 |
-
// UPDATE PLANE POSITION
|
128 |
-
plane.position.copy(planePos);
|
129 |
-
|
130 |
-
// KEEP PLANE ABOVE TERRAIN (crude collision)
|
131 |
-
if (planePos.y < -45) { // hit ground?
|
132 |
-
planePos.y = -45; // stick to ground
|
133 |
-
planeVel.y = Math.max(planeVel.y, 0); // no bounce
|
134 |
-
}
|
135 |
-
|
136 |
renderer.render(scene, camera);
|
137 |
}
|
138 |
animate();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
</script>
|
140 |
</body>
|
141 |
</html>
|
|
|
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 |
+
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 |
+
// SKY, SUN, AMBIENT LIGHT
|
23 |
+
scene.background = new THREE.Color(0x87CEEB); // sky blue
|
24 |
+
let sun = new THREE.DirectionalLight(0xffffff, 1);
|
25 |
+
sun.position.set(10, 20, 10);
|
26 |
+
scene.add(sun);
|
27 |
+
let ambient = new THREE.AmbientLight(0x666666);
|
28 |
+
scene.add(ambient);
|
29 |
|
30 |
+
// CLOUDS (just a few big transparent billboards)
|
31 |
+
function makeCloud(x, y, z) {
|
32 |
+
let geo = new THREE.PlaneGeometry(5, 2);
|
33 |
+
let mat = new THREE.MeshBasicMaterial({
|
34 |
+
map: new THREE.CanvasTexture(makeCloudCanvas()),
|
35 |
+
transparent: true,
|
36 |
+
depthWrite: false
|
37 |
+
});
|
38 |
+
let cloud = new THREE.Mesh(geo, mat);
|
39 |
+
cloud.position.set(x, y, z);
|
40 |
+
cloud.rotation.y = Math.random() * Math.PI * 2; // random rotation
|
41 |
+
scene.add(cloud);
|
42 |
+
// animate cloud floating
|
43 |
+
let phase = Math.random() * Math.PI * 2;
|
44 |
+
function animateCloud() {
|
45 |
+
requestAnimationFrame(animateCloud);
|
46 |
+
cloud.position.y = y + Math.sin(phase + Date.now() * 0.001) * 0.2;
|
47 |
+
}
|
48 |
+
animateCloud();
|
49 |
+
}
|
50 |
+
function makeCloudCanvas() {
|
51 |
+
let canvas = document.createElement('canvas');
|
52 |
+
canvas.width = 256;
|
53 |
+
canvas.height = 128;
|
54 |
+
let ctx = canvas.getContext('2d');
|
55 |
+
ctx.fillStyle = 'white';
|
56 |
+
ctx.beginPath();
|
57 |
+
for (let i = 0; i < 10; i++) {
|
58 |
+
ctx.arc(Math.random() * 256, Math.random() * 128, Math.random() * 20 + 10, 0, Math.PI * 2);
|
59 |
+
}
|
60 |
+
ctx.fill();
|
61 |
+
return canvas;
|
62 |
+
}
|
63 |
+
makeCloud(10, 8, -20);
|
64 |
+
makeCloud(-15, 7, -10);
|
65 |
+
makeCloud(5, 9, -30);
|
66 |
+
|
67 |
+
// MOUNTAINS (simple low-poly)
|
68 |
+
let mountainGeo = new THREE.BufferGeometry();
|
69 |
+
let mountainVerts = new Float32Array([
|
70 |
+
0, 0, 0, 5, 0, 0, 10, 0, 0,
|
71 |
+
5, 5, -5, 5, 5, 5, 0, 0, 0,
|
72 |
+
10, 0, 0, 15, 0, 0, 10, 5,-5,
|
73 |
+
10, 5,-5, 15, 0, 0, 10, 0, 5
|
74 |
+
]);
|
75 |
+
mountainGeo.setAttribute('position', new THREE.BufferAttribute(mountainVerts, 3));
|
76 |
+
let mountainMat = new THREE.MeshLambertMaterial({ color: 0x888888 });
|
77 |
+
for (let i = 0; i < 5; i++) {
|
78 |
+
let m = new THREE.Mesh(mountainGeo, mountainMat);
|
79 |
+
m.position.x = -20 + i * 10;
|
80 |
+
m.position.z = -40;
|
81 |
+
m.scale.y = 2 + Math.random() * 2;
|
82 |
+
scene.add(m);
|
83 |
+
}
|
84 |
|
85 |
+
// ROAD (long thin box)
|
86 |
+
let roadGeo = new THREE.BoxGeometry(1000, 0.1, 10);
|
87 |
+
let roadMat = new THREE.MeshLambertMaterial({ color: 0x444444 });
|
88 |
+
let road = new THREE.Mesh(roadGeo, roadMat);
|
89 |
+
road.position.z = -50;
|
90 |
+
scene.add(road);
|
91 |
+
// dashed line in the middle of the road
|
92 |
+
let lineGeo = new THREE.PlaneGeometry(1000, 0.2);
|
93 |
+
let lineMat = new THREE.MeshBasicMaterial({ color: 'yellow' });
|
94 |
+
let line = new THREE.Mesh(lineGeo, lineMat);
|
95 |
+
line.position.z = -50;
|
96 |
+
line.position.y = 0.11; // on top of road
|
97 |
+
line.rotation.x = -Math.PI / 2;
|
98 |
+
scene.add(line);
|
99 |
+
// make dashes (ugly simple way)
|
100 |
+
for (let d = -500; d < 500; d += 20) {
|
101 |
+
let dash = new THREE.Mesh(new THREE.PlaneGeometry(5, 0.2), lineMat);
|
102 |
+
dash.position.z = -50;
|
103 |
+
dash.position.x = d;
|
104 |
+
dash.position.y = 0.11;
|
105 |
+
dash.rotation.x = -Math.PI / 2;
|
106 |
+
scene.add(dash);
|
107 |
+
}
|
108 |
|
109 |
+
// TREES (green cylinders with sphere tops)
|
110 |
+
function makeTree(x, z) {
|
111 |
+
let trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.5, 0.5, 2), new THREE.MeshLambertMaterial({ color: 0x663300 }));
|
112 |
+
trunk.position.y = 1;
|
113 |
+
trunk.position.x = x;
|
114 |
+
trunk.position.z = z;
|
115 |
+
let leaf = new THREE.Mesh(new THREE.SphereGeometry(1.5), new THREE.MeshLambertMaterial({ color: 0x00CC00 }));
|
116 |
+
leaf.position.y = 3.5;
|
117 |
+
leaf.position.x = x;
|
118 |
+
leaf.position.z = z;
|
119 |
+
scene.add(trunk);
|
120 |
+
scene.add(leaf);
|
121 |
+
}
|
122 |
+
// put trees along road sides
|
123 |
+
for (let z = -40; z > -200; z -= 10) {
|
124 |
+
makeTree(-5, z);
|
125 |
+
makeTree(5, z);
|
126 |
+
}
|
127 |
|
128 |
+
// TRAIN (body is box, wheels circles, goes in circle)
|
129 |
+
let trainBody = new THREE.Mesh(new THREE.BoxGeometry(4, 2, 2), new THREE.MeshLambertMaterial({ color: 'red' }));
|
130 |
+
trainBody.position.y = 1;
|
131 |
+
let trainWheel1 = new THREE.Mesh(new THREE.CylinderGeometry(0.5, 0.5, 1), new THREE.MeshLambertMaterial({ color: 'black' }));
|
132 |
+
trainWheel1.rotation.z = Math.PI / 2;
|
133 |
+
trainWheel1.position.x = -1.5;
|
134 |
+
trainWheel1.position.y = 0.5;
|
135 |
+
let trainWheel2 = trainWheel1.clone();
|
136 |
+
trainWheel2.position.x = 1.5;
|
137 |
+
trainBody.add(trainWheel1);
|
138 |
+
trainBody.add(trainWheel2);
|
139 |
+
scene.add(trainBody);
|
140 |
+
trainBody.position.x = 20;
|
141 |
+
let trainAngle = 0;
|
142 |
+
function animateTrain() {
|
143 |
+
requestAnimationFrame(animateTrain);
|
144 |
+
trainAngle += 0.01;
|
145 |
+
trainBody.position.x = 20 * Math.cos(trainAngle);
|
146 |
+
trainBody.position.z = 20 * Math.sin(trainAngle);
|
147 |
+
trainBody.rotation.y = trainAngle + Math.PI / 2;
|
148 |
+
}
|
149 |
+
animateTrain();
|
150 |
|
151 |
+
// CAR (box body, sphere wheels, controllable)
|
152 |
+
let carBody = new THREE.Mesh(new THREE.BoxGeometry(3, 1.5, 1.8), new THREE.MeshLambertMaterial({ color: 'blue' }));
|
153 |
+
let carWheelGeo = new THREE.SphereGeometry(0.4);
|
154 |
+
let carWheelFL = new THREE.Mesh(carWheelGeo, new THREE.MeshLambertMaterial({ color: 'black' }));
|
155 |
+
carWheelFL.position.set(-1.2, -0.5, 0.8);
|
156 |
+
let carWheelFR = carWheelFL.clone();
|
157 |
+
carWheelFR.position.x = 1.2;
|
158 |
+
let carWheelBL = carWheelFL.clone();
|
159 |
+
carWheelBL.position.z = -0.8;
|
160 |
+
let carWheelBR = carWheelFR.clone();
|
161 |
+
carWheelBR.position.z = -0.8;
|
162 |
+
carBody.add(carWheelFL);
|
163 |
+
carBody.add(carWheelFR);
|
164 |
+
carBody.add(carWheelBL);
|
165 |
+
carBody.add(carWheelBR);
|
166 |
+
scene.add(carBody);
|
167 |
+
carBody.position.set(0, 0.8, 0); // start pos
|
168 |
+
let carSpeed = 0;
|
169 |
+
let carAngle = 0;
|
170 |
+
let keys = { ArrowUp: false, ArrowDown: false, ArrowLeft: false, ArrowRight: false,
|
171 |
+
KeyW: false, KeyS: false, KeyA: false, KeyD: false };
|
172 |
+
document.addEventListener('keydown', e => {
|
173 |
+
if (e.code in keys) keys[e.code] = true;
|
174 |
+
});
|
175 |
+
document.addEventListener('keyup', e => {
|
176 |
+
if (e.code in keys) keys[e.code] = false;
|
177 |
+
});
|
178 |
+
function updateCar() {
|
179 |
+
if (keys.ArrowUp || keys.KeyW) carSpeed += 0.01;
|
180 |
+
if (keys.ArrowDown || keys.KeyS) carSpeed -= 0.01;
|
181 |
+
if (keys.ArrowLeft || keys.KeyA) carAngle -= 0.05;
|
182 |
+
if (keys.ArrowRight || keys.KeyD) carAngle += 0.05;
|
183 |
+
carSpeed *= 0.98; // friction
|
184 |
+
carBody.position.x += Math.sin(carAngle) * carSpeed;
|
185 |
+
carBody.position.z += Math.cos(carAngle) * carSpeed;
|
186 |
+
carBody.rotation.y = -carAngle;
|
187 |
+
// keep camera behind car
|
188 |
+
camera.position.x = carBody.position.x - Math.sin(carAngle) * 5;
|
189 |
+
camera.position.z = carBody.position.z - Math.cos(carAngle) * 5;
|
190 |
+
camera.position.y = 3;
|
191 |
+
camera.lookAt(carBody.position);
|
192 |
}
|
193 |
|
194 |
+
// MAIN LOOP
|
195 |
+
function animate() {
|
196 |
requestAnimationFrame(animate);
|
197 |
+
updateCar();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
renderer.render(scene, camera);
|
199 |
}
|
200 |
animate();
|
201 |
+
|
202 |
+
// resize handling
|
203 |
+
window.addEventListener('resize', () => {
|
204 |
+
camera.aspect = window.innerWidth / window.innerHeight;
|
205 |
+
camera.updateProjectionMatrix();
|
206 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
207 |
+
});
|
208 |
</script>
|
209 |
</body>
|
210 |
</html>
|