Skip to content

Commit c503acd

Browse files
committed
Cleanup
1 parent bdee185 commit c503acd

File tree

8 files changed

+64
-185
lines changed

8 files changed

+64
-185
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import * as d from 'typegpu/data';
66
import * as THREE from 'three/webgpu';
7-
import { fromTSL, toTSL, uv } from '@typegpu/three';
7+
import { access, fromTSL, toTSL } from '@typegpu/three';
88
import * as std from 'typegpu/std';
99

1010
import * as TSL from 'three/tsl';
@@ -248,7 +248,8 @@ function setupClothMesh(): THREE.Mesh {
248248

249249
clothMaterial.colorNode = toTSL(() => {
250250
'use gpu';
251-
const pattern = checkerBoard(uv().$.mul(5));
251+
const uv = access.uv().$;
252+
const pattern = checkerBoard(uv.mul(5));
252253
return std.mix(d.vec4f(0.4, 0.3, 0.3, 1), d.vec4f(1, 0.5, 0.4, 1), pattern);
253254
});
254255

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

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as THREE from 'three/webgpu';
1+
import { access, fromTSL, toTSL, type TSLAccessor } from '@typegpu/three';
22
import * as TSL from 'three/tsl';
3-
import { fromTSL, toTSL, type TSLAccessor } from '@typegpu/three';
3+
import * as THREE from 'three/webgpu';
44
import * as d from 'typegpu/data';
55
import * as std from 'typegpu/std';
66
import { triNoise3D } from './triNoise.ts';
@@ -29,6 +29,11 @@ interface VerletSimulationOptions {
2929
spherePositionUniform: TSLAccessor<d.Vec3f, THREE.UniformNode<THREE.Vector3>>;
3030
}
3131

32+
type TSLStorageAccessor<T extends d.AnyWgslData> = TSLAccessor<
33+
T,
34+
THREE.StorageBufferNode
35+
>;
36+
3237
export class VerletSimulation {
3338
readonly vertices: Vertex[];
3439
readonly springs: Spring[];
@@ -38,42 +43,22 @@ export class VerletSimulation {
3843
readonly windUniform: TSLAccessor<d.F32, THREE.UniformNode<number>>;
3944
readonly dampeningUniform: TSLAccessor<d.F32, THREE.UniformNode<number>>;
4045

41-
readonly vertexPositionBuffer: TSLAccessor<
42-
d.WgslArray<d.Vec3f>,
43-
THREE.StorageBufferNode
44-
>;
45-
readonly vertexForceBuffer: TSLAccessor<
46-
d.WgslArray<d.Vec3f>,
47-
THREE.StorageBufferNode
48-
>;
49-
readonly vertexParamsBuffer: TSLAccessor<
50-
d.WgslArray<d.Vec3u>,
51-
THREE.StorageBufferNode
52-
>;
53-
readonly springListBuffer: TSLAccessor<
54-
d.WgslArray<d.U32>,
55-
THREE.StorageBufferNode
56-
>;
57-
readonly springVertexIdBuffer: TSLAccessor<
58-
d.WgslArray<d.Vec2u>,
59-
THREE.StorageBufferNode
60-
>;
61-
readonly springRestLengthBuffer: TSLAccessor<
62-
d.WgslArray<d.F32>,
63-
THREE.StorageBufferNode
64-
>;
65-
readonly springForceBuffer: TSLAccessor<
66-
d.WgslArray<d.Vec3f>,
67-
THREE.StorageBufferNode
68-
>;
46+
readonly vertexPositionBuffer: TSLStorageAccessor<d.WgslArray<d.Vec3f>>;
47+
readonly vertexForceBuffer: TSLStorageAccessor<d.WgslArray<d.Vec3f>>;
48+
readonly vertexParamsBuffer: TSLStorageAccessor<d.WgslArray<d.Vec3u>>;
49+
readonly springListBuffer: TSLStorageAccessor<d.WgslArray<d.U32>>;
50+
readonly springVertexIdBuffer: TSLStorageAccessor<d.WgslArray<d.Vec2u>>;
51+
readonly springRestLengthBuffer: TSLStorageAccessor<d.WgslArray<d.F32>>;
52+
readonly springForceBuffer: TSLStorageAccessor<d.WgslArray<d.Vec3f>>;
6953

7054
readonly computeSpringForces: TSL.ShaderNodeObject<THREE.ComputeNode>;
7155
readonly computeVertexForces: TSL.ShaderNodeObject<THREE.ComputeNode>;
7256

73-
constructor(
74-
{ sphereRadius, sphereUniform, spherePositionUniform }:
75-
VerletSimulationOptions,
76-
) {
57+
constructor({
58+
sphereRadius,
59+
sphereUniform,
60+
spherePositionUniform,
61+
}: VerletSimulationOptions) {
7762
this.vertices = [];
7863
this.springs = [];
7964
this.vertexColumns = [];
@@ -120,7 +105,7 @@ export class VerletSimulation {
120105
for (let y = 0; y <= clothNumSegmentsY; y++) {
121106
const posX = x * (clothWidth / clothNumSegmentsX) - clothWidth * 0.5;
122107
const posZ = y * (clothHeight / clothNumSegmentsY);
123-
const isFixed = (y === 0) && ((x % 5) === 0); // make some of the top vertices' positions fixed
108+
const isFixed = y === 0 && x % 5 === 0; // make some of the top vertices' positions fixed
124109
const vertex = addVerletVertex(posX, clothHeight * 0.5, posZ, isFixed);
125110
column.push(vertex);
126111
}
@@ -229,32 +214,31 @@ export class VerletSimulation {
229214
// This sets up the compute shaders for the verlet simulation
230215
// There are two shaders that are executed for each simulation step
231216

232-
const instanceIndex = fromTSL(TSL.instanceIndex, { type: d.u32 });
233-
234217
// 1. computeSpringForces:
235218
// This shader computes a force for each spring, depending on the distance between the two vertices connected by that spring and the targeted rest length
236219

237220
this.computeSpringForces = toTSL(() => {
238221
'use gpu';
239222

240-
if (instanceIndex.$ >= springCount) {
223+
const idx = access.instanceIndex.$;
224+
if (idx >= springCount) {
241225
// compute Shaders are executed in groups of 64, so instanceIndex might be bigger than the amount of springs.
242226
// in that case, return.
243227
return;
244228
}
245229

246-
const vertexId = this.springVertexIdBuffer.$[instanceIndex.$];
247-
const restLength = this.springRestLengthBuffer.$[instanceIndex.$];
230+
const vertexId = this.springVertexIdBuffer.$[idx];
231+
const restLength = this.springRestLengthBuffer.$[idx];
248232

249233
const vertex0Position = this.vertexPositionBuffer.$[vertexId.x];
250234
const vertex1Position = this.vertexPositionBuffer.$[vertexId.y];
251235

252236
const delta = vertex1Position.sub(vertex0Position);
253237
const dist = std.max(std.length(delta), 0.000001);
254238
const force = delta.mul(
255-
(dist - restLength) * this.stiffnessUniform.$ * 0.5 / dist,
239+
((dist - restLength) * this.stiffnessUniform.$ * 0.5) / dist,
256240
);
257-
this.springForceBuffer.$[instanceIndex.$] = d.vec3f(force);
241+
this.springForceBuffer.$[idx] = d.vec3f(force);
258242
}).compute(springCount);
259243

260244
// 2. computeVertexForces:
@@ -267,7 +251,7 @@ export class VerletSimulation {
267251

268252
this.computeVertexForces = toTSL(() => {
269253
'use gpu';
270-
const idx = instanceIndex.$;
254+
const idx = access.instanceIndex.$;
271255

272256
if (idx >= vertexCount) {
273257
// compute shaders are executed in groups of 64, so instanceIndex might be bigger than the amount of vertices.
@@ -315,7 +299,7 @@ export class VerletSimulation {
315299
const deltaSphere = position.add(force).sub(spherePositionUniform.$);
316300
const dist = std.length(deltaSphere);
317301
const sphereForce = deltaSphere.mul(
318-
std.max(0, sphereRadius - dist) / dist * sphereUniform.$,
302+
(std.max(0, sphereRadius - dist) / dist) * sphereUniform.$,
319303
);
320304
force = force.add(sphereForce);
321305

apps/typegpu-docs/src/examples/threejs/simple/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as THREE from 'three/webgpu';
2-
import { time, toTSL, uv } from '@typegpu/three';
2+
import { access, toTSL } from '@typegpu/three';
33
import { perlin3d } from '@typegpu/noise';
44
import * as d from 'typegpu/data';
55
import { tanh } from 'typegpu/std';
@@ -27,8 +27,8 @@ const material = new THREE.MeshBasicNodeMaterial();
2727

2828
material.colorNode = toTSL(() => {
2929
'use gpu';
30-
const coords = uv().$.mul(2);
31-
const pattern = perlin3d.sample(d.vec3f(coords, time.$ * 0.2));
30+
const coords = access.uv().$.mul(2);
31+
const pattern = perlin3d.sample(d.vec3f(coords, access.time.$ * 0.2));
3232
return d.vec4f(tanh(pattern * 5), 0.2, 0.4, 1);
3333
});
3434

packages/typegpu-three/README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,30 @@
88

99
A helper library for using TypeGPU with Three.js.
1010

11-
## API draft
12-
13-
- TSL nodes don't really have types associated with them.
14-
- We can infer the return type of a node like in shell-less functions
15-
1611
```ts
12+
import * as TSL from 'three/tsl';
13+
import { access, fromTSL, toTSL } from '@typegpu/three';
1714
import { fract } from 'typegpu/std';
18-
import tgpu3, { uv } from '@typegpu/three';
19-
20-
const material = new THREE.MeshBasicNodeMaterial();
21-
// We reexport builtin TSL nodes as `accessors`
22-
material.colorNode = toTSL(() => {
23-
'kernel';
24-
return fract(uv.$.mul(4));
25-
});
2615

27-
// Users can also wrap custom TSL nodes and use them the same way
16+
const material1 = new THREE.MeshBasicNodeMaterial();
2817
const pattern = TSL.texture(detailMap, TSL.uv().mul(10));
18+
// `fromTSL` can be used to access any TSL node from a TypeGPU function
2919
const patternAccess = fromTSL(pattern, { type: d.vec4f });
30-
material.colorNode = toTSL(() => {
31-
'kernel';
20+
material1.colorNode = toTSL(() => {
21+
'use gpu';
3222
return patternAccess.$;
3323
});
24+
25+
const material2 = new THREE.MeshBasicNodeMaterial();
26+
material2.colorNode = toTSL(() => {
27+
'use gpu';
28+
// Many builtin TSL nodes are already reexported as `accessors`
29+
const uv = access.uv().$;
30+
31+
if (uv.x < 0.5) {
32+
return d.vec4f(fract(uv.mul(4)), 0, 1);
33+
}
34+
35+
return d.vec4f(1, 0, 0, 1);
36+
});
3437
```

packages/typegpu-three/src/builtin-accessors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export const uv = tgpu['~unstable'].comptime((index?: number | undefined) =>
88
);
99

1010
export const time = fromTSL(TSL.time, { type: d.f32 });
11+
12+
export const instanceIndex = fromTSL(TSL.instanceIndex, { type: d.u32 });
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
// export { TypeGPUMaterial } from './typegpu-material.ts';
2-
3-
export { time, uv } from './builtin-accessors.ts';
4-
export { fromTSL, type TSLAccessor, toTSL } from './typegpu-node.ts';
1+
export * as access from './builtin-accessors.ts';
2+
export { fromTSL, toTSL, type TSLAccessor } from './typegpu-node.ts';

packages/typegpu-three/src/typegpu-material.ts

Lines changed: 0 additions & 109 deletions
This file was deleted.

pnpm-workspace.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
packages:
2-
- "packages/*"
3-
- "apps/*"
2+
- 'packages/*'
3+
- 'apps/*'
44

55
catalog:
66
arktype: ^2.1.22
@@ -12,13 +12,13 @@ catalogs:
1212
jiti: ^2.6.0
1313
types:
1414
typescript: ^5.8.2
15-
"@webgpu/types": ^0.1.66
16-
"@types/three": "^0.180.0"
15+
'@webgpu/types': ^0.1.66
16+
'@types/three': '^0.180.0'
1717
test:
1818
vitest: ^3.2.4
1919
frontend:
20-
"vite-imagetools": ^9.0.0
21-
"fuse.js": ^7.1.0
20+
'vite-imagetools': ^9.0.0
21+
'fuse.js': ^7.1.0
2222
example:
23-
"wgpu-matrix": ^3.4.0
23+
'wgpu-matrix': ^3.4.0
2424
three: ^0.180.0

0 commit comments

Comments
 (0)