Prompt specification
Build or customize the Three.js 3D scene "Two Discs and Contact Normal" 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: Two Discs and Contact Normal
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-discs-and-contact-normal-x3613
Scene Description Prompt:
Two touching stylized discs on a board, with the contact normal distinguished from incoming velocity. Create one HTML file with Three.js from cdnjs.
Working Three.js Starter (two-discs-and-contact-normal-x3613):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Oblique Contact — Three.js</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf3ff;
--muted: #91a6bd;
--velocity: #ffc857;
--normal: #ff5fa2;
--contact: #f5fbff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111d;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
#label-layer {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.panel {
position: fixed;
z-index: 3;
top: 28px;
left: 28px;
width: min(360px, calc(100vw - 56px));
padding: 20px 22px;
border: 1px solid rgba(163, 199, 232, 0.16);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(13, 27, 44, 0.9), rgba(8, 18, 31, 0.78));
box-shadow:
0 22px 70px rgba(0, 0, 0, 0.3),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: #7dd9d0;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: var(--ink);
font-size: clamp(24px, 3vw, 33px);
font-weight: 650;
letter-spacing: -0.035em;
}
.description {
margin: 9px 0 18px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
display: grid;
gap: 9px;
padding-top: 15px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.legend-item {
display: flex;
align-items: center;
gap: 10px;
color: #c9d8e8;
font-size: 12px;
}
.swatch {
width: 25px;
height: 3px;
flex: 0 0 auto;
border-radius: 999px;
box-shadow: 0 0 12px currentColor;
}
.swatch.velocity {
color: var(--velocity);
background: currentColor;
}
.swatch.normal {
color: var(--normal);
background: currentColor;
}
.swatch.contact {
width: 8px;
height: 8px;
margin: 0 8px;
color: var(--contact);
background: currentColor;
border-radius: 50%;
}
#reset-camera {
position: fixed;
z-index: 4;
top: 28px;
right: 28px;
width: 42px;
height: 42px;
display: grid;
place-items: center;
border: 1px solid rgba(163, 199, 232, 0.17);
border-radius: 13px;
color: #c7d8ea;
background: rgba(10, 24, 39, 0.82);
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
cursor: pointer;
transition:
color 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
#reset-camera:hover {
color: white;
border-color: rgba(125, 217, 208, 0.55);
transform: translateY(-1px);
}
#reset-camera svg {
width: 19px;
height: 19px;
}
.hint {
position: fixed;
z-index: 3;
right: 28px;
bottom: 24px;
color: rgba(187, 207, 226, 0.62);
font-size: 11px;
letter-spacing: 0.035em;
pointer-events: none;
}
.scene-label {
position: absolute;
top: 0;
left: 0;
white-space: nowrap;
will-change: transform;
}
.vector-label {
padding: 7px 10px;
border-radius: 8px;
color: #f7fbff;
background: rgba(7, 16, 28, 0.84);
border: 1px solid rgba(255, 255, 255, 0.13);
box-shadow: 0 7px 22px rgba(0, 0, 0, 0.28);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.025em;
backdrop-filter: blur(8px);
}
#velocity-label {
color: var(--velocity);
border-color: rgba(255, 200, 87, 0.32);
}
#normal-label {
color: var(--normal);
border-color: rgba(255, 95, 162, 0.34);
}
#contact-label {
padding: 5px 8px;
color: #e9f6ff;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.disc-label {
color: rgba(236, 246, 255, 0.76);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
text-shadow: 0 2px 8px rgba(0, 0, 0, 0.9);
}
@media (max-width: 700px) {
.panel {
top: 16px;
left: 16px;
width: min(320px, calc(100vw - 32px));
padding: 16px 17px;
}
.description {
margin-bottom: 13px;
}
#reset-camera {
top: auto;
right: 16px;
bottom: 48px;
}
.hint {
right: 16px;
bottom: 18px;
}
.disc-label {
display: none;
}
}
</style>
</head>
<body>
<div id="label-layer">
<div id="velocity-label" class="scene-label vector-label">incoming velocity · v</div>
<div id="normal-label" class="scene-label vector-label">contact normal · n</div>
<div id="contact-label" class="scene-label vector-label">contact point</div>
<div id="disc-a-label" class="scene-label disc-label">incoming disc</div>
<div id="disc-b-label" class="scene-label disc-label">support disc</div>
</div>
<aside class="panel">
<div class="eyebrow">Collision study · 01</div>
<h1>Oblique contact</h1>
<p class="description">
Incoming velocity may contain a tangential component. The contact normal
follows the line between the disc centers.
</p>
<div class="legend">
<div class="legend-item">
<span class="swatch velocity"></span>
<span>Incoming velocity vector</span>
</div>
<div class="legend-item">
<span class="swatch normal"></span>
<span>Contact normal vector</span>
</div>
<div class="legend-item">
<span class="swatch contact"></span>
<span>Point of contact</span>
</div>
</div>
</aside>
<button id="reset-camera" type="button" aria-label="Reset camera" title="Reset camera">
<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path
d="M4.9 7.8A8 8 0 1 1 4 12"
stroke="currentColor"
stroke-width="1.8"
stroke-linecap="round"
/>
<path
d="M4.9 3.8v4h4"
stroke="currentColor"
stroke-width="1.8"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</button>
<div class="hint">Drag to orbit · Scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.body;
const labelLayer = document.getElementById("label-layer");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 11, 23);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance",
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Two touching discs showing incoming velocity and contact normal"
);
container.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.05, 0);
const spherical = new THREE.Spherical(8.7, 1.02, 0.69);
const initialSpherical = spherical.clone();
function updateCamera() {
spherical.makeSafe();
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xbadfff, 0x182335, 1.75);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x58b9ff, 1.25);
rimLight.position.set(-5, 4, -4);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xff9b64, 1.4, 10, 2);
warmFill.position.set(-3.2, 2.2, 2.2);
scene.add(warmFill);
function roundedRectangle(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
shape.closePath();
return shape;
}
const boardShape = roundedRectangle(8.9, 6.45, 0.3);
const boardGeometry = new THREE.ExtrudeGeometry(boardShape, {
depth: 0.28,
steps: 1,
bevelEnabled: true,
bevelThickness: 0.035,
bevelSize: 0.055,
bevelSegments: 3,
curveSegments: 12,
});
boardGeometry.rotateX(-Math.PI / 2);
const boardEdge = new THREE.Mesh(
boardGeometry,
new THREE.MeshStandardMaterial({
color: 0x172b43,
roughness: 0.58,
metalness: 0.18,
})
);
boardEdge.position.y = -0.39;
boardEdge.scale.set(1.016, 0.82, 1.016);
boardEdge.receiveShadow = true;
scene.add(boardEdge);
const board = new THREE.Mesh(
boardGeometry,
new THREE.MeshStandardMaterial({
color: 0x0b1828,
roughness: 0.76,
metalness: 0.08,
})
);
board.position.y = -0.32;
board.receiveShadow = true;
scene.add(board);
const gridVertices = [];
const gridColors = [];
const minorColor = new THREE.Color(0x35506c);
const majorColor = new THREE.Color(0x54718e);
const boardWidth = 8.62;
const boardDepth = 6.18;
const gridY = -0.025;
for (let x = -4.2; x <= 4.21; x += 0.42) {
const major = Math.abs(x % 1.68) < 0.01;
const color = major ? majorColor : minorColor;
gridVertices.push(x, gridY, -boardDepth / 2, x, gridY, boardDepth / 2);
gridColors.push(color.r, color.g, color.b, color.r, color.g, color.b);
}
for (let z = -3; z <= 3.01; z += 0.42) {
const major = Math.abs(z % 1.68) < 0.01;
const color = major ? majorColor : minorColor;
gridVertices.push(-boardWidth / 2, gridY, z, boardWidth / 2, gridY, z);
gridColors.push(color.r, color.g, color.b, color.r, color.g, color.b);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
gridGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(gridColors, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
vertexColors: true,
transparent: true,
opacity: 0.25,
})
);
scene.add(grid);
const axisVertices = new Float32Array([
-4.15, -0.018, 0, 4.15, -0.018, 0,
0, -0.018, -2.95, 0, -0.018, 2.95,
]);
const axisGeometry = new THREE.BufferGeometry();
axisGeometry.setAttribute(
"position",
new THREE.BufferAttribute(axisVertices, 3)
);
const axes = new THREE.LineSegments(
axisGeometry,
new THREE.LineBasicMaterial({
color: 0x668aa8,
transparent: true,
opacity: 0.3,
})
);
scene.add(axes);
function createDisc({
x,
z,
radius,
height,
sideColor,
topColor,
ringColor,
rotation = 0,
}) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
const body = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, 64, 1, false),
[
new THREE.MeshStandardMaterial({
color: sideColor,
roughness: 0.32,
metalness: 0.2,
}),
new THREE.MeshStandardMaterial({
color: topColor,
roughness: 0.28,
metalness: 0.16,
}),
new THREE.MeshStandardMaterial({
color: sideColor,
roughness: 0.4,
metalness: 0.15,
}),
]
);
body.position.y = height / 2 - 0.01;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const inlay = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 0.77, radius * 0.77, 0.014, 64),
new THREE.MeshStandardMaterial({
color: topColor,
roughness: 0.23,
metalness: 0.25,
emissive: new THREE.Color(topColor).multiplyScalar(0.06),
})
);
inlay.position.y = height + 0.001;
inlay.receiveShadow = true;
group.add(inlay);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(radius * 0.79, 0.025, 12, 64),
new THREE.MeshStandardMaterial({
color: ringColor,
roughness: 0.25,
metalness: 0.35,
emissive: new THREE.Color(ringColor).multiplyScalar(0.12),
})
);
rim.rotation.x = Math.PI / 2;
rim.position.y = height + 0.016;
group.add(rim);
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(body.geometry, 24),
new THREE.LineBasicMaterial({
color: ringColor,
transparent: true,
opacity: 0.35,
})
);
edge.position.copy(body.position);
group.add(edge);
scene.add(group);
return group;
}
const radius = 0.78;
const discA = createDisc({
x: -radius,
z: 0,
radius,
height: 0.32,
sideColor: 0xc84627,
topColor: 0xff7043,
ringColor: 0xffb06e,
rotation: 0.08,
});
const discB = createDisc({
x: radius,
z: 0,
radius,
height: 0.32,
sideColor: 0x087f82,
topColor: 0x20c8bd,
ringColor: 0x89fff2,
rotation: -0.05,
});
function createArrow(origin, direction, length, color, options = {}) {
const group = new THREE.Group();
const dir = direction.clone().normalize();
const headLength = options.headLength ?? 0.25;
const headRadius = options.headRadius ?? 0.105;
const shaftRadius = options.shaftRadius ?? 0.032;
const shaftLength = Math.max(0.01, length - headLength);
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.22,
metalness: 0.25,
emissive: new THREE.Color(color).multiplyScalar(0.25),
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
shaftRadius,
shaftRadius,
shaftLength,
20
),
material
);
shaft.position.copy(origin).addScaledVector(dir, shaftLength / 2);
shaft.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
dir
);
shaft.castShadow = true;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(headRadius, headLength, 24),
material
);
head.position
.copy(origin)
.addScaledVector(dir, shaftLength + headLength / 2);
head.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
dir
);
head.castShadow = true;
group.add(head);
scene.add(group);
return { group, material };
}
const contactPoint = new THREE.Vector3(0, 0.39, 0);
const velocityOrigin = new THREE.Vector3(-2.6, 0.53, 1.4);
const velocityDirection = contactPoint.clone().sub(velocityOrigin);
const velocityLength = velocityDirection.length();
velocityDirection.normalize();
const velocityArrow = createArrow(
velocityOrigin,
velocityDirection,
velocityLength,
0xffc857,
{
headLength: 0.29,
headRadius: 0.12,
shaftRadius: 0.038,
}
);
const normalArrow = createArrow(
new THREE.Vector3(0, 0.51, 0),
new THREE.Vector3(-1, 0, 0),
1.22,
0xff5fa2,
{
headLength: 0.24,
headRadius: 0.095,
shaftRadius: 0.032,
}
);
const contactMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.052, 20, 20),
new THREE.MeshBasicMaterial({ color: 0xf5fbff })
);
contactMarker.position.copy(contactPoint);
contactMarker.renderOrder = 10;
scene.add(contactMarker);
const contactRing = new THREE.Mesh(
new THREE.TorusGeometry(0.13, 0.016, 10, 48),
new THREE.MeshBasicMaterial({
color: 0xdff7ff,
transparent: true,
opacity: 0.75,
})
);
contactRing.rotation.x = Math.PI / 2;
contactRing.position.set(0, 0.385, 0);
scene.add(contactRing);
const contactLight = new THREE.PointLight(0xd9f6ff, 0.7, 1.4, 2);
contactLight.position.set(0, 0.62, 0);
scene.add(contactLight);
const tangentGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0.37, -0.2),
new THREE.Vector3(0, 0.37, 0.2),
]);
const tangent = new THREE.Line(
tangentGeometry,
new THREE.LineBasicMaterial({
color: 0xaac4d8,
transparent: true,
opacity: 0.42,
})
);
scene.add(tangent);
const beads = [];
for (let i = 0; i < 3; i += 1) {
const bead = new THREE.Mesh(
new THREE.SphereGeometry(0.042, 16, 16),
new THREE.MeshBasicMaterial({
color: 0xffdd82,
transparent: true,
opacity: 0.8,
})
);
scene.add(bead);
beads.push(bead);
}
const labels = {
velocity: document.getElementById("velocity-label"),
normal: document.getElementById("normal-label"),
contact: document.getElementById("contact-label"),
discA: document.getElementById("disc-a-label"),
discB: document.getElementById("disc-b-label"),
};
const projection = new THREE.Vector3();
function projectLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
projection.copy(worldPosition).project(camera);
const visible = projection.z > -1 && projection.z < 1;
element.style.opacity = visible ? "1" : "0";
const x = (projection.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projection.y * 0.5 + 0.5) * window.innerHeight;
element.style.transform =
`translate(-50%, -50%) translate(${x + offsetX}px, ${y + offsetY}px)`;
}
function updateLabels() {
const velocityMid = velocityOrigin
.clone()
.addScaledVector(velocityDirection, velocityLength * 0.48);
velocityMid.y += 0.12;
projectLabel(labels.velocity, velocityMid, 0, -34);
projectLabel(
labels.normal,
new THREE.Vector3(-0.66, 0.58, 0),
0,
31
);
projectLabel(labels.contact, contactPoint, 54, -5);
projectLabel(
labels.discA,
new THREE.Vector3(-radius, 0.44, 0),
0,
-42
);
projectLabel(
labels.discB,
new THREE.Vector3(radius, 0.44, 0),
0,
-42
);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
if (!event.isPrimary) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || !event.isPrimary) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
spherical.theta -= deltaX * 0.0052;
spherical.phi -= deltaY * 0.0045;
spherical.phi = THREE.MathUtils.clamp(spherical.phi, 0.38, 1.38);
updateCamera();
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event?.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius + event.deltaY * 0.007,
6.3,
14.5
);
updateCamera();
},
{ passive: false }
);
document.getElementById("reset-camera").addEventListener("click", () => {
spherical.copy(initialSpherical);
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const pulse = 1 + Math.sin(elapsed * 4.2) * 0.12;
contactRing.scale.setScalar(pulse);
contactRing.material.opacity = 0.62 + Math.sin(elapsed * 4.2) * 0.18;
contactLight.intensity = 0.58 + Math.sin(elapsed * 4.2) * 0.16;
velocityArrow.material.emissiveIntensity = 0.2 + Math.sin(elapsed * 2.2) * 0.06;
normalArrow.material.emissiveIntensity = 0.22 + Math.sin(elapsed * 2.2 + 1.2) * 0.06;
beads.forEach((bead, index) => {
const phase = (elapsed * 0.34 - index * 0.13) % 1;
const normalized = phase < 0 ? phase + 1 : phase;
bead.position
.copy(velocityOrigin)
.addScaledVector(velocityDirection, normalized * velocityLength);
bead.position.y += 0.025;
bead.material.opacity = Math.sin(normalized * Math.PI) * 0.9;
});
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```