Skip to content

Commit 2076be6

Browse files
authored
feat: Tweaks to feat/tgpu-three (#1954)
1 parent d9e0446 commit 2076be6

File tree

17 files changed

+399
-366
lines changed

17 files changed

+399
-366
lines changed

apps/typegpu-docs/astro.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ export default defineConfig({
211211
label: '@typegpu/noise',
212212
slug: 'ecosystem/typegpu-noise',
213213
},
214+
{
215+
label: '@typegpu/three',
216+
slug: 'ecosystem/typegpu-three',
217+
badge: { text: 'new' },
218+
},
214219
DEV && {
215220
label: '@typegpu/color',
216221
slug: 'ecosystem/typegpu-color',
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: "@typegpu/three"
3+
---
4+
5+
[TSL (Three.js Shading Language)](https://github.com/mrdoob/three.js/wiki/Three.js-Shading-Language#function) is a node-based shader composition system for Three.js. Shader logic and control flow is built up by composing special functions,
6+
with a focus on composability, intuitive sharing of logic across modules and customizability. TypeGPU fits naturally into this system thanks to the `@typegpu/three` package. You can choose to write your TSL building blocks in TypeGPU, which has a few benefits:
7+
- Control-flow like `if` statements and `for` loops makes use of familiar JavaScript syntax instead of special functions.
8+
- The code you write is semantically valid JavaScript, with types flowing through each expression.
9+
- Unit-testability, since you can call these functions on the CPU
10+
11+
Below are a select few cases comparing TSL and TypeGPU:
12+
13+
import { Card, CardGrid } from '@astrojs/starlight/components';
14+
15+
## Node definition
16+
17+
TSL:
18+
```ts
19+
const simulate = Fn(() => {
20+
//
21+
// ... TSL code ...
22+
//
23+
});
24+
```
25+
26+
TypeGPU:
27+
```ts twoslash
28+
import * as t3 from '@typegpu/three';
29+
// ---cut---
30+
const simulate = t3.toTSL(() => {
31+
'use gpu';
32+
//
33+
// ... TypeGPU code ...
34+
//
35+
});
36+
```
37+
38+
## Function definition
39+
40+
TSL:
41+
```ts
42+
const oscSine = Fn(([t = time]) => {
43+
return t.add(0.75).mul(Math.PI * 2).sin().mul(0.5).add(0.5);
44+
});
45+
```
46+
47+
TypeGPU:
48+
```ts twoslash
49+
import * as std from 'typegpu/std';
50+
// ---cut---
51+
const oscSine = (t: number) => {
52+
'use gpu';
53+
return std.sin((t + 0.75) * Math.PI * 2) * 0.5 + 0.5;
54+
};
55+
```
56+
57+
## If statements
58+
59+
TSL:
60+
```ts
61+
If(instanceIndex.greaterThanEqual(uint(vertexCount)), () => {
62+
Return();
63+
});
64+
```
65+
66+
TypeGPU:
67+
```ts twoslash
68+
import * as t3 from '@typegpu/three';
69+
declare const vertexCount: number;
70+
const some = () => {
71+
// ---cut-before---
72+
if (t3.instanceIndex.$ >= vertexCount) {
73+
return;
74+
}
75+
// ---cut-after---
76+
};
77+
```
78+
79+
## For loops
80+
81+
TSL:
82+
```ts
83+
Loop({ start: ptrStart, end: ptrEnd, type: 'uint', condition: '<' }, ({ i }) => {
84+
const springId = springListBuffer.element( i ).toVar( 'springId' );
85+
const springForce = springForceBuffer.element( springId );
86+
const springVertexIds = springVertexIdBuffer.element( springId );
87+
const factor = select( springVertexIds.x.equal( instanceIndex ), 1.0, - 1.0 );
88+
force.addAssign( springForce.mul( factor ) );
89+
});
90+
```
91+
92+
TypeGPU:
93+
```ts
94+
for (let i = ptrStart; i < ptrEnd; i++) {
95+
const springId = springListBuffer.$[i];
96+
const springForce = springForceBuffer.$[springId];
97+
const springVertexIds = springVertexIdBuffer.$[springId];
98+
const factor = std.select(-1, 1, springVertexIds.x === idx);
99+
force = force.add(springForce.mul(d.f32(factor)));
100+
}
101+
```

0 commit comments

Comments
 (0)