// The bay: a wide plane whose normals ripple with a few travelling waves, so the low sun glints // on it and the sky's reflection breaks up. Waves move with game time, not wall time, so a // screenshot at t seconds always shows the same water. import * as THREE from "three"; import { Water } from "three/addons/objects/Water.js"; import { BAY } from "../palette.js"; import { SUN_DIR } from "./sky.js"; /** A tiling normal map for small waves, made from sine waves with whole-number frequencies so * its edges meet. No texture file, and the same every time. */ function waveNormals(size = 256) { const waves = [[3, 1, 0.5, 0.3], [1, 4, 0.36, 1.7], [5, -2, 0.26, 2.9], [-7, 3, 0.16, 4.1], [9, 7, 0.1, 0.8], [13, -11, 0.06, 5.3], [-17, 5, 0.05, 2.2], [23, 19, 0.03, 3.6]]; const data = new Uint8Array(size * size * 4); for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) { let dx = 0; let dy = 0; for (const [kx, ky, a, ph] of waves) { const c = Math.cos(2 * Math.PI * ((kx * x) / size + (ky * y) / size) + ph); dx += a * kx * c; dy += a * ky * c; } const n = new THREE.Vector3(-dx * 0.09, -dy * 0.09, 1).normalize(); const i = (y * size + x) * 4; data[i] = (n.x * 0.5 + 0.5) * 255; data[i + 1] = (n.y * 0.5 + 0.5) * 255; data[i + 2] = (n.z * 0.5 + 0.5) * 255; data[i + 3] = 255; } const tex = new THREE.DataTexture(data, size, size, THREE.RGBAFormat); tex.wrapS = tex.wrapT = THREE.RepeatWrapping; tex.minFilter = THREE.LinearMipmapLinearFilter; tex.magFilter = THREE.LinearFilter; tex.generateMipmaps = true; tex.needsUpdate = true; return tex; } export function buildWater(scene, env, { reflect = false } = {}) { if (reflect) { // The real thing: the bay reflects the piers, the bridge and the skyline, broken up by // small waves, with the low sun glittering on it. const water = new Water(new THREE.PlaneGeometry(80000, 80000), { textureWidth: 1024, textureHeight: 1024, waterNormals: waveNormals(), sunDirection: SUN_DIR.clone(), sunColor: 0xffd9a8, waterColor: 0x1d4a63, distortionScale: 2.4, alpha: 1, fog: true, }); water.rotation.x = -Math.PI / 2; water.renderOrder = -5; water.material.uniforms.size.value = 2.2; scene.add(water); return { mesh: water, update(t) { water.material.uniforms.time.value = t * 0.55; }, }; } const uniforms = { uTime: { value: 0 } }; // Its own copy of the sky to reflect, at full strength (the scene's is turned down). const material = new THREE.MeshStandardMaterial({ color: BAY, roughness: 0.12, metalness: 0.0, envMap: env, envMapIntensity: 1.1 }); material.onBeforeCompile = (shader) => { shader.uniforms.uTime = uniforms.uTime; shader.vertexShader = shader.vertexShader .replace("#include ", "#include \nvarying vec3 vSea;") .replace("#include ", "#include \nvSea = (modelMatrix * vec4(transformed, 1.0)).xyz;"); shader.fragmentShader = shader.fragmentShader .replace("#include ", "#include \nvarying vec3 vSea;\nuniform float uTime;") .replace( "#include ", /* glsl */ `#include { vec2 p = vSea.xz; float dist = length(cameraPosition.xz - p); float fade = exp(-dist / 900.0); vec2 g = vec2(0.0); vec2 d1 = normalize(vec2(0.8, 0.6)); float ph1 = dot(d1, p) * 0.21 + uTime * 1.3; g += d1 * cos(ph1) * 0.21 * 0.09; vec2 d2 = normalize(vec2(-0.3, 1.0)); float ph2 = dot(d2, p) * 0.37 + uTime * 1.7; g += d2 * cos(ph2) * 0.37 * 0.05; vec2 d3 = normalize(vec2(1.0, -0.4)); float ph3 = dot(d3, p) * 0.83 + uTime * 2.6; g += d3 * cos(ph3) * 0.83 * 0.022; vec2 d4 = normalize(vec2(-0.7, -0.7)); float ph4 = dot(d4, p) * 1.9 + uTime * 3.9; g += d4 * cos(ph4) * 1.9 * 0.008; vec2 d5 = normalize(vec2(0.2, -1.0)); float ph5 = dot(d5, p) * 0.05 + uTime * 0.5; g += d5 * cos(ph5) * 0.05 * 0.5; g *= mix(0.25, 1.0, fade); vec3 nw = normalize(vec3(-g.x, 1.0, -g.y)); normal = normalize((viewMatrix * vec4(nw, 0.0)).xyz); }`, ) .replace( "#include ", /* glsl */ `#include { float dist = length(cameraPosition.xz - vSea.xz); diffuseColor.rgb = mix(diffuseColor.rgb * 0.8, diffuseColor.rgb * 1.08 + vec3(0.02, 0.03, 0.03), smoothstep(80.0, 1600.0, dist)); }`, ); }; material.customProgramCacheKey = () => "claudia-bay"; const mesh = new THREE.Mesh(new THREE.PlaneGeometry(80000, 80000, 1, 1), material); mesh.rotation.x = -Math.PI / 2; mesh.receiveShadow = true; mesh.renderOrder = -5; scene.add(mesh); return { mesh, update(t) { uniforms.uTime.value = t; }, }; }