Using HTML + JS (one file) make 3d water using Babylon.js, add waves
- User
Using HTML + JS (one file) make 3d water using Babylon.js, add waves
- Assistant
Using HTML + JS (one file) make 3d water using Babylon.js, add waves
Sure! Below is a complete HTML file that uses Babylon.js to create a 3D water scene with animated waves. This single-file implementation includes all necessary scripts via CDN and sets up a basic scene with a water plane that simulates waves.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Babylon.js 3D Water with Waves</title>
<!-- Include Babylon.js CDN -->
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<!-- Include Babylon.js Materials Library for Water Material -->
<script src="https://cdn.babylonjs.com/materialsLibrary/babylonjs.materials.min.js"></script>
<style>
/* Make the canvas fill the full screen */
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
</style>
</head>
<body>
<!-- Canvas Element -->
<canvas id="renderCanvas"></canvas>
<script>
// Get the canvas element
const canvas = document.getElementById("renderCanvas");
// Generate the Babylon 3D engine
const engine = new BABYLON.Engine(canvas, true);
// Create the scene
const createScene = function () {
const scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color3(0.8, 0.9, 1); // Light blue sky
// Create a camera and attach it to the canvas
const camera = new BABYLON.ArcRotateCamera("Camera",
BABYLON.Tools.ToRadians(45),
BABYLON.Tools.ToRadians(60),
50,
new BABYLON.Vector3(0, 0, 0),
scene);
camera.attachControl(canvas, true);
// Add a hemispheric light
const hemiLight = new BABYLON.HemisphericLight("hemiLight",
new BABYLON.Vector3(0, 1, 0),
scene);
hemiLight.intensity = 0.9;
// Create a ground plane for the water
const ground = BABYLON.MeshBuilder.CreateGround("ground",
{width: 100, height: 100},
scene);
// Import the water material from the materials library
const waterMaterial = new BABYLON.WaterMaterial("waterMaterial", scene, new BABYLON.Vector2(512, 512));
// Set water material properties
waterMaterial.backFaceCulling = true;
waterMaterial.bumpTexture = new BABYLON.Texture("https://assets.babylonjs.com/environments/waterbump.png", scene);
waterMaterial.windForce = -10;
waterMaterial.waveHeight = 0.5;
waterMaterial.bumpHeight = 0.1;
waterMaterial.windDirection = new BABYLON.Vector2(1, 1);
waterMaterial.waveLength = 0.1;
waterMaterial.colorBlendFactor = 0;
waterMaterial.addToRenderList(ground); // Reflect the ground in water
// Apply the water material to the ground mesh
ground.material = waterMaterial;
// Optional: Add a simple skybox for better visuals
const skybox = BABYLON.MeshBuilder.CreateBox("skyBox", {size:1000.0}, scene);
const skyboxMaterial = new BABYLON.StandardMaterial("skyBoxMaterial", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.disableLighting = true;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("https://playground.babylonjs.com/textures/skybox", scene);
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skybox.material = skyboxMaterial;
return scene;
};
const scene = createScene();
// Register a render loop to repeatedly render the scene
engine.runRenderLoop(function () {
scene.render();
});
// Watch for browser/canvas resize events
window.addEventListener("resize", function () {
engine.resize();
});
</script>
</body>
</html>
HTML Structure and Styling
<canvas> element with id="renderCanvas" is where Babylon.js will render the 3D scene.Including Babylon.js and Materials Library
https://cdn.babylonjs.com/babylon.js.WaterMaterial, which provides realistic water effects.Creating the Scene
ArcRotateCamera is used, allowing the user to rotate around the scene interactively. The camera is attached to the canvas for user controls.HemisphericLight simulates ambient lighting, giving the scene a natural illumination.Water Surface
ground) is created to represent the water surface.WaterMaterial is applied to the ground. This material simulates water by using a bump texture and various wave parameters:
addToRenderList method ensures that the ground is reflected in the water surface for added realism.Skybox (Optional Enhancement)
Render Loop and Responsiveness
engine.runRenderLoop continuously renders the scene, allowing animations like waves to be visible.Wave Parameters: Adjust the waveHeight, waveLength, windForce, and windDirection properties of the waterMaterial to achieve different wave behaviors.
Textures: You can replace the bumpTexture URL with your own water bump textures to change the appearance of the waves.
Lighting and Skybox: Modify the lighting intensity or use different skybox textures to create various times of day or atmospheric conditions.
Save the Code: Copy the above code into a file and save it with an .html extension, for example, waterScene.html.
Open in Browser: Double-click the saved HTML file or open it in your favorite web browser.
Interact: Use your mouse to rotate around the scene. You should see a water surface with animated waves reflecting the sky.
Feel free to tweak the parameters and add more elements to the scene to better suit your needs!
Not yet included in a published release.