Skip to content

Commit 22fccea

Browse files
committed
More updates of sdf renderer
1 parent cc540b5 commit 22fccea

File tree

1 file changed

+45
-73
lines changed

1 file changed

+45
-73
lines changed

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

Lines changed: 45 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -218,77 +218,49 @@ const auxLayout = tgpu.bindGroupLayout({
218218
auxOutput: { storageTexture: 'rgba16float' },
219219
});
220220

221-
const auxComputeFn = tgpu['~unstable']
222-
.computeFn({
223-
workgroupSize: [BlockSize, BlockSize],
224-
in: { gid: d.builtin.globalInvocationId },
225-
})(
226-
/* wgsl */ `{
227-
let offset = vec2f(
228-
0.5,
229-
0.5,
230-
);
231-
232-
var march_result: MarchResult;
233-
var shape_ctx: ShapeContext;
234-
shape_ctx.rayPos = constructRayPos();
235-
shape_ctx.rayDir = constructRayDir(
236-
vec2f(in.gid.xy) + offset
237-
);
238-
shape_ctx.rayDistance = 0.;
239-
240-
march(&shape_ctx, MAX_STEPS, &march_result);
241-
242-
var world_normal: vec3f;
243-
244-
if (march_result.steps >= MAX_STEPS) {
245-
world_normal = -shape_ctx.rayDir;
246-
}
247-
else {
248-
world_normal = estimateNormal(march_result.position, shape_ctx);
249-
}
221+
const auxComputeFn = tgpu['~unstable'].computeFn({
222+
workgroupSize: [BlockSize, BlockSize],
223+
in: { gid: d.builtin.globalInvocationId },
224+
})((input) => {
225+
const offset = d.vec2f(0.5);
250226

251-
var material: Material;
252-
worldMat(march_result.position, shape_ctx, &material);
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;
253232

254-
let white = vec3f(1., 1., 1.);
255-
let mat_color = min(material.albedo, white);
233+
march(shape_ctx, MarchParams.maxSteps.$, march_result);
256234

257-
var albedo_luminance = convertRgbToY(mat_color);
258-
var emission_luminance = 0.;
259-
if (material.emissive) {
260-
// albedo_luminance = 0.3;
261-
// emission_luminance = albedo_luminance;
262-
}
235+
let world_normal = d.vec3f();
263236

264-
let camera = getCameraProps;
265-
let view_normal = camera.view_matrix * vec4f(world_normal, 0);
266-
267-
let aux = vec4(
268-
view_normal.xy,
269-
albedo_luminance,
270-
emission_luminance
271-
);
272-
273-
// TODO: maybe apply gamma correction to the albedo luminance parameter??
274-
275-
textureStore(auxOutput, in.gid.xy, aux);
276-
}`,
277-
)
278-
.$uses({
279-
MAX_STEPS: MarchParams.maxSteps,
280-
MarchResult,
281-
ShapeContext,
282-
Material,
283-
constructRayPos,
284-
constructRayDir,
285-
march,
286-
estimateNormal,
287-
worldMat,
288-
convertRgbToY,
289-
getCameraProps,
290-
auxOutput: auxLayout.bound.auxOutput,
291-
});
237+
if (march_result.steps >= MarchParams.maxSteps.$) {
238+
world_normal = std.neg(shape_ctx.rayDir);
239+
} else {
240+
world_normal = estimateNormal(march_result.position, shape_ctx);
241+
}
242+
243+
const material = Material();
244+
worldMat(march_result.position, shape_ctx, material);
245+
246+
const mat_color = std.min(material.albedo, d.vec3f(1));
247+
248+
const albedo_luminance = convertRgbToY(mat_color);
249+
const emission_luminance = 0;
250+
if (material.emissive) {
251+
// albedo_luminance = 0.3;
252+
// emission_luminance = albedo_luminance;
253+
}
254+
255+
const camera = getCameraProps.$;
256+
const view_normal = camera.view_matrix.mul(d.vec4f(world_normal, 0));
257+
258+
const aux = d.vec4f(view_normal.xy, albedo_luminance, emission_luminance);
259+
260+
// TODO: maybe apply gamma correction to the albedo luminance parameter??
261+
262+
std.textureStore(auxLayout.$.auxOutput, input.gid.xy, aux);
263+
});
292264

293265
export interface SDFRendererOptions {
294266
root: TgpuRoot;
@@ -304,9 +276,9 @@ export function createSDFRenderer(options: SDFRendererOptions) {
304276
const LABEL = 'SDF Renderer';
305277
const camera = new Camera(root);
306278

307-
const randomSeedPrimerBuffer = root.createBuffer(d.f32).$usage('uniform');
279+
const randomSeedPrimerUniform = root.createUniform(d.f32);
308280
// How many layers (previous renders) are stacked on top of each other to reduce noise.
309-
const layersBuffer = root.createBuffer(d.f32).$usage('uniform');
281+
const layersUniform = root.createUniform(d.f32);
310282

311283
// ---
312284

@@ -316,8 +288,8 @@ export function createSDFRenderer(options: SDFRendererOptions) {
316288

317289
const mainPipeline = root['~unstable']
318290
// filling slots
319-
.with(getRandomSeedPrimer, randomSeedPrimerBuffer.as('uniform'))
320-
.with(getAccumulatedLayers, layersBuffer.as('uniform'))
291+
.with(getRandomSeedPrimer, randomSeedPrimerUniform)
292+
.with(getAccumulatedLayers, layersUniform)
321293
.with(getCameraProps, camera.cameraBuffer.as('uniform'))
322294
.with(accessViewportSize, d.vec2f(mainPassSize[0], mainPassSize[1]))
323295
.with(MarchParams.sampleSdf, worldSdf)
@@ -340,8 +312,8 @@ export function createSDFRenderer(options: SDFRendererOptions) {
340312

341313
return {
342314
perform() {
343-
randomSeedPrimerBuffer.write(Math.random());
344-
layersBuffer.write(store.get(accumulatedLayersAtom));
315+
randomSeedPrimerUniform.write(Math.random());
316+
layersUniform.write(store.get(accumulatedLayersAtom));
345317
camera.update();
346318

347319
const mainBindGroup = root.createBindGroup(mainLayout, {

0 commit comments

Comments
 (0)