Using WebGL and some JG library create HTML file with realistic water simulation.
- User
Using WebGL and some JG library create HTML file with realistic water simulation.
- Assistant
Using WebGL and some JG library create HTML file with realistic water simulation.
Certainly! Below is an example of an HTML file that uses WebGL and the Three.js library (a popular JavaScript library for 3D graphics) to create a realistic water simulation.
HTML File:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebGL Water Simulation</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<!-- Include Three.js Library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/three.min.js"></script>
<!-- Include OrbitControls for camera manipulation -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/examples/js/controls/OrbitControls.js"></script>
<!-- Include Water Shader -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/examples/js/objects/Water.js"></script>
<!-- Include Skybox utilities -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/examples/js/loaders/CubeTextureLoader.js"></script>
<script>
let scene, camera, renderer, controls, water, sun, sky;
init();
animate();
function init() {
// Create scene
scene = new THREE.Scene();
// Create camera
camera = new THREE.PerspectiveCamera(
55,
window.innerWidth / window.innerHeight,
1,
20000
);
camera.position.set(30, 30, 100);
// Create renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);
// Add orbit controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.maxPolarAngle = Math.PI * 0.495;
controls.target.set(0, 10, 0);
controls.minDistance = 40.0;
controls.maxDistance = 200.0;
controls.update();
// Add sun light
sun = new THREE.Vector3();
// Water geometry
const waterGeometry = new THREE.PlaneGeometry(10000, 10000);
// Water
water = new THREE.Water(waterGeometry, {
textureWidth: 512,
textureHeight: 512,
waterNormals: new THREE.TextureLoader().load(
'https://threejs.org/examples/textures/waternormals.jpg',
function (texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
}
),
alpha: 1.0,
sunDirection: new THREE.Vector3(),
sunColor: 0xffffff,
waterColor: 0x001e0f,
distortionScale: 3.7,
fog: scene.fog !== undefined
});
water.rotation.x = -Math.PI / 2;
scene.add(water);
// Skybox
const sky = new THREE.CubeTextureLoader().load([
'https://threejs.org/examples/textures/cube/skybox_px.jpg',
'https://threejs.org/examples/textures/cube/skybox_nx.jpg',
'https://threejs.org/examples/textures/cube/skybox_py.jpg',
'https://threejs.org/examples/textures/cube/skybox_ny.jpg',
'https://threejs.org/examples/textures/cube/skybox_pz.jpg',
'https://threejs.org/examples/textures/cube/skybox_nz.jpg'
]);
scene.background = sky;
// Resize listener
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
const time = performance.now() * 0.001;
water.material.uniforms['time'].value += 1.0 / 60.0;
renderer.render(scene, camera);
}
</script>
</body>
</html>
Explanation of the Code:
Including Libraries:
Scene Setup:
Controls:
Water Creation:
Skybox:
Animation Loop:
render() at every frame.Event Listeners:
Notes:
Textures: The URLs used for textures (like water normals and skybox images) are hosted on the Three.js website. Ensure you have internet connectivity when running the code, or download the textures and serve them locally.
Customization:
waterColor, distortionScale, and sunColor to change the appearance.camera.position.set() to change the initial view.controls.minDistance, controls.maxDistance, and controls.maxPolarAngle to refine how the user can navigate the scene.Performance:
Running the Simulation:
Save the Code: Copy the HTML code into a file and save it with a .html extension (e.g., water_simulation.html).
Open in Browser: Open the HTML file in a modern web browser like Chrome or Firefox.
Interact:
Further Enhancements:
Add Floating Objects: Introduce 3D models or shapes that interact with the water surface.
Dynamic Environment: Implement a dynamic skybox that changes based on the time of day.
User Controls: Add GUI controls (using libraries like dat.GUI) to allow real-time adjustments of water properties.
Physics Integration: Incorporate physics engines (like Cannon.js) for realistic object-water interactions.
Feel free to explore and modify the code to suit your needs. If you have any questions or need further assistance, don't hesitate to ask!