Prompt specification
Build or customize the Three.js 3D scene "Knee joint surface study" 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: Knee joint surface study
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/knee-joint-surface-study-x1005
Scene Description Prompt:
Create a schematic knee cutaway with femur, tibia, kneecap, and cartilage contact regions, avoiding a perfect-hinge depiction. Three.js from cdnjs, one HTML file.
Working Three.js Starter (knee-joint-surface-study-x1005):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Schematic Knee Cutaway</title>
<style>
:root {
--ink: #17232d;
--muted: #60717e;
--line: rgba(23, 35, 45, 0.16);
--glass: rgba(255, 255, 255, 0.78);
--teal: #38bfb5;
--coral: #ef6d61;
--violet: #8066cc;
--amber: #f0ad45;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9eef1;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#app canvas {
display: block;
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.intro {
position: fixed;
z-index: 10;
top: 30px;
left: 32px;
width: min(420px, calc(100vw - 64px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: #50707a;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 2px;
content: "";
background: var(--teal);
}
h1 {
margin: 0;
font-size: clamp(32px, 4vw, 54px);
font-weight: 730;
letter-spacing: -0.055em;
line-height: 0.98;
}
.intro p {
max-width: 360px;
margin: 14px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.scale-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 15px;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.48);
color: #647681;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.scale-badge::before {
width: 6px;
height: 6px;
border-radius: 50%;
content: "";
background: var(--amber);
}
.panel {
position: fixed;
z-index: 20;
top: 24px;
right: 24px;
width: 250px;
padding: 16px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 18px;
background: var(--glass);
box-shadow: 0 18px 50px rgba(44, 61, 72, 0.13);
backdrop-filter: blur(16px);
}
.panel-title {
margin: 0 0 13px;
color: #71818b;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.control {
margin-top: 14px;
}
.control:first-of-type {
margin-top: 0;
}
.control-head {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 8px;
font-size: 12px;
font-weight: 650;
}
output {
color: #2f7f7a;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
outline: none;
appearance: none;
background: linear-gradient(
90deg,
var(--teal) 0%,
var(--teal) var(--range, 46%),
#d6e0e3 var(--range, 46%),
#d6e0e3 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid white;
border-radius: 50%;
appearance: none;
background: var(--teal);
box-shadow: 0 2px 8px rgba(29, 77, 74, 0.26);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 8px rgba(29, 77, 74, 0.26);
cursor: pointer;
}
.toggle {
display: flex;
justify-content: space-between;
align-items: center;
padding: 7px 0;
color: #43545e;
font-size: 12px;
cursor: pointer;
user-select: none;
}
.toggle input {
position: relative;
width: 34px;
height: 19px;
margin: 0;
border-radius: 999px;
appearance: none;
outline: none;
background: #cbd6da;
transition: background 160ms ease;
cursor: pointer;
}
.toggle input::after {
position: absolute;
top: 3px;
left: 3px;
width: 13px;
height: 13px;
border-radius: 50%;
content: "";
background: white;
box-shadow: 0 1px 4px rgba(20, 32, 40, 0.2);
transition: transform 160ms ease;
}
.toggle input:checked {
background: var(--teal);
}
.toggle input:checked::after {
transform: translateX(15px);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 15px;
}
button {
min-height: 34px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.74);
color: #455760;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: 150ms ease;
}
button:hover {
border-color: rgba(56, 191, 181, 0.55);
color: #267c75;
transform: translateY(-1px);
}
.legend {
position: fixed;
z-index: 10;
bottom: 25px;
left: 30px;
display: flex;
flex-wrap: wrap;
gap: 8px 15px;
max-width: 430px;
padding: 11px 13px;
border: 1px solid rgba(255, 255, 255, 0.62);
border-radius: 13px;
background: rgba(255, 255, 255, 0.62);
box-shadow: 0 10px 30px rgba(46, 65, 75, 0.08);
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #5a6b74;
font-size: 10px;
font-weight: 650;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
}
.hint {
position: fixed;
z-index: 10;
right: 28px;
bottom: 25px;
color: rgba(54, 71, 80, 0.68);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.04em;
pointer-events: none;
}
#leaders {
position: fixed;
z-index: 7;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#labels {
position: fixed;
z-index: 8;
inset: 0;
pointer-events: none;
}
.annotation {
position: absolute;
min-width: 98px;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 9px;
background: rgba(255, 255, 255, 0.82);
box-shadow: 0 5px 18px rgba(35, 54, 64, 0.1);
backdrop-filter: blur(9px);
transform: translate(-50%, -50%);
white-space: nowrap;
}
.annotation strong {
display: block;
color: #30434d;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.025em;
}
.annotation small {
display: block;
margin-top: 2px;
color: #7b8b93;
font-size: 8px;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #e9eef1;
color: #60717e;
font-size: 12px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 800px) {
.intro {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
h1 {
font-size: 34px;
}
.intro p,
.scale-badge {
display: none;
}
.panel {
top: auto;
right: 14px;
bottom: 14px;
width: 218px;
padding: 13px;
}
.legend,
.hint,
#labels,
#leaders {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="intro">
<div class="eyebrow">Anatomy / joint mechanics</div>
<h1>Knee,<br>in section.</h1>
<p>
A schematic cutaway emphasizing separate condylar contacts, patellar
glide, and crossed ligament constraints rather than a simple hinge.
</p>
<div class="scale-badge">Schematic · not to scale</div>
</header>
<aside class="panel">
<div class="panel-title">Interactive section</div>
<div class="control">
<div class="control-head">
<label for="flexion">Flexion</label>
<output id="flexionValue">16°</output>
</div>
<input
id="flexion"
type="range"
min="0"
max="35"
step="1"
value="16"
>
</div>
<label class="toggle">
Articular cartilage
<input id="cartilageToggle" type="checkbox" checked>
</label>
<label class="toggle">
Ligaments & tendons
<input id="ligamentToggle" type="checkbox" checked>
</label>
<label class="toggle">
Anatomy labels
<input id="labelToggle" type="checkbox" checked>
</label>
<div class="buttons">
<button id="resetView" type="button">Reset view</button>
<button id="resetPose" type="button">Reset pose</button>
</div>
</aside>
<div class="legend">
<div class="legend-item">
<span class="swatch" style="background:#39bdb4"></span>
Cartilage
</div>
<div class="legend-item">
<span class="swatch" style="background:#ef6d61"></span>
ACL
</div>
<div class="legend-item">
<span class="swatch" style="background:#8066cc"></span>
PCL
</div>
<div class="legend-item">
<span class="swatch" style="background:#f0ad45"></span>
Contact region
</div>
</div>
<div class="hint">
Drag to orbit · Shift/right-drag to pan · Scroll to zoom
</div>
<svg id="leaders" aria-hidden="true"></svg>
<div id="labels" aria-hidden="true"></div>
<div id="loading">Preparing anatomical section</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, and environment
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9eef1);
scene.fog = new THREE.Fog(0xe9eef1, 20, 38);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive schematic cutaway of the human knee"
);
app.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x70828e, 2.15);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xfff7e9, 3.25);
key.position.set(7, 11, 8);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 10;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 1;
key.shadow.camera.far = 30;
key.shadow.bias = -0.0003;
scene.add(key);
const fill = new THREE.DirectionalLight(0xb9e9ff, 1.35);
fill.position.set(-7, 4, -6);
scene.add(fill);
const rim = new THREE.DirectionalLight(0xffd8b8, 1.4);
rim.position.set(-2, 6, 7);
scene.add(rim);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xe9eef1,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -5.02;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(34, 34, 0xaebdc4, 0xcbd5da);
grid.position.y = -5;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const boneMaterial = new THREE.MeshStandardMaterial({
color: 0xe6d3ad,
roughness: 0.76,
metalness: 0
});
const boneSoftMaterial = new THREE.MeshStandardMaterial({
color: 0xead9b9,
roughness: 0.82,
metalness: 0
});
const cortexMaterial = new THREE.MeshStandardMaterial({
color: 0xdfc49a,
roughness: 0.9,
side: THREE.DoubleSide
});
const cutMaterial = new THREE.MeshStandardMaterial({
color: 0xd78c68,
roughness: 0.85
});
const marrowMaterial = new THREE.MeshStandardMaterial({
color: 0xc96f72,
roughness: 1
});
const marrowSpeckMaterial = new THREE.MeshStandardMaterial({
color: 0x9f4e5d,
roughness: 1
});
const cartilageMaterial = new THREE.MeshPhysicalMaterial({
color: 0x35bcb3,
roughness: 0.32,
metalness: 0,
clearcoat: 0.72,
clearcoatRoughness: 0.28,
emissive: 0x073f3d,
emissiveIntensity: 0.22,
transparent: true,
opacity: 0.92,
side: THREE.DoubleSide
});
const cartilageMaterialAlt = cartilageMaterial.clone();
cartilageMaterialAlt.color.setHex(0x65d7ca);
cartilageMaterialAlt.emissive.setHex(0x0a514b);
const contactMaterial = new THREE.MeshStandardMaterial({
color: 0xffb64f,
emissive: 0xd96b00,
emissiveIntensity: 0.5,
roughness: 0.35
});
const aclMaterial = new THREE.MeshStandardMaterial({
color: 0xef6d61,
roughness: 0.62
});
const pclMaterial = new THREE.MeshStandardMaterial({
color: 0x8066cc,
roughness: 0.62
});
const tendonMaterial = new THREE.MeshStandardMaterial({
color: 0xe2bf6d,
roughness: 0.7
});
const contactLineMaterial = new THREE.LineDashedMaterial({
color: 0xf0ad45,
dashSize: 0.12,
gapSize: 0.09,
transparent: true,
opacity: 0.7
});
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function signedPow(value, exponent) {
return Math.sign(value) * Math.pow(Math.abs(value), exponent);
}
function createLoftGeometry(
rings,
{
segments = 48,
thetaStart = 0,
thetaLength = Math.PI * 2,
exponent = 0.72,
caps = true
} = {}
) {
const positions = [];
const uvs = [];
const indices = [];
const full = Math.abs(thetaLength - Math.PI * 2) < 0.001;
const columns = full ? segments : segments + 1;
for (let r = 0; r < rings.length; r++) {
const ring = rings[r];
for (let i = 0; i < columns; i++) {
const t = i / segments;
const angle = thetaStart + t * thetaLength;
const c = Math.cos(angle);
const s = Math.sin(angle);
positions.push(
ring.rx * signedPow(c, exponent),
ring.y,
(ring.cz || 0) + ring.rz * signedPow(s, exponent)
);
uvs.push(t, r / Math.max(1, rings.length - 1));
}
}
for (let r = 0; r < rings.length - 1; r++) {
for (let i = 0; i < segments; i++) {
const j = (i + 1) % columns;
const a = r * columns + i;
const b = r * columns + j;
const d = (r + 1) * columns + i;
const c = (r + 1) * columns + j;
indices.push(a, d, b, b, d, c);
}
}
if (caps && full) {
const bottomCenter = positions.length / 3;
const first = rings[0];
positions.push(0, first.y, first.cz || 0);
uvs.push(0.5, 0);
for (let i = 0; i < segments; i++) {
const a = i;
const b = (i + 1) % segments;
indices.push(bottomCenter, b, a);
}
const topCenter = positions.length / 3;
const last = rings[rings.length - 1];
positions.push(0, last.y, last.cz || 0);
uvs.push(0.5, 1);
const offset = (rings.length - 1) * columns;
for (let i = 0; i < segments; i++) {
const a = offset + i;
const b = offset + (i + 1) % segments;
indices.push(topCenter, a, b);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createEllipsoidCap(
center,
radii,
maxAngle = 1.25,
segments = 64,
rings = 17
) {
const positions = [];
const uvs = [];
const indices = [];
for (let r = 0; r <= rings; r++) {
const t = (r / rings) * maxAngle;
for (let i = 0; i <= segments; i++) {
const a = (i / segments) * Math.PI * 2;
positions.push(
center.x + radii.x * Math.sin(t) * Math.cos(a),
center.y - radii.y * Math.cos(t),
center.z + radii.z * Math.sin(t) * Math.sin(a)
);
uvs.push(i / segments, r / rings);
}
}
for (let r = 0; r < rings; r++) {
for (let i = 0; i < segments; i++) {
const a = r * (segments + 1) + i;
const b = a + 1;
const d = (r + 1) * (segments + 1) + i;
const c = d + 1;
indices.push(a, d, b, b, d, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createDishPatch(
center,
radii,
baseY,
rise,
segments = 56,
rings = 12
) {
const positions = [];
const uvs = [];
const indices = [];
for (let r = 0; r <= rings; r++) {
const radius = r / rings;
for (let i = 0; i <= segments; i++) {
const angle = (i / segments) * Math.PI * 2;
positions.push(
center.x + radii.x * radius * Math.cos(angle),
baseY + rise * radius * radius,
center.z + radii.z * radius * Math.sin(angle)
);
uvs.push(i / segments, radius);
}
}
for (let r = 0; r < rings; r++) {
for (let i = 0; i < segments; i++) {
const a = r * (segments + 1) + i;
const b = a + 1;
const d = (r + 1) * (segments + 1) + i;
const c = d + 1;
indices.push(a, b, d, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createPatellarCartilageGeometry() {
const positions = [];
const uvs = [];
const indices = [];
const segments = 52;
const rings = 12;
for (let r = 0; r <= rings; r++) {
const radius = r / rings;
for (let i = 0; i <= segments; i++) {
const angle = (i / segments) * Math.PI * 2;
positions.push(
0.51 * radius * Math.cos(angle),
0.61 * radius * Math.sin(angle),
-0.275 - 0.075 * (1 - radius * radius)
);
uvs.push(i / segments, radius);
}
}
for (let r = 0; r < rings; r++) {
for (let i = 0; i < segments; i++) {
const a = r * (segments + 1) + i;
const b = a + 1;
const d = (r + 1) * (segments + 1) + i;
const c = d + 1;
indices.push(a, b, d, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createEllipsoid(
material,
position,
scale,
segments = 48
) {
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(1, segments, Math.max(24, segments / 1.5)),
material
);
mesh.position.copy(position);
mesh.scale.copy(scale);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function createTube(material, radius) {
const mesh = new THREE.Mesh(
new THREE.BufferGeometry(),
material
);
mesh.userData.radius = radius;
mesh.castShadow = true;
return mesh;
}
function updateTube(mesh, points) {
const curve = new THREE.CatmullRomCurve3(points, false, "centripetal");
mesh.geometry.dispose();
mesh.geometry = new THREE.TubeGeometry(
curve,
34,
mesh.userData.radius,
10,
false
);
mesh.userData.curve = curve;
}
// -------------------------------------------------------------------------
// Anatomical model
// -------------------------------------------------------------------------
const model = new THREE.Group();
scene.add(model);
const femurGroup = new THREE.Group();
femurGroup.rotation.order = "YXZ";
model.add(femurGroup);
const tibiaGroup = new THREE.Group();
model.add(tibiaGroup);
const patellaGroup = new THREE.Group();
model.add(patellaGroup);
const cartilageMeshes = [];
const ligamentMeshes = [];
const contactLinks = [];
// Femur: a windowed cortical shell exposing cancellous material.
const femurRings = [
{ y: 3.05, rx: 0.73, rz: 0.56, cz: 0.03 },
{ y: 3.75, rx: 0.62, rz: 0.48, cz: 0.06 },
{ y: 5.25, rx: 0.56, rz: 0.44, cz: 0.02 },
{ y: 6.85, rx: 0.62, rz: 0.47, cz: -0.03 },
{ y: 7.45, rx: 0.53, rz: 0.41, cz: -0.05 }
];
const gapAngle = 1.72;
const cutStart = Math.PI / 2 + gapAngle / 2;
const cutLength = Math.PI * 2 - gapAngle;
const femurShell = new THREE.Mesh(
createLoftGeometry(femurRings, {
thetaStart: cutStart,
thetaLength: cutLength,
segments: 58,
exponent: 0.74,
caps: false
}),
cortexMaterial
);
femurShell.castShadow = true;
femurShell.receiveShadow = true;
femurGroup.add(femurShell);
const marrowCore = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.48, 4.25, 40),
marrowMaterial
);
marrowCore.position.set(0, 5.26, 0.02);
marrowCore.scale.z = 0.75;
marrowCore.castShadow = true;
femurGroup.add(marrowCore);
// Speckled cancellous pattern visible in the cut window.
for (let i = 0; i < 28; i++) {
const y = 3.3 + Math.random() * 3.85;
const angle = 0.77 + Math.random() * (Math.PI / 2 + 0.83);
const radius = 0.024 + Math.random() * 0.032;
const speck = new THREE.Mesh(
new THREE.SphereGeometry(radius, 10, 7),
marrowSpeckMaterial
);
speck.position.set(
0.445 * Math.cos(angle),
y,
0.338 * Math.sin(angle) + 0.02
);
speck.scale.z = 0.6;
femurGroup.add(speck);
}
// Exposed cortical edges make the section explicit.
function addCutEdge(angle, radius = 0.034) {
const points = femurRings.map((ring) => {
const c = Math.cos(angle);
const s = Math.sin(angle);
return new THREE.Vector3(
ring.rx * signedPow(c, 0.74),
ring.y,
(ring.cz || 0) + ring.rz * signedPow(s, 0.74)
);
});
const edge = new THREE.Mesh(
new THREE.TubeGeometry(
new THREE.CatmullRomCurve3(points),
34,
radius,
8,
false
),
cutMaterial
);
edge.castShadow = true;
femurGroup.add(edge);
}
addCutEdge(cutStart);
addCutEdge(cutStart + cutLength);
// Distal femur and separate condyles.
const distalBody = createEllipsoid(
boneMaterial,
new THREE.Vector3(0, 1.42, -0.12),
new THREE.Vector3(1.34, 1.18, 0.72)
);
femurGroup.add(distalBody);
const lateralCondyle = createEllipsoid(
boneSoftMaterial,
new THREE.Vector3(0.79, 0.63, 0.01),
new THREE.Vector3(0.78, 0.84, 0.91)
);
femurGroup.add(lateralCondyle);
const medialCondyle = createEllipsoid(
boneSoftMaterial,
new THREE.Vector3(-0.79, 0.60, -0.035),
new THREE.Vector3(0.77, 0.82, 0.89)
);
femurGroup.add(medialCondyle);
const lateralCartilage = new THREE.Mesh(
createEllipsoidCap(
new THREE.Vector3(0.79, 0.63, 0.01),
new THREE.Vector3(0.802, 0.865, 0.935),
1.25
),
cartilageMaterial
);
lateralCartilage.renderOrder = 2;
femurGroup.add(lateralCartilage);
cartilageMeshes.push(lateralCartilage);
const medialCartilage = new THREE.Mesh(
createEllipsoidCap(
new THREE.Vector3(-0.79, 0.60, -0.035),
new THREE.Vector3(0.792, 0.845, 0.915),
1.25
),
cartilageMaterialAlt
);
medialCartilage.renderOrder = 2;
femurGroup.add(medialCartilage);
cartilageMeshes.push(medialCartilage);
// Tibial plateau and shaft.
const plateau = new THREE.Mesh(
new THREE.CylinderGeometry(1.57, 1.48, 0.36, 64),
boneMaterial
);
plateau.position.set(0, -0.02, 0);
plateau.scale.z = 0.66;
plateau.castShadow = true;
plateau.receiveShadow = true;
tibiaGroup.add(plateau);
const tibialRings = [
{ y: -0.17, rx: 1.26, rz: 0.81, cz: 0 },
{ y: -0.62, rx: 0.86, rz: 0.63, cz: 0.02 },
{ y: -1.35, rx: 0.59, rz: 0.49, cz: 0.03 },
{ y: -3.05, rx: 0.61, rz: 0.47, cz: 0.01 },
{ y: -4.82, rx: 0.70, rz: 0.51, cz: -0.02 }
];
const tibiaShaft = new THREE.Mesh(
createLoftGeometry(tibialRings, {
segments: 48,
exponent: 0.7,
caps: true
}),
boneMaterial
);
tibiaShaft.castShadow = true;
tibiaShaft.receiveShadow = true;
tibiaGroup.add(tibiaShaft);
const tibialTuberosity = createEllipsoid(
boneSoftMaterial,
new THREE.Vector3(0, -0.72, 0.51),
new THREE.Vector3(0.51, 0.70, 0.31)
);
tibiaGroup.add(tibialTuberosity);
const lateralPlateauCartilage = new THREE.Mesh(
createDishPatch(
new THREE.Vector3(0.78, 0, 0),
new THREE.Vector2(0.77, 0.67),
0.185,
0.095
),
cartilageMaterial
);
lateralPlateauCartilage.renderOrder = 2;
tibiaGroup.add(lateralPlateauCartilage);
cartilageMeshes.push(lateralPlateauCartilage);
const medialPlateauCartilage = new THREE.Mesh(
createDishPatch(
new THREE.Vector3(-0.78, 0, -0.025),
new THREE.Vector2(0.75, 0.65),
0.185,
0.095
),
cartilageMaterialAlt
);
medialPlateauCartilage.renderOrder = 2;
tibiaGroup.add(medialPlateauCartilage);
cartilageMeshes.push(medialPlateauCartilage);
// Intercondylar spines.
for (const x of [-0.22, 0.22]) {
const spine = new THREE.Mesh(
new THREE.ConeGeometry(0.16, 0.38, 22),
boneSoftMaterial
);
spine.position.set(x, 0.28, -0.02);
spine.castShadow = true;
tibiaGroup.add(spine);
const cap = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 22, 12),
cartilageMaterial
);
cap.scale.set(1, 0.45, 1);
cap.position.set(x, 0.47, -0.02);
tibiaGroup.add(cap);
cartilageMeshes.push(cap);
}
// Patella.
const patella = createEllipsoid(
boneMaterial,
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0.69, 0.79, 0.28)
);
patellaGroup.add(patella);
const patellarCartilage = new THREE.Mesh(
createPatellarCartilageGeometry(),
cartilageMaterialAlt
);
patellarCartilage.renderOrder = 2;
patellaGroup.add(patellarCartilage);
cartilageMeshes.push(patellarCartilage);
// Highlighted contact regions. Their separation and alignment intentionally
// vary slightly to avoid implying a perfectly coaxial hinge.
const contactDefinitions = [
{
femoral: new THREE.Vector3(0.79, -0.255, 0.01),
tibial: new THREE.Vector3(0.78, 0.225, 0.015)
},
{
femoral: new THREE.Vector3(-0.79, -0.245, -0.09),
tibial: new THREE.Vector3(-0.78, 0.225, -0.035)
}
];
for (const definition of contactDefinitions) {
const femoralDot = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 18, 12),
contactMaterial
);
femoralDot.position.copy(definition.femoral);
femurGroup.add(femoralDot);
cartilageMeshes.push(femoralDot);
const tibialDot = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 18, 12),
contactMaterial
);
tibialDot.position.copy(definition.tibial);
tibiaGroup.add(tibialDot);
cartilageMeshes.push(tibialDot);
const lineGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]);
const line = new THREE.Line(lineGeometry, contactLineMaterial);
line.computeLineDistances();
model.add(line);
cartilageMeshes.push(line);
contactLinks.push({ line, definition });
}
// Cruciate ligaments and extensor-mechanism tendons.
const acl = createTube(aclMaterial, 0.068);
const pcl = createTube(pclMaterial, 0.071);
const quadricepsTendon = createTube(tendonMaterial, 0.072);
const patellarTendon = createTube(tendonMaterial, 0.066);
model.add(acl, pcl, quadricepsTendon, patellarTendon);
ligamentMeshes.push(acl, pcl, quadricepsTendon, patellarTendon);
// -------------------------------------------------------------------------
// Pose and ligament kinematics
// -------------------------------------------------------------------------
function updateModel(flexionDegrees) {
const radians = THREE.MathUtils.degToRad(flexionDegrees);
const normalized = flexionDegrees / 35;
// Sagittal flexion plus a small axial rotation: not a single-axis hinge.
femurGroup.rotation.y = -0.065;
femurGroup.rotation.x = -radians;
femurGroup.rotation.z = 0.012;
// Simplified patellar tracking: inferior glide around the distal femur.
patellaGroup.position.set(
0.02,
2.30 - normalized * 0.24,
1.31 - normalized * 0.10
);
patellaGroup.rotation.set(
-0.22 - normalized * 0.08,
0.045,
0.018
);
femurGroup.updateMatrixWorld(true);
tibiaGroup.updateMatrixWorld(true);
patellaGroup.updateMatrixWorld(true);
const femurPoint = (x, y, z) =>
femurGroup.localToWorld(new THREE.Vector3(x, y, z));
const tibiaPoint = (x, y, z) =>
tibiaGroup.localToWorld(new THREE.Vector3(x, y, z));
const patellaPoint = (x, y, z) =>
patellaGroup.localToWorld(new THREE.Vector3(x, y, z));
// ACL and PCL cross between non-coaxial femoral and tibial attachments.
const aclPoints = [
femurPoint(-0.18, 1.14, -0.25),
new THREE.Vector3(-0.12, 0.55, 0.00),
new THREE.Vector3(0.08, -0.15, 0.18),
tibiaPoint(0.18, -0.70, 0.25)
];
const pclPoints = [
femurPoint(0.18, 1.12, -0.42),
new THREE.Vector3(0.11, 0.48, -0.20),
new THREE.Vector3(-0.08, -0.32, -0.24),
tibiaPoint(-0.18, -0.88, -0.16)
];
const quadPoints = [
femurPoint(0, 3.62, 0.46),
patellaPoint(0, 0.76, -0.02),
patellaPoint(0, 0.53, 0.01)
];
const patellarTendonPoints = [
patellaPoint(0, -0.72, 0.01),
new THREE.Vector3(0.02, -0.78, 0.68),
tibiaPoint(0, -0.80, 0.62)
];
updateTube(acl, aclPoints);
updateTube(pcl, pclPoints);
updateTube(quadricepsTendon, quadPoints);
updateTube(patellarTendon, patellarTendonPoints);
// Update the short contact-region connectors.
for (const { line, definition } of contactLinks) {
const a = femurPoint(
definition.femoral.x,
definition.femoral.y,
definition.femoral.z
);
const b = tibiaPoint(
definition.tibial.x,
definition.tibial.y,
definition.tibial.z
);
const position = line.geometry.getAttribute("position");
position.setXYZ(0, a.x, a.y, a.z);
position.setXYZ(1, b.x, b.y, b.z);
position.needsUpdate = true;
line.geometry.computeBoundingSphere();
line.computeLineDistances();
}
}
// -------------------------------------------------------------------------
// Camera controls implemented directly, without external controls modules.
// -------------------------------------------------------------------------
const desired = {
radius: 18.5,
theta: 0.72,
phi: 1.22,
target: new THREE.Vector3(0, 1.0, 0)
};
const current = {
radius: desired.radius,
theta: desired.theta,
phi: desired.phi,
target: desired.target.clone()
};
function resetView() {
desired.radius = 18.5;
desired.theta = 0.72;
desired.phi = 1.22;
desired.target.set(0, 1.0, 0);
}
let dragging = false;
let panning = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
panning =
event.button === 2 ||
event.shiftKey ||
event.ctrlKey ||
event.metaKey;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (panning) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const scale = desired.radius * 0.00145;
desired.target
.addScaledVector(right, -dx * scale)
.addScaledVector(up, dy * scale);
} else {
desired.theta -= dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi - dy * 0.005,
0.45,
2.35
);
}
});
function endDrag(event) {
dragging = false;
panning = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius + event.deltaY * 0.012,
11,
28
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
// -------------------------------------------------------------------------
// Projected anatomy labels
// -------------------------------------------------------------------------
const labelsElement = document.getElementById("labels");
const leadersElement = document.getElementById("leaders");
const svgNamespace = "http://www.w3.org/2000/svg";
const annotationDefinitions = [
{
title: "Femur",
subtitle: "windowed cortex",
offset: [-118, -30],
color: "#d1a878",
group: "bone",
anchor: () =>
femurGroup.localToWorld(new THREE.Vector3(0.12, 5.25, 0.42))
},
{
title: "Femoral condyle",
subtitle: "independent contact",
offset: [122, -18],
color: "#35bcb3",
group: "cartilage",
anchor: () =>
femurGroup.localToWorld(new THREE.Vector3(0.79, 0.22, 0.52))
},
{
title: "Patella",
subtitle: "gliding sesamoid",
offset: [122, -38],
color: "#d1a878",
group: "bone",
anchor: () =>
patellaGroup.localToWorld(new THREE.Vector3(0.02, 0.46, 0.04))
},
{
title: "ACL / PCL",
subtitle: "crossed constraint",
offset: [-125, 35],
color: "#ef6d61",
group: "ligament",
anchor: () =>
acl.userData.curve
? acl.userData.curve.getPoint(0.48)
: new THREE.Vector3(0, 0, 0)
},
{
title: "Tibial plateau",
subtitle: "cartilage interface",
offset: [128, 24],
color: "#35bcb3",
group: "cartilage",
anchor: () =>
tibiaGroup.localToWorld(new THREE.Vector3(0.76, 0.22, 0.12))
},
{
title: "Tibia",
subtitle: "load-bearing shaft",
offset: [112, 26],
color: "#d1a878",
group: "bone",
anchor: () =>
tibiaGroup.localToWorld(new THREE.Vector3(0.30, -2.55, 0.40))
}
];
for (const definition of annotationDefinitions) {
const label = document.createElement("div");
label.className = "annotation";
label.innerHTML = `
<strong>${definition.title}</strong>
<small>${definition.subtitle}</small>
`;
labelsElement.appendChild(label);
const line = document.createElementNS(svgNamespace, "line");
line.setAttribute("stroke", definition.color);
line.setAttribute("stroke-width", "1");
line.setAttribute("stroke-opacity", "0.6");
const circle = document.createElementNS(svgNamespace, "circle");
circle.setAttribute("r", "3");
circle.setAttribute("fill", definition.color);
circle.setAttribute("stroke", "white");
circle.setAttribute("stroke-width", "1.5");
leadersElement.append(line, circle);
definition.element = label;
definition.line = line;
definition.circle = circle;
}
function updateAnnotations() {
const width = innerWidth;
const height = innerHeight;
const compactScale = width < 1200 ? 0.78 : 1;
for (const annotation of annotationDefinitions) {
const anchor = annotation.anchor();
const projected = anchor.clone().project(camera);
const inView =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.3 &&
projected.x < 1.3 &&
projected.y > -1.3 &&
projected.y < 1.3;
const groupVisible =
annotation.group !== "cartilage" ||
cartilageMeshes.some((mesh) => mesh.visible);
const ligamentVisible =
annotation.group !== "ligament" ||
ligamentMeshes.some((mesh) => mesh.visible);
const visible = inView && groupVisible && ligamentVisible;
annotation.element.style.display = visible ? "block" : "none";
annotation.line.style.display = visible ? "block" : "none";
annotation.circle.style.display = visible ? "block" : "none";
if (!visible) continue;
const sx = (projected.x * 0.5 + 0.5) * width;
const sy = (-projected.y * 0.5 + 0.5) * height;
const lx = THREE.MathUtils.clamp(
sx + annotation.offset[0] * compactScale,
75,
width - 75
);
const ly = THREE.MathUtils.clamp(
sy + annotation.offset[1] * compactScale,
55,
height - 55
);
annotation.element.style.left = `${lx}px`;
annotation.element.style.top = `${ly}px`;
annotation.line.setAttribute("x1", sx);
annotation.line.setAttribute("y1", sy);
annotation.line.setAttribute("x2", lx);
annotation.line.setAttribute("y2", ly);
annotation.circle.setAttribute("cx", sx);
annotation.circle.setAttribute("cy", sy);
}
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const flexionInput = document.getElementById("flexion");
const flexionValue = document.getElementById("flexionValue");
function updateRangeFill() {
const percent =
((Number(flexionInput.value) - Number(flexionInput.min)) /
(Number(flexionInput.max) - Number(flexionInput.min))) *
100;
flexionInput.style.setProperty("--range", `${percent}%`);
}
function setFlexion(value) {
flexionValue.value = `${value}°`;
updateRangeFill();
updateModel(Number(value));
}
flexionInput.addEventListener("input", () => {
setFlexion(flexionInput.value);
});
document
.getElementById("cartilageToggle")
.addEventListener("change", (event) => {
const visible = event.target.checked;
for (const mesh of cartilageMeshes) {
mesh.visible = visible;
}
});
document
.getElementById("ligamentToggle")
.addEventListener("change", (event) => {
const visible = event.target.checked;
for (const mesh of ligamentMeshes) {
mesh.visible = visible;
}
});
document
.getElementById("labelToggle")
.addEventListener("change", (event) => {
labelsElement.style.display = event.target.checked
? "block"
: "none";
leadersElement.style.display = event.target.checked
? "block"
: "none";
});
document.getElementById("resetView").addEventListener("click", resetView);
document.getElementById("resetPose").addEventListener("click", () => {
flexionInput.value = "16";
setFlexion(16);
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
setFlexion(flexionInput.value);
const clock = new THREE.Clock();
const spherical = new THREE.Spherical();
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
const smoothing = 1 - Math.pow(0.0008, delta);
current.radius +=
(desired.radius - current.radius) * smoothing;
current.theta +=
(desired.theta - current.theta) * smoothing;
current.phi +=
(desired.phi - current.phi) * smoothing;
current.target.lerp(desired.target, smoothing);
spherical.set(
current.radius,
current.phi,
current.theta
);
camera.position.setFromSpherical(spherical).add(current.target);
camera.lookAt(current.target);
camera.updateMatrixWorld();
contactMaterial.emissiveIntensity =
0.46 + Math.sin(clock.elapsedTime * 2.8) * 0.13;
updateAnnotations();
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```