Skip to content

Commit 1ceacdf

Browse files
authored
docs: Small cleanup to "Fluid double buffering" (#1275)
* docs: Small cleanup to "Fluid double buffering" * Review fixes * Update formatting
1 parent 7661ddb commit 1ceacdf

File tree

1 file changed

+21
-42
lines changed
  • apps/typegpu-docs/src/content/examples/simulation/fluid-double-buffering

1 file changed

+21
-42
lines changed

apps/typegpu-docs/src/content/examples/simulation/fluid-double-buffering/index.ts

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const GridData = d.arrayOf(d.vec4f, MAX_GRID_SIZE ** 2);
2828

2929
type BoxObstacle = typeof BoxObstacle;
3030
const BoxObstacle = d.struct({
31-
center: d.vec2u,
32-
size: d.vec2u,
31+
center: d.vec2i,
32+
size: d.vec2i,
3333
enabled: d.u32,
3434
});
3535

@@ -132,16 +132,10 @@ const isInsideObstacle = tgpu['~unstable'].fn([d.i32, d.i32], d.bool)(
132132
continue;
133133
}
134134

135-
const minX = std.max(0, d.i32(obs.center.x) - d.i32(obs.size.x) / 2);
136-
const maxX = std.min(
137-
d.i32(gridSize),
138-
d.i32(obs.center.x) + d.i32(obs.size.x) / 2,
139-
);
140-
const minY = std.max(0, d.i32(obs.center.y) - d.i32(obs.size.y) / 2);
141-
const maxY = std.min(
142-
d.i32(gridSize),
143-
d.i32(obs.center.y) + d.i32(obs.size.y) / 2,
144-
);
135+
const minX = std.max(0, obs.center.x - d.i32(obs.size.x / 2));
136+
const maxX = std.min(gridSize, obs.center.x + d.i32(obs.size.x / 2));
137+
const minY = std.max(0, obs.center.y - d.i32(obs.size.y / 2));
138+
const maxY = std.min(gridSize, obs.center.y + d.i32(obs.size.y / 2));
145139

146140
if (x >= minX && x <= maxX && y >= minY && y <= maxY) {
147141
return true;
@@ -222,7 +216,7 @@ const mainInitWorld = tgpu['~unstable'].computeFn({
222216
let value = d.vec4f();
223217

224218
if (!isValidFlowOut(x, y)) {
225-
value = d.vec4f(0, 0, 0, 0);
219+
value = d.vec4f();
226220
} else {
227221
// Ocean
228222
if (y < d.i32(gridSizeUniform.value) / 2) {
@@ -244,37 +238,25 @@ const mainMoveObstacles = tgpu['~unstable'].computeFn({ workgroupSize: [1] })(
244238
continue;
245239
}
246240

247-
const diff = std.sub(
248-
d.vec2i(d.i32(nextObs.center.x), d.i32(nextObs.center.y)),
249-
d.vec2i(d.i32(obs.center.x), d.i32(obs.center.y)),
250-
);
241+
const diff = std.sub(nextObs.center.xy, obs.center.xy);
251242

252-
const minX = std.max(0, d.i32(obs.center.x) - d.i32(obs.size.x) / 2);
253-
const maxX = std.min(
254-
d.i32(gridSize),
255-
d.i32(obs.center.x) + d.i32(obs.size.x) / 2,
256-
);
257-
const minY = std.max(0, d.i32(obs.center.y) - d.i32(obs.size.y) / 2);
258-
const maxY = std.min(
259-
d.i32(gridSize),
260-
d.i32(obs.center.y) + d.i32(obs.size.y) / 2,
261-
);
243+
const minX = std.max(0, obs.center.x - d.i32(obs.size.x / 2));
244+
const maxX = std.min(gridSize, obs.center.x + d.i32(obs.size.x / 2));
245+
const minY = std.max(0, obs.center.y - d.i32(obs.size.y) / 2);
246+
const maxY = std.min(gridSize, obs.center.y + d.i32(obs.size.y / 2));
262247

263-
const nextMinX = std.max(
264-
0,
265-
d.i32(nextObs.center.x) - d.i32(obs.size.x) / 2,
266-
);
248+
const nextMinX = std.max(0, nextObs.center.x - d.i32(obs.size.x / 2));
267249
const nextMaxX = std.min(
268-
d.i32(gridSize),
269-
d.i32(nextObs.center.x) + d.i32(obs.size.x) / 2,
250+
gridSize,
251+
nextObs.center.x + d.i32(obs.size.x / 2),
270252
);
271253
const nextMinY = std.max(
272254
0,
273-
d.i32(nextObs.center.y) - d.i32(obs.size.y) / 2,
255+
nextObs.center.y - d.i32(obs.size.y / 2),
274256
);
275257
const nextMaxY = std.min(
276-
d.i32(gridSize),
277-
d.i32(nextObs.center.y) + d.i32(obs.size.y) / 2,
258+
gridSize,
259+
nextObs.center.y + d.i32(obs.size.y / 2),
278260
);
279261

280262
// does it move right
@@ -378,10 +360,7 @@ const getMinimumInFlow = tgpu['~unstable'].fn([d.i32, d.i32], d.f32)((x, y) => {
378360
sourceParamsUniform.value.center.y * gridSizeF,
379361
);
380362

381-
if (
382-
std.length(d.vec2f(d.f32(x) - sourcePos.x, d.f32(y) - sourcePos.y)) <
383-
sourceRadius
384-
) {
363+
if (std.distance(d.vec2f(d.f32(x), d.f32(y)), sourcePos) < sourceRadius) {
385364
return sourceParamsUniform.value.intensity;
386365
}
387366

@@ -435,8 +414,8 @@ const obstacles: {
435414

436415
function obstaclesToConcrete(): d.Infer<BoxObstacle>[] {
437416
return obstacles.map(({ x, y, width, height, enabled }) => ({
438-
center: d.vec2u(Math.round(x * gridSize), Math.round(y * gridSize)),
439-
size: d.vec2u(Math.round(width * gridSize), Math.round(height * gridSize)),
417+
center: d.vec2i(Math.round(x * gridSize), Math.round(y * gridSize)),
418+
size: d.vec2i(Math.round(width * gridSize), Math.round(height * gridSize)),
440419
enabled: enabled ? 1 : 0,
441420
}));
442421
}

0 commit comments

Comments
 (0)