Using HTML + JS (one file) make 3d fps shooter with endless generated city (in our range), for this use babylon.js
Assistant
Here's an HTML file that creates a simple 3D first-person shooter (FPS) game with an endlessly generated city using Babylon.js. All the code is contained within one file for your convenience.
html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Endless City FPS with Babylon.js</title>
<style>
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
</style>
</head>
<body>
<canvas id="renderCanvas"></canvas> <!-- The canvas element for Babylon.js -->
<!-- Include Babylon.js library -->
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<script>
// Get the canvas element
var canvas = document.getElementById("renderCanvas");
// Generate the BABYLON 3D engine
var engine = new BABYLON.Engine(canvas, true);
var createScene = function () {
// Create the scene space
var scene = new BABYLON.Scene(engine);
// Add a camera to the scene and attach it to the canvas
var camera = new BABYLON.UniversalCamera("camera", new BABYLON.Vector3(0, 2, 0), scene);
camera.attachControl(canvas, true);
// Enable mouse and keyboard controls for the camera (FPS controls)
camera.keysUp.push(87); // W
camera.keysDown.push(83); // S
camera.keysLeft.push(65); // A
camera.keysRight.push(68); // D
camera.speed = 5;
// Add a simple ambient light
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
// Create the ground
var ground = BABYLON.MeshBuilder.CreateGround("ground", {width:1000, height:1000}, scene);
var groundMaterial = new BABYLON.StandardMaterial("groundMaterial", scene);
groundMaterial.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
ground.material = groundMaterial;
// Function to generate buildings in a grid pattern
function generateCity(range) {
var minSize = 1;
var maxSize = 5;
var spacing = 10;
for (var x = -range; x <= range; x += spacing) {
for (var z = -range; z <= range; z += spacing) {
if (Math.random() > 0.3) { // Randomly skip some spots
var width = minSize + Math.random() * (maxSize - minSize);
var height = minSize + Math.random() * (maxSize * 10 - minSize);
var depth = minSize + Math.random() * (maxSize - minSize);
var building = BABYLON.MeshBuilder.CreateBox("building", {width: width, height: height, depth: depth}, scene);
building.position.x = x + (Math.random() - 0.5) * spacing;
building.position.z = z + (Math.random() - 0.5) * spacing;
building.position.y = height / 2;
// Assign a random color to the building
var buildingMaterial = new BABYLON.StandardMaterial("buildingMaterial", scene);
buildingMaterial.diffuseColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
building.material = buildingMaterial;
}
}
}
}
// Initial city generation
var cityRange = 100;
generateCity(cityRange);
// Listen for camera movement to generate more city
scene.registerBeforeRender(function () {
var position = camera.position;
// Generate new buildings when approaching the edge
if (Math.abs(position.x) > cityRange - 50 || Math.abs(position.z) > cityRange - 50) {
cityRange += 100;
// Remove old buildings outside the new range
scene.meshes.forEach(function(mesh) {
if (mesh.name === "building") {
if (Math.abs(mesh.position.x) > cityRange || Math.abs(mesh.position.z) > cityRange) {
mesh.dispose();
}
}
});
// Generate additional city
generateCity(cityRange);
}
});
return scene;
};
var scene = createScene(); // Call the createScene function
// Run the render loop
engine.runRenderLoop(function () {
scene.render();
});
// Resize the engine on window resize event
window.addEventListener("resize", function () {
engine.resize();
});
</script>
</body>
</html>
How the Code Works:
HTML Structure: Sets up a full-screen canvas for rendering the 3D content.
Babylon.js Inclusion: The Babylon.js library is included via a CDN link within the same file.
Scene Creation:
A scene is created along with a universal camera that's set up for FPS controls.
Mouse and keyboard controls are enabled for navigating the scene.
Lighting and Ground:
A hemispheric light provides basic illumination.
A large ground plane represents the city floor.
City Generation:
The generateCity function creates buildings in a grid pattern within a specified range.
Buildings are randomly sized and positioned to create variety.
Each building is a box mesh with random height and color.
Endless Generation:
The scene checks the camera's position before each render.
If the camera is near the edge of the current city range, new buildings are generated further out.
Buildings that are too far away are disposed of to optimize performance.
Controls:
Movement: Use W/A/S/D keys to move around.
Look Around: Move the mouse to look in different directions.
Instructions:
Save the code above as an .html file (e.g., endless_city_fps.html).
Open the file in a modern web browser that supports WebGL (like Chrome or Firefox).
Click inside the browser window to focus the input.
Use the W/A/S/D keys to move and the mouse to look around.
Explore the endlessly generated city!
Note: This is a basic implementation meant to demonstrate how to create an endless city with FPS controls using Babylon.js. You can expand upon this by adding textures, more sophisticated building generation algorithms, or game mechanics like shooting.