Here is the full updated code that adds robots collecting garbage and recycling them in factories. When you hover over a robot, it will display what the robot is currently doing in the UI.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Endless City FPS with Robots and Factories - Babylon.js</title>
<style>
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
#robotStatus {
position: absolute;
bottom: 10px;
left: 10px;
color: white;
background-color: rgba(0, 0, 0, 0.5);
padding: 10px;
font-family: Arial, sans-serif;
display: none; /* Hidden by default */
}
</style>
</head>
<body>
<canvas id="renderCanvas"></canvas> <!-- The canvas element for Babylon.js -->
<div id="robotStatus">Robot Status: Idle</div> <!-- UI Element for robot status -->
<!-- Include Babylon.js Core Library -->
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<!-- Include Babylon.js CannonJS physics engine plugin -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.min.js"></script>
<!-- Include Babylon.js Materials Library for PBR materials -->
<script src="https://cdn.babylonjs.com/materialsLibrary/babylonjs.materials.min.js"></script>
<!-- Include Babylon.js Procedural Textures Library for rain textures -->
<script src="https://cdn.babylonjs.com/proceduralTexturesLibrary/babylonjs.proceduralTextures.min.js"></script>
<!-- Include Babylon.js GUI Library for UI elements -->
<script src="https://cdn.babylonjs.com/gui/babylon.gui.min.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);
// Enable physics
scene.enablePhysics(new BABYLON.Vector3(0, -9.81, 0), new BABYLON.CannonJSPlugin());
// Create a universal camera with no rotation (to prevent initial tilt)
var camera = new BABYLON.UniversalCamera("camera", new BABYLON.Vector3(0, 2, 0), scene);
camera.rotation = new BABYLON.Vector3(0, 0, 0);
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 = 2; // Slowed down character speed
// Apply gravity to the camera
camera.applyGravity = true;
camera.ellipsoid = new BABYLON.Vector3(1, 1, 1);
camera.checkCollisions = true;
// Make sure collisions are enabled in the scene
scene.collisionsEnabled = true;
// Add a directional light for shadows
var light = new BABYLON.DirectionalLight("dirLight", new BABYLON.Vector3(-1, -2, -1), scene);
light.position = new BABYLON.Vector3(50, 100, 50);
light.intensity = 1.0;
// Create a shadow generator
var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
shadowGenerator.useBlurExponentialShadowMap = true;
shadowGenerator.blurKernel = 32;
// Create the ground with a PBR material for reflections
var ground = BABYLON.MeshBuilder.CreateGround("ground", {width:1000, height:1000}, scene);
var groundMaterial = new BABYLON.PBRMaterial("groundMaterial", scene);
groundMaterial.albedoColor = new BABYLON.Color3(0.4, 0.4, 0.4);
groundMaterial.reflectivityColor = new BABYLON.Color3(0.4, 0.4, 0.4); // Wet look
groundMaterial.microSurface = 0.5; // Wet look
groundMaterial.environmentTexture = BABYLON.CubeTexture.CreateFromPrefilteredData("https://playground.babylonjs.com/textures/environment.dds", scene);
ground.material = groundMaterial;
// Enable collisions and physics for the ground
ground.checkCollisions = true;
ground.receiveShadows = true;
// Assign a physics impostor to the ground
ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0.5, restitution: 0 }, scene);
// Create a skybox for reflections
var skybox = BABYLON.MeshBuilder.CreateBox("skyBox", { size: 1000 }, scene);
var skyboxMaterial = new BABYLON.StandardMaterial("skyBoxMaterial", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("https://playground.babylonjs.com/textures/skybox", scene);
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
skybox.material = skyboxMaterial;
// Global environment texture for reflections
scene.environmentTexture = groundMaterial.environmentTexture;
// Arrays to keep track of various entities
var buildings = [];
var factories = [];
var garbages = [];
var robots = [];
// Function to generate buildings, factories, and garbage in a grid pattern
function generateCity(range) {
var minSize = 5; // Adjusted to make buildings wider
var maxSize = 20; // Adjusted to make buildings wider
var spacing = 30; // Increased spacing between buildings for wider buildings and roads
var roadFrequency = 5; // Every 5th line will be a road
for (var x = -range; x <= range; x += spacing) {
for (var z = -range; z <= range; z += spacing) {
// Determine if current position is a road
var isRoad = (Math.abs(Math.floor(x / spacing)) % roadFrequency === 0) || (Math.abs(Math.floor(z / spacing)) % roadFrequency === 0);
if (isRoad) {
// Create road segment
var road = BABYLON.MeshBuilder.CreateGround("road", {width: spacing, height: spacing}, scene);
road.position.x = x;
road.position.z = z;
var roadMaterial = new BABYLON.StandardMaterial("roadMaterial", scene);
roadMaterial.diffuseColor = new BABYLON.Color3(0.1, 0.1, 0.1);
roadMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
road.material = roadMaterial;
// Ensure roads are slightly lower than ground to avoid z-fighting
road.position.y = 0.01;
} else {
// Randomly decide to create a building, factory, or garbage
var rand = Math.random();
if (rand < 0.1) {
// Create factory
var width = minSize + Math.random() * (maxSize - minSize);
var height = minSize + Math.random() * (maxSize - minSize);
var depth = minSize + Math.random() * (maxSize - minSize);
var factory = BABYLON.MeshBuilder.CreateBox("factory", {width: width, height: height, depth: depth}, scene);
factory.position.x = x + (Math.random() - 0.5) * (spacing / 3);
factory.position.z = z + (Math.random() - 0.5) * (spacing / 3);
factory.position.y = height / 2;
// Create PBR material for factories
var factoryMaterial = new BABYLON.PBRMaterial("factoryMaterial", scene);
factoryMaterial.albedoColor = new BABYLON.Color3(0.6, 0.6, 0.6);
factoryMaterial.reflectivityColor = new BABYLON.Color3(0.1, 0.1, 0.1);
factoryMaterial.microSurface = 0.3;
factoryMaterial.environmentTexture = scene.environmentTexture;
factory.material = factoryMaterial;
// Enable collisions and physics for factories
factory.checkCollisions = true;
factory.physicsImpostor = new BABYLON.PhysicsImpostor(factory, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0.5, restitution: 0 }, scene);
// Factories cast shadows
factory.receiveShadows = true;
shadowGenerator.addShadowCaster(factory);
// Add factory to array
factories.push(factory);
} else if (rand < 0.3) {
// Create garbage
var garbage = BABYLON.MeshBuilder.CreateSphere("garbage", {diameter: 1}, scene);
garbage.position.x = x + (Math.random() - 0.5) * (spacing / 3);
garbage.position.z = z + (Math.random() - 0.5) * (spacing / 3);
garbage.position.y = 0.5;
var garbageMaterial = new BABYLON.StandardMaterial("garbageMaterial", scene);
garbageMaterial.diffuseColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
garbage.material = garbageMaterial;
// Enable physics for garbage
garbage.physicsImpostor = new BABYLON.PhysicsImpostor(garbage, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 1, friction: 0.5, restitution: 0 }, scene);
// Garbages cast shadows
garbage.receiveShadows = true;
shadowGenerator.addShadowCaster(garbage);
// Add garbage to array
garbages.push(garbage);
} else if (rand < 0.8) {
// Create building
var width = minSize + Math.random() * (maxSize - minSize);
var height = minSize + Math.random() * (maxSize * 2 - 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 / 3);
building.position.z = z + (Math.random() - 0.5) * (spacing / 3);
building.position.y = height / 2;
// Create PBR material with reflections for buildings
var buildingMaterial = new BABYLON.PBRMaterial("buildingMaterial", scene);
buildingMaterial.albedoColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
buildingMaterial.reflectivityColor = new BABYLON.Color3(0.3, 0.3, 0.3); // Wet look
buildingMaterial.microSurface = 0.4; // Wet look
buildingMaterial.environmentTexture = scene.environmentTexture;
building.material = buildingMaterial;
// Enable collisions and physics for buildings
building.checkCollisions = true;
building.physicsImpostor = new BABYLON.PhysicsImpostor(building, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0.5, restitution: 0 }, scene);
// Buildings cast shadows
building.receiveShadows = true;
shadowGenerator.addShadowCaster(building);
// Add building to array for management
buildings.push(building);
}
// Else, leave empty space (could be a park or empty lot)
}
}
}
}
// Initial city generation
var cityRange = 100;
generateCity(cityRange);
// Add fog to the scene for depth perception
scene.fogMode = BABYLON.Scene.FOGMODE_EXP2;
scene.fogDensity = 0.001;
scene.fogColor = new BABYLON.Color3(0.9, 0.9, 0.85);
// Rain particle system
var rain = new BABYLON.ParticleSystem("rain", 10000, scene);
rain.particleTexture = new BABYLON.Texture("https://playground.babylonjs.com/textures/raindrop.png", scene);
rain.emitter = new BABYLON.Vector3(0, 100, 0); // Emitter position will follow the camera
rain.minEmitBox = new BABYLON.Vector3(-50, 0, -50);
rain.maxEmitBox = new BABYLON.Vector3(50, 0, 50);
// Rain particle properties
rain.color1 = new BABYLON.Color4(0.8, 0.8, 1.0, 0.5);
rain.color2 = new BABYLON.Color4(0.8, 0.8, 1.0, 0.5);
rain.colorDead = new BABYLON.Color4(0.8, 0.8, 1.0, 0);
rain.minSize = 0.1;
rain.maxSize = 0.2;
rain.minLifeTime = 0.8;
rain.maxLifeTime = 1.0;
rain.emitRate = 5000; // Increased emit rate
rain.blendMode = BABYLON.ParticleSystem.BLENDMODE_STANDARD;
rain.gravity = new BABYLON.Vector3(0, -9.81 * 5, 0); // Faster falling particles
rain.direction1 = new BABYLON.Vector3(-1, -10, -1);
rain.direction2 = new BABYLON.Vector3(1, -10, 1);
rain.minEmitPower = 1;
rain.maxEmitPower = 3;
rain.updateSpeed = 0.01;
rain.disposeOnStop = false;
rain.start();
// Variables to track movement
var isMoving = false;
var inputMap = {};
scene.actionManager = new BABYLON.ActionManager(scene);
scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyDownTrigger, function (evt) {
inputMap[evt.sourceEvent.key.toLowerCase()] = evt.sourceEvent.type === "keydown";
}));
scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyUpTrigger, function (evt) {
inputMap[evt.sourceEvent.key.toLowerCase()] = evt.sourceEvent.type === "keydown";
}));
var lastTime = 0;
// Create robots
function createRobots(numRobots) {
for (var i = 0; i < numRobots; i++) {
// Create robot mesh
var robot = BABYLON.MeshBuilder.CreateBox("robot", {size: 1}, scene);
robot.position = new BABYLON.Vector3(
camera.position.x + (Math.random() - 0.5) * 20,
0.5,
camera.position.z + (Math.random() - 0.5) * 20
);
var robotMaterial = new BABYLON.StandardMaterial("robotMaterial", scene);
robotMaterial.diffuseColor = new BABYLON.Color3(0.8, 0.8, 0.2);
robot.material = robotMaterial;
robot.checkCollisions = true;
robot.receiveShadows = true;
shadowGenerator.addShadowCaster(robot);
// Robot properties
robot.currentTask = "Idle";
robot.isBusy = false;
// Add robot to array
robots.push(robot);
}
}
// Create initial robots
createRobots(5);
// Create a GUI for robot status (using HTML element)
var robotStatusDiv = document.getElementById("robotStatus");
// Raycaster for detecting hovered robots
var pickedRobot = null;
var pickResult = null;
// Main loop
scene.registerBeforeRender(function () {
var position = camera.position;
// Update rain emitter position to be above the camera
rain.emitter = new BABYLON.Vector3(camera.position.x, camera.position.y + 50, camera.position.z);
// Update isMoving based on input keys
isMoving = inputMap["w"] || inputMap["a"] || inputMap["s"] || inputMap["d"];
// Camera bobbing effect
if (isMoving) { // Apply bobbing only when moving
var time = performance.now();
var deltaTime = time - lastTime;
lastTime = time;
var bobbingFrequency = 0.005; // Adjust speed of bobbing
var bobbingAmount = 0.02; // Adjust the amplitude of bobbing
camera.position.y += Math.sin(time * bobbingFrequency) * bobbingAmount;
} else {
lastTime = performance.now(); // Reset time when not moving
}
// 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
var newBuildings = [];
buildings.forEach(function(building) {
if (Math.abs(building.position.x) > cityRange || Math.abs(building.position.z) > cityRange) {
// Remove from shadow generator
shadowGenerator.removeShadowCaster(building);
building.dispose();
} else {
newBuildings.push(building);
}
});
buildings = newBuildings;
// Similarly update factories, garbages, and robots arrays
// ...
// Generate additional city
generateCity(cityRange);
}
// Robot logic
robots.forEach(function(robot) {
if (!robot.isBusy) {
// Assign a garbage to collect
if (garbages.length > 0) {
var garbage = garbages[0]; // Simple selection, could be improved
robot.targetGarbage = garbage;
robot.isBusy = true;
robot.currentTask = "Moving to Garbage";
}
} else if (robot.currentTask === "Moving to Garbage") {
// Move towards the garbage
var direction = robot.targetGarbage.position.subtract(robot.position).normalize();
robot.moveWithCollisions(direction.scale(0.05));
// Check if reached garbage
if (BABYLON.Vector3.Distance(robot.position, robot.targetGarbage.position) < 1) {
// Pick up garbage
robot.currentTask = "Carrying Garbage";
robot.carryingGarbage = robot.targetGarbage;
garbages.splice(garbages.indexOf(robot.targetGarbage), 1); // Remove garbage from array
robot.targetGarbage.isVisible = false; // Hide the garbage mesh
}
} else if (robot.currentTask === "Carrying Garbage") {
// Move towards the nearest factory
if (!robot.targetFactory) {
// Find the nearest factory
var nearestFactory = null;
var minDistance = Infinity;
factories.forEach(function(factory) {
var dist = BABYLON.Vector3.Distance(robot.position, factory.position);
if (dist < minDistance) {
minDistance = dist;
nearestFactory = factory;
}
});
robot.targetFactory = nearestFactory;
}
var direction = robot.targetFactory.position.subtract(robot.position).normalize();
robot.moveWithCollisions(direction.scale(0.05));
// Check if reached factory
if (BABYLON.Vector3.Distance(robot.position, robot.targetFactory.position) < (robot.targetFactory.scaling.x / 2 + 1)) {
// Drop off garbage
robot.currentTask = "Returning to Idle";
robot.carryingGarbage.dispose(); // Dispose of the garbage mesh
robot.carryingGarbage = null;
robot.targetFactory = null;
}
} else if (robot.currentTask === "Returning to Idle") {
// Return to idle state
robot.isBusy = false;
robot.currentTask = "Idle";
}
});
// Hover detection
var pickResult = scene.pick(scene.pointerX, scene.pointerY, function(mesh) {
return robots.indexOf(mesh) !== -1;
});
if (pickResult.hit) {
pickedRobot = pickResult.pickedMesh;
// Update UI
robotStatusDiv.style.display = "block";
robotStatusDiv.innerText = "Robot Status: " + pickedRobot.currentTask;
} else {
pickedRobot = null;
robotStatusDiv.style.display = "none";
}
});
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>
Explanation of the Updates:
-
Factories Generation:
- In the
generateCity function, factories are generated when a random value is less than 0.1. They are added to the factories array for later use.
-
Garbage Generation:
- Garbage objects (spheres) are generated when a random value is less than
0.3. They are added to the garbages array.
-
Robot Creation:
- A
createRobots function is introduced to create robots. Robots are simple box meshes with properties such as currentTask, isBusy, targetGarbage, etc.
- Robots are added to the
robots array.
-
Robot Behavior:
- Robots have a simple state machine:
- Idle: Waiting for a task.
- Moving to Garbage: Moving towards the assigned garbage.
- Carrying Garbage: After picking up garbage, moving towards the nearest factory.
- Returning to Idle: After dropping off garbage, returning to idle state.
- Movement is handled by moving the robot mesh towards its target using normalized direction vectors.
- When the robot reaches the garbage, it picks it up (hides the garbage mesh), and proceeds to the factory.
- Upon reaching the factory, the robot disposes of the garbage mesh and returns to idle.
-
UI for Robot Status:
- An HTML
<div> element with the ID robotStatus is added to display the robot's status.
- When the player hovers over a robot, a raycaster detects the intersection, and the UI displays the robot's current task.
- The UI is hidden when the cursor is not over a robot.
-
Mouse Hover Detection:
- In the
registerBeforeRender loop, a pick result checks if the mouse is over any robot mesh.
- If a robot is detected under the cursor, the
robotStatusDiv is displayed and updated with the robot's status.
- If not, the
robotStatusDiv is hidden.
Note: This implementation is a simplified version to demonstrate the requested features. In a production environment, you might want to improve:
How to Run:
- Save the code as an HTML file (e.g.,
index.html).
- Open the file in a modern web browser that supports Babylon.js (e.g., Chrome, Firefox).
- Use
W, A, S, D keys to move around.
- Move the mouse to look around (FPS controls).
- Hover over robots (small boxes) to see what they are doing.
Controls:
- Movement:
W - Move Forward
S - Move Backward
A - Move Left
D - Move Right
- Mouse:
- Move the mouse to look around.
- Hover over robots to see their status.
Enjoy exploring the city and watching the robots collect garbage and recycle them in factories!