Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Leave shadow-only parts out of camera rays on the native, webgpu and WebGL2 Cook backends (#973)

Nguyen Xuan An9eddfa0adae6
diff --git a/changes/973.md b/changes/973.md
new file mode 100644
index 00000000..ddacaa05
--- /dev/null
+++ b/changes/973.md
@@ -0,0 +1 @@
+- The Cook gpu, webgpu and native backends leave a `shadowOnly` node out of camera rays like the cpu backend, while it still casts shadows and shows in bounces, and `docs/agent-scene.md` lists the backends that honour it (#973)
diff --git a/docs/agent-scene.md b/docs/agent-scene.md
index 23853b08..5a9b382b 100644
--- a/docs/agent-scene.md
+++ b/docs/agent-scene.md
@@ -345,7 +345,7 @@ Five node fields change how a node renders; `add-node` and `set-node` take them,
- `castShadow: false` and `receiveShadow: false` (mesh nodes) stop a mesh casting or receiving shadows. Both default to true; glass casts none unless `castShadow` is set.
- `holdout: true` writes zero colour and zero alpha where the mesh covers, and still hides what is behind it and still casts its shadow. `shadowCatcher: true` draws only the shadow it receives, black with the shadow as premultiplied alpha, and casts none. Both are for compositing over footage and need a transparent canvas: `new Renderer67(canvas, parts, { transparent: true })`. A node is never both. The shadow catcher shows the sun shadow only.
-- `shadowOnly: true` (mesh nodes) keeps the node in the shadow passes and leaves it out of the picture, the same as Blender's Ray Visibility > Camera off. Use it for a gobo, such as a window frame outside the shot that throws bars of sun onto a wall. It is also left out of depth, object ID and wireframe passes. It defaults to false, so no scene changes. A node cannot set `shadowOnly` together with `castShadow: false`, `holdout` or `shadowCatcher`. The raster renderer and the Cook cpu backend honour it: Cook leaves the node out of camera rays and keeps it for shadow and bounce rays, so a mirror or glass can still show it. The GPU path tracers still draw it, and say so in their ignored-feature notes.
+- `shadowOnly: true` (mesh nodes) keeps the node in the shadow passes and leaves it out of the picture, the same as Blender's Ray Visibility > Camera off. Use it for a gobo, such as a window frame outside the shot that throws bars of sun onto a wall. It is also left out of depth, object ID and wireframe passes. It defaults to false, so no scene changes. A node cannot set `shadowOnly` together with `castShadow: false`, `holdout` or `shadowCatcher`. Every backend honours it: the raster renderer, and Cook on the cpu, gpu (WebGL2), webgpu and native backends (both native kernels). Cook leaves the node out of camera rays (the first hit of each path) and keeps it for shadow and bounce rays, so a mirror or glass can still show it. No backend lists it as an ignored feature.
Parts with different `castShadow`, `receiveShadow`, `holdout`, `shadowCatcher` or `shadowOnly` values batch apart; `visible` never splits a batch. `receiveShadow`, `holdout`, `shadowCatcher` and `shadowOnly` changes need `replaceParts`, not `updateParts`.
diff --git a/packages/render-native/pack-input-v2.mjs b/packages/render-native/pack-input-v2.mjs
index 978efc9b..b5bd06ae 100644
--- a/packages/render-native/pack-input-v2.mjs
+++ b/packages/render-native/pack-input-v2.mjs
@@ -45,6 +45,7 @@ const KERNEL_FLAG_ADAPTIVE_SAMPLING = 16;
const KERNEL_FLAG_TRANSPARENT_BACKGROUND = 32;
export const KERNEL_FLAG_WHITE_NOISE_SAMPLER = 64;
+const INSTANCE_VISIBILITY_CAMERA = 1;
const INSTANCE_DEFAULT_VISIBILITY = 1 | 2 | 4 | 8 | 16 | 32;
const INSTANCE_FLAG_NEGATIVE_SCALE = 64;
@@ -2410,6 +2411,7 @@ export function upgradeNativeInputV1ToV2(v1Input, options = {}) {
const matSource = v1Input.materials;
const materialRecords = [];
const materialLightMasks = [];
+ const materialShadowOnly = [];
for (let m = 0; m < materialCount; m++) {
const base = m * 40;
const baseColor = [matSource[base], matSource[base + 1], matSource[base + 2]];
@@ -2424,6 +2426,8 @@ export function upgradeNativeInputV1ToV2(v1Input, options = {}) {
const alphaCutoff = matSource[base + 14];
const maskLo = ((Math.round(matSource[base + 15]) & 0xffff) | ((Math.round(matSource[base + 16]) & 0xffff) << 16)) >>> 0;
materialLightMasks.push(maskLo);
+ // The v1 tables flag a shadow-only part in the second channel of material texel 5 (SHADOW_ONLY_SLOT in src/path-tracer-gpu-pack.js).
+ materialShadowOnly.push(matSource[base + 21] > 0);
const baseTex = registerAtlasRect(matSource[base + 24], matSource[base + 25], matSource[base + 26], matSource[base + 27], matSource[base + 17]);
const normalTex = registerAtlasRect(matSource[base + 28], matSource[base + 29], matSource[base + 30], matSource[base + 31], matSource[base + 18]);
@@ -2586,7 +2590,7 @@ export function upgradeNativeInputV1ToV2(v1Input, options = {}) {
opaqueTriangleCount: opaque.length,
totalTriangleCount: ordered.length,
materialOverride: INVALID_INDEX_V2,
- visibilityFlags: INSTANCE_DEFAULT_VISIBILITY,
+ visibilityFlags: materialShadowOnly[matIdx] ? INSTANCE_DEFAULT_VISIBILITY & ~INSTANCE_VISIBILITY_CAMERA : INSTANCE_DEFAULT_VISIBILITY,
lightGroupMaskLo: maskLo,
lightGroupMaskHi: maskHi,
emissiveBaseOffset,
diff --git a/packages/render-native/pack-input.mjs b/packages/render-native/pack-input.mjs
index fe7200b6..f1af3c87 100644
--- a/packages/render-native/pack-input.mjs
+++ b/packages/render-native/pack-input.mjs
@@ -46,7 +46,7 @@ export function buildNativeInput(scene, camera, options, pack) {
}
// The subsurface table (two vectors per material) follows the material table in the same buffer; its offset is 0 when no
// material scatters light under its surface, which keeps the walk out of the kernel.
- const materialData = pack.packMaterials(scene.materials, atlas).data;
+ const materialData = pack.packMaterials(scene.materials, atlas, scene.shadowOnlyParts).data;
const usesSubsurface = pack.sceneUsesSubsurface(scene.materials);
const subsurfaceData = usesSubsurface ? pack.packSubsurface(scene.materials).data : new Float32Array(0);
const materials = new Float32Array(materialData.length + subsurfaceData.length);
diff --git a/packages/render-native/src/kernel.wgsl b/packages/render-native/src/kernel.wgsl
index 0af9a1d1..e49ca9dd 100644
--- a/packages/render-native/src/kernel.wgsl
+++ b/packages/render-native/src/kernel.wgsl
@@ -113,7 +113,8 @@ fn alphaAtSlot(slot: i32, secondWeight: f32, thirdWeight: f32) -> f32 {
return sampleAtlas(mapRectangle, materials[material + 4].y, uv).a;
}
-fn traceRay(origin: vec3<f32>, direction: vec3<f32>, limit: f32, anyHit: bool) {
+// A camera ray ignores the parts flagged shadow-only (the flag sits in texel 5 of the material); every other ray still hits them.
+fn traceRay(origin: vec3<f32>, direction: vec3<f32>, limit: f32, anyHit: bool, cameraRay: bool) {
nearestDistance = limit;
nearestSlot = -1;
var query: ray_query;
@@ -123,6 +124,7 @@ fn traceRay(origin: vec3<f32>, direction: vec3<f32>, limit: f32, anyHit: bool) {
let candidate = rayQueryGetCandidateIntersection(&query);
let slot = primitiveSlot(candidate.geometry_index, candidate.primitive_index);
let material = i32(triangles[slot * TRIANGLE_TEXELS].w) * MATERIAL_TEXELS;
+ if (cameraRay && materials[material + 5].y > 0.0) { continue; }
let cutoff = materials[material + 3].z;
if (alphaAtSlot(slot, candidate.barycentrics.x, candidate.barycentrics.y) >= cutoff) {
rayQueryConfirmIntersection(&query);
@@ -282,15 +284,15 @@ fn fillHit(origin: vec3<f32>, direction: vec3<f32>, distance: f32) -> f32 {
}
// The nearest solid hit; transparent texels of cutout materials never reach here (see `traceRay`).
-fn intersectScene(origin: vec3<f32>, direction: vec3<f32>) -> bool {
- traceRay(origin, direction, params.environmentRotationAndRays.w, false);
+fn intersectScene(origin: vec3<f32>, direction: vec3<f32>, cameraRay: bool) -> bool {
+ traceRay(origin, direction, params.environmentRotationAndRays.w, false, cameraRay);
if (nearestSlot < 0) { return false; }
fillHit(origin, direction, nearestDistance);
return true;
}
fn occluded(origin: vec3<f32>, direction: vec3<f32>, limit: f32) -> bool {
- traceRay(origin, direction, limit, true);
+ traceRay(origin, direction, limit, true, false);
return nearestSlot >= 0;
}
@@ -883,7 +885,7 @@ fn randomWalk(point: vec3<f32>, normal: vec3<f32>, meanFreePath: vec3<f32>, colo
let height = sqrt(max(0.0, 1.0 - radius * radius));
walkDirection = frame[0] * (radius * cos(angle)) + frame[1] * (radius * sin(angle)) - normal * height;
for (var scatters = 0; scatters <= WALK_STEPS; scatters++) {
- let found = intersectScene(position, walkDirection);
+ let found = intersectScene(position, walkDirection, false);
if (!found || hitMaterialIndex != material) { return select(WALK_LOST, WALK_THIN, scatters == 0); }
// One channel draws the distance; weighting by the mean density over the three channels keeps every channel unbiased.
let hero = min(2, i32(floor(nextRandom() * 3.0)));
@@ -945,7 +947,8 @@ fn enterSubsurface(depth: i32, entryNormal: vec3<f32>) -> bool {
addLights(depth, subsurfaceNormal);
return true;
}
-fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleIndex: u32) {
+// `cameraRays` is true when the path starts at the camera, so its first leg steps through shadow-only parts.
+fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleIndex: u32, cameraRays: bool) {
resetRandom(pixel, sampleIndex, params.seedAndAtlas.x);
radiance = vec3<f32>(0.0);
throughput = vec3<f32>(1.0);
@@ -961,7 +964,7 @@ fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleInd
let bounces = params.sizeBouncesLights.z;
let surfaceOffset = params.environmentRotationAndRays.z;
for (var depth = 0; depth <= bounces; depth++) {
- let found = intersectScene(position, direction);
+ let found = intersectScene(position, direction, cameraRays && depth == 0);
var inFog = false;
if (HAS_VOLUME) { inFog = scatterInFog(position, select(params.environmentRotationAndRays.w, hitDistance, found)); }
if (!inFog && !found) {
@@ -1076,7 +1079,7 @@ fn main(@builtin(global_invocation_id) id: vec3<u32>) {
let screenX = ((f32(id.x) + jitterX) / f32(width) * 2.0 - 1.0) * params.cameraAndClamp.x;
let screenY = (1.0 - (f32(id.y) + jitterY) / f32(height) * 2.0) * params.cameraAndClamp.y;
let rayDirection = normalize(params.forward.xyz + params.right.xyz * screenX + params.above.xyz * screenY);
- tracePath(params.eye.xyz, rayDirection, pixel, sampleIndex);
+ tracePath(params.eye.xyz, rayDirection, pixel, sampleIndex, true);
if (!(all(radiance >= vec3<f32>(0.0)) && all(radiance < vec3<f32>(1e30)))) { radiance = vec3<f32>(0.0); }
if (!(featureDepth >= 0.0 && featureDepth < 1e30)) { featureDepth = 0.0; }
if (!(all(featureNormal >= vec3<f32>(-1.5)) && all(featureNormal < vec3<f32>(1.5)))) { featureNormal = vec3<f32>(0.0, 0.0, 1.0); }
diff --git a/packages/render-native/src/texel.wgsl b/packages/render-native/src/texel.wgsl
index 3c07e9eb..14787ce8 100644
--- a/packages/render-native/src/texel.wgsl
+++ b/packages/render-native/src/texel.wgsl
@@ -23,7 +23,7 @@ fn bakeTexels(@builtin(global_invocation_id) id: vec3<u32>, @builtin(num_workgro
let coneRadius = sqrt(firstUnit);
let angle = TWO_PI * secondUnit;
let rayDirection = buildFrame(normal) * vec3<f32>(coneRadius * cos(angle), coneRadius * sin(angle), sqrt(max(0.0, 1.0 - firstUnit)));
- tracePath(origin, rayDirection, texelIndex, sampleIndex);
+ tracePath(origin, rayDirection, texelIndex, sampleIndex, false);
if (!(all(radiance >= vec3<f32>(0.0)) && all(radiance < vec3<f32>(1e30)))) { radiance = vec3<f32>(0.0); }
accumulation[texelIndex] += vec4<f32>(radiance, 0.0);
accumulation[texelCount + texelIndex] += vec4<f32>(firstLegEscaped, 0.0, 0.0, 0.0);
diff --git a/src/path-tracer-gpu-pack.js b/src/path-tracer-gpu-pack.js
index 373a8043..9032a8fc 100644
--- a/src/path-tracer-gpu-pack.js
+++ b/src/path-tracer-gpu-pack.js
@@ -104,12 +104,18 @@ export function packAtlas(materials) {
return { placements, width: ATLAS_WIDTH, height, data };
}
+// The float of a material record that holds the shadow-only flag: texel 5, second channel (its first channel is the emission map flag).
+export const SHADOW_ONLY_SLOT = 21;
+
function textureFlags(texture) {
return (texture.repeat ? 1 : 0) + (texture.colorLookup ? 2 : 0);
}
-/** Ten texels per material: surface values, texture flags, then one atlas rectangle per map. */
-export function packMaterials(materials, atlas) {
+/**
+ * Ten texels per material: surface values, texture flags, then one atlas rectangle per map. A material of a shadow-only part
+ * (`shadowOnlyParts[index] === 1`) also gets 1 in SHADOW_ONLY_SLOT, which camera rays read to step through it.
+ */
+export function packMaterials(materials, atlas, shadowOnlyParts = null) {
const table = createTable(materials.length * MATERIAL_TEXELS);
const { data } = table;
const fields = ['texture', 'normalTexture', 'surfaceTexture', 'emissiveTexture'];
@@ -128,6 +134,7 @@ export function packMaterials(materials, atlas) {
data[base + 17 + fieldIndex] = textureFlags(texture);
data.set([place.x, place.y, texture.width, texture.height], base + (6 + fieldIndex) * 4);
});
+ if (shadowOnlyParts?.[index] === 1) data[base + SHADOW_ONLY_SLOT] = 1;
});
return table;
}
diff --git a/src/path-tracer-gpu-shader.js b/src/path-tracer-gpu-shader.js
index 2244a0e8..921f9d59 100644
--- a/src/path-tracer-gpu-shader.js
+++ b/src/path-tracer-gpu-shader.js
@@ -37,7 +37,7 @@ const CAMERA_MAIN = `void main() {
float screenX = ((float(pixelCoordinate.x) + jitterX) / float(uSize.x) * 2.0 - 1.0) * uHalfSize.x;
float screenY = (1.0 - (float(row) + jitterY) / float(uSize.y) * 2.0) * uHalfSize.y;
vec3 rayDirection = normalize(uForward + uRight * screenX + uAbove * screenY);
- tracePath(uEye, rayDirection, pixel, sampleIndex);`;
+ tracePath(uEye, rayDirection, pixel, sampleIndex, true);`;
// Texel mode: one pixel is one lightmap texel. The path starts on the texel's surface with a cosine weighted direction,
// so the radiance it returns is the light arriving at the texel (the caller multiplies by the albedo). The first
@@ -61,7 +61,7 @@ const TEXEL_MAIN = `void main() {
float angle = TWO_PI * secondUnit;
vec3 rayDirection = buildFrame(normal) * vec3(coneRadius * cos(angle), coneRadius * sin(angle), sqrt(max(0.0, 1.0 - firstUnit)));
if (texelIndex >= uTexelCount) rayDirection = normal;
- tracePath(origin, rayDirection, pixel, sampleIndex);`;
+ tracePath(origin, rayDirection, pixel, sampleIndex, false);`;
// The light tree walk of path-tracer-light-tree.js (nodeImportance, leftChildProbability, chooseLightFromTree), four texels a node.
const LIGHT_TREE_FUNCTIONS = `
@@ -500,18 +500,25 @@ float fillHit(vec3 origin, vec3 direction, float distance) {
return alpha;
}
-/** The nearest solid hit, stepping through the transparent texels of cutout materials. */
-bool intersectScene(vec3 origin, vec3 direction) {
+/** True when the nearest hit belongs to a shadow-only part (the flag sits in texel 5 of the material); camera rays step through it. */
+bool nearestHitIsShadowOnly() {
+ return fetchData(uMaterials, int(fetchData(uTriangles, nearestSlot * TRIANGLE_TEXELS).w) * MATERIAL_TEXELS + 5).y > 0.0;
+}
+
+/** The nearest solid hit, stepping through the transparent texels of cutout materials and, for a camera ray, shadow-only parts. */
+bool intersectScene(vec3 origin, vec3 direction, bool cameraRay) {
vec3 start = origin;
float travelled = 0.0;
for (int attempt = 0; attempt < ALPHA_STEP_LIMIT; attempt++) {
traceRay(start, direction, uFarDistance, false);
if (nearestSlot < 0) return false;
float foundDistance = nearestDistance;
- float alpha = fillHit(start, direction, foundDistance);
- if (hitCutoff == 0.0 || alpha >= hitCutoff) {
- hitDistance += travelled;
- return true;
+ if (!(cameraRay && nearestHitIsShadowOnly())) {
+ float alpha = fillHit(start, direction, foundDistance);
+ if (hitCutoff == 0.0 || alpha >= hitCutoff) {
+ hitDistance += travelled;
+ return true;
+ }
}
float advance = foundDistance + uSurfaceOffset;
travelled += advance;
@@ -1174,7 +1181,8 @@ bool scatterInFog(vec3 origin, float segmentLength) {
}
${hasSubsurface ? SUBSURFACE_FUNCTIONS : ''}
-void tracePath(vec3 origin, vec3 startDirection, uint pixel, uint sampleIndex) {
+// cameraRays is true when the path starts at the camera, so its first leg steps through shadow-only parts.
+void tracePath(vec3 origin, vec3 startDirection, uint pixel, uint sampleIndex, bool cameraRays) {
resetRandom(pixel, sampleIndex, uSeed);
radiance = vec3(0.0);
throughput = vec3(1.0);
@@ -1188,7 +1196,7 @@ void tracePath(vec3 origin, vec3 startDirection, uint pixel, uint sampleIndex) {
bool afterMirror = true;
vec3 normal = vec3(0.0, 1.0, 0.0);
for (int depth = 0; depth <= uBounces; depth++) {
- bool found = intersectScene(position, direction);
+ bool found = intersectScene(position, direction, cameraRays && depth == 0);
bool inFog = uHasVolume && scatterInFog(position, found ? hitDistance : uFarDistance);
if (!inFog && !found) {
if (depth == 0) firstLegEscaped = 1.0;
diff --git a/src/path-tracer-gpu-subsurface.js b/src/path-tracer-gpu-subsurface.js
index c9750f66..84e8ab29 100644
--- a/src/path-tracer-gpu-subsurface.js
+++ b/src/path-tracer-gpu-subsurface.js
@@ -68,7 +68,7 @@ int randomWalk(vec3 point, vec3 normal, vec3 meanFreePath, vec3 color, int mater
float height = sqrt(max(0.0, 1.0 - radius * radius));
walkDirection = frame[0] * (radius * cos(angle)) + frame[1] * (radius * sin(angle)) - normal * height;
for (int scatters = 0; scatters <= WALK_STEPS; scatters++) {
- bool found = intersectScene(position, walkDirection);
+ bool found = intersectScene(position, walkDirection, false);
if (!found || hitMaterialIndex != material) return scatters == 0 ? WALK_THIN : WALK_LOST;
// One channel draws the distance; weighting by the mean density over the three channels keeps every channel unbiased.
int hero = min(2, int(floor(nextRandom() * 3.0)));
diff --git a/src/path-tracer-gpu.js b/src/path-tracer-gpu.js
index 190af0ec..8f36d22b 100644
--- a/src/path-tracer-gpu.js
+++ b/src/path-tracer-gpu.js
@@ -78,7 +78,7 @@ export function uploadScene(gl, program, scene, options = {}) {
const environment = packEnvironment(scene.environment);
const nodes = packNodes(scene.bvh);
const triangles = packTriangles(scene);
- const materials = packMaterials(scene.materials, atlas);
+ const materials = packMaterials(scene.materials, atlas, scene.shadowOnlyParts);
const floatTables = [['uNodes', nodes], ['uTriangles', triangles], ['uMaterials', materials], ['uLights', lights.table]];
if (sceneUsesSubsurface(scene.materials)) floatTables.push(['uSubsurface', packSubsurface(scene.materials)]);
if (options.lightSelection === 'tree') floatTables.push(['uLightTree', packLightTree(scene.lights)]);
diff --git a/src/path-tracer-realtime-webgpu-kernel.js b/src/path-tracer-realtime-webgpu-kernel.js
index 8f1aad15..5d6cb770 100644
--- a/src/path-tracer-realtime-webgpu-kernel.js
+++ b/src/path-tracer-realtime-webgpu-kernel.js
@@ -142,7 +142,7 @@ fn traceRealtime(@builtin(global_invocation_id) id: vec3<u32>) {
let screenX = ((f32(id.x) + jitterX) / f32(width) * 2.0 - 1.0) * params.cameraAndClamp.x;
let screenY = (1.0 - (f32(id.y) + jitterY) / f32(height) * 2.0) * params.cameraAndClamp.y;
let rayDirection = normalize(params.forward.xyz + params.right.xyz * screenX + params.above.xyz * screenY);
- tracePath(params.eye.xyz, rayDirection, pixel, sampleIndex);
+ tracePath(params.eye.xyz, rayDirection, pixel, sampleIndex, true);
if (!(all(radiance >= vec3<f32>(0.0)) && all(radiance < vec3<f32>(1e30)))) { radiance = vec3<f32>(0.0); }
if (!(featureDepth >= 0.0 && featureDepth < 1e30)) { featureDepth = 0.0; }
if (!(all(featureNormal >= vec3<f32>(-1.5)) && all(featureNormal < vec3<f32>(1.5)))) { featureNormal = vec3<f32>(0.0, 0.0, 1.0); }
diff --git a/src/path-tracer-webgpu-kernel.js b/src/path-tracer-webgpu-kernel.js
index ee99d374..09135355 100644
--- a/src/path-tracer-webgpu-kernel.js
+++ b/src/path-tracer-webgpu-kernel.js
@@ -322,8 +322,13 @@ fn fillHit(origin: vec3<f32>, direction: vec3<f32>, distance: f32) -> f32 {
return alpha;
}
-// The nearest solid hit, stepping through the transparent texels of cutout materials.
-fn intersectScene(origin: vec3<f32>, direction: vec3<f32>) -> bool {
+// True when the nearest hit belongs to a shadow-only part (the flag sits in texel 5 of the material); camera rays step through it.
+fn nearestHitIsShadowOnly() -> bool {
+ return materials[i32(triangles[nearestSlot * TRIANGLE_TEXELS].w) * MATERIAL_TEXELS + 5].y > 0.0;
+}
+
+// The nearest solid hit, stepping through the transparent texels of cutout materials and, for a camera ray, shadow-only parts.
+fn intersectScene(origin: vec3<f32>, direction: vec3<f32>, cameraRay: bool) -> bool {
var start = origin;
var travelled = 0.0;
let farDistance = params.environmentRotationAndRays.w;
@@ -332,10 +337,12 @@ fn intersectScene(origin: vec3<f32>, direction: vec3<f32>) -> bool {
traceRay(start, direction, farDistance, false);
if (nearestSlot < 0) { return false; }
let foundDistance = nearestDistance;
- let alpha = fillHit(start, direction, foundDistance);
- if (hitCutoff == 0.0 || alpha >= hitCutoff) {
- hitDistance += travelled;
- return true;
+ if (!(cameraRay && nearestHitIsShadowOnly())) {
+ let alpha = fillHit(start, direction, foundDistance);
+ if (hitCutoff == 0.0 || alpha >= hitCutoff) {
+ hitDistance += travelled;
+ return true;
+ }
}
let advance = foundDistance + surfaceOffset;
travelled += advance;
@@ -986,7 +993,7 @@ fn main(@builtin(global_invocation_id) id: vec3<u32>) {
let screenX = ((f32(id.x) + jitterX) / f32(width) * 2.0 - 1.0) * params.cameraAndClamp.x;
let screenY = (1.0 - (f32(id.y) + jitterY) / f32(height) * 2.0) * params.cameraAndClamp.y;
let rayDirection = normalize(params.forward.xyz + params.right.xyz * screenX + params.above.xyz * screenY);
- tracePath(params.eye.xyz, rayDirection, pixel, sampleIndex);
+ tracePath(params.eye.xyz, rayDirection, pixel, sampleIndex, true);
if (!(all(radiance >= vec3<f32>(0.0)) && all(radiance < vec3<f32>(1e30)))) { radiance = vec3<f32>(0.0); }
if (!(featureDepth >= 0.0 && featureDepth < 1e30)) { featureDepth = 0.0; }
if (!(all(featureNormal >= vec3<f32>(-1.5)) && all(featureNormal < vec3<f32>(1.5)))) { featureNormal = vec3<f32>(0.0, 0.0, 1.0); }
@@ -1023,7 +1030,7 @@ fn bakeTexels(@builtin(global_invocation_id) id: vec3<u32>, @builtin(num_workgro
let coneRadius = sqrt(firstUnit);
let angle = TWO_PI * secondUnit;
let rayDirection = buildFrame(normal) * vec3<f32>(coneRadius * cos(angle), coneRadius * sin(angle), sqrt(max(0.0, 1.0 - firstUnit)));
- tracePath(origin, rayDirection, texelIndex, sampleIndex);
+ tracePath(origin, rayDirection, texelIndex, sampleIndex, false);
if (!(all(radiance >= vec3<f32>(0.0)) && all(radiance < vec3<f32>(1e30)))) { radiance = vec3<f32>(0.0); }
accumulation[texelIndex] += vec4<f32>(radiance, 0.0);
accumulation[texelCount + texelIndex] += vec4<f32>(firstLegEscaped, 0.0, 0.0, 0.0);
@@ -1052,7 +1059,8 @@ fn addLights(depth: i32, normal: vec3<f32>) {
selectionProbability = 1.0 / f32(emitterCount);
}`}
${hasSubsurface ? SUBSURFACE_FUNCTIONS : ''}
-fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleIndex: u32) {
+// cameraRays is true when the path starts at the camera, so its first leg steps through shadow-only parts.
+fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleIndex: u32, cameraRays: bool) {
resetRandom(pixel, sampleIndex, params.seedAndAtlas.x);
radiance = vec3<f32>(0.0);
throughput = vec3<f32>(1.0);
@@ -1068,7 +1076,7 @@ fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleInd
let bounces = params.sizeBouncesLights.z;
let surfaceOffset = params.environmentRotationAndRays.z;
for (var depth = 0; depth <= bounces; depth++) {
- let found = intersectScene(position, direction);
+ let found = intersectScene(position, direction, cameraRays && depth == 0);
${hasVolume ? 'let inFog = scatterInFog(position, select(params.environmentRotationAndRays.w, hitDistance, found));' : 'let inFog = false;'}
if (!inFog && !found) {
if (depth == 0) { firstLegEscaped = 1.0; }
@@ -1293,7 +1301,8 @@ fn extendPaths(@builtin(global_invocation_id) id: vec3<u32>) {
if (nearestSlot < 0) { break; }
let material = i32(triangles[nearestSlot * TRIANGLE_TEXELS].w) * MATERIAL_TEXELS;
let cutoff = materials[material + 3].z;
- if (cutoff == 0.0 || alphaAtNearestHit() >= cutoff) {
+ let steppedThrough = currentDepth() == 0 && nearestHitIsShadowOnly();
+ if (!steppedThrough && (cutoff == 0.0 || alphaAtNearestHit() >= cutoff)) {
found = true;
break;
}
diff --git a/src/path-tracer-webgpu-medium.js b/src/path-tracer-webgpu-medium.js
index bcad7e9e..561940e2 100644
--- a/src/path-tracer-webgpu-medium.js
+++ b/src/path-tracer-webgpu-medium.js
@@ -105,7 +105,7 @@ fn randomWalk(point: vec3<f32>, normal: vec3<f32>, meanFreePath: vec3<f32>, colo
let height = sqrt(max(0.0, 1.0 - radius * radius));
walkDirection = frame[0] * (radius * cos(angle)) + frame[1] * (radius * sin(angle)) - normal * height;
for (var scatters = 0; scatters <= WALK_STEPS; scatters++) {
- let found = intersectScene(position, walkDirection);
+ let found = intersectScene(position, walkDirection, false);
if (!found || hitMaterialIndex != material) { return select(WALK_LOST, WALK_THIN, scatters == 0); }
// One channel draws the distance; weighting by the mean density over the three channels keeps every channel unbiased.
let hero = min(2, i32(floor(nextRandom() * 3.0)));
diff --git a/src/path-tracer-webgpu.js b/src/path-tracer-webgpu.js
index 15e95c37..a93d427d 100644
--- a/src/path-tracer-webgpu.js
+++ b/src/path-tracer-webgpu.js
@@ -133,7 +133,7 @@ function uploadScene(device, scene, requestedLightSelection = 'uniform') {
environmentTables.set(environment.pixels.data.subarray(0, environment.width * environment.height), environment.height);
}
// The subsurface table (two vectors per material) follows the material table in the same buffer.
- const materialData = packMaterials(scene.materials, atlas).data;
+ const materialData = packMaterials(scene.materials, atlas, scene.shadowOnlyParts).data;
const subsurfaceOffset = materialData.length / 4;
const subsurfaceData = sceneUsesSubsurface(scene.materials) ? packSubsurface(scene.materials).data : new Float32Array(0);
const materialsWithSubsurface = new Float32Array(materialData.length + subsurfaceData.length);
diff --git a/src/path-tracer.js b/src/path-tracer.js
index fc4a5d71..3f7307d4 100644
--- a/src/path-tracer.js
+++ b/src/path-tracer.js
@@ -982,7 +982,6 @@ export function describeIgnoredFeatures(parts, environment, backend = 'cpu') {
if (!environment?.map) notes.add('There is no environment map, so the sky is black and only the lights shine.');
for (const part of parts) {
const material = part.material ?? {};
- if (part.shadowOnly === true && backend !== 'cpu') notes.add('shadowOnly is left out of camera rays by the cpu backend only; the GPU path tracers draw the part.');
for (const field of IGNORED_MAP_FIELDS) if (material[field]) notes.add(`${field} is ignored.`);
if (material.tag && material.tag !== 'surface') notes.add(`${material.tag} materials are traced as plain surfaces.`);
if (material.alphaMode === 'blend') notes.add(hasGraphOpacity(material) ? 'A graph opacity output is traced as a cutout at 0.5.' : 'Blended transparency is traced as opaque.');
diff --git a/tests/cook-kernel-parity.test.js b/tests/cook-kernel-parity.test.js
index 96591e78..cedbec5f 100644
--- a/tests/cook-kernel-parity.test.js
+++ b/tests/cook-kernel-parity.test.js
@@ -54,3 +54,13 @@ test('the tier bench gives every Cook backend the same firefly clamp and records
assert.match(driver, /fireflyClamp: 1000/);
assert.doesNotMatch(driver, /1000000000/);
});
+
+test('both kernels step camera rays through a part flagged in texel 5 of its material, and no other ray', () => {
+ const nativeTrace = extractFunction(NATIVE_KERNEL_SOURCE, 'traceRay');
+ assert.match(nativeTrace, /if \(cameraRay && materials\[material \+ 5\]\.y > 0\.0\) \{ continue; \}/);
+ assert.match(extractFunction(NATIVE_KERNEL_SOURCE, 'occluded'), /traceRay\(origin, direction, limit, true, false\)/);
+ assert.match(extractFunction(MEGAKERNEL_SOURCE, 'nearestHitIsShadowOnly'), /MATERIAL_TEXELS \+ 5\]\.y > 0\.0/);
+ assert.match(extractFunction(MEGAKERNEL_SOURCE, 'intersectScene'), /cameraRay && nearestHitIsShadowOnly\(\)/);
+ assert.match(extractFunction(NATIVE_KERNEL_SOURCE, 'tracePath'), /intersectScene\(position, direction, cameraRays && depth == 0\)/);
+ assert.match(extractFunction(MEGAKERNEL_SOURCE, 'tracePath'), /intersectScene\(position, direction, cameraRays && depth == 0\)/);
+});
diff --git a/tests/native-shadow-only.test.js b/tests/native-shadow-only.test.js
new file mode 100644
index 00000000..c04158bd
--- /dev/null
+++ b/tests/native-shadow-only.test.js
@@ -0,0 +1,127 @@
+import assert from 'node:assert/strict';
+import { mkdirSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import test from 'node:test';
+
+import { buildNativeInput, findNativeAddon, traceNative, upgradeNativeInputV1ToV2 } from '../packages/render-native/index.mjs';
+import { STRUCT_STRIDES_V2 } from '../packages/render-native/pack-input-v2.mjs';
+import { box } from '../src/geometry.js';
+import { createCamera } from '../src/path-tracer.js';
+import * as pack from '../src/path-tracer-gpu-pack.js';
+import { MATERIAL_TEXELS } from '../src/path-tracer-gpu-shader.js';
+import { buildTraceScene } from '../src/path-tracer-scene.js';
+
+const skipWithoutAddon = findNativeAddon() === null ? 'the native addon is not built' : false;
+const INSTANCE_VISIBILITY_CAMERA = 1;
+const INSTANCE_VISIBILITY_OFFSET = 116;
+const SIZE = 16;
+const environment = { keyColor: [3, 3, 3], lightDirection: [0, 1, 0] };
+const cameraDescription = { eye: [0, 6, 0], target: [0, 0, 0], up: [0, 0, -1], fov: 30 };
+
+function placed(translation, scale) {
+ return new Float32Array([scale[0], 0, 0, 0, 0, scale[1], 0, 0, 0, 0, scale[2], 0, translation[0], translation[1], translation[2], 1]);
+}
+
+function surface(color) {
+ return { tag: 'surface', metallic: 0, roughness: 1, emission: 0, color };
+}
+
+function floor() {
+ return { name: 'floor', geometry: box(0, 1), matrix: placed([0, -0.05, 0], [20, 0.1, 20]), material: surface([0.5, 0.5, 0.5]) };
+}
+
+function caster(extra) {
+ return { name: 'caster', geometry: box(0, 1), matrix: placed([0, 1.5, 0], [3, 0.2, 3]), material: surface([0.9, 0.2, 0.1]), ...extra };
+}
+
+function packScene(parts, size) {
+ const scene = buildTraceScene(parts, environment);
+ const camera = createCamera(cameraDescription, size, size);
+ return buildNativeInput(scene, camera, { width: size, height: size, samples: 32, bounces: 2, seed: 1 }, pack);
+}
+
+function render(parts, kernel, size = SIZE) {
+ const input = packScene(parts, size);
+ return traceNative(input, { width: size, height: size, samples: 32, bounces: 2, kernel }).rgba;
+}
+
+function centreBrightness(rgba, size = SIZE) {
+ let sum = 0;
+ const first = size / 2 - 2;
+ for (let row = first; row < first + 4; row++) {
+ for (let column = first; column < first + 4; column++) {
+ const offset = (row * size + column) * 4;
+ sum += rgba[offset] + rgba[offset + 1] + rgba[offset + 2];
+ }
+ }
+ return sum / 16;
+}
+
+function instanceVisibility(upgraded, instanceIndex) {
+ const bytes = upgraded.instances;
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
+ return view.getUint32(instanceIndex * STRUCT_STRIDES_V2.instance + INSTANCE_VISIBILITY_OFFSET, true);
+}
+
+test('the material table flags a shadow-only part and leaves the table of a scene without one unchanged', () => {
+ const plain = buildTraceScene([floor(), caster()], environment);
+ const withFlag = buildTraceScene([floor(), caster({ shadowOnly: true })], environment);
+ assert.equal(plain.shadowOnlyParts, null);
+ const atlas = pack.packAtlas(plain.materials);
+ const unflagged = pack.packMaterials(plain.materials, atlas, plain.shadowOnlyParts).data;
+ assert.deepEqual(unflagged, pack.packMaterials(plain.materials, atlas).data, 'a scene without shadow-only parts packs the same bytes');
+ const flagged = pack.packMaterials(withFlag.materials, atlas, withFlag.shadowOnlyParts).data;
+ const casterFloatOffset = MATERIAL_TEXELS * 4;
+ const differing = [];
+ flagged.forEach((value, index) => {
+ if (value !== unflagged[index]) differing.push(index);
+ });
+ assert.deepEqual(differing, [casterFloatOffset + pack.SHADOW_ONLY_SLOT], 'only the caster flag differs');
+});
+
+test('the cycles input hides a shadow-only part from camera rays and keeps every other ray bit', () => {
+ const scene = buildTraceScene([floor(), caster({ shadowOnly: true })], environment);
+ const camera = createCamera(cameraDescription, SIZE, SIZE);
+ const input = buildNativeInput(scene, camera, { width: SIZE, height: SIZE, samples: 4, bounces: 2, seed: 1 }, pack);
+ const upgraded = upgradeNativeInputV1ToV2(input, { width: SIZE, height: SIZE, samples: 4 });
+ assert.equal(upgraded.instanceCount, 2);
+ const visibility = [instanceVisibility(upgraded, 0), instanceVisibility(upgraded, 1)];
+ const cameraVisible = visibility.filter(flags => (flags & INSTANCE_VISIBILITY_CAMERA) !== 0);
+ const cameraHidden = visibility.filter(flags => (flags & INSTANCE_VISIBILITY_CAMERA) === 0);
+ assert.equal(cameraVisible.length, 1, 'the floor stays visible to the camera');
+ assert.equal(cameraHidden.length, 1, 'the caster is hidden from the camera');
+ assert.equal(cameraHidden[0] & 0x3e, 0x3e, 'the hidden part still takes diffuse, glossy, transmission, shadow and volume rays');
+});
+
+for (const kernel of ['legacy', 'cycles']) {
+ test(`the native ${kernel} kernel draws the shadow of a shadow-only caster and not the caster`, { skip: skipWithoutAddon }, () => {
+ const lit = centreBrightness(render([floor()], kernel));
+ const visibleCaster = centreBrightness(render([floor(), caster()], kernel));
+ const shadowOnly = centreBrightness(render([floor(), caster({ shadowOnly: true })], kernel));
+ assert.ok(lit > 0.5, `the bare floor is lit by the sun: ${lit}`);
+ assert.ok(visibleCaster > lit * 0.5, `a visible caster is drawn in the middle of the picture: ${visibleCaster} against ${lit}`);
+ assert.ok(shadowOnly < lit * 0.15, `the floor under a shadow-only caster is in its shadow: ${shadowOnly} against ${lit}`);
+ assert.ok(visibleCaster > shadowOnly * 5, `the shadow-only caster is not drawn: ${visibleCaster} against ${shadowOnly}`);
+ });
+}
+
+// Set COOK_SHADOW_ONLY_IMAGE_DIRECTORY to keep the three renders side by side (floor, visible caster, shadow-only caster) as a PPM.
+test('the native legacy kernel writes the comparison picture when asked', { skip: skipWithoutAddon || !process.env.COOK_SHADOW_ONLY_IMAGE_DIRECTORY }, () => {
+ const size = 128;
+ const renders = [[floor()], [floor(), caster()], [floor(), caster({ shadowOnly: true })]].map(parts => render(parts, 'legacy', size));
+ const pixels = Buffer.alloc(size * 3 * size * 3);
+ renders.forEach((rgba, panel) => {
+ for (let row = 0; row < size; row++) {
+ for (let column = 0; column < size; column++) {
+ for (let channel = 0; channel < 3; channel++) {
+ const linear = Math.min(1, Math.max(0, rgba[(row * size + column) * 4 + channel]));
+ const encoded = linear <= 0.0031308 ? linear * 12.92 : 1.055 * linear ** (1 / 2.4) - 0.055;
+ pixels[(row * size * 3 + panel * size + column) * 3 + channel] = Math.round(encoded * 255);
+ }
+ }
+ }
+ });
+ const directory = process.env.COOK_SHADOW_ONLY_IMAGE_DIRECTORY;
+ mkdirSync(directory, { recursive: true });
+ writeFileSync(join(directory, 'shadow-only-native.ppm'), Buffer.concat([Buffer.from(`P6\n${size * 3} ${size}\n255\n`), pixels]));
+});
diff --git a/tests/path-tracer-shadow-only.test.js b/tests/path-tracer-shadow-only.test.js
index 837b547f..26358d7f 100644
--- a/tests/path-tracer-shadow-only.test.js
+++ b/tests/path-tracer-shadow-only.test.js
@@ -44,8 +44,9 @@ test('a part without the flag leaves the trace scene and the notes unchanged', (
assert.deepEqual(describeIgnoredFeatures([floor(), caster()], {}, 'cpu').filter(note => note.startsWith('shadowOnly')), []);
});
-test('the GPU path tracers say they still draw a shadow-only part', () => {
- const notes = describeIgnoredFeatures([floor(), caster({ shadowOnly: true })], {}, 'gpu');
- assert.ok(notes.some(note => note.startsWith('shadowOnly')));
- assert.deepEqual(describeIgnoredFeatures([floor(), caster({ shadowOnly: true })], {}, 'cpu').filter(note => note.startsWith('shadowOnly')), []);
+test('no Cook backend lists shadowOnly as an ignored feature', () => {
+ for (const backend of ['cpu', 'gpu', 'webgpu', 'native']) {
+ const notes = describeIgnoredFeatures([floor(), caster({ shadowOnly: true })], {}, backend);
+ assert.deepEqual(notes.filter(note => note.startsWith('shadowOnly')), [], backend);
+ }
});