|
| 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 | +``` |
0 commit comments