Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.
JavaScript 3d3d-assets3d-modelingagent-skillsagent-toolsai-agentsasset-pipelinecligame-assetsglbgltfmcpmcp-servermodel-context-protocolprocedural-generationprocedural-modelingtext-to-3dthreejstypescript
const meta = { name: 'OHT rail straight' };
// ---- asset constants (metres; +X forward, +Y up, +Z right; datum = track underside at Y = 0) ----
const HALF_LEN = 1.8; // endA / endB at X = -/+1.8
const HANGER_X = [-0.9, 0.9]; // rod positions along X
const HANGER_TOP = 1.10; // upper face of the ceiling plates (world 6.0 with the rail at 4.9)
const ROD_R = 0.008;
const ROD_SEG = 12;
const CLAMP = { hx: 0.04, hz: 0.05, top: 0.23 }; // clamp block on the rail top, sunk 1 mm into it
const CEIL_PLATE = { hx: 0.08, hz: 0.08, t: 0.01 }; // top plate that meets the FFU face
const POWER = { u0: 0.149, u1: 0.154, v0: 0.09, v1: 0.15 }; // dark power bar on the +Z side, 4 mm proud of the wall
const LOD_INSET = 0.002;
const PALETTE = {
alu: ['aluminium-extrusion', 0xA8AEB4, 0.45, 1.0],
graphite: ['trim-graphite', 0x3B4148, 0.5, 0.1],
accent: ['accent-amhs', 0xE07B22, 0.5, 0.0],
};
// ===== SHARED RAIL SECTION (text identical in oht-rail-straight, oht-rail-curve and oht-rail-switch) =====
// Section seen from behind, looking along travel: u = lateral (positive to the right of travel), v = height above the underside.
const RAIL_W = 0.30; // overall width
const RAIL_H = 0.20; // overall height, underside at v = 0
const RAIL_SLOT = 0.03; // half-width of the 60 mm underside slot
const RAIL_CAV = 0.13; // half-width of the vehicle cavity (20 mm side walls)
const RAIL_LIP = 0.02; // lip thickness = cavity floor height
const RAIL_ROOF = 0.18; // cavity ceiling height (20 mm top wall)
const RAIL_CHAMFER = 0.03; // 45 degree lower-edge facet, accent orange
const RAIL_BAND = 0.07; // top of the vertical accent band on both side walls
const PLATE_T = 0.01; // join plate thickness along the rail
const PLATE_O = 0.002; // join plate overhang beyond the section (sides and top)
const RAIL_HW = RAIL_W / 2;
// Clockwise seen from behind. Edge k runs from point k to point k + 1. RAIL_EDGE_MAT: 0 = aluminium-extrusion, 1 = accent-amhs.
const RAIL_PROFILE = [
[-(RAIL_HW - RAIL_CHAMFER), 0], [-RAIL_HW, RAIL_CHAMFER], [-RAIL_HW, RAIL_BAND], [-RAIL_HW, RAIL_H],
[RAIL_HW, RAIL_H], [RAIL_HW, RAIL_BAND], [RAIL_HW, RAIL_CHAMFER], [RAIL_HW - RAIL_CHAMFER, 0],
[RAIL_SLOT, 0], [RAIL_SLOT, RAIL_LIP], [RAIL_CAV, RAIL_LIP], [RAIL_CAV, RAIL_ROOF],
[-RAIL_CAV, RAIL_ROOF], [-RAIL_CAV, RAIL_LIP], [-RAIL_SLOT, RAIL_LIP], [-RAIL_SLOT, 0],
];
const RAIL_EDGE_MAT = [1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
// Join plate: outer frame 2 mm larger than the section on the sides and top, flush with the underside, cavity and slot left open.
const PLATE_HW = RAIL_HW + PLATE_O;
const PLATE_TOP = RAIL_H + PLATE_O;
const PLATE_OUTLINE = [
[-PLATE_HW, 0], [-PLATE_HW, PLATE_TOP], [PLATE_HW, PLATE_TOP], [PLATE_HW, 0],
[RAIL_SLOT, 0], [RAIL_SLOT, RAIL_LIP], [RAIL_CAV, RAIL_LIP], [RAIL_CAV, RAIL_ROOF],
[-RAIL_CAV, RAIL_ROOF], [-RAIL_CAV, RAIL_LIP], [-RAIL_SLOT, RAIL_LIP], [-RAIL_SLOT, 0],
];
const PLATE_TRIS = [[1, 2, 8], [2, 7, 8], [0, 1, 9], [1, 8, 9], [2, 3, 6], [2, 6, 7], [0, 9, 10], [0, 10, 11], [3, 4, 5], [3, 5, 6]];
const RECT_TRIS = [[0, 1, 2], [0, 2, 3]];
function rectOutline(u0, u1, v0, v1) { return [[u0, v0], [u0, v1], [u1, v1], [u1, v0]]; }
function railMaterial(name, hex, roughness, metalness) {
const m = gameMaterial(hex, { roughness: roughness, metalness: metalness, flatShading: false });
m.name = name;
return m;
}
// A station is a section plane: c = centreline point at underside height, e = unit lateral (+u), t = unit heading.
function stationAt(x, z, headingDeg) {
const h = (headingDeg * Math.PI) / 180, s = Math.sin(h), c = Math.cos(h);
return { c: [x, 0, z], e: [-s, 0, c], t: [c, 0, s] };
}
// Station on a right-hand circle about (cx, cz); headingDeg 0 = heading +X at the point directly -Z of the centre.
function arcStation(cx, cz, radius, headingDeg) {
const h = (headingDeg * Math.PI) / 180, s = Math.sin(h), c = Math.cos(h);
return { c: [cx + radius * s, 0, cz - radius * c], e: [-s, 0, c], t: [c, 0, s] };
}
function newGeo(grouped) { return { p: [], n: [], i: [], g: [], grouped: grouped }; }
function pushV(G, x, y, z, nx, ny, nz) { G.p.push(x, y, z); G.n.push(nx, ny, nz); return G.p.length / 3 - 1; }
function stationPoint(st, q) { return [st.c[0] + q[0] * st.e[0], st.c[1] + q[1], st.c[2] + q[0] * st.e[2]]; }
function finishGeo(G) {
const geo = meshGeo({ positions: G.p, normals: G.n, indices: G.i });
const merged = [];
for (const g of G.g) {
const last = merged[merged.length - 1];
if (last && last[2] === g[2] && last[0] + last[1] === g[0]) last[1] += g[1]; else merged.push([g[0], g[1], g[2]]);
}
for (const g of merged) geo.addGroup(g[0], g[1], g[2]);
return geo;
}
// Side walls of an outline swept through consecutive stations. Crisp corners (per-edge vertices), normals follow the station frame.
function sweepWalls(G, stations, outline, edgeMat) {
const n = outline.length, base = [];
for (const st of stations) {
base.push(G.p.length / 3);
for (let k = 0; k < n; k++) {
const a = outline[k], b = outline[(k + 1) % n];
const du = b[0] - a[0], dv = b[1] - a[1], len = Math.hypot(du, dv);
const nu = -dv / len, nv = du / len;
for (const q of [a, b]) {
const w = stationPoint(st, q);
pushV(G, w[0], w[1], w[2], nu * st.e[0], nv, nu * st.e[2]);
}
}
}
const maxMat = Math.max(...edgeMat);
for (let m = 0; m <= maxMat; m++) {
const start = G.i.length;
for (let s = 0; s + 1 < stations.length; s++) {
for (let k = 0; k < n; k++) {
if (edgeMat[k] !== m) continue;
const a0 = base[s] + 2 * k, b0 = a0 + 1, a1 = base[s + 1] + 2 * k, b1 = a1 + 1;
G.i.push(a0, b0, b1, a0, b1, a1);
}
}
if (G.grouped && G.i.length > start) G.g.push([start, G.i.length - start, m]);
}
}
// Flat cap on a station plane. sign +1 faces along the heading, -1 faces against it.
function capOutline(G, st, outline, tris, sign, mat) {
const b = G.p.length / 3, start = G.i.length;
for (const q of outline) {
const w = stationPoint(st, q);
pushV(G, w[0], w[1], w[2], sign * st.t[0], 0, sign * st.t[2]);
}
for (const t of tris) {
if (sign > 0) G.i.push(b + t[0], b + t[1], b + t[2]); else G.i.push(b + t[0], b + t[2], b + t[1]);
}
if (G.grouped) G.g.push([start, G.i.length - start, mat]);
}
// Closed prism between two stations (caps first so equal-material groups merge).
function addPrism(G, sA, sB, outline, tris, edgeMat) {
capOutline(G, sB, outline, tris, 1, 0);
capOutline(G, sA, outline, tris, -1, 0);
sweepWalls(G, [sA, sB], outline, edgeMat || outline.map(() => 0));
}
function addPlate(G, sA, sB) { addPrism(G, sA, sB, PLATE_OUTLINE, PLATE_TRIS); }
// Open vertical cylinder (both ends are buried in neighbouring solids).
function addRod(G, x, z, r, y0, y1, seg) {
const b = G.p.length / 3;
for (let j = 0; j < seg; j++) {
const a = (2 * Math.PI * j) / seg, cx = Math.cos(a), cz = Math.sin(a);
pushV(G, x + r * cx, y0, z + r * cz, cx, 0, cz);
pushV(G, x + r * cx, y1, z + r * cz, cx, 0, cz);
}
for (let j = 0; j < seg; j++) {
const j2 = (j + 1) % seg;
G.i.push(b + 2 * j2, b + 2 * j, b + 2 * j + 1, b + 2 * j2, b + 2 * j + 1, b + 2 * j2 + 1);
}
}
function railPart(parent, name, geo, material) {
const mesh = new THREE.Mesh(geo, material);
mesh.name = name;
parent.add(mesh);
return mesh;
}
function railLocator(parent, name, x, y, z) {
const o = new THREE.Object3D();
o.name = name;
o.position.set(x, y, z);
parent.add(o);
return o;
}
// ===== END SHARED RAIL SECTION =====
function build() {
const root = createRoot('oht-rail-straight');
const alu = railMaterial(...PALETTE.alu);
const graphite = railMaterial(...PALETTE.graphite);
const accent = railMaterial(...PALETTE.accent);
const xa = -HALF_LEN, xb = HALF_LEN;
const bodyA = stationAt(xa + PLATE_T, 0, 0), bodyB = stationAt(xb - PLATE_T, 0, 0);
// trackBody: the shared section swept once between the two join plates (aluminium walls, accent chamfers and bands)
const body = newGeo(true);
sweepWalls(body, [bodyA, bodyB], RAIL_PROFILE, RAIL_EDGE_MAT);
railPart(root, 'trackBody', finishGeo(body), [alu, accent]);
// powerLine: slim dark bar on the +Z side, between the plates
const power = newGeo(false);
addPrism(power, bodyA, bodyB, rectOutline(POWER.u0, POWER.u1, POWER.v0, POWER.v1), RECT_TRIS);
railPart(root, 'powerLine', finishGeo(power), graphite);
// hangers: two rods with a clamp block on the rail and a ceiling plate at Y = 1.10
const hang = newGeo(false);
for (const x of HANGER_X) {
addPrism(hang, stationAt(x - CLAMP.hx, 0, 0), stationAt(x + CLAMP.hx, 0, 0), rectOutline(-CLAMP.hz, CLAMP.hz, RAIL_H - 0.001, CLAMP.top), RECT_TRIS);
addRod(hang, x, 0, ROD_R, CLAMP.top - 0.01, HANGER_TOP - CEIL_PLATE.t / 2, ROD_SEG);
addPrism(hang, stationAt(x - CEIL_PLATE.hx, 0, 0), stationAt(x + CEIL_PLATE.hx, 0, 0), rectOutline(-CEIL_PLATE.hz, CEIL_PLATE.hz, HANGER_TOP - CEIL_PLATE.t, HANGER_TOP), RECT_TRIS);
}
railPart(root, 'hangers', finishGeo(hang), alu);
// joinPlates: thin flanges at both ends, outer faces exactly at X = -/+1.8
const plates = newGeo(false);
addPlate(plates, stationAt(xa, 0, 0), bodyA);
addPlate(plates, bodyB, stationAt(xb, 0, 0));
railPart(root, 'joinPlates', finishGeo(plates), graphite);
// lod1: one 12-triangle box for the track body (accent sides), 2 mm inside the detailed skin so the two never share a plane
const li = LOD_INSET;
const lod = newGeo(true);
addPrism(lod, stationAt(xa + li, 0, 0), stationAt(xb - li, 0, 0), rectOutline(-RAIL_HW + li, RAIL_HW - li, li, RAIL_H - li), RECT_TRIS, [1, 0, 1, 0]);
railPart(root, 'lod1', finishGeo(lod), [alu, accent]);
railLocator(root, 'endA', xa, 0, 0);
railLocator(root, 'endB', xb, 0, 0);
return root;
}