Skip to content

Commit dd0494b

Browse files
committed
Update example to match the new API
1 parent 841fa65 commit dd0494b

3 files changed

Lines changed: 23 additions & 20 deletions

File tree

apps/typegpu-docs/src/examples/react/confetti/index.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import tgpu, { d, std, type TgpuVertexFn } from 'typegpu';
1+
import tgpu, { d, std, type TgpuBuffer, type TgpuVertexFn } from 'typegpu';
22
import {
33
useFrame,
44
useRoot,
@@ -51,12 +51,14 @@ const rotate = (v: d.v2f, angle: number) => {
5151
return pos;
5252
};
5353

54-
function createRandomPositions() {
55-
return Array.from({ length: PARTICLE_AMOUNT }, () => ({
56-
position: d.vec2f(Math.random() * 2 - 1, Math.random() * 2 + 1),
57-
velocity: d.vec2f((Math.random() * 2 - 1) / 50, -(Math.random() / 25 + 0.01)),
58-
seed: Math.random(),
59-
}));
54+
function writeRandomPositions(buffer: TgpuBuffer<ReturnType<typeof dataLayout.schemaForCount>>) {
55+
buffer.write(
56+
Array.from({ length: PARTICLE_AMOUNT }, () => ({
57+
position: d.vec2f(Math.random() * 2 - 1, Math.random() * 2 + 1),
58+
velocity: d.vec2f((Math.random() * 2 - 1) / 50, -(Math.random() / 25 + 0.01)),
59+
seed: Math.random(),
60+
})),
61+
);
6062
}
6163

6264
const computeLayout = tgpu.bindGroupLayout({
@@ -113,16 +115,19 @@ function App() {
113115
// buffers
114116

115117
const particleGeometryBuffer = useBuffer(d.arrayOf(ParticleGeometry, PARTICLE_AMOUNT), {
116-
initial: () =>
117-
Array.from({ length: PARTICLE_AMOUNT }, () => ({
118-
angle: Math.floor(Math.random() * 50) - 10,
119-
tilt: Math.floor(Math.random() * 10) - 10 - 10,
120-
color: COLOR_PALETTE[Math.floor(Math.random() * COLOR_PALETTE.length)],
121-
})),
118+
initial: (buffer) => {
119+
buffer.write(
120+
Array.from({ length: PARTICLE_AMOUNT }, () => ({
121+
angle: Math.floor(Math.random() * 50) - 10,
122+
tilt: Math.floor(Math.random() * 10) - 10 - 10,
123+
color: COLOR_PALETTE[Math.floor(Math.random() * COLOR_PALETTE.length)],
124+
})),
125+
);
126+
},
122127
}).$usage('vertex');
123128

124129
const particleDataBuffer = useBuffer(d.arrayOf(ParticleData, PARTICLE_AMOUNT), {
125-
initial: createRandomPositions,
130+
initial: writeRandomPositions,
126131
}).$usage('storage', 'uniform', 'vertex');
127132

128133
const aspectRatio = useUniformValue(d.f32, 1);
@@ -185,7 +190,7 @@ function App() {
185190
<button
186191
type="button"
187192
className="text-4xl shadow rounded p-4"
188-
onClick={() => particleDataBuffer.write(createRandomPositions())}
193+
onClick={() => writeRandomPositions(particleDataBuffer)}
189194
>
190195
🎉
191196
</button>

apps/typegpu-docs/src/examples/react/shifting-gradient/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useMemo } from 'react';
12
import { d, common, std } from 'typegpu';
23
import { useConfigureContext, useFrame, useRoot, useUniformValue } from '@typegpu/react';
34
import { oklabToRgb, hexToOklab } from '@typegpu/color';
@@ -28,7 +29,7 @@ function App() {
2829
[root, time],
2930
);
3031

31-
const { canvasRefCallback, ctxRef } = useConfigureContext();
32+
const { canvasRefCallback, ctxRef } = useConfigureContext({ alphaMode: 'premultiplied' });
3233
useFrame(({ elapsedSeconds }) => {
3334
if (!ctxRef.current) return;
3435

@@ -42,7 +43,6 @@ function App() {
4243
// #region Example controls and cleanup
4344

4445
import { createRoot } from 'react-dom/client';
45-
import { useMemo } from 'react';
4646
const reactRoot = createRoot(document.getElementById('example-app') as HTMLDivElement);
4747
reactRoot.render(<App />);
4848

packages/typegpu-react/src/use-buffer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { useChangeDetection, useDeferredCleanup, useStableSchema } from './helpe
44
import { useRoot } from './root-context.tsx';
55

66
export interface UseBufferOptions<TSchema extends d.AnyData> {
7-
initial?:
8-
| ((buffer: TgpuBuffer<TSchema>) => d.InferInput<NoInfer<TSchema>>)
9-
| d.InferInput<NoInfer<TSchema>>;
7+
initial?: ((buffer: TgpuBuffer<TSchema>) => void) | d.InferInput<NoInfer<TSchema>>;
108
onInit?: (buffer: TgpuBuffer<TSchema>) => void;
119
}
1210

0 commit comments

Comments
 (0)