Skip to content

Commit 23ff5c5

Browse files
committed
Use published @typegpu/sdf
1 parent 22fccea commit 23ff5c5

File tree

5 files changed

+103
-95
lines changed

5 files changed

+103
-95
lines changed

apps/phoure-www/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@typegpu/color": "workspace:*",
3030
"@typegpu/common": "workspace:*",
3131
"@typegpu/noise": "^0.1.0",
32-
"@typegpu/sdf": "workspace:*",
32+
"@typegpu/sdf": "^0.0.1",
3333
"@types/react": "^18.0.2",
3434
"@types/react-dom": "^18.0.2",
3535
"astro": "^5.7.0",

apps/phoure-www/src/lib/GameEngine/postProcessingStep.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export const PostProcessingStep = ({
3737
const passColorAttachment = {
3838
// view is acquired and set in render loop.
3939
view: undefined as unknown as GPUTextureView,
40-
4140
clearValue: [0, 0, 0, 1],
4241
loadOp: 'clear' as const,
4342
storeOp: 'store' as const,

apps/phoure-www/src/lib/GameEngine/sdfRenderer/sdfRenderer.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const ONE_OVER_SUPER_SAMPLES = 1 / SUPER_SAMPLES;
3131
const SUB_SAMPLES = 16;
3232
const MAX_REFL = 3;
3333

34-
const getRandomSeedPrimer = tgpu['~unstable'].accessor(d.f32);
35-
const getAccumulatedLayers = tgpu['~unstable'].accessor(d.f32);
34+
const randomSeedPrimerAccess = tgpu['~unstable'].accessor(d.f32);
35+
const accumulatedLayersAccess = tgpu['~unstable'].accessor(d.f32);
3636

3737
const Reflection = d.struct({
3838
color: d.vec3f,
@@ -178,11 +178,11 @@ const mainComputeFn = tgpu['~unstable'].computeFn({
178178
})((input) => {
179179
const preSeed = d.vec2f(input.gid.xy);
180180
randf.seed2(
181-
preSeed.mul(0.1646936793).add(getRandomSeedPrimer.$ * 0.934534732),
181+
preSeed.mul(0.1646936793).add(randomSeedPrimerAccess.$ * 0.934534732),
182182
);
183183

184-
const prev_layers = getAccumulatedLayers.$;
185-
const prev_render = std.textureLoad(
184+
const prevLayers = accumulatedLayersAccess.$;
185+
const prevRender = std.textureLoad(
186186
mainLayout.$.previousRender,
187187
input.gid.xy,
188188
0,
@@ -203,15 +203,15 @@ const mainComputeFn = tgpu['~unstable'].computeFn({
203203
const gamma = 2.2;
204204
acc = std.pow(acc, d.vec3f(1 / gamma));
205205

206-
let new_render = d.vec4f(acc, 1);
207-
if (prev_layers > 0) {
208-
new_render = prev_render
209-
.mul(prev_layers)
206+
let newRender = d.vec4f(acc, 1);
207+
if (prevLayers > 0) {
208+
newRender = prevRender
209+
.mul(prevLayers)
210210
.add(d.vec4f(acc, 1.0))
211-
.div(prev_layers + 1);
211+
.div(prevLayers + 1);
212212
}
213213

214-
std.textureStore(mainLayout.$.mainOutput, input.gid.xy, new_render);
214+
std.textureStore(mainLayout.$.mainOutput, input.gid.xy, newRender);
215215
});
216216

217217
const auxLayout = tgpu.bindGroupLayout({
@@ -222,40 +222,39 @@ const auxComputeFn = tgpu['~unstable'].computeFn({
222222
workgroupSize: [BlockSize, BlockSize],
223223
in: { gid: d.builtin.globalInvocationId },
224224
})((input) => {
225-
const offset = d.vec2f(0.5);
226-
227-
const march_result = MarchResult();
228-
const shape_ctx = ShapeContext();
229-
shape_ctx.rayPos = constructRayPos();
230-
shape_ctx.rayDir = constructRayDir(d.vec2f(input.gid.xy).add(offset));
231-
shape_ctx.rayDistance = 0;
225+
const marchResult = MarchResult();
226+
const shapeCtx = ShapeContext({
227+
rayPos: constructRayPos(),
228+
rayDir: constructRayDir(d.vec2f(input.gid.xy).add(d.vec2f(0.5))),
229+
rayDistance: 0,
230+
});
232231

233-
march(shape_ctx, MarchParams.maxSteps.$, march_result);
232+
march(shapeCtx, MarchParams.maxSteps.$, marchResult);
234233

235-
let world_normal = d.vec3f();
234+
let worldNormal = d.vec3f();
236235

237-
if (march_result.steps >= MarchParams.maxSteps.$) {
238-
world_normal = std.neg(shape_ctx.rayDir);
236+
if (marchResult.steps >= MarchParams.maxSteps.$) {
237+
worldNormal = std.neg(shapeCtx.rayDir);
239238
} else {
240-
world_normal = estimateNormal(march_result.position, shape_ctx);
239+
worldNormal = estimateNormal(marchResult.position, shapeCtx);
241240
}
242241

243242
const material = Material();
244-
worldMat(march_result.position, shape_ctx, material);
243+
worldMat(marchResult.position, shapeCtx, material);
245244

246-
const mat_color = std.min(material.albedo, d.vec3f(1));
245+
const matColor = std.min(material.albedo, d.vec3f(1));
247246

248-
const albedo_luminance = convertRgbToY(mat_color);
249-
const emission_luminance = 0;
247+
const albedoLuminance = convertRgbToY(matColor);
248+
const emissionLuminance = 0;
250249
if (material.emissive) {
251-
// albedo_luminance = 0.3;
252-
// emission_luminance = albedo_luminance;
250+
// albedoLuminance = 0.3;
251+
// emissionLuminance = albedoLuminance;
253252
}
254253

255254
const camera = getCameraProps.$;
256-
const view_normal = camera.view_matrix.mul(d.vec4f(world_normal, 0));
255+
const viewNormal = camera.view_matrix.mul(d.vec4f(worldNormal, 0));
257256

258-
const aux = d.vec4f(view_normal.xy, albedo_luminance, emission_luminance);
257+
const aux = d.vec4f(viewNormal.xy, albedoLuminance, emissionLuminance);
259258

260259
// TODO: maybe apply gamma correction to the albedo luminance parameter??
261260

@@ -288,8 +287,8 @@ export function createSDFRenderer(options: SDFRendererOptions) {
288287

289288
const mainPipeline = root['~unstable']
290289
// filling slots
291-
.with(getRandomSeedPrimer, randomSeedPrimerUniform)
292-
.with(getAccumulatedLayers, layersUniform)
290+
.with(randomSeedPrimerAccess, randomSeedPrimerUniform)
291+
.with(accumulatedLayersAccess, layersUniform)
293292
.with(getCameraProps, camera.cameraBuffer.as('uniform'))
294293
.with(accessViewportSize, d.vec2f(mainPassSize[0], mainPassSize[1]))
295294
.with(MarchParams.sampleSdf, worldSdf)
Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { sphere } from '@typegpu/sdf';
2-
import { MarchParams, ShapeContext } from 'src/lib-ray-marching';
3-
import tgpu, { type TgpuFnShell } from 'typegpu';
1+
import tgpu from 'typegpu';
42
import * as d from 'typegpu/data';
53
import { abs, floor, min, mix, mul, pow } from 'typegpu/std';
4+
import { sdSphere } from '@typegpu/sdf';
5+
import { MarchParams, ShapeContext } from 'src/lib-ray-marching';
66

77
export const Material = d.struct({
88
albedo: d.vec3f,
@@ -12,14 +12,11 @@ export const Material = d.struct({
1212

1313
// const getTime = tgpu.accessor(d.f32);
1414

15-
const sdfShell: TgpuFnShell<[pos: d.Vec3f], d.F32> = tgpu['~unstable'].fn(
16-
[d.vec3f],
17-
d.f32,
18-
);
15+
const sdfShell = tgpu.fn([d.vec3f], d.f32);
1916

2017
const objLeftBlob = sdfShell((pos) => {
2118
'kernel';
22-
return sphere(pos, d.vec3f(-0.3, -0.2, 0), 0.2);
19+
return sdSphere(pos.sub(d.vec3f(-0.3, -0.2, 0)), 0.2);
2320
});
2421

2522
// ANIMATED LIGHT
@@ -29,78 +26,81 @@ const objLeftBlob = sdfShell((pos) => {
2926

3027
const objCenterBlob = sdfShell((pos) => {
3128
'kernel';
32-
return sphere(pos, d.vec3f(-0.3, 0.4, 0.4), 0.2);
29+
return sdSphere(pos.sub(d.vec3f(-0.3, 0.4, 0.4)), 0.2);
3330
});
3431

3532
const objRightBlob = sdfShell((pos) => {
3633
'kernel';
37-
return sphere(pos, d.vec3f(0.4, 0.2, 0), 0.4);
34+
return sdSphere(pos.sub(d.vec3f(0.4, 0.2, 0)), 0.4);
3835
});
3936

4037
const objFloor = sdfShell((pos) => {
4138
'kernel';
4239
return pos.y + 0.3;
4340
});
4441

45-
const matFloor = tgpu['~unstable']
46-
.fn([d.vec3f, d.ptrFn(Material)])((pos, mtr) => {
47-
const uv = floor(mul(5, pos.xz));
48-
const c = 0.2 + 0.5 * ((uv.x + uv.y) - 2.0 * floor((uv.x + uv.y) / 2.0));
49-
50-
mtr.albedo = mix(d.vec3f(1., 1., 1.), d.vec3f(0., 0., 0.), c);
51-
mtr.roughness = 0.9;
52-
})
53-
.$name('mat_floor');
42+
const matFloor = tgpu.fn([d.vec3f, d.ptrFn(Material)])((pos, mtr) => {
43+
const uv = floor(mul(5, pos.xz));
44+
const c = 0.2 + 0.5 * (uv.x + uv.y - 2.0 * floor((uv.x + uv.y) / 2.0));
45+
46+
mtr.albedo = mix(d.vec3f(1, 1, 1), d.vec3f(0, 0, 0), c);
47+
mtr.roughness = 0.9;
48+
});
5449

5550
export const FAR = 100;
5651

5752
export const worldSdf = sdfShell((pos) => {
5853
'kernel';
59-
let min_dist = d.f32(FAR);
54+
let minDist = d.f32(FAR);
6055

61-
min_dist = min(min_dist, objLeftBlob(pos));
62-
min_dist = min(min_dist, objCenterBlob(pos));
63-
min_dist = min(min_dist, objRightBlob(pos));
64-
min_dist = min(min_dist, objFloor(pos));
56+
minDist = min(minDist, objLeftBlob(pos));
57+
minDist = min(minDist, objCenterBlob(pos));
58+
minDist = min(minDist, objRightBlob(pos));
59+
minDist = min(minDist, objFloor(pos));
6560

66-
return min_dist;
61+
return minDist;
6762
});
6863

6964
// MATERIALS
7065

71-
export const skyColor = tgpu['~unstable'].fn([d.vec3f], d.vec3f)((dir) => {
72-
const t = pow(min(abs(dir.y) * 4, 1.), 0.4);
66+
export const skyColor = tgpu.fn(
67+
[d.vec3f],
68+
d.vec3f,
69+
)((dir) => {
70+
const t = pow(min(abs(dir.y) * 4, 1), 0.4);
7371
return mix(d.vec3f(0.7, 0.7, 0.75), d.vec3f(0.35, 0.4, 0.6), t);
7472
});
7573

76-
export const worldMat = tgpu['~unstable']
77-
.fn([d.vec3f, ShapeContext, d.ptrFn(Material)])((pos, ctx, out) => {
78-
const sd = MarchParams.getSurfaceThreshold.value(ctx);
79-
const d_left_blob = objLeftBlob(pos);
80-
const d_center_blob = objCenterBlob(pos);
81-
const d_right_blob = objRightBlob(pos);
82-
const d_floor_blob = objFloor(pos);
83-
84-
// defaults
85-
out.emissive = false;
86-
out.roughness = 1;
87-
88-
if (d_left_blob <= sd) {
89-
// left blob
90-
out.albedo = d.vec3f(1, 0.5, 0.2);
91-
out.roughness = 0.95;
92-
} else if (d_center_blob <= sd) {
93-
// test light
94-
out.albedo = mul(20, d.vec3f(1, 1, 0.5));
95-
out.emissive = true;
96-
} else if (d_right_blob <= sd) {
97-
out.albedo = mul(0.9, d.vec3f(0.5, 0.5, 0.6));
98-
out.roughness = 0.1;
99-
} else if (d_floor_blob <= sd) {
100-
matFloor(pos, out);
101-
} else {
102-
// out.albedo = vec3f(0.5, 0.5, 0.2);
103-
out.albedo = skyColor(ctx.rayDir);
104-
}
105-
})
106-
.$name('world_mat');
74+
export const worldMat = tgpu.fn([d.vec3f, ShapeContext, d.ptrFn(Material)])((
75+
pos,
76+
ctx,
77+
out,
78+
) => {
79+
const sd = MarchParams.getSurfaceThreshold.value(ctx);
80+
const d_left_blob = objLeftBlob(pos);
81+
const d_center_blob = objCenterBlob(pos);
82+
const d_right_blob = objRightBlob(pos);
83+
const d_floor_blob = objFloor(pos);
84+
85+
// defaults
86+
out.emissive = false;
87+
out.roughness = 1;
88+
89+
if (d_left_blob <= sd) {
90+
// left blob
91+
out.albedo = d.vec3f(1, 0.5, 0.2);
92+
out.roughness = 0.95;
93+
} else if (d_center_blob <= sd) {
94+
// test light
95+
out.albedo = mul(20, d.vec3f(1, 1, 0.5));
96+
out.emissive = true;
97+
} else if (d_right_blob <= sd) {
98+
out.albedo = mul(0.9, d.vec3f(0.5, 0.5, 0.6));
99+
out.roughness = 0.1;
100+
} else if (d_floor_blob <= sd) {
101+
matFloor(pos, out);
102+
} else {
103+
// out.albedo = vec3f(0.5, 0.5, 0.2);
104+
out.albedo = skyColor(ctx.rayDir);
105+
}
106+
});

pnpm-lock.yaml

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)