Skip to content

Commit d2eec05

Browse files
fix: Investigate why some examples work differently (#2088)
1 parent 2201eb0 commit d2eec05

7 files changed

Lines changed: 45 additions & 108 deletions

File tree

apps/typegpu-docs/src/examples/rendering/3d-fish/compute.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,14 @@ export const simulate = (fishIndex: number) => {
6262
}
6363
}
6464

65-
if (layout.$.mouseRay.activated === 1) {
66-
const proj = projectPointOnLine(
67-
fishData.position,
68-
layout.$.mouseRay.line,
69-
);
70-
const diff = fishData.position.sub(proj);
71-
const limit = p.fishMouseRayRepulsionDistance;
72-
const str = std.pow(2, std.clamp(limit - std.length(diff), 0, limit)) - 1;
73-
rayRepulsion = std.normalize(diff).mul(str);
74-
}
65+
const proj = projectPointOnLine(
66+
fishData.position,
67+
layout.$.mouseRay,
68+
);
69+
const diff = fishData.position.sub(proj);
70+
const limit = p.fishMouseRayRepulsionDistance;
71+
const str = std.pow(2, std.clamp(limit - std.length(diff), 0, limit)) - 1;
72+
rayRepulsion = std.normalize(diff).mul(str);
7573

7674
let direction = d.vec3f(fishData.direction);
7775

apps/typegpu-docs/src/examples/rendering/3d-fish/index.html

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<style>
2-
.controls-header {
2+
.attribution-header {
33
font-size: 1.1rem;
44
margin-bottom: 0.5rem;
55
}
66

7-
.controls {
8-
font-size: 0.8rem;
9-
}
10-
117
#attribution {
128
margin-top: 1rem;
139
font-size: 0.8rem;
@@ -42,12 +38,10 @@
4238
<div class="spinner">Loading...</div>
4339
</div>
4440
<div id="help">
45-
<h3 class="controls-header">Controls (click to dismiss)</h3>
46-
<p class="controls"><b>Left Mouse Button:</b> Look around</p>
47-
<p class="controls"><b>Right Mouse Button:</b> Repel fish from the cursor</p>
41+
<h3 class="attribution-header">Attribution (click to dismiss)</h3>
4842
<p id="attribution">
49-
Uses "Animated Low Poly Fish" by Sketchfab user Atlas, used under CC BY 4.0
50-
/ Modified from original
43+
Uses "Animated Low Poly Fish" by Sketchfab user Atlas, <br>used under CC BY
44+
4.0 / Modified from original
5145
</p>
5246
</div>
5347
<canvas data-fit-to-container></canvas>

apps/typegpu-docs/src/examples/rendering/3d-fish/index.ts

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
ModelData,
1616
ModelDataArray,
1717
modelVertexLayout,
18-
MouseRay,
1918
renderBindGroupLayout,
2019
renderInstanceLayout,
2120
} from './schemas.ts';
@@ -41,9 +40,9 @@ const presets = {
4140
separationDist: 0.3,
4241
separationStr: 0.0006,
4342
alignmentDist: 0.3,
44-
alignmentStr: 0.005,
43+
alignmentStr: 0.01,
4544
cohesionDist: 0.5,
46-
cohesionStr: 0.0004,
45+
cohesionStr: 0.0013,
4746
},
4847
init: {
4948
separationDist: 0.2,
@@ -148,16 +147,8 @@ const camera = {
148147
};
149148

150149
const cameraBuffer = root.createBuffer(Camera, camera).$usage('uniform');
151-
152-
const mouseRayBuffer = root
153-
.createBuffer(MouseRay, {
154-
activated: 0,
155-
line: Line3({ origin: d.vec3f(), dir: d.vec3f() }),
156-
})
157-
.$usage('uniform');
158-
150+
const mouseRayBuffer = root.createBuffer(Line3).$usage('uniform');
159151
const timePassedBuffer = root.createBuffer(d.f32).$usage('uniform');
160-
161152
const currentTimeBuffer = root.createBuffer(d.f32).$usage('uniform');
162153

163154
const fishBehaviorBuffer = root
@@ -321,10 +312,9 @@ export const controls = {
321312

322313
// Variables for interaction
323314

324-
let isLeftPressed = false;
315+
let isPressed = false;
325316
let previousMouseX = 0;
326317
let previousMouseY = 0;
327-
let isRightPressed = false;
328318

329319
let isPopupDiscarded = false;
330320
const controlsPopup = document.getElementById('help') as HTMLDivElement;
@@ -383,20 +373,12 @@ async function updateMouseRay(cx: number, cy: number) {
383373
worldPos.z / worldPos.w,
384374
);
385375

386-
mouseRayBuffer.write({
387-
activated: 1,
388-
line: Line3({
389-
origin: camera.position.xyz,
390-
dir: std.normalize(std.sub(worldPosNonUniform, camera.position.xyz)),
391-
}),
392-
});
376+
mouseRayBuffer.write(Line3({
377+
origin: camera.position.xyz,
378+
dir: std.normalize(std.sub(worldPosNonUniform, camera.position.xyz)),
379+
}));
393380
}
394381

395-
// Prevent the context menu from appearing on right click.
396-
canvas.addEventListener('contextmenu', (event) => {
397-
event.preventDefault();
398-
});
399-
400382
// Mouse controls
401383

402384
canvas.addEventListener('mousedown', async (event) => {
@@ -406,23 +388,14 @@ canvas.addEventListener('mousedown', async (event) => {
406388
isPopupDiscarded = true;
407389

408390
if (event.button === 0) {
409-
isLeftPressed = true;
410-
}
411-
if (event.button === 2) {
412-
isRightPressed = true;
413-
updateMouseRay(event.clientX, event.clientY);
391+
isPressed = true;
414392
}
393+
updateMouseRay(event.clientX, event.clientY);
415394
});
416395

417396
const mouseUpEventListener = (event: MouseEvent) => {
418397
if (event.button === 0) {
419-
isLeftPressed = false;
420-
}
421-
if (event.button === 2) {
422-
isRightPressed = false;
423-
mouseRayBuffer.writePartial({
424-
activated: 0,
425-
});
398+
isPressed = false;
426399
}
427400
};
428401
window.addEventListener('mouseup', mouseUpEventListener);
@@ -439,13 +412,11 @@ const mouseMoveEventListener = (event: MouseEvent) => {
439412
previousMouseX = event.clientX;
440413
previousMouseY = event.clientY;
441414

442-
if (isLeftPressed) {
415+
if (isPressed) {
443416
updateCameraTarget(dx, dy);
444417
}
445418

446-
if (isRightPressed) {
447-
updateMouseRay(event.clientX, event.clientY);
448-
}
419+
updateMouseRay(event.clientX, event.clientY);
449420
};
450421
window.addEventListener('mousemove', mouseMoveEventListener);
451422

@@ -458,9 +429,9 @@ canvas.addEventListener(
458429
if (event.touches.length === 1) {
459430
previousMouseX = event.touches[0].clientX;
460431
previousMouseY = event.touches[0].clientY;
461-
updateMouseRay(event.touches[0].clientX, event.touches[0].clientY);
462-
controlsPopup.style.opacity = '0';
463432
}
433+
updateMouseRay(event.touches[0].clientX, event.touches[0].clientY);
434+
controlsPopup.style.opacity = '0';
464435
},
465436
{ passive: false },
466437
);
@@ -473,18 +444,11 @@ const touchMoveEventListener = (event: TouchEvent) => {
473444
previousMouseY = event.touches[0].clientY;
474445

475446
updateCameraTarget(dx, dy);
476-
updateMouseRay(event.touches[0].clientX, event.touches[0].clientY);
477447
}
448+
updateMouseRay(event.touches[0].clientX, event.touches[0].clientY);
478449
};
479450
window.addEventListener('touchmove', touchMoveEventListener);
480451

481-
const touchEndEventListener = () => {
482-
mouseRayBuffer.writePartial({
483-
activated: 0,
484-
});
485-
};
486-
window.addEventListener('touchend', touchEndEventListener);
487-
488452
// observer and cleanup
489453

490454
const resizeObserver = new ResizeObserver(() => {
@@ -510,7 +474,6 @@ export function onCleanup() {
510474
window.removeEventListener('mouseup', mouseUpEventListener);
511475
window.removeEventListener('mousemove', mouseMoveEventListener);
512476
window.removeEventListener('touchmove', touchMoveEventListener);
513-
window.removeEventListener('touchend', touchEndEventListener);
514477
resizeObserver.disconnect();
515478
root.destroy();
516479
}

apps/typegpu-docs/src/examples/rendering/3d-fish/params.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export const fishModelScale = 0.07;
66

77
export const fishWallRepulsionDistance = 0.1;
88
export const fishWallRepulsionStrength = 0.0001;
9-
export const fishMouseRayRepulsionDistance = 0.9;
10-
export const fishMouseRayRepulsionStrength = 0.0005;
9+
export const fishMouseRayRepulsionDistance = 1.2;
10+
export const fishMouseRayRepulsionStrength = 0.0015;
1111

1212
export const aquariumSize = d.vec3f(10, 4, 10);
1313

apps/typegpu-docs/src/examples/rendering/3d-fish/schemas.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ export const ModelVertexOutput = {
5050
applySeaDesaturation: d.interpolate('flat', d.u32), // bool
5151
} as const;
5252

53-
export const MouseRay = d.struct({
54-
activated: d.u32,
55-
line: Line3,
56-
});
57-
5853
export const FishBehaviorParams = d.struct({
5954
separationDist: d.f32,
6055
separationStr: d.f32,
@@ -89,7 +84,7 @@ export const computeBindGroupLayout = tgpu.bindGroupLayout({
8984
storage: ModelDataArray,
9085
access: 'mutable',
9186
},
92-
mouseRay: { uniform: MouseRay },
87+
mouseRay: { uniform: Line3 },
9388
timePassed: { uniform: d.f32 },
9489
fishBehavior: { uniform: FishBehaviorParams },
9590
});

apps/typegpu-docs/src/examples/rendering/function-visualizer/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,7 @@ const renderBackgroundPipeline = device.createRenderPipeline({
190190
});
191191

192192
let msTexture = device.createTexture({
193-
size: [
194-
canvas.clientWidth * window.devicePixelRatio,
195-
canvas.clientHeight * window.devicePixelRatio,
196-
],
193+
size: [canvas.width, canvas.height],
197194
sampleCount: 4,
198195
format: presentationFormat,
199196
usage: GPUTextureUsage.RENDER_ATTACHMENT,
@@ -547,10 +544,7 @@ const resizeObserver = new ResizeObserver(() => {
547544

548545
msTexture.destroy();
549546
msTexture = device.createTexture({
550-
size: [
551-
canvas.clientWidth * window.devicePixelRatio,
552-
canvas.clientHeight * window.devicePixelRatio,
553-
],
547+
size: [canvas.width, canvas.height],
554548
sampleCount: 4,
555549
format: presentationFormat,
556550
usage: GPUTextureUsage.RENDER_ATTACHMENT,

packages/typegpu/tests/examples/individual/3d-fish.test.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,14 @@ describe('3d fish example', () => {
115115
dir: vec3f,
116116
}
117117
118-
struct MouseRay {
119-
activated: u32,
120-
line: Line3,
121-
}
122-
123-
@group(1) @binding(2) var<uniform> mouseRay: MouseRay;
124-
125118
fn projectPointOnLine(point: vec3f, line: Line3) -> vec3f {
126119
var pointVector = (point - line.origin);
127120
let projection = dot(pointVector, line.dir);
128121
return (line.origin + (line.dir * projection));
129122
}
130123
124+
@group(1) @binding(2) var<uniform> mouseRay: Line3;
125+
131126
@group(1) @binding(3) var<uniform> timePassed: f32;
132127
133128
@group(1) @binding(1) var<storage, read_write> nextFishData: array<ModelData>;
@@ -172,27 +167,25 @@ describe('3d fish example', () => {
172167
let axisPosition = (*fishData).position[i];
173168
const distance = 0.1;
174169
if ((axisPosition > (axisAquariumSize - distance))) {
175-
let str = (axisPosition - (axisAquariumSize - distance));
176-
wallRepulsion = (wallRepulsion - (repulsion * str));
170+
let str2 = (axisPosition - (axisAquariumSize - distance));
171+
wallRepulsion = (wallRepulsion - (repulsion * str2));
177172
}
178173
if ((axisPosition < (-(axisAquariumSize) + distance))) {
179-
let str = ((-(axisAquariumSize) + distance) - axisPosition);
180-
wallRepulsion = (wallRepulsion + (repulsion * str));
174+
let str2 = ((-(axisAquariumSize) + distance) - axisPosition);
175+
wallRepulsion = (wallRepulsion + (repulsion * str2));
181176
}
182177
}
183-
if ((mouseRay.activated == 1u)) {
184-
var proj = projectPointOnLine((*fishData).position, mouseRay.line);
185-
var diff = ((*fishData).position - proj);
186-
const limit = 0.9;
187-
let str = (pow(2f, clamp((limit - length(diff)), 0f, limit)) - 1f);
188-
rayRepulsion = (normalize(diff) * str);
189-
}
178+
var proj = projectPointOnLine((*fishData).position, mouseRay);
179+
var diff = ((*fishData).position - proj);
180+
const limit = 1.2;
181+
let str = (pow(2f, clamp((limit - length(diff)), 0f, limit)) - 1f);
182+
rayRepulsion = (normalize(diff) * str);
190183
var direction = (*fishData).direction;
191184
direction = (direction + (separation * fishBehavior.separationStr));
192185
direction = (direction + (alignment * fishBehavior.alignmentStr));
193186
direction = (direction + (cohesion * fishBehavior.cohesionStr));
194187
direction = (direction + (wallRepulsion * 1e-4));
195-
direction = (direction + (rayRepulsion * 5e-4));
188+
direction = (direction + (rayRepulsion * 0.0015));
196189
direction = (normalize(direction) * clamp(length((*fishData).direction), 0f, 0.01f));
197190
var translation = (direction * (min(999f, timePassed) / 8f));
198191
let nextFishData_1 = (&nextFishData[fishIndex]);

0 commit comments

Comments
 (0)