|
| 1 | +import tgpu from 'typegpu'; |
| 2 | +import * as d from 'typegpu/data'; |
| 3 | +import * as std from 'typegpu/std'; |
| 4 | +import { fullScreenTriangle } from 'typegpu/common'; |
| 5 | +import { |
| 6 | + FOV_FACTOR, |
| 7 | + NOISE_TEXTURE_SIZE, |
| 8 | + SKY_HORIZON, |
| 9 | + SKY_ZENITH_TINT, |
| 10 | + SUN_BRIGHTNESS, |
| 11 | + SUN_DIRECTION, |
| 12 | + SUN_GLOW, |
| 13 | + WIND_SPEED, |
| 14 | +} from './consts.ts'; |
| 15 | +import { raymarch } from './utils.ts'; |
| 16 | +import { cloudsLayout, CloudsParams } from './types.ts'; |
| 17 | +import { randf } from '@typegpu/noise'; |
| 18 | + |
| 19 | +const canvas = document.querySelector('canvas') as HTMLCanvasElement; |
| 20 | +const context = canvas.getContext('webgpu') as GPUCanvasContext; |
| 21 | +const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); |
| 22 | + |
| 23 | +const root = await tgpu.init(); |
| 24 | + |
| 25 | +context.configure({ |
| 26 | + device: root.device, |
| 27 | + format: presentationFormat, |
| 28 | + alphaMode: 'premultiplied', |
| 29 | +}); |
| 30 | + |
| 31 | +const paramsUniform = root.createUniform(CloudsParams, { |
| 32 | + time: 0, |
| 33 | + maxSteps: 50, |
| 34 | + maxDistance: 10.0, |
| 35 | +}); |
| 36 | +const resolutionUniform = root.createUniform( |
| 37 | + d.vec2f, |
| 38 | + d.vec2f(canvas.width, canvas.height), |
| 39 | +); |
| 40 | + |
| 41 | +const noiseData = new Uint8Array(NOISE_TEXTURE_SIZE * NOISE_TEXTURE_SIZE * 4); |
| 42 | +for (let i = 0; i < noiseData.length; i += 4) { |
| 43 | + const value = Math.random() * 255; |
| 44 | + noiseData[i] = value; |
| 45 | + noiseData[i + 1] = Math.random() * 255; |
| 46 | + noiseData[i + 2] = value; |
| 47 | + noiseData[i + 3] = 255; |
| 48 | +} |
| 49 | + |
| 50 | +const sampler = root['~unstable'].createSampler({ |
| 51 | + magFilter: 'linear', |
| 52 | + minFilter: 'linear', |
| 53 | + addressModeU: 'repeat', |
| 54 | + addressModeV: 'repeat', |
| 55 | +}); |
| 56 | + |
| 57 | +const noiseTexture = root['~unstable'] |
| 58 | + .createTexture({ |
| 59 | + size: [NOISE_TEXTURE_SIZE, NOISE_TEXTURE_SIZE], |
| 60 | + format: 'rgba8unorm', |
| 61 | + }) |
| 62 | + .$usage('sampled', 'render'); |
| 63 | + |
| 64 | +root.device.queue.writeTexture( |
| 65 | + { texture: root.unwrap(noiseTexture) }, |
| 66 | + noiseData, |
| 67 | + { bytesPerRow: NOISE_TEXTURE_SIZE * 4 }, |
| 68 | + { width: NOISE_TEXTURE_SIZE, height: NOISE_TEXTURE_SIZE }, |
| 69 | +); |
| 70 | + |
| 71 | +const bindGroup = root.createBindGroup(cloudsLayout, { |
| 72 | + params: paramsUniform.buffer, |
| 73 | + noiseTexture, |
| 74 | + sampler, |
| 75 | +}); |
| 76 | + |
| 77 | +const mainFragment = tgpu['~unstable'].fragmentFn({ |
| 78 | + in: { uv: d.vec2f }, |
| 79 | + out: d.vec4f, |
| 80 | +})(({ uv }) => { |
| 81 | + randf.seed2(uv.mul(cloudsLayout.$.params.time)); |
| 82 | + const screenRes = resolutionUniform.$; |
| 83 | + const aspect = screenRes.x / screenRes.y; |
| 84 | + |
| 85 | + let screenPos = std.mul(std.sub(uv, 0.5), 2.0); |
| 86 | + screenPos = d.vec2f( |
| 87 | + screenPos.x * std.max(aspect, 1.0), |
| 88 | + screenPos.y * std.max(1.0 / aspect, 1.0), |
| 89 | + ); |
| 90 | + |
| 91 | + const sunDir = std.normalize(SUN_DIRECTION); |
| 92 | + const time = cloudsLayout.$.params.time; |
| 93 | + const rayOrigin = d.vec3f( |
| 94 | + std.sin(time * 0.6) * 0.5, |
| 95 | + std.cos(time * 0.8) * 0.5 - 1, |
| 96 | + time * WIND_SPEED, |
| 97 | + ); |
| 98 | + const rayDir = std.normalize(d.vec3f(screenPos.x, screenPos.y, FOV_FACTOR)); |
| 99 | + |
| 100 | + const sunDot = std.clamp(std.dot(rayDir, sunDir), 0.0, 1.0); |
| 101 | + const sunGlow = std.pow( |
| 102 | + sunDot, |
| 103 | + 1.0 / (SUN_BRIGHTNESS * SUN_BRIGHTNESS * SUN_BRIGHTNESS), |
| 104 | + ); |
| 105 | + |
| 106 | + let skyCol = std.sub(SKY_HORIZON, std.mul(SKY_ZENITH_TINT, rayDir.y * 0.35)); |
| 107 | + skyCol = std.add(skyCol, std.mul(SUN_GLOW, sunGlow)); |
| 108 | + |
| 109 | + const cloudCol = raymarch(rayOrigin, rayDir, sunDir); |
| 110 | + const finalCol = std.add(std.mul(skyCol, 1.1 - cloudCol.w), cloudCol.xyz); |
| 111 | + |
| 112 | + return d.vec4f(finalCol, 1.0); |
| 113 | +}); |
| 114 | + |
| 115 | +const pipeline = root['~unstable'] |
| 116 | + .withVertex(fullScreenTriangle) |
| 117 | + .withFragment(mainFragment, { format: presentationFormat }) |
| 118 | + .createPipeline(); |
| 119 | + |
| 120 | +const resizeObserver = new ResizeObserver(() => { |
| 121 | + resolutionUniform.write(d.vec2f(canvas.width, canvas.height)); |
| 122 | +}); |
| 123 | +resizeObserver.observe(canvas); |
| 124 | + |
| 125 | +let frameId: number; |
| 126 | + |
| 127 | +function render() { |
| 128 | + paramsUniform.writePartial({ time: (performance.now() / 1000) % 500 }); |
| 129 | + |
| 130 | + pipeline |
| 131 | + .with(bindGroup) |
| 132 | + .withColorAttachment({ |
| 133 | + view: context.getCurrentTexture().createView(), |
| 134 | + clearValue: [0, 0, 0, 1], |
| 135 | + loadOp: 'clear', |
| 136 | + storeOp: 'store', |
| 137 | + }) |
| 138 | + .draw(6); |
| 139 | + |
| 140 | + frameId = requestAnimationFrame(render); |
| 141 | +} |
| 142 | + |
| 143 | +frameId = requestAnimationFrame(render); |
| 144 | + |
| 145 | +const qualityOptions = { |
| 146 | + 'very high': { |
| 147 | + maxSteps: 150, |
| 148 | + maxDistance: 13.0, |
| 149 | + }, |
| 150 | + high: { |
| 151 | + maxSteps: 100, |
| 152 | + maxDistance: 12.0, |
| 153 | + }, |
| 154 | + medium: { |
| 155 | + maxSteps: 50, |
| 156 | + maxDistance: 10.0, |
| 157 | + }, |
| 158 | + low: { |
| 159 | + maxSteps: 30, |
| 160 | + maxDistance: 6.0, |
| 161 | + }, |
| 162 | + 'very low': { |
| 163 | + maxSteps: 15, |
| 164 | + maxDistance: 4.0, |
| 165 | + }, |
| 166 | +} as Record<string, Partial<d.Infer<typeof CloudsParams>>>; |
| 167 | + |
| 168 | +export const controls = { |
| 169 | + Quality: { |
| 170 | + initial: 'medium', |
| 171 | + options: ['very high', 'high', 'medium', 'low', 'very low'], |
| 172 | + onSelectChange(value: string) { |
| 173 | + paramsUniform.writePartial(qualityOptions[value]); |
| 174 | + }, |
| 175 | + }, |
| 176 | +}; |
| 177 | + |
| 178 | +export function onCleanup() { |
| 179 | + cancelAnimationFrame(frameId); |
| 180 | + resizeObserver.disconnect(); |
| 181 | + root.destroy(); |
| 182 | +} |
0 commit comments