Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Merge main, keeping instancing variants from both sides (#1014)

Nguyen Xuan An948286fcc1a4
diff --cc docs/path-tracer.md
index 62ca4ad4,e51a3c35..7593b09b
--- a/docs/path-tracer.md
+++ b/docs/path-tracer.md
@@@ -63,10 -63,12 +63,14 @@@ Also checked before any ray is traced:
- Sharing is decided after the trace geometry is resolved. A skinned, flush-lifted or box-mapped part gets a geometry of its own, and a geometry with per-triangle materials is not shared either: those parts are moved into world space once and traced as instances with an identity matrix.
- Limits: 400,000 triangles counted once per shared geometry, 1,024 distinct geometries (the part limit, counted on geometries) and 65,536 instances. An instanced scene has no world-space triangle arrays or CPU BVH, so only `buildNativeInputV2` reads it.
- A part's material is its own entry in `materials`, so copies may differ in material; a multi-material mesh stays one geometry with the materials the part names.
- - `buildNativeInput` (the `legacy` kernel) refuses an instanced scene with an error; build the scene without `instancing` for that kernel. Render jobs still export flattened tables.
+ - `buildNativeInput` (the `legacy` kernel) refuses an instanced scene with an error; build the scene without `instancing` for that kernel. `traceNative` with `kernel: "legacy"` refuses a FORMAT-v2 input the same way.
+ - Render jobs with `renderer.backend: "native"` keep repeated geometry as instances when the job traces with the `cycles` kernel (the default): the job page exports FORMAT-v2 tables of the unique triangles and one record per part, the job runner rebuilds the input for `traceNative`, and the report names the `instances` and counts `triangles` once per shared geometry. With `JS67_NATIVE_KERNEL=legacy`, `JS67_NATIVE_INSTANCING=off` (flattened triangles on the `cycles` kernel, to compare the two), or a scene where no geometry is shared, the job exports flattened tables exactly as before.
+ - A job reduces the materials of an instanced scene exactly as it reduces those of a flattened one (`buildJobNativeInput` in `packages/render-native/job-input.mjs`): only the fields of the v1 material tables survive, so a part looks the same whether or not other geometry in the scene is shared. Calling `buildNativeInputV2` yourself on an instanced scene keeps the richer FORMAT-v2 material fields, as it does for a flattened scene.
+ - When the instanced packer refuses a scene (copies of an emissive mesh that emit different triangles, a singular matrix), the job exports it flattened as before and the report says `instancing: "flattened"` and `instancingReason`. A scene that fails to export no longer marks the native tracer unusable for the process; only a tracer or adapter failure does.
+ - A part with `shadowOnly: true` becomes an instance that camera rays skip (visibility bits for every other ray kind stay set). An instance whose material differs from the first copy's emits light by its own material: a copy made emissive by its material is sampled as a light, and a copy made non-emissive is not. Triangles of a shared mesh that are emissive keep one `local_emissive_offset` for all copies, so a copy scaled to a size that drops an emissive triangle the other copies keep is refused with its index.
+A part whose matrix mirrors (negative determinant) is flattened with its vertex normals kept outward and the last two corners of each triangle swapped, so the cross-product normal of every triangle points outward too. The native `cycles` kernel derives its geometric normal from that winding, and an instanced mirrored part reaches the same result through its negative-scale flag; a scene without a mirrored part is unchanged byte for byte.
+
The same scene and options give the same bytes. Random numbers come from a hash of the pixel, the sample and the seed, never from `Math.random` or the clock, and a pixel does not depend on which thread drew it.
## Choosing a backend
diff --cc tests/native-instancing-equivalence.test.js
index f168e8ce,a64cc633..b8c71b7b
--- a/tests/native-instancing-equivalence.test.js
+++ b/tests/native-instancing-equivalence.test.js
@@@ -22,41 -23,9 +23,9 @@@ function render(variant, instancing, se
return traceNative(input, { width: WIDTH, height: HEIGHT, samples: SAMPLES, kernel: 'cycles' }).rgba;
}
- function relativeMeanSquaredError(candidate, reference) {
- let total = 0;
- for (let pixel = 0; pixel < WIDTH * HEIGHT; pixel++) {
- for (let channel = 0; channel < 3; channel++) {
- const expected = reference[pixel * 4 + channel];
- const difference = candidate[pixel * 4 + channel] - expected;
- total += (difference * difference) / (expected * expected + 0.01);
- }
- }
- return total / (WIDTH * HEIGHT * 3);
- }
-
- function tileMeanRatios(candidate, reference) {
- const ratios = [];
- for (let tileRow = 0; tileRow < TILES; tileRow++) {
- for (let tileColumn = 0; tileColumn < TILES; tileColumn++) {
- let candidateSum = 0;
- let referenceSum = 0;
- for (let row = Math.floor(tileRow * HEIGHT / TILES); row < Math.floor((tileRow + 1) * HEIGHT / TILES); row++) {
- for (let column = Math.floor(tileColumn * WIDTH / TILES); column < Math.floor((tileColumn + 1) * WIDTH / TILES); column++) {
- for (let channel = 0; channel < 3; channel++) {
- candidateSum += candidate[(row * WIDTH + column) * 4 + channel];
- referenceSum += reference[(row * WIDTH + column) * 4 + channel];
- }
- }
- }
- if (referenceSum > 1e-3) ratios.push(candidateSum / referenceSum);
- }
- }
- return ratios;
- }
-
const addonSkip = findNativeAddon() ? false : 'the native addon is not built';
- for (const variant of ['translate', 'rotscale', 'mirror', 'mirror-rotscale', 'emissive']) {
-for (const variant of ['translate', 'rotscale', 'emissive', 'emissive-rotscale', 'emissive-degenerate']) {
++for (const variant of ['translate', 'rotscale', 'mirror', 'mirror-rotscale', 'emissive', 'emissive-rotscale', 'emissive-degenerate']) {
test(`instanced ${variant} copies render the flattened image within tolerance`, { skip: addonSkip }, t => {
let flattened;
try {