web-ocean README

项目的 README,来自 github.com/Noniv/web-ocean。合并为一个 markdown 文件:README.md。

web-ocean

A real-time ocean renderer written from scratch on WebGPU, with no engine or rendering library. It draws a sunset sea with simulated waves, whitecaps, sun glitter, and a physically based sky with clouds. You fly around it with a free camera.

Looking toward the sun from 2.4 m above the water

75 degrees off the sun, 2.4 m up Looking toward the sun from 30 m up

Run it

You need a recent Chrome with WebGPU and a reasonably capable GPU. On a laptop, force Chrome onto the dedicated GPU in the Windows graphics settings, or it will fall back to the integrated one.

Open ocean.html directly in the browser. ocean.min.html is the same page minified (61 KB instead of 102 KB). Both are self-contained and work from file://.

To run from source:

npm install
npm start        # serves http://localhost:8080/

After editing anything under js/, run npm run build to regenerate both bundles. The minified build strips WGSL comments and whitespace and minifies the JavaScript with esbuild.

Controls

Input Action
Click the canvas Capture the mouse (Esc releases it). Holding the right mouse button also works.
Mouse Look around
W A S D Move
Space or E / Q or C Up / down
Shift 5x speed
Mouse wheel Change base speed
H Settings panel: wind, swell, choppiness, turbidity, foam, sun, haze, clouds, exposure, bloom

The camera keeps itself above the displaced water surface, so waves never pass through it.

URL parameters are handy for testing. ?w=2560&h=1440 forces the render resolution, cam=x,y,z,yawDeg,pitchDeg sets the camera, fov=deg sets the field of view, ui=0 hides the HUD, and any settings key works too, for example windSpeed=12&swellHs=2&sunElev=8.

How it works

Waves

The sea is a Tessendorf FFT simulation with six cascades of 256 by 256 samples. The patches range from 1103 m down to 0.26 m, so wavelengths run from about a kilometre to a few millimetres. The smallest cascade produces the sharp sun sparkles near the camera.

The spectrum is a fetch-limited JONSWAP wind sea with Donelan-Banner directional spreading, plus a narrow long-crested swell. Dispersion includes the capillary term, and choppy horizontal displacement sharpens the crests. The inverse FFTs are radix-2 compute shaders working in workgroup memory.

The surface mesh is a CDLOD quadtree with 17 levels. Vertices are 6.25 cm apart near the camera and the mesh reaches 160 km, with continuous vertex morphing between levels.

Shading

Sky

The atmosphere follows Hillaire's approach, with transmittance, multiple-scattering, and sky-view lookup tables plus aerosol haze. There are two cloud layers, a broken low cumulus and a fibrous altostratus deck. Each is shaded with self-shadowing toward the sun, lit rims, silver linings when backlit, and direction-dependent multiple scattering.

Cloud noise octaves are filtered by pixel footprint, and silhouettes are anti-aliased analytically: the screen-space derivative of each layer's edge function sets the width of its opacity edge. Cloud transmittance along the sun direction dims the sunlight that reaches the water, and aerial perspective is applied over the sea.

Post-processing

The scene renders in HDR with 4x MSAA. Post-processing adds an 8-level bloom, AgX tone mapping, a slight white balance shift, and dithering.

Performance

At 2560 by 1440 on an RTX 5070 Ti, the GPU takes about 6 to 8 ms per frame, measured while other programs were also using the GPU. The HUD shows FPS and GPU time when the browser supports timestamp-query.

The per-frame code reuses preallocated typed arrays and descriptors, so the garbage collector has almost nothing to do. The only recurring allocations are the objects WebGPU itself requires, such as the swap-chain view and command buffers.

Known limitations

How it was made

I built this with Claude Code (Opus 5.5). I asked for the most realistic water it could produce, in WebGPU, with a floating camera, and left the architecture and implementation to it. It checked its own work by rendering screenshots in headless Chrome and having separate critic subagents review them, so it was not grading its own output. I gave feedback along the way, mainly on the clouds, which led to replacing the volumetric clouds with the flat layers and later fixing their edge aliasing.

Repository layout

Path Contents
index.html Page shell, HUD, and settings panel styles
js/main.js Device setup, resources, camera, per-frame loop
js/wgsl/ Shaders as JavaScript modules: sim.js (waves and foam), sky.js (atmosphere and clouds), ocean.js (surface and sky pass), post.js (bloom and tone mapping), common.js (shared structs)
server.mjs Tiny static server for development
build.mjs Builds ocean.html and ocean.min.html
docs/ README images

License

MIT. See LICENSE.

web-ocean (Noniv/web-ocean)

正在搜索数据库…

这是什么?这是一个实时海洋渲染器,完全从零开始基于 WebGPU 构建,没有使用任何游戏引擎或渲染库。它呈现 FFT 波浪、白浪、太阳闪光,以及基于物理的日落天空,天空中有云层,你可以用自由视角相机在其中飞行穿越。
这是一个打包好的应用,还是只需打开的一个文件?它是一个独立的 HTML 文件(压缩后 61 KB,未压缩 102 KB),可通过 file:// 直接从本地磁盘运行,无需安装,也不需要服务器。
我该如何运行它?直接在 Chrome 中打开 ocean.html(或压缩版 ocean.min.html)即可。如果你要从源码构建,请编辑 js/ 目录下的文件,然后运行 npm run build 重新生成两个打包文件。
我需要什么浏览器和显卡?需要支持 WebGPU 的较新版本 Chrome,以及性能足够的 GPU。在笔记本电脑上,你可能需要在 Windows 图形设置中强制 Chrome 使用独立显卡,否则它会退回到集成显卡。
它不能做什么?它只模拟开阔海域,没有海岸线、浪花或水中物体;远处的水面仍比地平线处的天空暗一些;两层云都是平面的,而非体积云。
它在哪些平台上运行?它完全在浏览器中借助 GPU 运行;文中没有提到移动端或离线应用版本,目前只有桌面版 Chrome。
是什么让水面和天空看起来如此逼真?海面采用六级级联的 Tessendorf FFT 模拟,配合 JONSWAP 风浪谱,使用 LEAN 映射的坡度统计来实现各向异性的太阳闪光,并以相机为中心的泡沫贴图生成白浪;天空则遵循 Hillaire 的大气散射模型,并包含两层自阴影云层。
这个项目究竟是怎么做出来的?作者使用了 Claude Code(Opus 5.5),给它的目标是做出尽可能逼真的 WebGPU 水面,并带有自由视角相机。智能体自行设计了架构,并通过 headless-Chrome 截图检查自己的工作,截图由独立的评审子智能体审阅。作者主要负责把控云层效果。
它运行得有多快?在 RTX 5070 Ti 上,分辨率 2560x1440 时,GPU 每帧耗时约 6 到 8 毫秒。若浏览器支持 timestamp-query,HUD 会实时显示 FPS 和 GPU 耗时。
不改代码能自定义场景吗?可以。URL 参数可以控制分辨率、相机位置、视场角、HUD 显示与否,以及风速、太阳高度角等任何设置,例如:windSpeed=12&swellHs=2&sunElev=8。
作者
Noniv
大小
0.7 MB
星标
1.1K
更新
提交Oct 17
时长
1个月
提交 · 90 天