Create a single HTML file using three.js r160 via importmap. Procedural ocean waves via vertex displacement (shader or CPU), low-poly boat, sun, sky gradient, clouds, fog, orbiting camera.
Plan:
- importmap for three.
- Scene with fog, large skydome or background gradient via shader.
- Ocean: PlaneGeometry with a custom ShaderMaterial doing Gerstner-ish waves in the vertex shader, colored in the fragment with lighting (pseudo-normal calculation). Or use onBeforeCompile. Easier: custom ShaderMaterial with lighting computed from wave derivatives.
- Boat: low-poly hull from geometry (maybe custom), mast, triangular sail via BufferGeometry. Bobbing: sample the same wave function in JS to get height/rotation.
- Sun: circle mesh with a shader or sprite, gradient, placed near the horizon.
- Clouds: low-poly blobby clusters of icosahedrons, drifting.
- Camera slowly orbits, looking at the boat.
Write the wave function to be shared: define GLSL waveHeight function, mirror in JS for boat bobbing.
Ocean shader: vertex displacement with sum of sine/Gerstner waves; compute normal by finite differences; fragment: mix deep/shallow color, specular from sun direction, fog.
Sky: large sphere with gradient shader (BackSide), colors from orange horizon to deep blue zenith.
Sun: circular mesh with radial gradient shader, placed far away, maybe additive blending. Also glow sprite via canvas texture? No external assets — canvas-generated textures are procedural so allowed. But keep simple with shader circle.
Clouds: groups of flat-shaded icosahedrons with MeshStandardMaterial? Need lights. Use MeshLambert/Standard with directional light + ambient. Or flat colored MeshBasicMaterial for stylized look. Use MeshStandardMaterial with flatShading and lights — but ocean is ShaderMaterial so lights only affect clouds/boat. Boat: MeshStandardMaterial flatShading with directional + ambient light.
Fog: match scene.fog color to horizon; ocean shader should apply fog manually — include fog chunks. Easier: manually compute fog in ocean shader using uniforms. Or use fog: true and include fog_pars chunks in ShaderMaterial. I'll manually add fog in ocean fragment shader.
Waves function (shared): height = sum of directional sine waves:
h(x,z,t) = A1*sin(dot(d1,(x,z))*f1 + t*s1) + ...
GLSL:
float wave(vec2 p, float t){
float h = 0.0;
h += 0.35 * sin(p.x*0.12 + t*0.9);
h += 0.25 * sin(p.y*0.15 - t*0.7 + p.x*0.05);
h += 0.15 * sin((p.x+p.y)*0.2 + t*1.3);
h += 0.1 * sin(p.x*0.4 - p.y*0.3 + t*2.0);
...
}
JS mirror with same numbers.
Ocean plane: 400x400 with 200x200 segments, rotated -PI/2. Displacement in vertex shader, normal via finite differences.
Fragment: color mix deep blue and warm color based on height, sun specular (Blinn), fresnel-ish toward horizon glow, fog.
Boat: hull from a stretched box with tapered bow — build custom BufferGeometry, or use primitives: hull = BoxGeometry scaled + cone for bow? Better: custom hull via ExtrudeGeometry? Keep simple: hull built with BufferGeometry from vertices — make a simple low-poly hull: define shape points. Or use CylinderGeometry rotated with radialSegments 4... let me make custom hull: define cross sections. Quick approach: create hull with scaled box, then rotate vertices to taper front. Actually simplest polished look: hull = flattened elongated octahedron-ish. Let me make custom hull geometry:
Vertices: bottom keel line and top deck. Define hull as lofted shape with 3 cross sections (stern, mid, bow). Write positions array manually.
Cross sections (x = length, y, z = width):
Stern x=-1.2: width 0.45, deck y=0.25
Mid x=0: width 0.6
Bow x=1.4: width ~0, point.
Each cross-section: points: deck left (-w,0.25), waterline... keep simple hexagonal cross section: top-left, top-right, mid-left(-w*1.0, 0.05)... let me just do: 4 points per section: (-w,0.3),(w,0.3),(w*0.6,-0.25),(-w*0.6,-0.25) — trapezoidal. Bow section: single point (1.6,0.3,0),(1.6,-0.2,0)? Just collapse width to 0.02.
Then build faces between consecutive sections. Use flatShading, compute normals.
Mast: thin cylinder. Sail: triangle geometry (BufferGeometry with 3 vertices, DoubleSide). Boom maybe skip. Add small flag.
Boat bobbing: sample wave height at boat position, set y and rotation (pitch/roll from finite differences).
Clouds: 5-6 cloud groups, each 3-5 flattened icosahedrons, MeshStandardMaterial whiteish flat-shaded, drifting slowly in x, wrap around.
Sun: place a large circle at the horizon in the direction the camera faces often — put it at fixed world position; camera orbits so sun visible part of time; that's fine. Sun: CircleGeometry with shader radial gradient (warm core to transparent), plus maybe glow ring. Use additive blending, depthWrite false.
Sky gradient: scene.background can't gradient; use large sphere BackSide with shader gradient based on world y direction. Colors: horizon orange (#ff9a5a) to zenith deep blue (#1a2a5a), also add a warm glow near sun direction? Keep gradient vertical with sun tint added.
Camera orbit: radius ~14, height ~4, lookAt (0,1,0) slow.
Fog: scene.fog = new THREE.Fog(0xff9a6a-ish, 30, 180)? Ocean shader manual fog with same color/near/far. Clouds use standard material so scene.fog applies. Sky sphere should not be fogged — set material fog false (ShaderMaterial default no fog). Boat standard material fogged - fine.
Tone mapping: renderer.toneMapping = ACESFilmic, outputColorSpace default sRGB.
Let me write the code.
Ocean vertex shader:
uniform float uTime;
varying vec3 vWorldPos; varying float vHeight; varying vec3 vNormal;
float waveH(vec2 p, float t){
float h = 0.0;
h += 0.40 * sin(p.x*0.14 + t*0.8);
h += 0.30 * sin(p.y*0.18 - t*0.6 + p.x*0.06);
h += 0.18 * sin((p.x+p.y)*0.22 + t*1.2);
h += 0.08 * sin(p.x*0.45 - p.y*0.35 + t*1.9);
h += 0.06 * sin((p.x-p.y)*0.6 + t*2.6);
return h;
}
vec2 p = position.xy (plane before rotation); actually easier: create plane, rotateX(-PI/2) on geometry itself so positions are x,z with y up, displace y. Then worldPos = position (mesh at origin).
Vertex:
vec3 pos = position;
vec2 p = pos.xz;
float h = waveH(p, uTime);
pos.y += h;
// finite diff normal
float e = 0.6;
float hx = waveH(p+vec2(e,0),uTime) - waveH(p-vec2(e,0),uTime);
float hz = waveH(p+vec2(0,e),uTime) - waveH(p-vec2(0,e),uTime);
vNormal = normalize(vec3(-hx/(2.0*e), 1.0, -hz/(2.0*e)));
vWorldPos = pos;
gl_Position = ...
Fragment:
uniform vec3 uSunDir; uniform vec3 uCamPos; fog uniforms.
vec3 N = normalize(vNormal);
vec3 V = normalize(cameraPosition - vWorldPos);
float sunDiff = max(dot(N,uSunDir),0.0);
vec3 base = mix(uDeepColor, uShallowColor, smoothstep(-0.6,0.8,vWorldPos.y));
// sun color reflection / specular
vec3 H = normalize(uSunDir + V);
float spec = pow(max(dot(N,H),0.0), 90.0);
// fresnel toward sky
float fres = pow(1.0 - max(dot(N,V),0.0), 3.0);
vec3 sky = uSkyColor;
vec3 col = mix(base, sky, fres*0.6);
col += uSunColor * spec * 2.0;
// warm reflection streak toward sun: use horizontal direction alignment
col += uSunColor * pow(max(dot(normalize(vec3(V.x,0,V.z)) * -1.0 ...
Keep simpler: add sun glitter: spec is enough plus warm tint by wave height. Fog:
float fogF = smoothstep(uFogNear, uFogFar, length(cameraPosition - vWorldPos));
col = mix(col, uFogColor, fogF);
Sun direction: normalize(vec3(-0.4, 0.12, -1)) roughly, place sun mesh along it at distance.
Sun mesh: circle radius ~12 at position sunDir * 300? Camera far plane 1000. Sky sphere radius 400. Sun at distance 350, but sky sphere is 400 BackSide — sun in front. Sun circle lookAt camera each frame? Use a shader circle with radial falloff, additive; billboard: sunMesh.lookAt(camera.position) each frame or use sprite-like: set quaternion = camera quaternion.
Clouds: MeshStandardMaterial flatShading, color slightly warm white; add emissive slight. Lit by directional light from sun direction and hemisphere light.
Lights: DirectionalLight warm from sun dir, HemisphereLight (sky blue / warm ground).
Boat motion:
function waveJS(x,z,t) mirrors GLSL.
boat.position.y = waveJS(bx,bz,t)*0.9 - 0.05;
pitch/roll from derivatives: sample at ±0.8.
boat.rotation.z = -(h2-h1)/(2*e)*... set rotation order; boat faces +x direction? Set boat group rotation.y = fixed heading; apply pitch on local x? Easier: compute h at bow/stern and port/starboard, set rotation.z (roll, around forward axis) and rotation.x. If boat forward is +x, then pitch around z axis: rotation.z = atan2 of slope along x. Roll around x: rotation.x = ... Sign: positive slope along x means bow up → rotation.z positive? For forward +x, rotation about z by angle θ lifts +x end? Rotation about z: x axis rotates toward y for positive θ. So rotation.z = atan(slopeX)? slope = (h(x+e)-h(x-e))/(2e). Bow up means rotation.z = +slope... positive rotation about z takes +x toward +y, so yes rotation.z ≈ slopeX? Actually positive rotation about z: point (1,0,0) → (cosθ, sinθ,0), y positive. So rotation.z = slopeX? slope positive means bow higher, θ positive. But wave slope sign: h increases with x → bow rises → rotation.z = atan(slopeX). Hmm but that gives bow up when slope positive: yes.
Roll about x: positive rotation about x takes +z toward... (0,0,1)→(0,-sinθ? ) Rotation about x by θ: y→ y cosθ - z sinθ? Standard: rotate about x: y' = y cosθ - z sinθ, z' = y sinθ + z cosθ. Point (0,0,1) → (0,-sinθ, cosθ). So positive θ lowers starboard (+z). If slope along z is positive (h increases with z), starboard should rise → θ negative: rotation.x = -slopeZ.
Good enough; small values anyway.
Also boat bobs at origin, camera orbits around origin — fine.
Clouds drift: each cloud group position.x += speed*dt; wrap at ±250. Place clouds at y 25-45, spread.
Renderer setup, resize handler, clock.
Sky shader:
varying vec3 vDir (world position normalized).
color = mix(horizon, zenith, pow(max(dir.y,0),0.55)); below horizon fade to fog color. Also add sun glow: glow = pow(max(dot(dir, uSunDir),0),8) * warm.
Sky material: side BackSide, depthWrite false, fog false.
Let me write the HTML.
Boat geometry code:
function makeHull(){
const sections = [
{x:-1.3, w:0.42, d:0.34, k:0.30},
{x:-0.4, w:0.55, d:0.42, k:0.28},
{x:0.5, w:0.5, d:0.36, k:0.22},
{x:1.3, w:0.18, d:0.2, k:0.1},
];
Each section 4 points: (-w, d),(w,d),(k? ...
Cross-section points: top-left(-w,d), top-right(w,d), bottom-right(k, -0.35), bottom-left(-k,-0.35)? K is keel half-width. Build quads between sections, plus caps at stern. Use non-indexed with computeVertexNormals with flatShading, or just computeVertexNormals on indexed; flatShading material handles faceted look.
Positions: for each section i, points p0..p3. Faces between section i and i+1: 4 quads (wrap). Stern cap: fan of last section. Bow: last section is small but not degenerate; add a point at tip (1.7, 0.28, 0) and fan.
Deck: top face quad — include top quad strip (top-left to top-right) as deck face, maybe slightly different color. Single material ok; add deck as separate thin box? Keep single material wood brown, flatShading.
Sail: main triangle: mast at x=0.1, height 2.2. Sail vertices: (0.12,0.45,0),(0.12,2.6,0),(1.35,0.5,0) — triangle in x-y plane, DoubleSide, color warm white. Also jib on front? Boat forward is +x; mainsail aft of mast... anyway stylized. Add small red pennant triangle at mast top.
Mast: CylinderGeometry(0.03,0.045,2.3,5) at x=0.1, y center 0.45+1.15.
Keep boat group scale ~1. Position boat group at origin, y handled per frame.
Also add subtle boat heading rotation.y = sin(t*0.1)*0.1? Keep mostly fixed heading with slight yaw.
Camera: radius 13, y = 4.5 + sin slow, angle = t*0.05. lookAt(0,1.2,0).
Sun position: direction normalize(-0.35, 0.10, -1.0). Place sun at dist 320 → y ~32ish, near horizon. Sun disc radius 22.
Also add reflection path on water toward sun in fragment:
vec3 toSunHoriz = normalize(vec3(uSunDir.x,0,uSunDir.z));
float align = pow(max(dot(normalize(vec3(V.x,0,V.z)), -toSunHoriz),0.0),?) hmm: V points from surface to camera; -toSun... path of glitter is where surface reflects sun toward camera. Approx: float glitter = pow(max(dot(normalize(vWorldPos - cameraPosition) ... skip complexity; spec + fresnel enough. Maybe add broad highlight: col += uSunColor * pow(max(dot(reflect(-V,N), uSunDir),0),8)*0.3.
Fog color should match sky horizon color for seamless blend: fog color 0xf2a06a-ish. Sky horizon color same.
Cloud color: MeshStandardMaterial({color:0xffd9c0? flatShading, roughness 1}) — lit by warm directional light, nice.
Clouds should be affected by fog too — yes standard material with scene.fog.
Let me write it all.
Ocean plane segments: 256x256 for 400 size = 65k verts, fine.
Ocean size 500, fog far ~200 so edges hidden by fog and sky.
Water color uniforms: deep 0x0a2a4a → maybe deep teal #06283d, shallow warm #1b6f8a; sun color #ffb36b.
Write final HTML.
importmap:
{"imports":{"three":"https://unpkg.com/three@0.160.0/build/three.module.js"}}
Module script imports three.
Clouds: create makeCloud() function: group of 4-6 icosahedrons (detail 0) with random offsets, flattened scale y 0.5. Merge? Just group meshes sharing material.
Drift: store speed per cloud.
Also slight emissive on clouds: emissive 0x224455? keep with hemisphere light.
Lights: hemi = HemisphereLight(0x8fb8e8, 0xff9a5a, 0.7); dir = DirectionalLight(0xffc080, 1.2) position sunDir*100.
renderer.toneMapping = ACESFilmicToneMapping; toneMappingExposure 1.1.
Ocean material tone mapping: ShaderMaterial not tone mapped by default? ShaderMaterial: tonemapping applied only if material.toneMapped true — for ShaderMaterial, tone mapping fragment chunk not auto-included. Fine, colors as authored. But boat standard material will be tone mapped; slight mismatch ok. Actually to keep consistent, set renderer without tone mapping and pick colors directly. Easier: keep ACES and pick colors; fine either way. Let me use ACES and adjust.
Hmm, colors passed as uniforms are in linear? THREE.Color set from hex is sRGB → in r160 with renderer.outputColorSpace default SRGB, Color hex is converted? Color stores as given; renderer converts uniform via color space? For built-in materials colors are used as-is; sRGB→linear conversion happens in shader only for textures/color via color management. In r160, THREE.ColorManagement.enabled = true by default, and renderer converts... For ShaderMaterial uniforms of Color type, no automatic conversion. To be safe, use .convertSRGBToLinear() on colors passed to shaders? Trivial; just tune visually. Set ColorManagement default and call convertSRGBToLinear on shader colors for accuracy. Actually simpler: leave, tune colors; output looks fine either way.
Let me write the code carefully.
Sun glow shader for sun disc:
varying vec2 vUv; frag: d = length(vUv-0.5)*2; core = smoothstep(1.0,0.0,d); col = mix(edge orange, core yellow); alpha = smoothstep(1.0,0.2,d); Use additive blending, transparent, depthWrite false. Circle geometry 1x1 plane with these uvs. Scale to 40.
Also add a larger glow plane behind: same shader with softer falloff, scale 140, low alpha. Combine into one shader with uniforms? Just make two meshes with same material but different scale — alpha profile same; fine, or make material with uniform softness. Just use two materials.
Billboard: sunGroup.quaternion.copy(camera.quaternion) each frame.
Now writing the file.
JS wave function:
function waveH(x,z,t){
return 0.40*Math.sin(x*0.14+t*0.8)
+0.30*Math.sin(z*0.18 - t*0.6 + x*0.06)
+0.18*Math.sin((x+z)*0.22 + t*1.2)
+0.08*Math.sin(x*0.45 - z*0.35 + t*1.9)
+0.06*Math.sin((x-z)*0.6 + t*2.6);
}
GLSL mirrors.
Boat bobbing:
const e=0.9;
h = waveH(0,0,t);
slopeX = (waveH(e,0,t)-waveH(-e,0,t))/(2e);
slopeZ = (waveH(0,e,t)-waveH(0,-e,t))/(2e);
boat.position.y = h*0.85 - 0.02;
boat.rotation.z = Math.atan(slopeX)*0.8; wait sign: bow at +x. Positive slope → bow higher → rotation.z positive as derived. But rotation.z positive rotates +x toward +y: yes.
boat.rotation.x = -Math.atan(slopeZ)*0.8; check: slope along z positive → h higher at +z → starboard(+z) higher → starboard rises → +z point should have higher y: rotation about x by θ: (0,0,1)→(0,-sinθ,cosθ). For +z to rise, need -sinθ>0 → θ<0. So rotation.x = -atan(slopeZ). ✓.
Also slight yaw: boat.rotation.y = 0.3 + sin(t*0.15)*0.08. rotation order default XYZ; fine.
Camera slow orbit; height oscillation.
Clouds count 7, radius 60-200 from center, y 20-50.
Ocean fragment fog uniforms: uFogColor, uFogNear=40, uFogFar=220. scene.fog Fog(color,40,220) for clouds/boat.
Sky sphere radius 500, camera far 2000.
Sky frag:
varying vec3 vWorld;
main: vec3 d = normalize(vWorld);
float y = d.y;
vec3 col = mix(uHorizon, uZenith, pow(clamp(y,0.0,1.0), 0.6));
below horizon: mix to uHorizon slightly darker: if y<0, col = mix(uHorizon*0.8, col, smoothstep(-0.2,0.0,y));
sun glow: float s = max(dot(d, uSunDir),0.0); col += uSunGlow * (pow(s, 20.0)*0.8 + pow(s,4.0)*0.25);
Colors: zenith #16294f... let me pick: zenith vec3(0.07,0.12,0.3), horizon vec3(0.98,0.55,0.3), glow vec3(1.0,0.45,0.2).
Ocean fog color should match horizon: 0xf58c4f-ish.
OK code.
Water fragment also needs cameraPosition — built-in available in fragment? cameraPosition is available in vertex; in fragment need to declare uniform vec3 cameraPosition? Three.js prepends common uniforms in fragment too? Three.js fragment shader prefix includes `uniform vec3 cameraPosition`? Yes — WebGLProgram adds cameraPosition to both vertex and fragment prefixes. Actually fragment prefix includes "uniform vec3 cameraPosition;"? Let me recall: fragment prefix has viewMatrix and cameraPosition. Yes, three.js fragment shader prefix includes `uniform vec3 cameraPosition;`? I believe so (used by fog etc? fog uses vFogDepth). Hmm. In three's ShaderChunk common, cameraPosition used only in vertex. To be safe, pass uCamPos uniform updated each frame. Let's do that.
Now writing final.
Also boat flag: small triangle, maybe flapping with vertex — skip, static.
Add slight foam near crest: in water fragment, foam = smoothstep(0.5,0.9,vHeight)*noise? No noise texture; use sin-based sparkle. Add crest brightening: col += vec3(1.0,0.6,0.4)*smoothstep(0.55,0.95,vWorldPos.y)*0.35. Nice warm crest highlight.
Let me write.
const canvas append; antialias true.
Cloud material: MeshStandardMaterial({color:0xfff0e6? flatShading true, roughness 1, metalness 0}). Under warm light looks peachy.
Cloud geometry: IcosahedronGeometry(r, 0) random scale per blob.
Boat hull material: MeshStandardMaterial({color:0x8a4b2a, flatShading:true, roughness:0.8}). Sail: 0xf7ede0 DoubleSide flat. Mast: darker wood.
Hull geometry build:
const secs=[[-1.3,0.42,0.34,0.30],[-0.4,0.55,0.45,0.26],[0.5,0.48,0.38,0.18],[1.3,0.16,0.22,0.05]];
Each sec: [x, w, deckY, keelHalf]
Points per section: A(-w,deck), B(w,deck), C(kh,-0.30), D(-kh,-0.30) in (x, y, z=±). z coords: A z=-w? Let me define cross-section in z-y: top-left(-w, deck), top-right(w, deck), bottom-right(kh,-0.3), bottom-left(-kh,-0.3).
positions: for each section push 4 vertices: (x, deck, -w),(x,deck,w),(x,-0.3,kh),(x,-0.3,-kh).
Faces between sec i,i+1: quad (0,1) top? Order: index array per pair: [a0,b0,b1,a1] etc for each side. Write generic quad function pushing 2 triangles.
Top deck: quad between top-left and top-right of consecutive sections: face (0_i, 1_i, 1_j, 0_j). Sides: right side (1_i,2_i,2_j,1_j), bottom (2,3), left (3,0). Bow tip: add vertex T=(1.75,0.30,0), fan from last section: (b0? ) triangles: (3_3, 2_3, T)? and (1_3? ...). Last section order 0,1,2,3; fan: triangles (3,T,0)? orientation whatever, use DoubleSide? flat shading with computeVertexNormals — if orientation wrong normals inward. Use material side: FrontSide but risk. Just set hull material side: THREE.DoubleSide for safety? flatShading with DoubleSide works. OK.
Stern cap: quad of first section: (0,1,2,3) — also DoubleSide covered.
Simplify: hull material DoubleSide.
Sail geometry: BufferGeometry with vertices: mast at x=0.05. Main triangle: (0.1,0.5,0),(0.1,2.7,0),(1.5,0.55,0). Actually sail should billow; static planar fine stylized. Add slight curve? Keep planar, DoubleSide. Jib: forward of mast: (0.15,0.5,0),(0.15,2.2,0),(1.6,0.5,0) overlaps main... skip jib, add boom? Add small red flag at top: triangle (0.1,2.7,0),(0.1,2.5,0),(0.45,2.62,0) color crimson DoubleSide.
Mast cylinder radius .035/.05 height 2.35, position x=0.1, y = 0.35+2.35/2 ≈ 1.5? Deck top 0.45; mast base y 0.3, center y = 0.3+1.175.
Boat group: hull mesh, mast, sail, flag. Position group at origin, y per frame.
Also small wake? skip.
Camera:
const R=13; ang = t*0.06; cam.position.set(cos*R, 4.2+ sin(t*0.11)*0.8, sin*R); lookAt(0,1.0,0).
Sun direction: new THREE.Vector3(-0.45,0.10,-1).normalize(). Sun mesh at sunDir*420 but sky sphere 500 — sun inside sky, good. Sun scale: disc plane 60, glow 220.
Clouds shouldn't intersect sky: place y 20-45, radius 80-260.
Fog far 220 hides clouds beyond? Clouds at 260 fully fogged → invisible pop on wrap; wrap at ±260 with fog far 220, pop hidden. Good.
Now write final HTML.