|
| 1 | +import tgpu from 'typegpu'; |
| 2 | +import * as d from 'typegpu/data'; |
| 3 | +import * as std from 'typegpu/std'; |
| 4 | +import * as p from './params.ts'; |
| 5 | +import { |
| 6 | + ExampleControls, |
| 7 | + ModelVertexInput, |
| 8 | + modelVertexLayout, |
| 9 | + ModelVertexOutput, |
| 10 | +} from './schemas.ts'; |
| 11 | +import { loadModel } from './load-model.ts'; |
| 12 | +import { Camera, setupOrbitCamera } from './setup-orbit-camera.ts'; |
| 13 | + |
| 14 | +// setup |
| 15 | +const canvas = document.querySelector('canvas') as HTMLCanvasElement; |
| 16 | +const context = canvas.getContext('webgpu') as GPUCanvasContext; |
| 17 | +const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); |
| 18 | +const root = await tgpu.init(); |
| 19 | + |
| 20 | +context.configure({ |
| 21 | + device: root.device, |
| 22 | + format: presentationFormat, |
| 23 | + alphaMode: 'premultiplied', |
| 24 | +}); |
| 25 | + |
| 26 | +// model (https://j5boom.itch.io/utah-teapot-obj) |
| 27 | +const model = await loadModel(root, '/TypeGPU/assets/phong/teapot.obj'); |
| 28 | + |
| 29 | +// camera |
| 30 | +const cameraUniform = root.createUniform(Camera); |
| 31 | + |
| 32 | +const { cleanupCamera } = setupOrbitCamera( |
| 33 | + canvas, |
| 34 | + { |
| 35 | + initPos: d.vec4f(-10, 4, -8, 1), |
| 36 | + target: d.vec4f(0, 1, 0, 1), |
| 37 | + minZoom: 8, |
| 38 | + maxZoom: 40, |
| 39 | + }, |
| 40 | + (updates) => cameraUniform.writePartial(updates), |
| 41 | +); |
| 42 | + |
| 43 | +// shaders |
| 44 | +const exampleControlsUniform = root.createUniform( |
| 45 | + ExampleControls, |
| 46 | + p.initialControls, |
| 47 | +); |
| 48 | + |
| 49 | +export const vertexShader = tgpu['~unstable'].vertexFn({ |
| 50 | + in: { ...ModelVertexInput.propTypes, instanceIndex: d.builtin.instanceIndex }, |
| 51 | + out: ModelVertexOutput, |
| 52 | +})((input) => { |
| 53 | + const worldPosition = d.vec4f(input.modelPosition, 1); |
| 54 | + const camera = cameraUniform.$; |
| 55 | + |
| 56 | + const canvasPosition = camera.projection.mul(camera.view).mul(worldPosition); |
| 57 | + |
| 58 | + return { |
| 59 | + worldPosition: input.modelPosition, |
| 60 | + worldNormal: input.modelNormal, |
| 61 | + canvasPosition: canvasPosition, |
| 62 | + }; |
| 63 | +}); |
| 64 | + |
| 65 | +// see https://gist.github.com/chicio/d983fff6ff304bd55bebd6ff05a2f9dd |
| 66 | +export const fragmentShader = tgpu['~unstable'].fragmentFn({ |
| 67 | + in: ModelVertexOutput, |
| 68 | + out: d.vec4f, |
| 69 | +})((input) => { |
| 70 | + const lightColor = std.normalize(exampleControlsUniform.$.lightColor); |
| 71 | + const lightDirection = std.normalize(exampleControlsUniform.$.lightDirection); |
| 72 | + // fixed color, can be replaced with texture sample |
| 73 | + const ambientColor = exampleControlsUniform.$.ambientColor; |
| 74 | + const ambientStrength = exampleControlsUniform.$.ambientStrength; |
| 75 | + const specularStrength = exampleControlsUniform.$.specularExponent; |
| 76 | + |
| 77 | + // ambient component |
| 78 | + const ambient = ambientColor.mul(ambientStrength); |
| 79 | + |
| 80 | + // diffuse component |
| 81 | + const cosTheta = std.dot(input.worldNormal, lightDirection); |
| 82 | + const diffuse = lightColor.mul(std.max(0, cosTheta)); |
| 83 | + |
| 84 | + // specular component |
| 85 | + const reflectionDirection = std.reflect( |
| 86 | + lightDirection.mul(-1), |
| 87 | + input.worldNormal, |
| 88 | + ); |
| 89 | + const viewDirection = std.normalize( |
| 90 | + cameraUniform.$.position.xyz.sub(input.worldPosition), |
| 91 | + ); |
| 92 | + const specular = lightColor.mul( |
| 93 | + std.pow( |
| 94 | + std.max(0, std.dot(reflectionDirection, viewDirection)), |
| 95 | + specularStrength, |
| 96 | + ), |
| 97 | + ); |
| 98 | + |
| 99 | + // add the components up |
| 100 | + const color = ambient.add(diffuse).add(specular); |
| 101 | + return d.vec4f(color, 1); |
| 102 | +}); |
| 103 | + |
| 104 | +// pipelines |
| 105 | +const renderPipeline = root['~unstable'] |
| 106 | + .withVertex(vertexShader, modelVertexLayout.attrib) |
| 107 | + .withFragment(fragmentShader, { format: presentationFormat }) |
| 108 | + .withDepthStencil({ |
| 109 | + format: 'depth24plus', |
| 110 | + depthWriteEnabled: true, |
| 111 | + depthCompare: 'less', |
| 112 | + }) |
| 113 | + .withPrimitive({ topology: 'triangle-list' }) |
| 114 | + .createPipeline(); |
| 115 | + |
| 116 | +let depthTexture = root.device.createTexture({ |
| 117 | + size: [canvas.width, canvas.height, 1], |
| 118 | + format: 'depth24plus', |
| 119 | + usage: GPUTextureUsage.RENDER_ATTACHMENT, |
| 120 | +}); |
| 121 | + |
| 122 | +// frame |
| 123 | +let frameId: number; |
| 124 | +function frame() { |
| 125 | + renderPipeline |
| 126 | + .withColorAttachment({ |
| 127 | + view: context.getCurrentTexture().createView(), |
| 128 | + clearValue: [ |
| 129 | + p.backgroundColor.x, |
| 130 | + p.backgroundColor.y, |
| 131 | + p.backgroundColor.z, |
| 132 | + 1, |
| 133 | + ], |
| 134 | + loadOp: 'clear', |
| 135 | + storeOp: 'store', |
| 136 | + }) |
| 137 | + .withDepthStencilAttachment({ |
| 138 | + view: depthTexture.createView(), |
| 139 | + depthClearValue: 1, |
| 140 | + depthLoadOp: 'clear', |
| 141 | + depthStoreOp: 'store', |
| 142 | + }) |
| 143 | + .with(modelVertexLayout, model.vertexBuffer) |
| 144 | + .draw(model.polygonCount); |
| 145 | + |
| 146 | + frameId = requestAnimationFrame(frame); |
| 147 | +} |
| 148 | +frameId = requestAnimationFrame(frame); |
| 149 | + |
| 150 | +// #region Example controls and cleanup |
| 151 | +export const controls = { |
| 152 | + 'light color': { |
| 153 | + initial: [...p.initialControls.lightColor], |
| 154 | + onColorChange(value: readonly [number, number, number]) { |
| 155 | + exampleControlsUniform.writePartial({ lightColor: d.vec3f(...value) }); |
| 156 | + }, |
| 157 | + }, |
| 158 | + 'light direction': { |
| 159 | + min: [-10, -10, -10], |
| 160 | + max: [10, 10, 10], |
| 161 | + initial: [...p.initialControls.lightDirection], |
| 162 | + step: [0.01, 0.01, 0.01], |
| 163 | + onVectorSliderChange(v: [number, number, number]) { |
| 164 | + exampleControlsUniform.writePartial({ lightDirection: d.vec3f(...v) }); |
| 165 | + }, |
| 166 | + }, |
| 167 | + 'ambient color': { |
| 168 | + initial: [...p.initialControls.ambientColor], |
| 169 | + onColorChange(value: readonly [number, number, number]) { |
| 170 | + exampleControlsUniform.writePartial({ ambientColor: d.vec3f(...value) }); |
| 171 | + }, |
| 172 | + }, |
| 173 | + 'ambient strength': { |
| 174 | + min: 0, |
| 175 | + max: 1, |
| 176 | + initial: p.initialControls.ambientStrength, |
| 177 | + step: 0.01, |
| 178 | + onSliderChange(v: number) { |
| 179 | + exampleControlsUniform.writePartial({ ambientStrength: v }); |
| 180 | + }, |
| 181 | + }, |
| 182 | + 'specular exponent': { |
| 183 | + min: 0.5, |
| 184 | + max: 16, |
| 185 | + initial: p.initialControls.specularExponent, |
| 186 | + step: 0.1, |
| 187 | + onSliderChange(v: number) { |
| 188 | + exampleControlsUniform.writePartial({ specularExponent: v }); |
| 189 | + }, |
| 190 | + }, |
| 191 | +}; |
| 192 | + |
| 193 | +const resizeObserver = new ResizeObserver(() => { |
| 194 | + depthTexture.destroy(); |
| 195 | + depthTexture = root.device.createTexture({ |
| 196 | + size: [canvas.width, canvas.height, 1], |
| 197 | + format: 'depth24plus', |
| 198 | + usage: GPUTextureUsage.RENDER_ATTACHMENT, |
| 199 | + }); |
| 200 | +}); |
| 201 | +resizeObserver.observe(canvas); |
| 202 | + |
| 203 | +export function onCleanup() { |
| 204 | + cancelAnimationFrame(frameId); |
| 205 | + cleanupCamera(); |
| 206 | + resizeObserver.unobserve(canvas); |
| 207 | + root.destroy(); |
| 208 | +} |
| 209 | + |
| 210 | +// #endregion |
0 commit comments