Skip to content

Commit d59e8ba

Browse files
committed
Tweaks and docs
1 parent c503acd commit d59e8ba

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

apps/typegpu-docs/astro.config.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ export default defineConfig({
209209
items: stripFalsy([
210210
{
211211
label: '@typegpu/noise',
212-
slug: 'ecosystem/typegpu-noise',
212+
j slug: 'ecosystem/typegpu-noise',
213+
},
214+
{
215+
label: '@typegpu/three',
216+
slug: 'ecosystem/typegpu-three',
213217
},
214218
DEV && {
215219
label: '@typegpu/color',
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
title: "@typegpu/three"
3+
---
4+
5+
TSL (Three.js Shading Language) 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 computeShader = Fn(() => {
20+
//
21+
// ... TSL code ...
22+
//
23+
}).compute(threadCount);
24+
```
25+
26+
TypeGPU:
27+
```ts
28+
const computeShader = toTSL(() => {
29+
'use gpu';
30+
//
31+
// ... TypeGPU code ...
32+
//
33+
}).compute(threadCount);
34+
```
35+
36+
## Function definition
37+
38+
TSL:
39+
```ts
40+
const oscSine = Fn(([t = time]) => {
41+
return t.add(0.75).mul(Math.PI * 2).sin().mul(0.5).add(0.5);
42+
});
43+
```
44+
45+
TypeGPU:
46+
```ts
47+
const oscSine = (t: number) => {
48+
'use gpu';
49+
return std.sin((t + 0.75) * Math.PI * 2) * 0.5 + 0.5;
50+
};
51+
```
52+
53+
## If statements
54+
55+
TSL:
56+
```ts
57+
If(instanceIndex.greaterThanEqual(uint(vertexCount)), () => {
58+
Return();
59+
});
60+
```
61+
62+
TypeGPU:
63+
```ts
64+
if (access.instanceIndex.$ >= vertexCount) {
65+
return;
66+
}
67+
```
68+
69+
## For loops
70+
71+
TSL:
72+
```ts
73+
Loop({ start: ptrStart, end: ptrEnd, type: 'uint', condition: '<' }, ({ i }) => {
74+
const springId = springListBuffer.element( i ).toVar( 'springId' );
75+
const springForce = springForceBuffer.element( springId );
76+
const springVertexIds = springVertexIdBuffer.element( springId );
77+
const factor = select( springVertexIds.x.equal( instanceIndex ), 1.0, - 1.0 );
78+
force.addAssign( springForce.mul( factor ) );
79+
});
80+
```
81+
82+
TypeGPU:
83+
```ts
84+
for (let i = ptrStart; i < ptrEnd; i++) {
85+
const springId = springListBuffer.$[i];
86+
const springForce = springForceBuffer.$[springId];
87+
const springVertexIds = springVertexIdBuffer.$[springId];
88+
const factor = std.select(-1, 1, springVertexIds.x === idx);
89+
force = force.add(springForce.mul(d.f32(factor)));
90+
}
91+
```

apps/typegpu-docs/src/examples/threejs/compute-cloth/verlet.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,6 @@ export class VerletSimulation {
247247
// Then it adds a gravital force, wind force, and the collision with the sphere.
248248
// In the end it adds the force to the vertex' position.
249249

250-
const time = fromTSL(TSL.time, { type: d.f32 });
251-
252250
this.computeVertexForces = toTSL(() => {
253251
'use gpu';
254252
const idx = access.instanceIndex.$;
@@ -279,19 +277,16 @@ export class VerletSimulation {
279277
const springId = this.springListBuffer.$[i];
280278
const springForce = this.springForceBuffer.$[springId];
281279
const springVertexIds = this.springVertexIdBuffer.$[springId];
282-
const factor = std.select(
283-
d.f32(-1),
284-
d.f32(1),
285-
springVertexIds.x === idx,
286-
);
287-
force = force.add(springForce.mul(factor));
280+
const factor = std.select(-1, 1, springVertexIds.x === idx);
281+
force = force.add(springForce.mul(d.f32(factor)));
288282
}
289283

290284
// gravity
291285
force.y -= 0.00005;
292286

293287
// wind
294-
const noise = (triNoise3D(position, 1, time.$) - 0.2) * 0.0001;
288+
const time = access.time.$;
289+
const noise = (triNoise3D(position, 1, time) - 0.2) * 0.0001;
295290
const windForce = noise * this.windUniform.$;
296291
force.z -= windForce;
297292

0 commit comments

Comments
 (0)