Skip to content

armgit / viz3d

Public Imported from GitHub
0

An AI agent skill that turns any subject into a 3D film or app

Coplanar check covers vertical surfaces; decals stand proud on walls and vehicles

Nguyen Xuan An51e2caa066ec
diff --git a/plugins/visualize-in-3d/skills/visualize-in-3d/references/brief.md b/plugins/visualize-in-3d/skills/visualize-in-3d/references/brief.md
index e4c297c..44d7fcd 100644
--- a/plugins/visualize-in-3d/skills/visualize-in-3d/references/brief.md
+++ b/plugins/visualize-in-3d/skills/visualize-in-3d/references/brief.md
@@ -114,7 +114,7 @@ keeps the previous and current pose of everything that moves and renders the pos
time, so motion and the follow camera change every frame (no stutter). No two surfaces share a plane and no two
modules overlap: lay repeats end to end, build curves (track, viaduct deck) as continuous swept geometry, lift
stacked floors a few centimetres apart, and set the camera near plane to about 0.5 m. Ground layers each get
-their own height: grass -0.2 m, pads -0.06, road and floor modules 0, paint and decals +0.04. Where a pad or plaza
+their own height: grass -0.2 m, pads -0.06, road and floor modules 0, paint and decals +0.04. Walls and vehicles follow the same rule: a sign, lamp or poster stands 4 cm proud of the glass or panel behind it, never at the same depth. Where a pad or plaza
replaces a road, leave the road modules out there instead of laying both. Run {{SKILL_DIR}}/scripts/coplanar-check.js
on the built scene and fix every pair it lists before any screenshot. Anything that must meet a vehicle (platform screen doors, jet bridges, loading docks, lock
gates) is placed from the vehicle's own door or hatch positions where it stops, not from a repeating module, and the
diff --git a/plugins/visualize-in-3d/skills/visualize-in-3d/scripts/coplanar-check.js b/plugins/visualize-in-3d/skills/visualize-in-3d/scripts/coplanar-check.js
index aee347c..0001e53 100644
--- a/plugins/visualize-in-3d/skills/visualize-in-3d/scripts/coplanar-check.js
+++ b/plugins/visualize-in-3d/skills/visualize-in-3d/scripts/coplanar-check.js
@@ -1,47 +1,65 @@
-// Finds flat surfaces that fight for the same depth (z-fighting): two meshes whose footprints overlap
-// and whose top faces lie within `tolerance` metres of each other. Run it in the page once the world is built:
+// Finds flat surfaces that fight for the same depth (z-fighting): two thin meshes, flat along the same axis, whose
+// faces overlap and lie within `tolerance` metres of each other. It checks all three axes, so it catches a sign on a
+// windscreen or a poster on a wall as well as paint on a road. Run it in the page once the world is built:
// import { findCoplanarPairs } from './coplanar-check.js';
// console.table(findCoplanarPairs(scene));
// Fix every pair it reports: remove the hidden surface, or move the layers at least 3 cm apart.
import * as THREE from 'three';
const FLAT_RATIO = 0.05;
+const AXES = ['x', 'y', 'z'];
-function flatFootprint(mesh) {
- const box = new THREE.Box3().setFromObject(mesh);
- const height = box.max.y - box.min.y;
- const width = box.max.x - box.min.x;
- const depth = box.max.z - box.min.z;
- if (width <= 0 || depth <= 0) return null;
- if (height > FLAT_RATIO * Math.max(width, depth) && height > 0.3) return null;
- return { box, top: box.max.y };
+/** The axes along which this box is thin, each with the two other axes that span its face. */
+function flatAxes(box) {
+ const size = { x: box.max.x - box.min.x, y: box.max.y - box.min.y, z: box.max.z - box.min.z };
+ const result = [];
+ for (const axis of AXES) {
+ const others = AXES.filter((other) => other !== axis);
+ const span = Math.max(size[others[0]], size[others[1]]);
+ if (size[others[0]] <= 0 || size[others[1]] <= 0) continue;
+ if (size[axis] <= FLAT_RATIO * span || size[axis] <= 0.3) result.push({ axis, others });
+ }
+ return result;
+}
+
+function overlapArea(first, second, others) {
+ const [one, two] = others;
+ const overlapOne = Math.min(first.max[one], second.max[one]) - Math.max(first.min[one], second.min[one]);
+ const overlapTwo = Math.min(first.max[two], second.max[two]) - Math.max(first.min[two], second.min[two]);
+ if (overlapOne <= 0 || overlapTwo <= 0) return 0;
+ return overlapOne * overlapTwo;
}
-function overlapArea(first, second) {
- const overlapX = Math.min(first.max.x, second.max.x) - Math.max(first.min.x, second.min.x);
- const overlapZ = Math.min(first.max.z, second.max.z) - Math.max(first.min.z, second.min.z);
- if (overlapX <= 0 || overlapZ <= 0) return 0;
- return overlapX * overlapZ;
+/** The smallest distance between a face of one box and the same-facing face of the other, along the axis. */
+function faceGap(first, second, axis) {
+ const maxGap = Math.abs(first.max[axis] - second.max[axis]);
+ const minGap = Math.abs(first.min[axis] - second.min[axis]);
+ return Math.min(maxGap, minGap);
}
-export function findCoplanarPairs(scene, { tolerance = 0.03, minimumArea = 0.25 } = {}) {
+export function findCoplanarPairs(scene, { tolerance = 0.03, minimumArea = 0.05 } = {}) {
scene.updateMatrixWorld(true);
const flats = [];
scene.traverse((object) => {
if (!object.isMesh || !object.visible || object.isInstancedMesh) return;
const material = Array.isArray(object.material) ? object.material[0] : object.material;
if (material && material.transparent && material.opacity < 0.5) return;
- const footprint = flatFootprint(object);
- if (footprint) flats.push({ name: object.name || object.parent?.name || '(unnamed)', ...footprint });
+ const box = new THREE.Box3().setFromObject(object);
+ const axes = flatAxes(box);
+ if (axes.length) flats.push({ name: object.name || object.parent?.name || '(unnamed)', box, axes });
});
const pairs = [];
for (let first = 0; first < flats.length; first++) {
for (let second = first + 1; second < flats.length; second++) {
- const gap = Math.abs(flats[first].top - flats[second].top);
- if (gap >= tolerance) continue;
- const area = overlapArea(flats[first].box, flats[second].box);
- if (area < minimumArea) continue;
- pairs.push({ first: flats[first].name, second: flats[second].name, gapCm: +(gap * 100).toFixed(1), overlapM2: +area.toFixed(1) });
+ for (const { axis, others } of flats[first].axes) {
+ if (!flats[second].axes.some((entry) => entry.axis === axis)) continue;
+ const gap = faceGap(flats[first].box, flats[second].box, axis);
+ if (gap >= tolerance) continue;
+ const area = overlapArea(flats[first].box, flats[second].box, others);
+ if (area < minimumArea) continue;
+ pairs.push({ first: flats[first].name, second: flats[second].name, axis, gapCm: +(gap * 100).toFixed(1), overlapM2: +area.toFixed(2) });
+ break;
+ }
}
}
return pairs.sort((left, right) => right.overlapM2 - left.overlapM2);