Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Trim the shadowOnly bytes (#926)

Nguyen Xuan Anfe21a7da86ed
diff --git a/changes/926-shadow-only-nodes.size.md b/changes/926-shadow-only-nodes.size.md
index 17be1383..22a77c44 100644
--- a/changes/926-shadow-only-nodes.size.md
+++ b/changes/926-shadow-only-nodes.size.md
@@ -1 +1,5 @@
-- entry:core: +96 bytes gzip so a node can cast a shadow while staying out of the picture, which a gobo outside the shot needs and no existing flag gives (#926). About 0.1 percent of the bundle.
+- scene:agent-scene: +126 bytes gzip (and +44 on lit-box, +29 on entry:core) so a node can cast a shadow while staying out of the picture, which a gobo outside the shot needs and no existing flag gives (#926). Under 0.1 percent of the bundle.
+- scene:agent-scene+lazy: same change as scene:agent-scene.
+- scene:lit-box: same change as scene:agent-scene.
+- scene:gltf-box: same change as scene:agent-scene.
+- scene:gltf-box+lazy: same change as scene:agent-scene.
diff --git a/src/agent-scene-capabilities.js b/src/agent-scene-capabilities.js
index a7e744de..a5d61aa6 100644
--- a/src/agent-scene-capabilities.js
+++ b/src/agent-scene-capabilities.js
@@ -281,7 +281,7 @@ export const AGENT_SCENE_CAPABILITIES = Object.freeze({
choose: 'agx for lit scenes and Blender comparisons, neutral for product and brand colour, aces for contrast; needs loadAgentSceneModules',
}),
// Flush surfaces flicker (z-fighting). They are ranked at draw time by default; coplanar-overlap warns about what is left.
- renderFlags: Object.freeze({ nodeFields: Object.freeze(['visible', 'castShadow', 'receiveShadow', 'holdout', 'shadowCatcher', 'shadowOnly']), motion: 'node track property visible: 1 or 0, STEP', note: 'visible:false hides a node and children; holdout and shadowCatcher need a transparent canvas (Renderer67 transparent:true); flags split batches. Blended and transmissive parts cast a sun shadow weighted by opacity and tinted by colour (1024 map, one extra draw of those parts only); shadowOnly:true keeps a node in the shadow passes and out of the picture (a gobo, such as a window frame outside the shot); castShadow:false turns it off, and cascades or splat/3-layer-map receivers skip the tint.' }),
+ renderFlags: Object.freeze({ nodeFields: Object.freeze(['visible', 'castShadow', 'receiveShadow', 'holdout', 'shadowCatcher', 'shadowOnly']), motion: 'node track property visible: 1 or 0, STEP', note: 'visible:false hides a node and children; holdout and shadowCatcher need a transparent canvas (Renderer67 transparent:true); flags split batches. Blended and transmissive parts cast a sun shadow weighted by opacity and tinted by colour (1024 map, one extra draw of those parts only); shadowOnly:true casts a shadow and is not drawn (gobo); castShadow:false turns it off, and cascades or splat/3-layer-map receivers skip the tint.' }),
depthBias: Object.freeze({ nodeFields: Object.freeze(['decal', 'depthLayer']), maxDepthLayer: 4, environment: 'environment.resolveCoplanar', modes: Object.freeze(['offset', false]), default: 'offset', warning: 'coplanar-overlap', note: 'Automatic: flush or near-flush faces, inside one mesh or across parts, are ranked per triangle at draw time, the front face wins, geometry never moves; no field needed. resolveCoplanar false opts out. decal wins over the surface below; depthLayer stacks decals. scene_inspect flush.ranked counts ranked parts. A coplanar-overlap warning carries reason rank-overflow (over 15 stacked faces: remove duplicates), double-sided-gap, unchecked or collapsed-node.' }),
proceduralMotion: true,
motionIk: Object.freeze(['two-bone', 'aim', 'chain', 'spline']),
diff --git a/src/agent-scene.js b/src/agent-scene.js
index bf5b158b..0eee6301 100644
--- a/src/agent-scene.js
+++ b/src/agent-scene.js
@@ -344,7 +344,7 @@ function nodeFrom(value) {
}
if (value.holdout === true && value.shadowCatcher === true) throw invalidField('shadowCatcher', value.shadowCatcher, 'false when holdout is true; a node is a holdout or a shadow catcher, not both', TypeError);
if (value.shadowOnly === true && (value.castShadow === false || value.holdout === true || value.shadowCatcher === true)) {
- throw invalidField('shadowOnly', value.shadowOnly, 'false when castShadow is false, or holdout or shadowCatcher is true; a shadow-only node casts a shadow and is not drawn', TypeError);
+ throw invalidField('shadowOnly', value.shadowOnly, 'false with castShadow false, holdout or shadowCatcher', TypeError);
}
if (value.skin !== undefined) {
if (value.geometry === undefined) {
diff --git a/src/renderer.js b/src/renderer.js
index eba078ae..c298bc4c 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -291,7 +291,6 @@ function validatePartFlags(part) {
if (part[flag] !== undefined && typeof part[flag] !== 'boolean') throw new TypeError(`${flag} must be a boolean`);
}
if (part.holdout === true && part.shadowCatcher === true) throw new Error(`A part is a holdout or a shadow catcher, not both: ${part.name}`);
- if (part.shadowOnly === true && (part.castShadow === false || part.holdout === true || part.shadowCatcher === true)) throw new Error(`A shadow-only part casts a shadow and is not drawn, so it cannot also set castShadow false, holdout or shadowCatcher: ${part.name}`);
}
/** A hidden part keeps its slot in the batch but draws and casts nothing: its instance matrices are packed as zeros. */
@@ -1529,7 +1528,7 @@ export class Renderer67 {
if (part.skin || part.morph) this.updatePartIdentities.add(part);
validatePartFlags(part);
if (castsShadow(part) !== location.batch.castShadow) throw new Error(`Shadow casting changes require a new renderer: ${part.name}`);
- if (receivesShadow(part) !== location.batch.receivesShadow || (part.shadowOnly === true) !== (batchPass(location.batch) === 'shadowOnly') || compositeModeOf(part) !== location.batch.compositeMode) throw new Error(`receiveShadow, holdout, shadowCatcher and shadowOnly changes require replaceParts: ${part.name}`);
+ if (receivesShadow(part) !== location.batch.receivesShadow || partPass(part) !== batchPass(location.batch) || compositeModeOf(part) !== location.batch.compositeMode) throw new Error(`receiveShadow, holdout, shadowCatcher and shadowOnly changes require replaceParts: ${part.name}`);
if (depthLayerOf(part) !== (location.batch.depthLayer ?? 0)) throw new Error(`depthLayer changes require replaceParts: ${part.name}`);
// A material whose fields still equal the ones that passed the full checks below needs none of them again.
const materialUnchanged = location.materialSnapshot?.matches(part.material) === true;
diff --git a/tests/render-flags-scene.test.js b/tests/render-flags-scene.test.js
index 57edc150..31c16304 100644
--- a/tests/render-flags-scene.test.js
+++ b/tests/render-flags-scene.test.js
@@ -96,7 +96,7 @@ test('shadowOnly reaches the part, splits batches and conflicts with the flags t
assert.ok(batches.some(names => names.join() === 'gobo,gobo2'));
const node = fields => recipe({ nodes: [{ id: 'one', geometry: 'box', material: 'finish', ...fields }] });
assert.throws(() => createAgentScene(node({ shadowOnly: 1 })), /nodes\[0\]\.shadowOnly: got 1; expected true or false/);
- assert.throws(() => createAgentScene(node({ shadowOnly: true, castShadow: false })), /nodes\[0\]\.shadowOnly: got true; expected false when castShadow is false/);
+ assert.throws(() => createAgentScene(node({ shadowOnly: true, castShadow: false })), /nodes\[0\]\.shadowOnly: got true; expected false with castShadow false, holdout or shadowCatcher/);
assert.throws(() => createAgentScene(node({ shadowOnly: true, holdout: true })), /nodes\[0\]\.shadowOnly: got true/);
assert.throws(() => createAgentScene(recipe({ nodes: [{ id: 'g', shadowOnly: true }] })), /nodes\[0\]\.shadowOnly: got true; expected a flag on a mesh node/);
});