-
-
Notifications
You must be signed in to change notification settings - Fork 51
feat: ThreeJS 'Compute Geometry' with tgpu #1948
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
45 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 2c0bd2d
working compute particles example
cieplypolar fb077da
TSL import
cieplypolar 6e32fcf
our controls instead of inspector
cieplypolar 4b67891
bumped down threejs, rewrite init in tgpu
cieplypolar 0343769
all compute shaders in tgpu
cieplypolar 2882b81
accessor naming convention
cieplypolar 6753bc1
new api alignment
cieplypolar 4eef60e
thumbnail
cieplypolar 127052c
working three js compute geometry example
cieplypolar 1cb6de5
controls + typescript happy
cieplypolar 7b63297
tiny refactor
cieplypolar 6e7f6be
no ts-ignore
cieplypolar f25d05c
all compute shaders in tgpu
cieplypolar da802c8
new api alignment
cieplypolar 681d824
thumbnail
cieplypolar f80c61d
working compute particles example
cieplypolar 3ed31bb
TSL import
cieplypolar 01aefc9
our controls instead of inspector
cieplypolar 04a6d19
bumped down threejs, rewrite init in tgpu
cieplypolar d33b431
all compute shaders in tgpu
cieplypolar 538d7d6
accessor naming convention
cieplypolar 0685012
new api alignment
cieplypolar 7fb20c9
thumbnail
cieplypolar 4e94772
pnpm lock
cieplypolar d25bf82
Merge branch 'feat/threejs-compute-particles' into feat/threejs-torso
cieplypolar 685a00c
review changes
cieplypolar a6e0a4f
Merge branch 'feat/tgpu-three' into feat/threejs-compute-particles
cieplypolar 263da97
Merge branch 'feat/threejs-compute-particles' into feat/threejs-torso
cieplypolar 12149df
review changes
cieplypolar 23a2e5d
Merge branch 'feat/threejs-compute-particles' into feat/threejs-torso
cieplypolar 4e7fa5c
review changes + info about model loading
cieplypolar 1f3458e
🦕
cieplypolar e8673cb
Merge branch 'feat/tgpu-three' into feat/threejs-torso
cieplypolar 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
Binary file added
BIN
+395 KB
apps/typegpu-docs/public/assets/threejs/compute-geometry/LeePerrySmith.glb
Binary file not shown.
5 changes: 5 additions & 0 deletions
5
apps/typegpu-docs/public/assets/threejs/compute-geometry/LeePerrySmith_License.txt
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,5 @@ | ||
| Creative Commons Licence | ||
| Infinite, 3D Head Scan by Lee Perry-Smith is licensed under a Creative Commons Attribution 3.0 Unported License. | ||
| Based on a work at www.triplegangers.com. | ||
| Permissions beyond the scope of this license may be available at http://www.ir-ltd.net/ | ||
| Please remember: Do what you want with the files, but always mention where you got them from... |
20 changes: 20 additions & 0 deletions
20
apps/typegpu-docs/src/examples/threejs/compute-geometry/index.html
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,20 @@ | ||
| <div class="loading">Model is loading...</div> | ||
| <canvas data-fit-to-container></canvas> | ||
| <style> | ||
| .loading { | ||
| position: absolute; | ||
| top: 50%; | ||
| left: 50%; | ||
| transform: translate(-50%, -50%); | ||
| font-size: 1.5rem; | ||
| color: white; | ||
| background: rgba(0, 0, 0, 0.7); | ||
| padding: 1rem; | ||
| border-radius: 0.5rem; | ||
| z-index: 10; | ||
| } | ||
|
|
||
| .loading.loaded { | ||
| display: none; | ||
| } | ||
| </style> |
231 changes: 231 additions & 0 deletions
231
apps/typegpu-docs/src/examples/threejs/compute-geometry/index.ts
aleksanderkatan marked this conversation as resolved.
Show resolved
Hide resolved
|
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,231 @@ | ||
| /* | ||
| * Based on: https://github.com/mrdoob/three.js/blob/master/examples/webgpu_compute_geometry.html | ||
| */ | ||
| import * as THREE from 'three/webgpu'; | ||
| import * as TSL from 'three/tsl'; | ||
| import { type GLTF, GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; | ||
| import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; | ||
| import * as t3 from '@typegpu/three'; | ||
| import * as d from 'typegpu/data'; | ||
| import * as std from 'typegpu/std'; | ||
|
|
||
| const canvas = document.querySelector('canvas') as HTMLCanvasElement; | ||
| const renderer = new THREE.WebGPURenderer({ canvas, antialias: true }); | ||
| renderer.setPixelRatio(window.devicePixelRatio); | ||
| renderer.setSize(canvas.clientWidth, canvas.clientHeight, false); | ||
|
|
||
| const camera = new THREE.PerspectiveCamera( | ||
| 50, | ||
| canvas.clientWidth / canvas.clientHeight, | ||
| 0.1, | ||
| 10, | ||
| ); | ||
| camera.position.set(0, 0, 1); | ||
|
|
||
| const scene = new THREE.Scene(); | ||
|
|
||
| const bgColor = TSL.screenUV.y.mix(TSL.color(0x9f87f7), TSL.color(0xf2cdcd)); | ||
| const bgVignette = TSL.screenUV.distance(0.5).remapClamp(0.3, 0.8).oneMinus(); | ||
| const bgIntensity = 4; | ||
| scene.backgroundNode = bgColor.mul( | ||
| bgVignette.mul(TSL.color(0xa78ff6).mul(bgIntensity)), | ||
| ); | ||
|
|
||
| const pointerPosition = t3.uniform(TSL.vec4(0), d.vec4f); | ||
| const elasticity = t3.uniform(0.4, d.f32); | ||
| const damping = t3.uniform(0.94, d.f32); | ||
| const brushSize = t3.uniform(0.25, d.f32); | ||
| const brushStrength = t3.uniform(0.22, d.f32); | ||
|
|
||
| const jelly = TSL.Fn(({ renderer, geometry, object }) => { | ||
| const count = geometry.attributes.position.count; | ||
|
|
||
| const positionStorageBufferAttribute = new THREE.StorageBufferAttribute( | ||
| count, | ||
| 3, | ||
| ); | ||
| geometry.setAttribute('storagePosition', positionStorageBufferAttribute); | ||
|
|
||
| const basePositionAccessor = t3.fromTSL( | ||
| TSL.storage( | ||
| geometry.attributes.position as THREE.BufferAttribute, | ||
| 'vec3', | ||
| count, | ||
| ), | ||
| d.arrayOf(d.vec3f), | ||
| ); | ||
| const positionAccessor = t3.fromTSL( | ||
| TSL.storage( | ||
| positionStorageBufferAttribute, | ||
| 'vec3', | ||
| count, | ||
| ), | ||
| d.arrayOf(d.vec3f), | ||
| ); | ||
| const speedAccessor = t3.fromTSL( | ||
| TSL.storage( | ||
| new THREE.StorageBufferAttribute(count, 3), | ||
| 'vec3', | ||
| count, | ||
| ), | ||
| d.arrayOf(d.vec3f), | ||
| ); | ||
|
|
||
| const computeInit = t3.toTSL(() => { | ||
| 'use gpu'; | ||
| positionAccessor.$[t3.instanceIndex.$] = | ||
| basePositionAccessor.$[t3.instanceIndex.$]; | ||
| }).compute(count).setName('Init Mesh'); | ||
|
|
||
| const modelMatrixAccessor = t3.fromTSL( | ||
| TSL.objectWorldMatrix(object), | ||
| d.mat4x4f, | ||
| ); | ||
|
|
||
| const computeUpdate = t3.toTSL(() => { | ||
| 'use gpu'; | ||
| const instanceIdx = t3.instanceIndex.$; | ||
| const basePosition = basePositionAccessor.$[instanceIdx]; | ||
| let position = positionAccessor.$[instanceIdx]; | ||
|
|
||
| if (pointerPosition.$.w === 1) { | ||
| const worldPosition = modelMatrixAccessor.$.mul( | ||
| d.vec4f(position, 1), | ||
| ).xyz; | ||
| const dist = std.distance(worldPosition, pointerPosition.$.xyz); | ||
| const direction = std.normalize( | ||
| pointerPosition.$.xyz.sub(worldPosition), | ||
| ); | ||
| const power = std.max(brushSize.$ - dist, 0) * | ||
| brushStrength.$; | ||
|
|
||
| positionAccessor.$[instanceIdx] = position.add( | ||
| direction.mul(power), | ||
| ); | ||
| position = positionAccessor.$[instanceIdx]; | ||
| } | ||
|
|
||
| const dist = std.distance( | ||
| basePosition, | ||
| position, | ||
| ); | ||
| const force = basePosition | ||
| .sub(position) | ||
| .mul(elasticity.$ * dist); | ||
| const speed = speedAccessor.$[instanceIdx] | ||
| .add(force) | ||
| .mul(damping.$); | ||
|
|
||
| speedAccessor.$[instanceIdx] = d.vec3f(speed); | ||
| positionAccessor.$[instanceIdx] = position.add(speed); | ||
| }).compute(count).setName('Update Jelly'); | ||
|
|
||
| computeUpdate.onInit(() => renderer.compute(computeInit)); | ||
|
|
||
| return computeUpdate; | ||
| }); | ||
|
|
||
| new GLTFLoader().load( | ||
| '/TypeGPU/assets/threejs/compute-geometry/LeePerrySmith.glb', | ||
| // on successful load | ||
| (gltf: GLTF) => { | ||
| const material = new THREE.MeshNormalNodeMaterial(); | ||
| material.geometryNode = jelly() as unknown as () => THREE.Node; | ||
| material.positionNode = TSL.attribute('storagePosition'); // global | ||
|
|
||
| const mesh = gltf.scene.children[0] as THREE.Mesh; | ||
| mesh.scale.setScalar(0.1); | ||
| mesh.material = material; | ||
| scene.add(mesh); | ||
| document.querySelector('.loading')?.classList.add('loaded'); | ||
| }, | ||
| ); | ||
|
|
||
| const raycaster = new THREE.Raycaster(); | ||
| const pointer = new THREE.Vector2(); | ||
| const onPointerMove = (event: PointerEvent) => { | ||
| const rect = canvas.getBoundingClientRect(); | ||
| pointer.set( | ||
| ((event.clientX - rect.left) / rect.width) * 2 - 1, | ||
| -((event.clientY - rect.top) / rect.height) * 2 + 1, | ||
| ); | ||
|
|
||
| raycaster.setFromCamera(pointer, camera); | ||
|
|
||
| const intersects = raycaster.intersectObject(scene); | ||
|
|
||
| if (intersects.length > 0) { | ||
| const { point } = intersects[0]; | ||
| pointerPosition.node.value.copy(new THREE.Vector4(...point, 1)); | ||
| pointerPosition.node.value.w = 1; // enable | ||
| } else { | ||
| pointerPosition.node.value.w = 0; // disable | ||
| } | ||
| }; | ||
| canvas.addEventListener('pointermove', onPointerMove); | ||
|
|
||
| const resizeObserver = new ResizeObserver(() => { | ||
| camera.aspect = canvas.clientWidth / canvas.clientHeight; | ||
| camera.updateProjectionMatrix(); | ||
| renderer.setSize(canvas.clientWidth, canvas.clientHeight, false); | ||
| }); | ||
| resizeObserver.observe(canvas); | ||
|
|
||
| const orbitControls = new OrbitControls(camera, canvas); | ||
| orbitControls.minDistance = 0.7; | ||
| orbitControls.maxDistance = 2; | ||
| if ('ontouchstart' in window) { | ||
| orbitControls.enableRotate = false; // disable rotation on mobile | ||
| } | ||
|
|
||
| function animate() { | ||
| renderer.render(scene, camera); | ||
| } | ||
| renderer.setAnimationLoop(animate); | ||
|
|
||
| // #region Example controls and cleanup | ||
| export const controls = { | ||
| elasticity: { | ||
| initial: 0.4, | ||
| min: 0, | ||
| max: 0.5, | ||
| step: 0.01, | ||
| onSliderChange: (value: number) => { | ||
| elasticity.node.value = value; | ||
| }, | ||
| }, | ||
| damping: { | ||
| initial: 0.94, | ||
| min: 0.9, | ||
| max: 0.98, | ||
| step: 0.01, | ||
| onSliderChange: (value: number) => { | ||
| damping.node.value = value; | ||
| }, | ||
| }, | ||
| 'brush size': { | ||
| initial: 0.25, | ||
| min: 0.1, | ||
| max: 0.5, | ||
| step: 0.01, | ||
| onSliderChange: (value: number) => { | ||
| brushSize.node.value = value; | ||
| }, | ||
| }, | ||
| 'brush strength': { | ||
| initial: 0.22, | ||
| min: 0.1, | ||
| max: 0.3, | ||
| step: 0.01, | ||
| onSliderChange: (value: number) => { | ||
| brushStrength.node.value = value; | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export function onCleanup() { | ||
| renderer.dispose(); | ||
| resizeObserver.unobserve(canvas); | ||
| } | ||
|
|
||
| // #endregion |
7 changes: 7 additions & 0 deletions
7
apps/typegpu-docs/src/examples/threejs/compute-geometry/license.ts
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,7 @@ | ||
| /* | ||
| * Creative Commons Licence | ||
| * Infinite, 3D Head Scan by Lee Perry-Smith is licensed under a Creative Commons Attribution 3.0 Unported License. | ||
| * Based on a work at www.triplegangers.com. | ||
| * Permissions beyond the scope of this license may be available at http://www.ir-ltd.net/ | ||
| * Please remember: Do what you want with the files, but always mention where you got them from... | ||
| */ |
5 changes: 5 additions & 0 deletions
5
apps/typegpu-docs/src/examples/threejs/compute-geometry/meta.json
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,5 @@ | ||
| { | ||
| "title": "Three.js - compute / geometry", | ||
| "category": "threejs", | ||
| "tags": ["experimental"] | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we care that the licence is not listed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add it, just in case