Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Refine optical instrument with smooth fillets and machined details

Nguyen Xuan Ane19378bd6c17
diff --git a/src/geometry.js b/src/geometry.js
index 399912ff..3fd437ee 100644
--- a/src/geometry.js
+++ b/src/geometry.js
@@ -39,7 +39,7 @@ function surface(columns, rows, sample) {
return geometry(positions, normals, indices);
}
-export function sphere(segments = 40, rings = 24) {
+export function sphere(segments = 64, rings = 40) {
return surface(segments, rings, (longitude, latitude) => {
const angle = longitude * FULL_TURN;
const elevation = latitude * Math.PI;
@@ -49,8 +49,8 @@ export function sphere(segments = 40, rings = 24) {
}
// The torus lies in XY, facing +Z. Partial arcs are useful for interrupted armor rings.
-export function torus(tubeRadius = 0.08, segments = 64, sides = 12, arc = FULL_TURN, start = 0) {
- return surface(segments, sides, (along, around) => {
+export function torus(tubeRadius = 0.08, segments = 96, sides = 24, arc = FULL_TURN, start = 0) {
+ const body = surface(segments, sides, (along, around) => {
const angle = start + along * arc;
const tubeAngle = around * FULL_TURN;
const radial = 1 + tubeRadius * Math.cos(tubeAngle);
@@ -59,12 +59,48 @@ export function torus(tubeRadius = 0.08, segments = 64, sides = 12, arc = FULL_T
normal: [Math.cos(angle) * Math.cos(tubeAngle), Math.sin(angle) * Math.cos(tubeAngle), Math.sin(tubeAngle)],
};
});
+ if (Math.abs(arc - FULL_TURN) < 1e-8) return body;
+ const positions = [...body.positions];
+ const normals = [...body.normals];
+ const indices = [...body.indices];
+ for (const end of [0, 1]) {
+ const angle = start + arc * end;
+ const direction = end === 0 ? -1 : 1;
+ const normal = [-Math.sin(angle) * direction, Math.cos(angle) * direction, 0];
+ const center = positions.length / 3;
+ positions.push(Math.cos(angle), Math.sin(angle), 0);
+ normals.push(...normal);
+ for (let index = 0; index <= sides; index++) {
+ const tubeAngle = index / sides * FULL_TURN;
+ const radial = 1 + tubeRadius * Math.cos(tubeAngle);
+ positions.push(radial * Math.cos(angle), radial * Math.sin(angle), tubeRadius * Math.sin(tubeAngle));
+ normals.push(...normal);
+ }
+ for (let index = 0; index < sides; index++) {
+ const first = center + index + 1;
+ indices.push(center, end === 0 ? first : first + 1, end === 0 ? first + 1 : first);
+ }
+ }
+ return geometry(positions, normals, indices);
}
-export function cylinder(segments = 48) {
- const wall = surface(segments, 1, (along, height) => {
+export function cylinder(segments = 80, bevel = 0.025, bevelSegments = 6) {
+ const profile = [];
+ for (let index = 0; index <= bevelSegments; index++) {
+ const angle = -Math.PI / 2 + index / bevelSegments * Math.PI / 2;
+ profile.push({ radius: 1 - bevel + bevel * Math.cos(angle), height: -0.5 + bevel + bevel * Math.sin(angle), radialNormal: Math.cos(angle), verticalNormal: Math.sin(angle) });
+ }
+ for (let index = 0; index <= bevelSegments; index++) {
+ const angle = index / bevelSegments * Math.PI / 2;
+ profile.push({ radius: 1 - bevel + bevel * Math.cos(angle), height: 0.5 - bevel + bevel * Math.sin(angle), radialNormal: Math.cos(angle), verticalNormal: Math.sin(angle) });
+ }
+ const wall = surface(segments, profile.length - 1, (along, height) => {
const angle = along * FULL_TURN;
- return { position: [Math.cos(angle), height - 0.5, Math.sin(angle)], normal: [Math.cos(angle), 0, Math.sin(angle)] };
+ const ring = profile[Math.round(height * (profile.length - 1))];
+ return {
+ position: [ring.radius * Math.cos(angle), ring.height, ring.radius * Math.sin(angle)],
+ normal: [ring.radialNormal * Math.cos(angle), ring.verticalNormal, ring.radialNormal * Math.sin(angle)],
+ };
});
const positions = [...wall.positions];
const normals = [...wall.normals];
@@ -75,7 +111,7 @@ export function cylinder(segments = 48) {
normals.push(0, sign, 0);
for (let index = 0; index <= segments; index++) {
const angle = index / segments * FULL_TURN;
- positions.push(Math.cos(angle), sign * 0.5, Math.sin(angle));
+ positions.push((1 - bevel) * Math.cos(angle), sign * 0.5, (1 - bevel) * Math.sin(angle));
normals.push(0, sign, 0);
}
for (let index = 0; index < segments; index++) {
@@ -86,18 +122,41 @@ export function cylinder(segments = 48) {
return geometry(positions, normals, indices);
}
-export function box(bevel = 0.06, subdivisions = 8) {
+export function box(bevel = 0.08, subdivisions = 6) {
const positions = [];
const normals = [];
const indices = [];
+ const coreExtent = 0.5 - bevel;
+ const coordinates = [];
+ if (bevel === 0) {
+ coordinates.push(-0.5, 0.5);
+ } else {
+ // Sample the curved border in angular steps, leaving the planar center as one span.
+ // Uniform grids miss narrow fillets and interpolate across the whole face instead.
+ for (let index = 0; index <= subdivisions; index++) {
+ const angle = (1 - index / subdivisions) * Math.PI / 4;
+ coordinates.push(-coreExtent - bevel * Math.tan(angle));
+ }
+ coordinates.push(coreExtent);
+ for (let index = 1; index <= subdivisions; index++) {
+ const angle = index / subdivisions * Math.PI / 4;
+ coordinates.push(coreExtent + bevel * Math.tan(angle));
+ }
+ }
+ const gridSegments = coordinates.length - 1;
for (let face = 0; face < 6; face++) {
const axis = Math.floor(face / 2);
const sign = face % 2 === 0 ? -1 : 1;
- const mesh = surface(subdivisions, subdivisions, (horizontal, vertical) => {
+ const mesh = surface(gridSegments, gridSegments, (horizontal, vertical) => {
const point = [0, 0, 0];
point[axis] = sign * 0.5;
- point[(axis + 1) % 3] = horizontal - 0.5;
- point[(axis + 2) % 3] = vertical - 0.5;
+ point[(axis + 1) % 3] = coordinates[Math.round(horizontal * gridSegments)];
+ point[(axis + 2) % 3] = coordinates[Math.round(vertical * gridSegments)];
+ if (bevel === 0) {
+ const normal = [0, 0, 0];
+ normal[axis] = sign;
+ return { position: point, normal };
+ }
const core = point.map(value => Math.max(-0.5 + bevel, Math.min(0.5 - bevel, value)));
const offset = point.map((value, coordinate) => value - core[coordinate]);
const length = Math.hypot(...offset);
diff --git a/src/model.js b/src/model.js
index fb47bc18..964b363f 100644
--- a/src/model.js
+++ b/src/model.js
@@ -6,10 +6,10 @@ const QUARTER_TURN = Math.PI / 2;
const materials = {
armor: { tag: 'armor', color: [0.53, 0.39, 0.24], metallic: 0.88, roughness: 0.32, emission: 0 },
- pale: { tag: 'armor', color: [0.67, 0.56, 0.39], metallic: 0.8, roughness: 0.29, emission: 0 },
- dark: { tag: 'graphite', color: [0.032, 0.045, 0.047], metallic: 0.76, roughness: 0.38, emission: 0 },
+ pale: { tag: 'satin', color: [0.61, 0.6, 0.52], metallic: 0.78, roughness: 0.25, emission: 0 },
+ dark: { tag: 'graphite', color: [0.032, 0.045, 0.047], metallic: 0.76, roughness: 0.29, emission: 0 },
rubber: { tag: 'rubber', color: [0.012, 0.018, 0.02], metallic: 0.08, roughness: 0.66, emission: 0 },
- steel: { tag: 'steel', color: [0.29, 0.34, 0.34], metallic: 0.96, roughness: 0.25, emission: 0 },
+ steel: { tag: 'steel', color: [0.43, 0.48, 0.47], metallic: 0.96, roughness: 0.22, emission: 0 },
copper: { tag: 'copper', color: [0.52, 0.19, 0.075], metallic: 0.9, roughness: 0.31, emission: 0 },
mint: { tag: 'light', color: [0.16, 0.8, 0.61], metallic: 0.15, roughness: 0.18, emission: 2.6 },
lens: { tag: 'lens', color: [0.018, 0.16, 0.13], metallic: 0.68, roughness: 0.12, emission: 0.15 },
@@ -25,14 +25,15 @@ function transform(position, scale, rotation) {
export function createModel() {
const parts = [];
const meshes = {
- plate: box(),
+ plate: box(0.08, 3),
+ shell: box(0.18, 8),
sphere: sphere(),
cylinder: cylinder(),
bolt: cylinder(6),
- ring: torus(0.065),
- heavyRing: torus(0.15),
+ ring: torus(0.045),
+ heavyRing: torus(0.105),
fineRing: torus(0.022),
- brow: torus(0.12, 44, 12, HALF_TURN * 0.84, HALF_TURN * 0.08),
+ brow: torus(0.09, 80, 24, HALF_TURN * 0.84, HALF_TURN * 0.08),
};
function add(name, mesh, position, scale, material = 'armor', rotation = [0, 0, 0]) {
parts.push({ name, geometry: meshes[mesh], matrix: transform(position, scale, rotation), material: materials[material] });
@@ -78,22 +79,30 @@ export function createModel() {
for (let index = 0; index < 7; index++) {
add('Neck compression collar', 'cylinder', [0, 0.66 + index * 0.074, -0.12], [0.29, 0.033, 0.29], index % 2 ? 'steel' : 'dark');
}
- plate('Lower jaw chassis', [0, 1.19, 0.035], [1.35, 0.26, 0.84], 'dark');
- plate('Lower jaw armor', [0, 1.22, 0.49], [1.18, 0.2, 0.16], 'armor');
+ add('Lower jaw chassis', 'shell', [0, 1.19, 0.035], [1.24, 0.24, 0.75], 'dark');
+ add('Lower jaw armor', 'shell', [0, 1.22, 0.49], [1.09, 0.16, 0.17], 'armor');
plate('Jaw inset panel', [0, 1.225, 0.58], [0.66, 0.065, 0.035], 'rubber');
for (let index = 0; index < 9; index++) {
plate('Jaw grille', [-0.28 + index * 0.07, 1.225, 0.607], [0.025, 0.055, 0.012], 'steel');
}
add('Inner pressure vessel', 'sphere', [0, 1.83, -0.03], [0.82, 0.73, 0.61], 'dark');
- plate('Rear controller enclosure', [0, 1.86, -0.51], [0.96, 1.02, 0.3], 'dark');
- plate('Rear service hatch', [0, 1.86, -0.685], [0.72, 0.8, 0.045], 'armor');
+ add('Rear controller enclosure', 'shell', [0, 1.86, -0.51], [0.96, 1.02, 0.3], 'dark');
+ add('Rear service hatch', 'shell', [0, 1.86, -0.685], [0.72, 0.8, 0.045], 'armor');
for (const side of [-1, 1]) {
- plate('Cheek armor', [side * 0.72, 1.73, 0.19], [0.3, 0.89, 0.72], 'armor', [0, side * 0.12, side * -0.12]);
- plate('Temple armor', [side * 0.67, 2.22, -0.025], [0.36, 0.34, 0.93], 'pale', [0, 0, side * 0.13]);
- plate('Cheek inset', [side * 0.86, 1.69, 0.23], [0.035, 0.47, 0.42], 'rubber');
+ add('Cheek armor', 'shell', [side * 0.71, 1.69, 0.27], [0.24, 0.79, 0.6], 'armor', [0, side * 0.18, side * -0.16]);
+ add('Temple sculpted cap', 'sphere', [side * 0.62, 2.25, -0.035], [0.26, 0.22, 0.49], 'pale');
+ add('Temporal bearing body', 'cylinder', [side * 0.79, 1.93, -0.1], [0.34, 0.19, 0.34], 'dark', [0, 0, QUARTER_TURN]);
+ add('Temporal bearing retaining ring', 'ring', [side * 0.905, 1.93, -0.1], [0.29, 0.29, 0.29], 'armor', [0, QUARTER_TURN, 0]);
+ add('Temporal silver locking ring', 'fineRing', [side * 0.916, 1.93, -0.1], [0.235, 0.235, 0.235], 'steel', [0, QUARTER_TURN, 0]);
+ add('Temporal bearing cover', 'cylinder', [side * 0.902, 1.93, -0.1], [0.205, 0.042, 0.205], 'dark', [0, 0, QUARTER_TURN]);
+ for (let index = 0; index < 6; index++) {
+ const angle = index / 6 * Math.PI * 2;
+ bolt('Temporal bearing fastener', [side * 0.925, 1.93 + Math.cos(angle) * 0.26, -0.1 + Math.sin(angle) * 0.26], 0.019, [0, 0, QUARTER_TURN]);
+ }
+ add('Cheek inset', 'shell', [side * 0.836, 1.61, 0.19], [0.035, 0.35, 0.36], 'rubber');
for (let index = 0; index < 8; index++) {
- plate('Side cooling lamella', [side * 0.89, 1.46 + index * 0.065, 0.14], [0.095, 0.023, 0.48], 'steel', [0, 0.1 * side, 0]);
+ plate('Side cooling lamella', [side * 0.865, 1.42 + index * 0.048, 0.17], [0.074, 0.018, 0.34], 'steel', [0, 0.1 * side, 0]);
}
for (const height of [1.39, 2.08]) {
bolt('Cheek face fastener', [side * 0.72, height, 0.578]);
@@ -111,6 +120,13 @@ export function createModel() {
// Nested lens rings carry the focal point; the pupil remains dark inside a luminous annulus.
barrel('Optic pressure housing', [0, 1.87, 0.5], 0.645, 0.34);
+ for (let index = 0; index < 4; index++) {
+ ring('Optic barrel machining groove', [0, 1.87, 0.48 + index * 0.053], 0.641, 'steel', 'fineRing', 0.45);
+ }
+ for (let index = 0; index < 48; index++) {
+ const angle = index / 48 * Math.PI * 2;
+ plate('Focus grip flute', [Math.cos(angle) * 0.642, 1.87 + Math.sin(angle) * 0.642, 0.576], [0.015, 0.026, 0.135], 'dark', [0, 0, angle]);
+ }
barrel('Optic machined bezel', [0, 1.87, 0.695], 0.61, 0.14, 'armor');
ring('Bezel raised rim', [0, 1.87, 0.77], 0.56, 'pale', 'heavyRing', 0.48);
barrel('Optic inner recess', [0, 1.87, 0.772], 0.51, 0.05, 'rubber');
@@ -131,12 +147,13 @@ export function createModel() {
}
for (let index = 0; index < 12; index++) {
const angle = index / 12 * Math.PI * 2;
- plate('Iris radial segment', [Math.cos(angle) * 0.26, 1.87 + Math.sin(angle) * 0.26, 0.906], [0.105, 0.016, 0.012], 'steel', [0, 0, angle + 0.55]);
+ plate('Iris radial segment', [Math.cos(angle) * 0.26, 1.87 + Math.sin(angle) * 0.26, 0.906], [0.128, 0.033, 0.008], 'dark', [0, 0, angle + 0.7]);
}
add('Interrupted armored brow', 'brow', [0, 1.88, 0.46], [0.77, 0.72, 0.75], 'armor');
- plate('Crown bridge', [0, 2.5, 0.01], [0.89, 0.16, 0.88], 'pale', [-0.075, 0, 0]);
+ add('Crown sculpted shell', 'sphere', [0, 2.38, -0.03], [0.64, 0.22, 0.53], 'pale');
+ add('Crown bridge', 'shell', [0, 2.525, -0.055], [0.65, 0.095, 0.63], 'armor', [-0.075, 0, 0]);
for (let index = -2; index <= 2; index++) {
- plate('Crown graphite seam', [index * 0.14, 2.59, -0.05], [0.022, 0.012, 0.57], 'dark');
+ plate('Crown graphite seam', [index * 0.11, 2.576, -0.05], [0.011, 0.01, 0.42], 'dark');
}
for (const side of [-1, 1]) {
plate('Brow hinge', [side * 0.56, 2.37, 0.48], [0.21, 0.1, 0.22], 'steel', [0, 0, side * -0.45]);
@@ -144,7 +161,7 @@ export function createModel() {
}
// Offset rangefinder and copper aerial break the symmetry without competing with the lens.
- plate('Rangefinder mounting wing', [0.96, 2.12, 0.13], [0.43, 0.38, 0.59], 'dark');
+ add('Rangefinder mounting wing', 'shell', [0.94, 2.12, 0.13], [0.32, 0.29, 0.47], 'dark');
barrel('Rangefinder brass body', [1.03, 2.13, 0.44], 0.175, 0.26, 'armor');
ring('Rangefinder ring', [1.03, 2.13, 0.585], 0.142, 'steel', 'heavyRing', 0.55);
barrel('Rangefinder glass', [1.03, 2.13, 0.592], 0.117, 0.026, 'lens');
diff --git a/tests/model.test.js b/tests/model.test.js
index 33adf372..ea6fc4c5 100644
--- a/tests/model.test.js
+++ b/tests/model.test.js
@@ -39,14 +39,64 @@ test('closed primitive surfaces have finite unit normals and outward winding', (
});
test('partial armor arc keeps its winding', () => {
- validateGeometry(torus(0.12, 44, 12, Math.PI * 0.84, Math.PI * 0.08));
+ const mesh = torus(0.12, 80, 24, Math.PI * 0.84, Math.PI * 0.08);
+ assert.ok(validateGeometry(mesh) > 0);
+ assertClosedJoins(mesh, false);
+});
+
+function assertClosedJoins(mesh, smoothNormals = true) {
+ const vertexKeys = [];
+ const normalsAtPosition = new Map();
+ for (let offset = 0; offset < mesh.positions.length; offset += 3) {
+ const key = Array.from(mesh.positions.slice(offset, offset + 3), value => Math.round(value * 100000)).join(',');
+ vertexKeys.push(key);
+ const normal = Array.from(mesh.normals.slice(offset, offset + 3));
+ const previousNormal = normalsAtPosition.get(key);
+ if (previousNormal && smoothNormals) {
+ assert.ok(normal.every((value, axis) => Math.abs(value - previousNormal[axis]) < 1e-5), 'smooth normals match at patch joins');
+ }
+ normalsAtPosition.set(key, normal);
+ }
+ const edgeCounts = new Map();
+ for (let offset = 0; offset < mesh.indices.length; offset += 3) {
+ const triangle = Array.from(mesh.indices.slice(offset, offset + 3), index => vertexKeys[index]);
+ assert.equal(new Set(triangle).size, 3, 'joins do not produce collapsed triangles');
+ for (let edge = 0; edge < 3; edge++) {
+ const edgeKey = [triangle[edge], triangle[(edge + 1) % 3]].sort().join('|');
+ edgeCounts.set(edgeKey, (edgeCounts.get(edgeKey) ?? 0) + 1);
+ }
+ }
+ assert.ok(Array.from(edgeCounts.values()).every(count => count === 2), 'each welded edge has two adjacent triangles');
+}
+
+test('rounded box fillets are sampled locally with closed smooth patch joins', () => {
+ for (const bevel of [0.015, 0.08, 0.18]) {
+ const mesh = box(bevel, 6);
+ validateGeometry(mesh);
+ assertClosedJoins(mesh);
+ assert.ok(Array.from(mesh.positions).every(value => Math.abs(value) <= 0.500001));
+ const intermediateNormals = new Set(Array.from(mesh.normals).filter(value => value > 0.01 && value < 0.99).map(value => value.toFixed(3)));
+ assert.ok(intermediateNormals.size > 15, 'fillets carry enough normal samples for continuous highlights');
+ }
+ validateGeometry(box(0));
+});
+
+test('cylinder rim fillets join flat caps and walls without gaps or normal discontinuities', () => {
+ const mesh = cylinder();
+ assertClosedJoins(mesh);
+ assert.ok(Array.from(mesh.positions).every((value, index) => Math.abs(value) <= (index % 3 === 1 ? 0.500001 : 1.000001)));
+ const rimNormals = Array.from(mesh.normals).filter((value, index) => index % 3 === 1 && value > 0.01 && value < 0.99);
+ assert.ok(new Set(rimNormals.map(value => value.toFixed(3))).size >= 5);
});
test('model respects the renderer contract, instancing and authored bounds', () => {
const parts = createModel();
- assert.ok(parts.length > 200);
+ assert.ok(parts.length >= 252);
const geometries = new Set(parts.map(part => part.geometry));
assert.ok(geometries.size < 12, 'repeated details reuse geometry identities');
+ for (const mesh of geometries) {
+ validateGeometry(mesh);
+ }
const minimum = [Infinity, Infinity, Infinity];
const maximum = [-Infinity, -Infinity, -Infinity];
for (const part of parts) {