Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Keep the studio floor from burying small objects (closes #468)

Nguyen Xuan An88fafcc9c462
diff --git a/changes/468-small-scene-floor.md b/changes/468-small-scene-floor.md
new file mode 100644
index 00000000..6a39cc12
--- /dev/null
+++ b/changes/468-small-scene-floor.md
@@ -0,0 +1 @@
+- The default studio floor no longer buries small objects. It stays at y 0.019, but when that would hide more than a tenth of the scene's height (a pencil, scissors, a robot hand hanging below y 0), it sits 2% up from the scene's lowest point instead. Four files that drew an empty frame now draw: the Menagerie Tetheria hand and the SimReady forceps, pencil and scissors (issue #468).
diff --git a/changes/468-small-scene-floor.size.md b/changes/468-small-scene-floor.size.md
new file mode 100644
index 00000000..7ff9ea93
--- /dev/null
+++ b/changes/468-small-scene-floor.size.md
@@ -0,0 +1,6 @@
+- entry:core: +166 B for the studio floor height: one pass over the parts' cached local boxes. It fixes small objects that drew an empty frame under the default floor.
+- scene:lit-box: the same core code, +181 B.
+- scene:gltf-box: the same core code, +179 B.
+- scene:gltf-box+lazy: the same core code, +179 B.
+- scene:agent-scene: the same core code, +172 B.
+- scene:agent-scene+lazy: the same core code, +174 B.
diff --git a/docs/guide.md b/docs/guide.md
index 4cfc3790..f2bc9b23 100644
--- a/docs/guide.md
+++ b/docs/guide.md
@@ -330,7 +330,7 @@ Tags select specialized behavior: `armor` allows studio finish overrides. `floor
| --- | --- | --- |
| batching | true | Share instance batches by geometry and map identity |
| pixelRatio | 1.5 | Render scale relative to CSS size. Not automatically devicePixelRatio |
-| ground | true | Add the studio floor. Disable for worlds, 2D and assets |
+| ground | true | Add the studio floor at y 0.019, or 2% up from the lowest point of a scene it would bury more than a tenth of (small objects, scenes below y 0). Disable for worlds, 2D and assets |
| world | false | Enable outdoor rendering and world clipping defaults |
| samples | 4 | Requested MSAA count. Negotiated against hardware |
| hdr | false | Request floating-point color targets |
diff --git a/src/render-culling.js b/src/render-culling.js
index 8a38f731..d6d179be 100644
--- a/src/render-culling.js
+++ b/src/render-culling.js
@@ -33,7 +33,7 @@ export function geometryBoundingSphere(geometry) {
const dz = positions[index + 2] - center[2];
radiusSquared = Math.max(radiusSquared, dx * dx + dy * dy + dz * dz);
}
- const sphere = Object.freeze({ center: Object.freeze(center), radius: Math.sqrt(radiusSquared) });
+ const sphere = Object.freeze({ center: Object.freeze(center), radius: Math.sqrt(radiusSquared), minimum, maximum });
boundingSpheres.set(geometry, sphere);
return sphere;
}
diff --git a/src/renderer.js b/src/renderer.js
index 2e7f624b..e06b301a 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -297,11 +297,37 @@ function createLevelSet(levels, instanceCount) {
return { levelErrors: levels.map(level => level.error), levelGeometries: levels.map(level => level.geometry), levelDraws: [], selection: new InstanceSelection(instanceCount, levels.length) };
}
-function groundPart() {
+/**
+ * The studio floor sits at y 0.019. When that would hide more than a tenth of the scene's height (a pencil, a hand that
+ * hangs below y 0), it sits 2% up from the scene's lowest point instead, so small objects are not buried (issue #468).
+ * Scenes a metre or more tall resting on y 0, and large scenes that dip below it, keep the floor where it was.
+ */
+export function studioFloorHeight(parts) {
+ let bottom = Infinity;
+ let top = -Infinity;
+ for (const { geometry, matrix } of parts) {
+ if (!geometry?.positions?.length || !matrix) continue;
+ // The world heights of the part's local box: each axis adds its row-one term at the box's lower or upper end.
+ const { minimum, maximum } = geometryBoundingSphere(geometry);
+ let low = matrix[13];
+ let spread = 0;
+ for (let axis = 0; axis < 3; axis++) {
+ const scale = matrix[1 + axis * 4];
+ low += Math.min(scale * minimum[axis], scale * maximum[axis]);
+ spread += Math.abs(scale) * (maximum[axis] - minimum[axis]);
+ }
+ bottom = Math.min(bottom, low);
+ top = Math.max(top, low + spread);
+ }
+ const height = top - bottom;
+ return 0.019 - bottom > height * 0.1 ? bottom + height * 0.02 : 0.019;
+}
+
+function groundPart(height = 0.019) {
return {
name: 'Studio floor',
geometry: {
- positions: new Float32Array([-100, 0.019, -100, -100, 0.019, 100, 100, 0.019, 100, 100, 0.019, -100]),
+ positions: new Float32Array([-100, height, -100, -100, height, 100, 100, height, 100, 100, height, -100]),
normals: new Float32Array([0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]),
indices: new Uint32Array([0, 1, 2, 0, 2, 3]),
},
@@ -504,7 +530,7 @@ export class Renderer67 {
this.geometryBytes = 0;
this.geometryBuffers = new Map();
this.batching = batching;
- this.groundPart = ground === true ? groundPart() : ground || null;
+ this.groundPart = ground === true ? groundPart(studioFloorHeight(parts)) : ground || null;
this.buffers = [];
this.batches = [];
this.partLocations = new Map();
diff --git a/tests/studio-floor.test.js b/tests/studio-floor.test.js
new file mode 100644
index 00000000..70c43947
--- /dev/null
+++ b/tests/studio-floor.test.js
@@ -0,0 +1,27 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { mat4 } from 'gl-matrix';
+import { box } from '../src/geometry.js';
+import { studioFloorHeight } from '../src/renderer.js';
+
+function placed(geometry, scale, translation) {
+ const matrix = mat4.fromTranslation(mat4.create(), translation);
+ mat4.scale(matrix, matrix, [scale, scale, scale]);
+ return { geometry, matrix };
+}
+
+test('the studio floor stays at 0.019 under a scene a metre or more tall, or a large scene that dips below y 0', () => {
+ assert.equal(studioFloorHeight([placed(box(), 1, [0, 0.5, 0])]), 0.019);
+ // 4 m tall, dipping 0.2 m below y 0: the floor hides 5% of it and stays.
+ assert.equal(studioFloorHeight([placed(box(), 4, [0, 1.8, 0])]), 0.019);
+ assert.equal(studioFloorHeight([]), 0.019);
+});
+
+test('the studio floor drops under small objects and scenes below y 0 instead of burying them (issue #468)', () => {
+ // A pencil-sized box resting on y 0, 8 mm tall: the old floor at 19 mm covered all of it.
+ const pencil = studioFloorHeight([placed(box(), 0.008, [0, 0.004, 0])]);
+ assert.ok(Math.abs(pencil - 0.008 * 0.02) < 1e-9, `floor at ${pencil}`);
+ // A hand from y -0.11 to 0.007 after node scales, like the Menagerie hand.
+ const hand = studioFloorHeight([placed(box(), 0.117, [0, -0.0515, 0])]);
+ assert.ok(hand < -0.105 && hand > -0.11, `floor at ${hand}`);
+});