-
-
Notifications
You must be signed in to change notification settings - Fork 51
feat: Tweaks to feat/tgpu-three #1954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c503acd
Cleanup
iwoplaza d59e8ba
Tweaks and docs
iwoplaza a2e96fb
Cleanup
iwoplaza 7fbab34
Update to t3 instead of access
iwoplaza a3f5f9a
More docs
iwoplaza 82109fc
Update index.ts
iwoplaza 33da2e0
Simpler API
iwoplaza d66e5e3
Working on shorthands
iwoplaza ee1ab05
Using shorthands in attractor example
iwoplaza 5a4888d
More refactors
iwoplaza 0886c95
Using shorthands in other examples
iwoplaza 5ff4950
Update sandboxModules.ts
iwoplaza 1aa0015
Merge branch 'main' into feat/tgpu-three-tweaks
iwoplaza ac8497c
Merge branch 'feat/tgpu-three' into feat/tgpu-three-tweaks
iwoplaza 079e2e1
Fix compute cloth example
iwoplaza ffbe8b0
Review fixes
iwoplaza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
apps/typegpu-docs/src/content/docs/ecosystem/typegpu-three.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| --- | ||
| title: "@typegpu/three" | ||
| --- | ||
|
|
||
| [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, | ||
| 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: | ||
| - Control-flow like `if` statements and `for` loops makes use of familiar JavaScript syntax instead of special functions. | ||
| - The code you write is semantically valid JavaScript, with types flowing through each expression. | ||
| - Unit-testability, since you can call these functions on the CPU | ||
|
|
||
| Below are a select few cases comparing TSL and TypeGPU: | ||
|
|
||
| import { Card, CardGrid } from '@astrojs/starlight/components'; | ||
|
|
||
| ## Node definition | ||
|
|
||
| TSL: | ||
| ```ts | ||
| const simulate = Fn(() => { | ||
| // | ||
| // ... TSL code ... | ||
| // | ||
| }); | ||
| ``` | ||
|
|
||
| TypeGPU: | ||
| ```ts twoslash | ||
| import * as t3 from '@typegpu/three'; | ||
| // ---cut--- | ||
| const simulate = t3.toTSL(() => { | ||
| 'use gpu'; | ||
| // | ||
| // ... TypeGPU code ... | ||
| // | ||
| }); | ||
| ``` | ||
|
|
||
| ## Function definition | ||
|
|
||
| TSL: | ||
| ```ts | ||
| const oscSine = Fn(([t = time]) => { | ||
| return t.add(0.75).mul(Math.PI * 2).sin().mul(0.5).add(0.5); | ||
| }); | ||
| ``` | ||
|
|
||
| TypeGPU: | ||
| ```ts twoslash | ||
| import * as std from 'typegpu/std'; | ||
| // ---cut--- | ||
| const oscSine = (t: number) => { | ||
| 'use gpu'; | ||
| return std.sin((t + 0.75) * Math.PI * 2) * 0.5 + 0.5; | ||
| }; | ||
| ``` | ||
|
|
||
| ## If statements | ||
|
|
||
| TSL: | ||
| ```ts | ||
| If(instanceIndex.greaterThanEqual(uint(vertexCount)), () => { | ||
| Return(); | ||
| }); | ||
| ``` | ||
|
|
||
| TypeGPU: | ||
| ```ts twoslash | ||
| import * as t3 from '@typegpu/three'; | ||
| declare const vertexCount: number; | ||
| const some = () => { | ||
| // ---cut-before--- | ||
| if (t3.instanceIndex.$ >= vertexCount) { | ||
| return; | ||
| } | ||
| // ---cut-after--- | ||
| }; | ||
| ``` | ||
|
|
||
| ## For loops | ||
|
|
||
| TSL: | ||
| ```ts | ||
| Loop({ start: ptrStart, end: ptrEnd, type: 'uint', condition: '<' }, ({ i }) => { | ||
| const springId = springListBuffer.element( i ).toVar( 'springId' ); | ||
| const springForce = springForceBuffer.element( springId ); | ||
| const springVertexIds = springVertexIdBuffer.element( springId ); | ||
| const factor = select( springVertexIds.x.equal( instanceIndex ), 1.0, - 1.0 ); | ||
| force.addAssign( springForce.mul( factor ) ); | ||
| }); | ||
| ``` | ||
|
|
||
| TypeGPU: | ||
| ```ts | ||
| for (let i = ptrStart; i < ptrEnd; i++) { | ||
| const springId = springListBuffer.$[i]; | ||
| const springForce = springForceBuffer.$[springId]; | ||
| const springVertexIds = springVertexIdBuffer.$[springId]; | ||
| const factor = std.select(-1, 1, springVertexIds.x === idx); | ||
| force = force.add(springForce.mul(d.f32(factor))); | ||
| } | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.