Prompt specification
Build or customize the Three.js 3D scene "Block With Three Datum Faces" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Block With Three Datum Faces
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/block-with-three-datum-faces-x2057
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing reference datums for locating part features. Model a rectangular workpiece with three mutually perpendicular reference faces and an origin marker. Show measured offsets from those faces rather than arbitrary camera coordinates. A 3D cutaway view.
Working Three.js Starter (block-with-three-datum-faces-x2057):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>3D Datum Reference Study</title>
<style>
:root {
color-scheme: dark;
--bg: #07111d;
--panel: rgba(10, 24, 39, 0.86);
--panel-border: rgba(148, 183, 210, 0.19);
--text: #e8f3fb;
--muted: #91a9bb;
--accent: #5eead4;
--x: #ff6b7a;
--y: #54e3a1;
--z: #5aa7ff;
--a: #ffc857;
--b: #4fc3f7;
--c: #a78bfa;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 65% 18%, #142c42 0, transparent 38%),
linear-gradient(145deg, #07111d, #0a1724 55%, #050b13);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene:active {
cursor: grabbing;
}
#leaders,
#labels {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 2;
}
#labels {
z-index: 3;
}
.hide-labels #leaders,
.hide-labels #labels {
opacity: 0;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--panel-border);
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
border-radius: 14px;
}
#header {
top: 22px;
left: 22px;
width: 330px;
padding: 18px 19px 16px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 20px;
line-height: 1.15;
letter-spacing: -0.02em;
}
.subtitle {
margin-top: 7px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.badge-row {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 13px;
}
.badge {
padding: 5px 8px;
border: 1px solid rgba(94, 234, 212, 0.25);
border-radius: 999px;
background: rgba(94, 234, 212, 0.08);
color: #bdf8eb;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
}
#datum-panel {
left: 22px;
bottom: 22px;
width: 330px;
padding: 17px 18px 16px;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
color: #b8c9d7;
}
.section-title span:last-child {
color: var(--muted);
font-weight: 600;
letter-spacing: 0.03em;
text-transform: none;
}
.datum-row {
display: grid;
grid-template-columns: 26px 1fr auto;
align-items: center;
gap: 9px;
min-height: 31px;
border-bottom: 1px solid rgba(148, 183, 210, 0.09);
font-size: 12px;
}
.datum-row:last-of-type {
border-bottom: 0;
}
.datum-chip {
display: grid;
place-items: center;
width: 23px;
height: 23px;
border-radius: 6px;
font-size: 11px;
font-weight: 900;
color: #06101a;
}
.datum-a {
background: var(--a);
}
.datum-b {
background: var(--b);
}
.datum-c {
background: var(--c);
}
.datum-value {
color: #c8d8e5;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 11px;
}
.formula {
margin: 11px 0 13px;
padding: 10px 11px;
border: 1px solid rgba(148, 183, 210, 0.13);
border-radius: 9px;
background: rgba(2, 9, 16, 0.3);
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.formula code {
color: #d7e8f5;
font-family: "SFMono-Regular", Consolas, monospace;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 10px;
}
th {
padding: 0 0 7px;
color: #7f98ab;
font-size: 9px;
font-weight: 700;
text-align: right;
letter-spacing: 0.08em;
}
th:first-child,
td:first-child {
text-align: left;
}
td {
padding: 6px 0;
border-top: 1px solid rgba(148, 183, 210, 0.09);
text-align: right;
color: #c7d7e3;
font-family: "SFMono-Regular", Consolas, monospace;
}
td:first-child {
color: var(--accent);
font-weight: 800;
}
#toolbar {
top: 22px;
right: 22px;
display: flex;
gap: 8px;
padding: 8px;
}
button {
appearance: none;
border: 1px solid rgba(148, 183, 210, 0.19);
border-radius: 8px;
padding: 9px 12px;
background: rgba(17, 38, 57, 0.82);
color: #d8e6ef;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
button:hover {
background: rgba(29, 60, 83, 0.95);
border-color: rgba(94, 234, 212, 0.42);
}
button:active {
transform: translateY(1px);
}
#hint {
position: fixed;
z-index: 10;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
padding: 9px 13px;
border: 1px solid rgba(148, 183, 210, 0.15);
border-radius: 999px;
background: rgba(5, 15, 24, 0.72);
color: #91a9bb;
font-size: 10px;
letter-spacing: 0.04em;
white-space: nowrap;
backdrop-filter: blur(12px);
}
.measure-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
pointer-events: none;
user-select: none;
}
.datum-label {
display: flex;
align-items: center;
gap: 7px;
padding: 6px 8px;
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 7px;
background: rgba(5, 14, 23, 0.88);
box-shadow: 0 8px 22px rgba(0, 0, 0, 0.24);
color: #dceaf4;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.025em;
}
.datum-label b {
display: grid;
place-items: center;
width: 20px;
height: 20px;
border-radius: 5px;
color: #07111d;
font-size: 11px;
font-weight: 950;
}
.origin-label {
padding: 6px 9px;
border: 1px solid rgba(255, 200, 87, 0.58);
border-radius: 999px;
background: rgba(25, 19, 5, 0.9);
color: #ffe6a3;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.05em;
}
.feature-label {
width: 178px;
overflow: hidden;
border: 1px solid rgba(94, 234, 212, 0.32);
border-radius: 10px;
background: rgba(5, 16, 25, 0.92);
box-shadow: 0 12px 34px rgba(0, 0, 0, 0.3);
}
.feature-title {
display: flex;
align-items: center;
gap: 7px;
padding: 8px 9px;
border-bottom: 1px solid rgba(94, 234, 212, 0.17);
color: #e8f7f4;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.045em;
}
.feature-id {
display: grid;
place-items: center;
min-width: 25px;
height: 20px;
border-radius: 5px;
background: var(--accent);
color: #062019;
font-size: 10px;
font-weight: 950;
}
.coord-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1px;
background: rgba(148, 183, 210, 0.11);
}
.coord {
padding: 7px 4px 8px;
background: rgba(4, 14, 22, 0.98);
text-align: center;
}
.coord span {
display: block;
color: #7892a7;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
}
.coord b {
display: block;
margin-top: 3px;
color: #e3eff7;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 11px;
}
.dim-label {
padding: 4px 7px;
border: 1px solid currentColor;
border-radius: 5px;
background: rgba(4, 13, 21, 0.9);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
font-weight: 800;
}
@media (max-width: 800px) {
#header {
width: 280px;
top: 12px;
left: 12px;
}
#datum-panel {
width: 280px;
left: 12px;
bottom: 12px;
}
#toolbar {
top: 12px;
right: 12px;
}
#hint {
display: none;
}
.feature-label {
width: 155px;
}
}
@media (max-height: 680px) {
#datum-panel {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D datum reference model"></canvas>
<svg id="leaders" aria-hidden="true"></svg>
<div id="labels" aria-hidden="true"></div>
<section id="header" class="panel">
<div class="eyebrow">Engineering visualization / 3D datum study</div>
<h1>Feature Location from Datum Faces</h1>
<div class="subtitle">
Rectangular workpiece with a stepped corner cutaway. Dimensions are
calculated in the model datum frame, not from the viewing camera.
</div>
<div class="badge-row">
<div class="badge">UNITS: mm</div>
<div class="badge">3 ORTHOGONAL DATUMS</div>
<div class="badge">INTERACTIVE ORBIT</div>
</div>
</section>
<section id="datum-panel" class="panel">
<div class="section-title">
<span>Datum reference frame</span>
<span>Measured inward</span>
</div>
<div class="datum-row">
<div class="datum-chip datum-a">A</div>
<div>Top face</div>
<div class="datum-value">Y = 80</div>
</div>
<div class="datum-row">
<div class="datum-chip datum-b">B</div>
<div>Front face</div>
<div class="datum-value">Z = 60</div>
</div>
<div class="datum-row">
<div class="datum-chip datum-c">C</div>
<div>Right face</div>
<div class="datum-value">X = 120</div>
</div>
<div class="formula">
<code>X = 120 − x</code><br />
<code>Y = 80 − y</code><br />
<code>Z = 60 − z</code>
</div>
<div class="section-title">
<span>Located features</span>
<span>mm</span>
</div>
<table>
<thead>
<tr>
<th>FEATURE</th>
<th style="color: var(--x)">X / C</th>
<th style="color: var(--y)">Y / A</th>
<th style="color: var(--z)">Z / B</th>
</tr>
</thead>
<tbody id="feature-table"></tbody>
</table>
</section>
<nav id="toolbar" class="panel" aria-label="View controls">
<button id="reset-view" type="button">Reset view</button>
<button id="toggle-labels" type="button">Hide labels</button>
</nav>
<div id="hint">
DRAG TO ORBIT · SCROLL TO ZOOM · DIMENSIONS ARE MODEL-COORDINATE OFFSETS
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Model and datum definitions
// ------------------------------------------------------------
const W = 120;
const H = 80;
const D = 60;
const DATUM = {
A: { name: "TOP", value: H },
B: { name: "FRONT", value: D },
C: { name: "RIGHT", value: W }
};
const COLORS = {
x: 0xff6b7a,
y: 0x54e3a1,
z: 0x5aa7ff,
a: 0xffc857,
b: 0x4fc3f7,
c: 0xa78bfa,
feature: 0x5eead4
};
const canvas = document.getElementById("scene");
const labelsRoot = document.getElementById("labels");
const leaderRoot = document.getElementById("leaders");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setClearColor(0x07111d, 1);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.FogExp2(0x07111d, 0.0022);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
1200
);
// Direct camera controls: no addons.
const view = {
theta: 0.72,
phi: 1.02,
radius: 215,
target: new THREE.Vector3(55, 34, 27),
dragging: false,
pointerX: 0,
pointerY: 0
};
function updateCamera() {
const sinPhi = Math.sin(view.phi);
camera.position.set(
view.target.x + view.radius * sinPhi * Math.sin(view.theta),
view.target.y + view.radius * Math.cos(view.phi),
view.target.z + view.radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(view.target);
camera.updateMatrixWorld();
}
function resetView() {
view.theta = 0.72;
view.phi = 1.02;
view.radius = 215;
view.target.set(55, 34, 27);
updateCamera();
}
resetView();
// ------------------------------------------------------------
// Lighting and environment
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xbcdcff,
0x17202b,
1.65
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(90, 170, 120);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -180;
keyLight.shadow.camera.right = 180;
keyLight.shadow.camera.top = 180;
keyLight.shadow.camera.bottom = -180;
keyLight.shadow.camera.near = 10;
keyLight.shadow.camera.far = 420;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x78bfff, 1.25);
fillLight.position.set(-120, 70, -80);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xffb45e, 35, 260, 2);
rimLight.position.set(20, 95, 100);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(420, 420),
new THREE.ShadowMaterial({
color: 0x000000,
opacity: 0.36
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(
320,
32,
0x31536b,
0x183044
);
grid.position.y = -0.9;
grid.material.transparent = true;
grid.material.opacity = 0.58;
scene.add(grid);
// ------------------------------------------------------------
// Custom cutaway geometry
//
// The upper/front/left corner is removed. Geometry is generated as
// exterior faces only, avoiding coincident internal box faces.
// ------------------------------------------------------------
function createCutawayGeometry() {
const positions = [];
const normals = [];
const uvs = [];
const indices = [];
const groups = [];
function addQuad(center, u, v, materialIndex = 0) {
const base = positions.length / 3;
const normal = new THREE.Vector3()
.crossVectors(u, v)
.normalize();
const corners = [
center.clone()
.addScaledVector(u, -0.5)
.addScaledVector(v, -0.5),
center.clone()
.addScaledVector(u, 0.5)
.addScaledVector(v, -0.5),
center.clone()
.addScaledVector(u, 0.5)
.addScaledVector(v, 0.5),
center.clone()
.addScaledVector(u, -0.5)
.addScaledVector(v, 0.5)
];
const quadUV = [
[0, 0],
[1, 0],
[1, 1],
[0, 1]
];
corners.forEach((p, i) => {
positions.push(p.x, p.y, p.z);
normals.push(normal.x, normal.y, normal.z);
uvs.push(...quadUV[i]);
});
const groupStart = indices.length;
indices.push(
base, base + 1, base + 2,
base, base + 2, base + 3
);
groups.push({
start: groupStart,
count: 6,
materialIndex
});
}
const X = new THREE.Vector3(1, 0, 0);
const Y = new THREE.Vector3(0, 1, 0);
const Z = new THREE.Vector3(0, 0, 1);
// Exterior faces.
addQuad(
new THREE.Vector3(W / 2, 0, D / 2),
new THREE.Vector3(W, 0, 0),
new THREE.Vector3(0, 0, D)
);
addQuad(
new THREE.Vector3(W, H / 2, D / 2),
new THREE.Vector3(0, H, 0),
new THREE.Vector3(0, 0, D)
);
addQuad(
new THREE.Vector3(W / 2, H / 2, 0),
new THREE.Vector3(0, H, 0),
new THREE.Vector3(W, 0, 0)
);
addQuad(
new THREE.Vector3(0, 20, 30),
new THREE.Vector3(0, 0, 60),
new THREE.Vector3(0, 40, 0)
);
addQuad(
new THREE.Vector3(0, 60, 15),
new THREE.Vector3(0, 0, 30),
new THREE.Vector3(0, 40, 0)
);
addQuad(
new THREE.Vector3(60, 20, 60),
new THREE.Vector3(120, 0, 0),
new THREE.Vector3(0, 40, 0)
);
addQuad(
new THREE.Vector3(90, 60, 60),
new THREE.Vector3(60, 0, 0),
new THREE.Vector3(0, 40, 0)
);
addQuad(
new THREE.Vector3(90, 80, 30),
new THREE.Vector3(0, 0, 60),
new THREE.Vector3(60, 0, 0)
);
addQuad(
new THREE.Vector3(30, 80, 15),
new THREE.Vector3(0, 0, 30),
new THREE.Vector3(60, 0, 0)
);
// Exposed cutaway faces, using a distinct section material.
addQuad(
new THREE.Vector3(60, 60, 45),
new THREE.Vector3(0, 0, 30),
new THREE.Vector3(0, 40, 0),
1
);
addQuad(
new THREE.Vector3(30, 40, 45),
new THREE.Vector3(0, 0, 30),
new THREE.Vector3(60, 0, 0),
1
);
addQuad(
new THREE.Vector3(30, 60, 30),
new THREE.Vector3(60, 0, 0),
new THREE.Vector3(0, 40, 0),
1
);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"normal",
new THREE.Float32BufferAttribute(normals, 3)
);
geometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(uvs, 2)
);
geometry.setIndex(indices);
groups.forEach((g) => geometry.addGroup(g.start, g.count, g.materialIndex));
geometry.computeBoundingBox();
geometry.computeBoundingSphere();
return geometry;
}
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0x58758c,
metalness: 0.34,
roughness: 0.29
});
const sectionMaterial = new THREE.MeshStandardMaterial({
color: 0xe8a13b,
metalness: 0.18,
roughness: 0.4,
side: THREE.DoubleSide
});
const body = new THREE.Mesh(
createCutawayGeometry(),
[bodyMaterial, sectionMaterial]
);
body.castShadow = true;
body.receiveShadow = true;
scene.add(body);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(body.geometry, 18),
new THREE.LineBasicMaterial({
color: 0xc7dce9,
transparent: true,
opacity: 0.32
})
);
edges.renderOrder = 2;
scene.add(edges);
// Section hatching on the exposed cut faces.
function addHatching(origin, u, v, uLength, vLength, spacing = 7) {
const points = [];
for (let c = -vLength; c <= uLength; c += spacing) {
const s0 = Math.max(0, -c);
const s1 = Math.min(uLength, vLength - c);
if (s1 <= s0) continue;
const p0 = origin.clone()
.addScaledVector(u, s0)
.addScaledVector(v, s0 + c);
const p1 = origin.clone()
.addScaledVector(u, s1)
.addScaledVector(v, s1 + c);
points.push(p0, p1);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color: 0x8c451d,
transparent: true,
opacity: 0.38,
depthTest: false
});
const lines = new THREE.LineSegments(geometry, material);
lines.renderOrder = 4;
scene.add(lines);
}
addHatching(
new THREE.Vector3(59.97, 40, 30),
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(0, 1, 0),
30,
40
);
addHatching(
new THREE.Vector3(0, 40.03, 30),
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(0, 0, 1),
60,
30
);
addHatching(
new THREE.Vector3(0, 40, 30.03),
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(0, 1, 0),
60,
40
);
// ------------------------------------------------------------
// Highlighted datum faces
// ------------------------------------------------------------
function makeDatumMaterial(color) {
return new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.16,
side: THREE.DoubleSide,
depthWrite: false,
polygonOffset: true,
polygonOffsetFactor: -1,
polygonOffsetUnits: -1
});
}
const datumMaterials = {
A: makeDatumMaterial(COLORS.a),
B: makeDatumMaterial(COLORS.b),
C: makeDatumMaterial(COLORS.c)
};
function addDatumPlane(datum, width, height, position, rotation) {
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
datumMaterials[datum]
);
mesh.position.copy(position);
mesh.rotation.set(...rotation);
mesh.renderOrder = 1;
scene.add(mesh);
}
// Datum A: top.
addDatumPlane(
"A", 60, 60,
new THREE.Vector3(90, 80.12, 30),
[-Math.PI / 2, 0, 0]
);
addDatumPlane(
"A", 60, 30,
new THREE.Vector3(30, 80.12, 15),
[-Math.PI / 2, 0, 0]
);
// Datum B: front.
addDatumPlane(
"B", 120, 40,
new THREE.Vector3(60, 20, 60.12),
[0, 0, 0]
);
addDatumPlane(
"B", 60, 40,
new THREE.Vector3(90, 60, 60.12),
[0, 0, 0]
);
// Datum C: right.
addDatumPlane(
"C", 60, 80,
new THREE.Vector3(120.12, 40, 30),
[0, Math.PI / 2, 0]
);
// ------------------------------------------------------------
// Origin and inward datum-frame axes
// ------------------------------------------------------------
const origin = new THREE.Vector3(W, H, D);
const originMarker = new THREE.Mesh(
new THREE.SphereGeometry(2.35, 24, 16),
new THREE.MeshStandardMaterial({
color: 0xffd86b,
emissive: 0xff9d25,
emissiveIntensity: 1.7,
metalness: 0.25,
roughness: 0.25
})
);
originMarker.position.copy(origin);
scene.add(originMarker);
const originRing = new THREE.Mesh(
new THREE.TorusGeometry(4.2, 0.55, 10, 48),
new THREE.MeshBasicMaterial({
color: 0xffd86b,
transparent: true,
opacity: 0.85,
depthTest: false
})
);
originRing.position.copy(origin);
originRing.renderOrder = 6;
scene.add(originRing);
const axisDefinitions = [
{
dir: new THREE.Vector3(-1, 0, 0),
color: COLORS.x,
length: 34
},
{
dir: new THREE.Vector3(0, -1, 0),
color: COLORS.y,
length: 29
},
{
dir: new THREE.Vector3(0, 0, -1),
color: COLORS.z,
length: 27
}
];
axisDefinitions.forEach((axis) => {
const helper = new THREE.ArrowHelper(
axis.dir,
origin,
axis.length,
axis.color,
4.5,
2.7
);
helper.line.material.depthTest = false;
helper.cone.material.depthTest = false;
helper.line.renderOrder = 7;
helper.cone.renderOrder = 7;
scene.add(helper);
});
// ------------------------------------------------------------
// Located features and measured offsets
// ------------------------------------------------------------
const features = [
{
id: "F1",
name: "TOP BORE",
point: new THREE.Vector3(84, 80, 15),
normal: new THREE.Vector3(0, 1, 0),
radius: 6.2,
labelOffset: [65, -68]
},
{
id: "F2",
name: "FRONT BORE",
point: new THREE.Vector3(95, 58, 60),
normal: new THREE.Vector3(0, 0, 1),
radius: 6.2,
labelOffset: [-82, -42]
},
{
id: "F3",
name: "SIDE BORE",
point: new THREE.Vector3(120, 24, 12),
normal: new THREE.Vector3(1, 0, 0),
radius: 6.2,
labelOffset: [78, 28]
}
];
function getOffsets(point) {
return {
X: DATUM.C.value - point.x,
Y: DATUM.A.value - point.y,
Z: DATUM.B.value - point.z
};
}
const zAxis = new THREE.Vector3(0, 0, 1);
function addFeatureVisual(feature) {
const offsets = getOffsets(feature.point);
feature.offsets = offsets;
const disk = new THREE.Mesh(
new THREE.CircleGeometry(feature.radius * 0.79, 48),
new THREE.MeshBasicMaterial({
color: 0x061019,
side: THREE.DoubleSide
})
);
disk.position.copy(feature.point).addScaledVector(feature.normal, 0.16);
disk.quaternion.setFromUnitVectors(zAxis, feature.normal);
disk.renderOrder = 3;
scene.add(disk);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(feature.radius, 1.05, 12, 48),
new THREE.MeshStandardMaterial({
color: COLORS.feature,
emissive: COLORS.feature,
emissiveIntensity: 0.7,
metalness: 0.35,
roughness: 0.25
})
);
ring.position.copy(feature.point).addScaledVector(feature.normal, 0.28);
ring.quaternion.setFromUnitVectors(zAxis, feature.normal);
ring.castShadow = true;
scene.add(ring);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(feature.radius + 3, 0.32, 8, 48),
new THREE.MeshBasicMaterial({
color: COLORS.feature,
transparent: true,
opacity: 0.48,
depthTest: false
})
);
halo.position.copy(feature.point).addScaledVector(feature.normal, 0.34);
halo.quaternion.setFromUnitVectors(zAxis, feature.normal);
halo.renderOrder = 6;
scene.add(halo);
// Small tangent-plane crosshair marks the exact feature center.
const tangentA = Math.abs(feature.normal.y) < 0.9
? new THREE.Vector3(0, 1, 0)
: new THREE.Vector3(1, 0, 0);
tangentA.cross(feature.normal).normalize();
const tangentB = new THREE.Vector3()
.crossVectors(feature.normal, tangentA)
.normalize();
const center = feature.point.clone()
.addScaledVector(feature.normal, 0.38);
const crossPoints = [
center.clone().addScaledVector(tangentA, -2.8),
center.clone().addScaledVector(tangentA, 2.8),
center.clone().addScaledVector(tangentB, -2.8),
center.clone().addScaledVector(tangentB, 2.8)
];
const cross = new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(crossPoints),
new THREE.LineBasicMaterial({
color: COLORS.feature,
depthTest: false
})
);
cross.renderOrder = 7;
scene.add(cross);
}
features.forEach(addFeatureVisual);
// ------------------------------------------------------------
// HTML overlay labels and SVG leader lines
// ------------------------------------------------------------
const overlayItems = [];
function makeSvgElement(tag) {
return document.createElementNS("http://www.w3.org/2000/svg", tag);
}
function registerLabel({
element,
position,
offset = [0, 0],
leader = true,
leaderColor = "#8aa4b8"
}) {
labelsRoot.appendChild(element);
const item = {
element,
position: position.clone(),
offset,
leader: null,
leaderDot: null
};
if (leader) {
const line = makeSvgElement("line");
line.setAttribute("stroke", leaderColor);
line.setAttribute("stroke-width", "1");
line.setAttribute("stroke-opacity", "0.72");
line.setAttribute("vector-effect", "non-scaling-stroke");
const dot = makeSvgElement("circle");
dot.setAttribute("r", "2.7");
dot.setAttribute("fill", leaderColor);
dot.setAttribute("stroke", "#07111d");
dot.setAttribute("stroke-width", "1");
leaderRoot.appendChild(line);
leaderRoot.appendChild(dot);
item.leader = line;
item.leaderDot = dot;
}
overlayItems.push(item);
return item;
}
function createDatumLabel(letter, position, offset) {
const el = document.createElement("div");
el.className = "measure-label datum-label";
el.innerHTML = `
<b class="datum-${letter.toLowerCase()}">${letter}</b>
<span>${DATUM[letter].name} · ${letter === "A" ? "Y" : letter === "B" ? "Z" : "X"}=${DATUM[letter].value}</span>
`;
registerLabel({
element: el,
position,
offset,
leaderColor: `#${new THREE.Color(COLORS[letter.toLowerCase()]).getHexString()}`
});
}
createDatumLabel(
"A",
new THREE.Vector3(92, 80.5, 32),
[24, -38]
);
createDatumLabel(
"B",
new THREE.Vector3(63, 20, 60.5),
[-38, 9]
);
createDatumLabel(
"C",
new THREE.Vector3(120.5, 50, 22),
[50, 8]
);
const originLabel = document.createElement("div");
originLabel.className = "measure-label origin-label";
originLabel.textContent = "O · DATUM ORIGIN";
registerLabel({
element: originLabel,
position: origin,
offset: [38, -18],
leaderColor: "#ffd86b"
});
const tableBody = document.getElementById("feature-table");
features.forEach((feature) => {
const el = document.createElement("div");
el.className = "measure-label feature-label";
el.innerHTML = `
<div class="feature-title">
<span class="feature-id">${feature.id}</span>
${feature.name}
</div>
<div class="coord-grid">
<div class="coord">
<span>X / C</span>
<b>${feature.offsets.X.toFixed(1)}</b>
</div>
<div class="coord">
<span>Y / A</span>
<b>${feature.offsets.Y.toFixed(1)}</b>
</div>
<div class="coord">
<span>Z / B</span>
<b>${feature.offsets.Z.toFixed(1)}</b>
</div>
</div>
`;
registerLabel({
element: el,
position: feature.point,
offset: feature.labelOffset,
leaderColor: "#5eead4"
});
const row = document.createElement("tr");
row.innerHTML = `
<td>${feature.id}</td>
<td>${feature.offsets.X.toFixed(1)}</td>
<td>${feature.offsets.Y.toFixed(1)}</td>
<td>${feature.offsets.Z.toFixed(1)}</td>
`;
tableBody.appendChild(row);
});
// ------------------------------------------------------------
// Engineering dimension lines
// ------------------------------------------------------------
const up = new THREE.Vector3(0, 1, 0);
function createOverlayLine(points, color, opacity = 0.92) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthTest: false
});
const line = new THREE.Line(geometry, material);
line.renderOrder = 8;
scene.add(line);
return line;
}
function createDimension({
start,
end,
color,
text,
labelOffset = [0, 0],
extensions = []
}) {
createOverlayLine([start, end], color, 0.98);
extensions.forEach(([a, b]) => {
createOverlayLine([a, b], color, 0.48);
});
const arrowGeometry = new THREE.ConeGeometry(1.8, 4.8, 12);
const arrowMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.98,
depthTest: false
});
[
{ tip: start, direction: start.clone().sub(end).normalize() },
{ tip: end, direction: end.clone().sub(start).normalize() }
].forEach(({ tip, direction }) => {
const arrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
arrow.quaternion.setFromUnitVectors(up, direction);
arrow.position.copy(tip).addScaledVector(direction, -2.4);
arrow.renderOrder = 9;
scene.add(arrow);
});
const label = document.createElement("div");
label.className = "measure-label dim-label";
label.textContent = text;
label.style.color = `#${new THREE.Color(color).getHexString()}`;
registerLabel({
element: label,
position: start.clone().add(end).multiplyScalar(0.5),
offset: labelOffset,
leader: false
});
}
const f1 = features[0];
const f2 = features[1];
const f3 = features[2];
// F1 top bore: X from right datum and Z from front datum.
createDimension({
start: new THREE.Vector3(W, 88, f1.point.z),
end: new THREE.Vector3(f1.point.x, 88, f1.point.z),
color: COLORS.x,
text: `X ${f1.offsets.X.toFixed(1)} mm`,
labelOffset: [0, -13],
extensions: [
[
new THREE.Vector3(W, H, f1.point.z),
new THREE.Vector3(W, 88, f1.point.z)
],
[
f1.point.clone(),
new THREE.Vector3(f1.point.x, 88, f1.point.z)
]
]
});
createDimension({
start: new THREE.Vector3(f1.point.x, 88, D),
end: new THREE.Vector3(f1.point.x, 88, f1.point.z),
color: COLORS.z,
text: `Z ${f1.offsets.Z.toFixed(1)} mm`,
labelOffset: [16, 0],
extensions: [
[
new THREE.Vector3(f1.point.x, H, D),
new THREE.Vector3(f1.point.x, 88, D)
],
[
f1.point.clone(),
new THREE.Vector3(f1.point.x, 88, f1.point.z)
]
]
});
// F2 front bore: X from right datum and Y from top datum.
createDimension({
start: new THREE.Vector3(W, f2.point.y, 67),
end: new THREE.Vector3(f2.point.x, f2.point.y, 67),
color: COLORS.x,
text: `X ${f2.offsets.X.toFixed(1)} mm`,
labelOffset: [0, 14],
extensions: [
[
new THREE.Vector3(W, f2.point.y, D),
new THREE.Vector3(W, f2.point.y, 67)
],
[
f2.point.clone(),
new THREE.Vector3(f2.point.x, f2.point.y, 67)
]
]
});
createDimension({
start: new THREE.Vector3(f2.point.x, H, 67),
end: new THREE.Vector3(f2.point.x, f2.point.y, 67),
color: COLORS.y,
text: `Y ${f2.offsets.Y.toFixed(1)} mm`,
labelOffset: [-13, 0],
extensions: [
[
new THREE.Vector3(f2.point.x, H, D),
new THREE.Vector3(f2.point.x, H, 67)
],
[
f2.point.clone(),
new THREE.Vector3(f2.point.x, f2.point.y, 67)
]
]
});
// F3 side bore: Y from top datum and Z from front datum.
createDimension({
start: new THREE.Vector3(127, H, f3.point.z),
end: new THREE.Vector3(127, f3.point.y, f3.point.z),
color: COLORS.y,
text: `Y ${f3.offsets.Y.toFixed(1)} mm`,
labelOffset: [16, 0],
extensions: [
[
new THREE.Vector3(W, H, f3.point.z),
new THREE.Vector3(127, H, f3.point.z)
],
[
f3.point.clone(),
new THREE.Vector3(127, f3.point.y, f3.point.z)
]
]
});
createDimension({
start: new THREE.Vector3(127, f3.point.y, D),
end: new THREE.Vector3(127, f3.point.y, f3.point.z),
color: COLORS.z,
text: `Z ${f3.offsets.Z.toFixed(1)} mm`,
labelOffset: [0, -14],
extensions: [
[
new THREE.Vector3(W, f3.point.y, D),
new THREE.Vector3(127, f3.point.y, D)
],
[
f3.point.clone(),
new THREE.Vector3(127, f3.point.y, f3.point.z)
]
]
});
// ------------------------------------------------------------
// Projection of HTML annotations into the 3D view
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function updateOverlayPositions() {
const width = window.innerWidth;
const height = window.innerHeight;
overlayItems.forEach((item) => {
projected.copy(item.position).project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
item.element.style.left = `${x + item.offset[0]}px`;
item.element.style.top = `${y + item.offset[1]}px`;
const inFront = projected.z > -1 && projected.z < 1;
item.element.style.visibility = inFront ? "visible" : "hidden";
if (item.leader) {
item.leader.setAttribute("x1", x);
item.leader.setAttribute("y1", y);
item.leader.setAttribute("x2", x + item.offset[0]);
item.leader.setAttribute("y2", y + item.offset[1]);
item.leader.style.visibility = inFront ? "visible" : "hidden";
item.leaderDot.setAttribute("cx", x);
item.leaderDot.setAttribute("cy", y);
item.leaderDot.style.visibility = inFront ? "visible" : "hidden";
}
});
}
// ------------------------------------------------------------
// Direct orbit, pan-free camera controls
// ------------------------------------------------------------
canvas.addEventListener("pointerdown", (event) => {
view.dragging = true;
view.pointerX = event.clientX;
view.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!view.dragging) return;
const dx = event.clientX - view.pointerX;
const dy = event.clientY - view.pointerY;
view.pointerX = event.clientX;
view.pointerY = event.clientY;
view.theta -= dx * 0.006;
view.phi = THREE.MathUtils.clamp(
view.phi - dy * 0.005,
0.24,
2.55
);
updateCamera();
});
function endDrag(event) {
view.dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius * Math.exp(event.deltaY * 0.001),
120,
390
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
document.getElementById("reset-view").addEventListener("click", resetView);
const labelButton = document.getElementById("toggle-labels");
labelButton.addEventListener("click", () => {
const hidden = document.body.classList.toggle("hide-labels");
labelButton.textContent = hidden ? "Show labels" : "Hide labels";
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetView();
}
});
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
leaderRoot.setAttribute("viewBox", `0 0 ${width} ${height}`);
}
window.addEventListener("resize", resize);
resize();
function animate() {
requestAnimationFrame(animate);
updateCamera();
updateOverlayPositions();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```