Certainly! Below is an HTML5 program that simulates an infinite loop of a car driving down a windy road using the Three.js library for 3D rendering. This example will create a simple 3D environment with a road and a car that moves along a winding path.
HTML5 Program
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Car Driving Simulation</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r146/three.min.js"></script>
<script>
// Scene setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 10, 10);
scene.add(ambientLight, directionalLight);
// Road geometry
const roadWidth = 2;
const roadLength = 100;
const roadGeometry = new THREE.PlaneGeometry(roadWidth, roadLength);
const roadMaterial = new THREE.MeshStandardMaterial({ color: 0x444444, side: THREE.DoubleSide });
const road = new THREE.Mesh(roadGeometry, roadMaterial);
road.rotation.x = -Math.PI / 2;
scene.add(road);
// Car model
const carGeometry = new THREE.BoxGeometry(1, 0.5, 2);
const carMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
const car = new THREE.Mesh(carGeometry, carMaterial);
car.position.set(0, 0.25, 0);
scene.add(car);
// Camera setup
camera.position.set(0, 2, 5);
camera.lookAt(car.position);
// Animation variables
let carSpeed = 0.1;
let roadPosition = 0;
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Move the car forward
car.position.z -= carSpeed;
// Move the road backward to simulate driving
roadPosition -= carSpeed;
if (roadPosition <= -roadLength) {
roadPosition += roadLength;
}
road.position.z = roadPosition;
// Adjust camera to follow the car
camera.position.z = car.position.z + 5;
camera.lookAt(car.position);
// Render the scene
renderer.render(scene, camera);
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Start animation
animate();
</script>
</body>
</html>
Explanation
Scene Setup:
A THREE.Scene object is created to hold all 3D objects.
A THREE.PerspectiveCamera is used to define the view into the 3D world.
A THREE.WebGLRenderer renders the scene onto the HTML canvas.
Lighting:
An ambient light and a directional light are added to the scene to illuminate the objects.
Road Geometry:
The road is a flat plane (THREE.PlaneGeometry) that is rotated to lie on the ground.
The road is made of a dark material to simulate asphalt.
Car Model:
The car is a simple green box (THREE.BoxGeometry) placed above the road.
Camera Setup:
The camera is positioned behind the car and set to follow it.
Animation Loop:
The car moves forward along the z-axis.
The road moves backward to simulate the car driving forward.
The camera follows the car to keep it in view.
Responsive Design:
The program adjusts the camera aspect ratio and renderer size when the window is resized.
Running the Code
Copy the code into an .html file (e.g., index.html).
Open the file in a modern web browser.
You should see a green car driving along a gray road in a 3D environment.
This implementation provides a basic framework for a 3D car driving simulation. You can enhance it further by adding textures, more complex car models, or physics-based movement.