Skip to content

Commit a9b39f7

Browse files
committed
Update docs
1 parent 5206db5 commit a9b39f7

1 file changed

Lines changed: 94 additions & 4 deletions

File tree

  • apps/typegpu-docs/src/content/docs/ecosystem/typegpu-react

apps/typegpu-docs/src/content/docs/ecosystem/typegpu-react/index.mdx

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ help with it.
1010
import { Tabs, TabItem } from '@astrojs/starlight/components';
1111

1212
Refer to [TypeGPU's installation guide](/TypeGPU/getting-started/) for setting up TypeGPU if you haven't already.
13-
After that, install React and @typegpu/react using the package manager of your choice.
13+
After that, install React and `@typegpu/react` using the package manager of your choice.
1414

1515
<Tabs syncKey="package-manager">
1616
<TabItem label="npm" icon="seti:npm">
1717
```sh frame=none
18-
npm install react @typegpu/react
18+
npm install @typegpu/react
1919
```
2020
</TabItem>
2121
<TabItem label="pnpm" icon="pnpm">
2222
```sh frame=none
23-
pnpm add react @typegpu/react
23+
pnpm add @typegpu/react
2424
```
2525
</TabItem>
2626
<TabItem label="yarn" icon="seti:yarn">
2727
```sh frame=none
28-
yarn add react @typegpu/react
28+
yarn add @typegpu/react
2929
```
3030
</TabItem>
3131
</Tabs>
@@ -39,6 +39,96 @@ import { useRoot } from '@typegpu/react';
3939

4040
function MyEffect() {
4141
const root = useRoot();
42+
43+
// the rest of our code will be here
44+
}
45+
```
46+
47+
Then we create a [Render Pipeline](/TypeGPU/fundamentals/pipelines) that emits a red color over the whole canvas, and we memoize it.
48+
49+
```tsx twoslash
50+
import React from 'react';
51+
import { useMemo } from 'react';
52+
import { d, common } from 'typegpu';
53+
import { useConfigureContext, useFrame, useRoot, useUniform } from '@typegpu/react';
54+
55+
function MyEffect() {
56+
const root = useRoot();
57+
// ---cut---
58+
59+
const renderPipeline = useMemo(
60+
() =>
61+
root.createRenderPipeline({
62+
vertex: common.fullScreenTriangle,
63+
fragment: ({ uv }) => {
64+
'use gpu';
65+
// r g b a
66+
return d.vec4f(1, 0, 0, 1);
67+
},
68+
}),
69+
[root],
70+
);
71+
// ---cut-after---
72+
}
73+
```
74+
75+
We need to update our imports to get access to both `d` and `common` from typegpu:
76+
77+
```ts
78+
import { d, common } from 'typegpu';
79+
```
80+
81+
Then we call the `useConfigureContext` hook, which will give us a `ref` to pass into a `<canvas>` element, as well as access to the WebGPU context via `ctxRef`.
82+
83+
```ts
84+
// ...
85+
const { ref, ctxRef } = useConfigureContext();
86+
87+
return <canvas ref={ref} className="absolute inset-0" />;
88+
}
89+
```
90+
91+
After these changes, you should see a blank red canvas on your webpage. Let's make the effect more interesting.
92+
93+
### Dynamic values
94+
95+
To pass dynamic values to your shader without having to recompile it (compilation takes significant time), you can use `useUniform` or `useMirroredUniform`, depending on the situation.
96+
97+
### Complete example
98+
99+
```tsx twoslash
100+
import React from 'react';
101+
// ---cut---
102+
import { useMemo } from 'react';
103+
import { d, common } from 'typegpu';
104+
import { useConfigureContext, useFrame, useRoot, useUniform } from '@typegpu/react';
105+
106+
function MyEffect() {
107+
const root = useRoot();
108+
const time = useUniform(d.f32);
109+
110+
const renderPipeline = useMemo(
111+
() =>
112+
root.createRenderPipeline({
113+
vertex: common.fullScreenTriangle,
114+
fragment: ({ uv }) => {
115+
'use gpu';
116+
return d.vec4f((uv * 5 + time.$) % 1, 0, 1);
117+
},
118+
}),
119+
[root, time],
120+
);
121+
122+
const { ref, ctxRef } = useConfigureContext();
123+
useFrame(({ elapsedSeconds }) => {
124+
if (!ctxRef.current) return;
125+
126+
time.write(elapsedSeconds);
127+
128+
renderPipeline.withColorAttachment({ view: ctxRef.current }).draw(3);
129+
});
130+
131+
return <canvas ref={ref} className="absolute inset-0" />;
42132
}
43133
```
44134

0 commit comments

Comments
 (0)