|
| 1 | +import { cancelFrame, frame as motionFrame } from "motion" |
| 2 | +import { animate } from "motion/vgpu" |
| 3 | +import { |
| 4 | + clock, |
| 5 | + draw, |
| 6 | + effect, |
| 7 | + frame as vgpuFrame, |
| 8 | + geometry, |
| 9 | + init, |
| 10 | + sampler, |
| 11 | + surface, |
| 12 | + target, |
| 13 | +} from "vgpu" |
| 14 | +import { |
| 15 | + box, |
| 16 | + directionalLight, |
| 17 | + mesh, |
| 18 | + orbitControls, |
| 19 | + perspectiveCamera, |
| 20 | + srgb, |
| 21 | + unlitMaterial, |
| 22 | +} from "vgpu/scene" |
| 23 | +import "./gpu-adapters.css" |
| 24 | + |
| 25 | +const stage = document.querySelector(".stage") |
| 26 | +const canvas = document.querySelector("canvas") |
| 27 | +const status = document.querySelector("#status") |
| 28 | + |
| 29 | +const objectShader = ` |
| 30 | + struct Camera { viewProjection: mat4x4f } |
| 31 | + struct Model { model: mat4x4f } |
| 32 | + struct Material { color: vec3f, opacity: f32 } |
| 33 | + struct Light { direction: vec3f, intensity: f32, color: vec3f } |
| 34 | +
|
| 35 | + @group(0) @binding(0) var<uniform> camera: Camera; |
| 36 | + @group(0) @binding(1) var<uniform> model: Model; |
| 37 | + @group(0) @binding(2) var<uniform> material: Material; |
| 38 | + @group(0) @binding(3) var<uniform> light: Light; |
| 39 | +
|
| 40 | + struct VertexOut { |
| 41 | + @builtin(position) position: vec4f, |
| 42 | + @location(0) normal: vec3f, |
| 43 | + } |
| 44 | +
|
| 45 | + @vertex |
| 46 | + fn vs_main( |
| 47 | + @location(0) position: vec3f, |
| 48 | + @location(1) normal: vec3f |
| 49 | + ) -> VertexOut { |
| 50 | + var out: VertexOut; |
| 51 | + out.position = camera.viewProjection * model.model * vec4f(position, 1.0); |
| 52 | + out.normal = (model.model * vec4f(normal, 0.0)).xyz; |
| 53 | + return out; |
| 54 | + } |
| 55 | +
|
| 56 | + @fragment |
| 57 | + fn fs_main(@location(0) normal: vec3f) -> @location(0) vec4f { |
| 58 | + let n = normalize(normal); |
| 59 | + let l = normalize(-light.direction); |
| 60 | + let diffuse = max(dot(n, l), 0.0) * light.intensity * light.color; |
| 61 | + let shade = material.color * (vec3f(0.12) + diffuse); |
| 62 | + return vec4f(shade * material.opacity, material.opacity); |
| 63 | + } |
| 64 | +` |
| 65 | + |
| 66 | +const presentShader = ` |
| 67 | + @group(0) @binding(0) var scene: texture_2d<f32>; |
| 68 | + @group(0) @binding(1) var sceneSampler: sampler; |
| 69 | +
|
| 70 | + // Material colors are linear (srgb() / animate()), so encode for the canvas. |
| 71 | + @fragment |
| 72 | + fn fs_main(@location(0) uv: vec2f) -> @location(0) vec4f { |
| 73 | + let color = textureSampleLevel(scene, sceneSampler, uv, 0.0); |
| 74 | + return vec4f(pow(color.rgb, vec3f(1.0 / 2.2)), color.a); |
| 75 | + } |
| 76 | +` |
| 77 | + |
| 78 | +async function start() { |
| 79 | + if (!navigator.gpu) { |
| 80 | + stage.innerHTML = |
| 81 | + '<div class="error">This example needs a browser with WebGPU enabled.</div>' |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + const gpu = await init() |
| 86 | + const time = clock(gpu) |
| 87 | + const canvasSurface = surface(gpu, canvas, { dpr: [1, 2] }) |
| 88 | + const sceneTarget = target(gpu, { size: canvasSurface.size, depth: true }) |
| 89 | + |
| 90 | + const material = unlitMaterial({ color: srgb("#8b5cf6") }) |
| 91 | + const cube = mesh(box({ size: 1.4 }), material, { position: [-1, 0, 0] }) |
| 92 | + const light = directionalLight({ |
| 93 | + direction: [-1, -2, -1], |
| 94 | + intensity: 1.2, |
| 95 | + }) |
| 96 | + const camera = perspectiveCamera({ |
| 97 | + fov: 42, |
| 98 | + aspect: canvasSurface.size[0] / canvasSurface.size[1], |
| 99 | + position: [2.5, 2, 5], |
| 100 | + target: [0, 0, 0], |
| 101 | + }) |
| 102 | + const controls = orbitControls(camera, { element: canvas, damping: 0.1 }) |
| 103 | + |
| 104 | + const cubeDraw = draw(gpu, { |
| 105 | + shader: objectShader, |
| 106 | + geometry: geometry(gpu, box({ size: 1 })), |
| 107 | + cull: "back", |
| 108 | + blend: "premultiplied", |
| 109 | + }) |
| 110 | + const present = effect(gpu, presentShader, { |
| 111 | + set: { |
| 112 | + scene: sceneTarget, |
| 113 | + sceneSampler: sampler(gpu, { |
| 114 | + minFilter: "linear", |
| 115 | + magFilter: "linear", |
| 116 | + }), |
| 117 | + }, |
| 118 | + }) |
| 119 | + |
| 120 | + canvasSurface.onResize(({ width, height }) => { |
| 121 | + sceneTarget.resize([width, height]) |
| 122 | + camera.set({ aspect: width / height }) |
| 123 | + }) |
| 124 | + |
| 125 | + let meshActive = false |
| 126 | + document.querySelector("#move").addEventListener("click", () => { |
| 127 | + meshActive = !meshActive |
| 128 | + status.textContent = "Animating node transform and material" |
| 129 | + |
| 130 | + const transition = { type: "spring", visualDuration: 0.7, bounce: 0.25 } |
| 131 | + |
| 132 | + Promise.all([ |
| 133 | + animate( |
| 134 | + cube, |
| 135 | + meshActive |
| 136 | + ? { x: -0.3, rotateX: 35, rotateY: 180, scale: 1.25 } |
| 137 | + : { x: -1, rotateX: 0, rotateY: 0, scale: 1 }, |
| 138 | + transition |
| 139 | + ), |
| 140 | + animate( |
| 141 | + material, |
| 142 | + meshActive |
| 143 | + ? { color: "#f43f5e", opacity: 0.65 } |
| 144 | + : { color: "#8b5cf6", opacity: 1 }, |
| 145 | + transition |
| 146 | + ), |
| 147 | + ]).then(() => { |
| 148 | + status.textContent = "Node animation complete" |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + let lightActive = false |
| 153 | + document.querySelector("#light").addEventListener("click", () => { |
| 154 | + lightActive = !lightActive |
| 155 | + status.textContent = "Animating light direction, color and intensity" |
| 156 | + |
| 157 | + animate( |
| 158 | + light, |
| 159 | + lightActive |
| 160 | + ? { |
| 161 | + directionX: 1, |
| 162 | + directionZ: 1, |
| 163 | + intensity: 2, |
| 164 | + color: "#ffd27a", |
| 165 | + } |
| 166 | + : { |
| 167 | + directionX: -1, |
| 168 | + directionZ: -1, |
| 169 | + intensity: 1.2, |
| 170 | + color: "#ffffff", |
| 171 | + }, |
| 172 | + { duration: 1.2, ease: "easeInOut" } |
| 173 | + ).then(() => { |
| 174 | + status.textContent = "Light animation complete" |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + let cameraActive = false |
| 179 | + document.querySelector("#camera").addEventListener("click", () => { |
| 180 | + cameraActive = !cameraActive |
| 181 | + status.textContent = "Animating orbit controls and fov" |
| 182 | + |
| 183 | + Promise.all([ |
| 184 | + animate( |
| 185 | + controls, |
| 186 | + cameraActive |
| 187 | + ? { yaw: controls.yaw + Math.PI, pitch: 0.9, distance: 9 } |
| 188 | + : { yaw: controls.yaw - Math.PI, pitch: 0.4, distance: 6 }, |
| 189 | + { duration: 1.4, ease: "easeInOut" } |
| 190 | + ), |
| 191 | + animate(camera, { fov: cameraActive ? 70 : 42 }, { duration: 1.4 }), |
| 192 | + ]).then(() => { |
| 193 | + status.textContent = "Camera animation complete" |
| 194 | + }) |
| 195 | + }) |
| 196 | + |
| 197 | + function update({ delta }) { |
| 198 | + // Motion owns the timeline, so it drives vgpu's clock (see vgpu's |
| 199 | + // "External ticker" guide) and the orbit controls damping. |
| 200 | + time.advance(Math.max(0, delta) / 1000) |
| 201 | + controls.update(time.deltaTime) |
| 202 | + } |
| 203 | + |
| 204 | + function render() { |
| 205 | + cubeDraw.set({ |
| 206 | + camera: { viewProjection: camera.viewProjection }, |
| 207 | + model: { model: cube.worldMatrix }, |
| 208 | + material: { color: material.color, opacity: material.opacity }, |
| 209 | + light: { |
| 210 | + direction: light.direction, |
| 211 | + intensity: light.intensity, |
| 212 | + color: light.color, |
| 213 | + }, |
| 214 | + }) |
| 215 | + |
| 216 | + vgpuFrame(gpu, (currentFrame) => { |
| 217 | + currentFrame.pass( |
| 218 | + { target: sceneTarget, clear: [0.003, 0.003, 0.0035, 1] }, |
| 219 | + (pass) => pass.draw(cubeDraw) |
| 220 | + ) |
| 221 | + currentFrame.pass(canvasSurface, present) |
| 222 | + }) |
| 223 | + } |
| 224 | + |
| 225 | + motionFrame.update(update, true) |
| 226 | + motionFrame.render(render, true) |
| 227 | + |
| 228 | + window.addEventListener("pagehide", () => { |
| 229 | + cancelFrame(update) |
| 230 | + cancelFrame(render) |
| 231 | + controls.dispose() |
| 232 | + gpu.dispose() |
| 233 | + }) |
| 234 | +} |
| 235 | + |
| 236 | +start().catch((error) => { |
| 237 | + stage.innerHTML = `<div class="error">${error.message}</div>` |
| 238 | + throw error |
| 239 | +}) |
0 commit comments