Prompt specification
Build or customize the Three.js 3D scene "Ellipse and parabola section comparison" 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: Ellipse and parabola section comparison
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/ellipse-and-parabola-section-comparison-x1686
Scene Description Prompt:
Ellipse and parabola section comparison. Place selected cone-plane arrangements side by side with their extracted curves; identify the slicing conditions and avoid drawing a parabola as a stretched ellipse. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (ellipse-and-parabola-section-comparison-x1686):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ellipse and Parabola Cone Sections</title>
<style>
:root {
color-scheme: dark;
--bg: #07111d;
--panel: #0c1b2a;
--text: #eef7ff;
--muted: #9fb5c8;
--ellipse: #5eead4;
--parabola: #fb923c;
--focus: #facc15;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 10;
top: 15px;
left: 18px;
right: 18px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.eyebrow {
display: block;
margin-bottom: 5px;
color: #79d9ce;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.2vw, 31px);
font-weight: 680;
letter-spacing: -0.035em;
}
.topbar-right {
display: flex;
align-items: center;
gap: 12px;
pointer-events: auto;
}
.hint {
color: var(--muted);
font-size: 11px;
white-space: nowrap;
}
button {
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 999px;
padding: 9px 14px;
background: rgba(12, 27, 42, 0.82);
color: var(--text);
font: inherit;
font-size: 12px;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease;
}
button:hover {
background: rgba(24, 45, 65, 0.95);
border-color: rgba(255, 255, 255, 0.3);
}
#panels {
position: fixed;
z-index: 4;
inset: 83px 16px 58px;
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
pointer-events: none;
}
.panel {
--accent: var(--ellipse);
position: relative;
min-width: 0;
overflow: hidden;
border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
border-radius: 20px;
background: rgba(7, 17, 29, 0.12);
box-shadow:
inset 0 0 0 1px rgba(255, 255, 255, 0.018),
0 20px 60px rgba(0, 0, 0, 0.16);
}
.panel::before {
content: "";
position: absolute;
inset: 0;
pointer-events: none;
background:
linear-gradient(
180deg,
rgba(4, 11, 20, 0.48) 0%,
transparent 26%,
transparent 72%,
rgba(4, 11, 20, 0.2) 100%
);
}
.panel.parabola {
--accent: var(--parabola);
}
.panel-head {
position: absolute;
z-index: 2;
top: 17px;
left: 18px;
right: 18px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.panel-kicker {
margin-bottom: 5px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.panel h2 {
margin: 0;
font-size: clamp(22px, 2.4vw, 34px);
font-weight: 670;
letter-spacing: -0.04em;
}
.panel-subtitle {
margin-top: 4px;
color: #a9bdce;
font-size: 12px;
}
.badge {
flex: 0 0 auto;
border: 1px solid color-mix(in srgb, var(--accent) 38%, transparent);
border-radius: 999px;
padding: 6px 9px;
background: rgba(7, 17, 29, 0.72);
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.condition {
position: absolute;
z-index: 2;
left: 14px;
right: 14px;
bottom: 14px;
display: grid;
grid-template-columns: auto 1fr;
align-items: center;
gap: 12px;
max-width: 570px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 14px;
padding: 11px 13px;
background: rgba(6, 15, 26, 0.78);
backdrop-filter: blur(8px);
}
.equation {
min-width: 136px;
border-right: 1px solid rgba(255, 255, 255, 0.1);
padding-right: 12px;
color: var(--accent);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 14px;
font-weight: 750;
white-space: nowrap;
}
.condition-copy {
color: #c7d6e2;
font-size: 11px;
line-height: 1.45;
}
.condition-copy strong {
color: var(--text);
font-weight: 650;
}
.inline-key {
display: inline-block;
width: 7px;
height: 7px;
margin: 0 4px 0 7px;
border-radius: 50%;
background: var(--focus);
box-shadow: 0 0 10px rgba(250, 204, 21, 0.8);
}
.inline-key:first-child {
margin-left: 0;
}
.footer {
position: fixed;
z-index: 10;
left: 18px;
right: 18px;
bottom: 14px;
display: flex;
justify-content: space-between;
gap: 18px;
color: #8098ad;
font-size: 10px;
line-height: 1.4;
pointer-events: none;
}
.footer code {
color: #b8cad8;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (max-width: 760px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
h1 {
max-width: 260px;
font-size: 20px;
}
.hint {
display: none;
}
#panels {
inset: 94px 8px 64px;
grid-template-columns: 1fr;
grid-template-rows: repeat(2, minmax(0, 1fr));
gap: 8px;
}
.panel {
border-radius: 15px;
}
.panel-head {
top: 11px;
left: 12px;
right: 12px;
}
.panel h2 {
font-size: 21px;
}
.panel-subtitle {
display: none;
}
.badge {
padding: 5px 7px;
font-size: 9px;
}
.condition {
left: 9px;
right: 9px;
bottom: 9px;
grid-template-columns: 1fr;
gap: 5px;
padding: 8px 10px;
}
.equation {
min-width: 0;
border-right: 0;
padding-right: 0;
font-size: 12px;
}
.condition-copy {
font-size: 10px;
}
.footer {
left: 10px;
right: 10px;
bottom: 10px;
display: block;
font-size: 9px;
}
.footer span {
display: block;
}
}
@media (max-height: 700px) and (min-width: 761px) {
#panels {
inset-top: 74px;
}
.panel-head {
top: 12px;
}
.panel h2 {
font-size: 24px;
}
.condition {
bottom: 9px;
padding: 8px 11px;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive 3D comparison of elliptical and parabolic sections of a cone"
></canvas>
<header class="topbar">
<div>
<span class="eyebrow">Conic sections · interactive comparison</span>
<h1>One cone. Two slicing geometries.</h1>
</div>
<div class="topbar-right">
<span class="hint">Drag to orbit · wheel/pinch to zoom · double-click to reset</span>
<button id="reset" type="button">Reset views</button>
</div>
</header>
<main id="panels">
<section id="ellipsePanel" class="panel" aria-label="Elliptical cone section">
<div class="panel-head">
<div>
<div class="panel-kicker">Closed section</div>
<h2>Ellipse</h2>
<div class="panel-subtitle">Oblique plane cutting one nappe</div>
</div>
<div class="badge">β > γ</div>
</div>
<div class="condition">
<div id="ellipseEquation" class="equation"></div>
<div class="condition-copy">
<strong>Plane is not parallel to a generator.</strong>
The curve closes on itself and the finite cone captures the complete ellipse.
</div>
</div>
</section>
<section id="parabolaPanel" class="panel parabola" aria-label="Parabolic cone section">
<div class="panel-head">
<div>
<div class="panel-kicker">Open section</div>
<h2>Parabola</h2>
<div class="panel-subtitle">Plane parallel to a cone generator</div>
</div>
<div class="badge">β = γ</div>
</div>
<div class="condition">
<div id="parabolaEquation" class="equation"></div>
<div class="condition-copy">
<strong>Plane ∥ generator.</strong>
Dashed orange is the infinite-cone continuation beyond the display solid.
<span class="inline-key"></span>focus and directrix
</div>
</div>
</section>
</main>
<footer class="footer">
<span>
Same right cone:
<code>x² + z² = (0.65y)²</code>
· β is the plane-to-axis angle · γ is the cone half-angle
</span>
<span>Stylized finite solids are used for display; the underlying conics are exact.</span>
</footer>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Shared mathematical model
//
// Right circular cone:
// x^2 + z^2 = s^2 y^2, y >= 0
//
// Plane:
// y = a*x + b
//
// Ellipse:
// |a| < 1/s
//
// Parabola:
// |a| = 1/s, so the plane is parallel to a generator.
// -------------------------------------------------------------------------
const SLOPE = 0.65;
const CONE_HEIGHT = 8;
const PARABOLA_LIMIT = 4.8;
const ellipsePlane = {
a: 0.45,
b: 4.5
};
const parabolaPlane = {
a: 1 / SLOPE,
b: 2.6
};
const coneHalfAngle = THREE.MathUtils.radToDeg(Math.atan(SLOPE));
const ellipseAxisAngle = THREE.MathUtils.radToDeg(
Math.atan2(1, Math.abs(ellipsePlane.a))
);
const parabolaAxisAngle = THREE.MathUtils.radToDeg(
Math.atan2(1, Math.abs(parabolaPlane.a))
);
document.getElementById("ellipseEquation").textContent =
`β = ${ellipseAxisAngle.toFixed(1)}° > γ = ${coneHalfAngle.toFixed(1)}°`;
document.getElementById("parabolaEquation").textContent =
`β = ${parabolaAxisAngle.toFixed(1)}° = γ = ${coneHalfAngle.toFixed(1)}°`;
const canvas = document.getElementById("scene");
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.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.autoClear = false;
const views = [];
// -------------------------------------------------------------------------
// Minimal custom orbit camera: no controls addon is used.
// -------------------------------------------------------------------------
class OrbitState {
constructor(camera, target, radius, theta, phi) {
this.camera = camera;
this.target = target.clone();
this.radius = radius;
this.theta = theta;
this.phi = phi;
this.initial = {
radius,
theta,
phi
};
}
reset() {
this.radius = this.initial.radius;
this.theta = this.initial.theta;
this.phi = this.initial.phi;
}
rotate(dx, dy) {
this.theta -= dx * 0.006;
this.phi = THREE.MathUtils.clamp(
this.phi - dy * 0.005,
0.25,
1.80
);
}
zoom(factor) {
this.radius = THREE.MathUtils.clamp(
this.radius * factor,
9,
31
);
}
update() {
const sinPhi = Math.sin(this.phi);
const cosPhi = Math.cos(this.phi);
this.camera.position.set(
this.target.x + this.radius * sinPhi * Math.sin(this.theta),
this.target.y + this.radius * cosPhi,
this.target.z + this.radius * sinPhi * Math.cos(this.theta)
);
this.camera.lookAt(this.target);
}
}
// -------------------------------------------------------------------------
// Scene helpers
// -------------------------------------------------------------------------
function createScene(accent, background) {
const scene = new THREE.Scene();
const hemisphere = new THREE.HemisphereLight(
0xc7e9ff,
0x132235,
2.0
);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xffffff, 2.7);
key.position.set(-5, 12, 8);
scene.add(key);
const rim = new THREE.DirectionalLight(accent, 1.7);
rim.position.set(5, 4, -7);
scene.add(rim);
const fill = new THREE.PointLight(accent, 12, 25, 2);
fill.position.set(-4, 5, 4);
scene.add(fill);
return {
scene,
background
};
}
function circleLineGeometry(radius, y, segments = 96) {
const points = [];
for (let i = 0; i <= segments; i++) {
const angle = (i / segments) * Math.PI * 2;
points.push(
new THREE.Vector3(
radius * Math.cos(angle),
y,
radius * Math.sin(angle)
)
);
}
return new THREE.BufferGeometry().setFromPoints(points);
}
function addCone(parent, accent, emphasizeGenerator = false) {
const group = new THREE.Group();
const coneGeometry = new THREE.CylinderGeometry(
SLOPE * CONE_HEIGHT,
0,
CONE_HEIGHT,
72,
1,
false
);
// Cylinder top is the broad base; its bottom is the apex at y = 0.
coneGeometry.translate(0, CONE_HEIGHT / 2, 0);
const coneMaterial = new THREE.MeshStandardMaterial({
color: 0x315c72,
roughness: 0.58,
metalness: 0.04,
transparent: true,
opacity: 0.31,
side: THREE.DoubleSide,
depthWrite: false
});
const cone = new THREE.Mesh(coneGeometry, coneMaterial);
cone.renderOrder = 1;
group.add(cone);
// Broad top cap.
const cap = new THREE.Mesh(
new THREE.CircleGeometry(SLOPE * CONE_HEIGHT, 72),
new THREE.MeshStandardMaterial({
color: 0x416f83,
roughness: 0.68,
metalness: 0.03,
transparent: true,
opacity: 0.16,
side: THREE.DoubleSide,
depthWrite: false
})
);
cap.rotation.x = -Math.PI / 2;
cap.position.y = CONE_HEIGHT;
cap.renderOrder = 2;
group.add(cap);
// Circular construction rings.
for (const fraction of [0.22, 0.44, 0.66, 0.84, 1]) {
const y = CONE_HEIGHT * fraction;
const ring = new THREE.Line(
circleLineGeometry(SLOPE * y, y),
new THREE.LineBasicMaterial({
color: 0x93c9d7,
transparent: true,
opacity: fraction === 1 ? 0.38 : 0.15,
depthWrite: false
})
);
ring.renderOrder = 2;
group.add(ring);
}
// Cone generators.
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const isHighlighted =
emphasizeGenerator && Math.abs(angle) < 0.0001;
const points = [
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(
SLOPE * CONE_HEIGHT * Math.cos(angle),
CONE_HEIGHT,
SLOPE * CONE_HEIGHT * Math.sin(angle)
)
];
const generator = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color: isHighlighted ? 0x75f0d2 : 0x9ac8d5,
transparent: true,
opacity: isHighlighted ? 0.95 : 0.12,
depthWrite: false
})
);
generator.renderOrder = 3;
group.add(generator);
}
// Dashed cone axis.
const axis = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -0.25, 0),
new THREE.Vector3(0, CONE_HEIGHT + 1.0, 0)
]),
new THREE.LineDashedMaterial({
color: 0xb8d0dc,
transparent: true,
opacity: 0.22,
dashSize: 0.14,
gapSize: 0.11,
depthWrite: false
})
);
axis.computeLineDistances();
axis.renderOrder = 2;
group.add(axis);
// Apex marker.
const apex = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 16, 12),
new THREE.MeshBasicMaterial({
color: accent,
toneMapped: false
})
);
apex.renderOrder = 6;
group.add(apex);
parent.add(group);
return group;
}
function addPlane(
parent,
a,
b,
xMin,
xMax,
zMin,
zMax,
accent
) {
const q = Math.sqrt(1 + a * a);
// Local X and Y are in the plane; local Y is its unit normal.
const localX = new THREE.Vector3(1, a, 0).normalize();
const localY = new THREE.Vector3(-a, 1, 0).normalize();
const localZ = new THREE.Vector3(0, 0, 1);
const basis = new THREE.Matrix4().makeBasis(
localX,
localY,
localZ
);
const quaternion = new THREE.Quaternion().setFromRotationMatrix(basis);
const xMiddle = (xMin + xMax) / 2;
const center = new THREE.Vector3(
xMiddle,
a * xMiddle + b,
0
);
// A thin slab reads more clearly as a solid slicing plane.
const slab = new THREE.Mesh(
new THREE.BoxGeometry(
q * (xMax - xMin),
0.085,
zMax - zMin
),
new THREE.MeshStandardMaterial({
color: accent,
roughness: 0.64,
metalness: 0.02,
transparent: true,
opacity: 0.13,
side: THREE.DoubleSide,
depthWrite: false
})
);
slab.position.copy(center);
slab.quaternion.copy(quaternion);
slab.renderOrder = 2;
parent.add(slab);
const slabEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(slab.geometry),
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.43,
depthWrite: false
})
);
slabEdges.position.copy(center);
slabEdges.quaternion.copy(quaternion);
slabEdges.renderOrder = 4;
parent.add(slabEdges);
// A sparse grid makes the plane's orientation visible in 3D.
const gridPositions = [];
for (let x = xMin; x <= xMax + 0.001; x += 1) {
gridPositions.push(
x, a * x + b, zMin,
x, a * x + b, zMax
);
}
for (let z = zMin; z <= zMax + 0.001; z += 1) {
gridPositions.push(
xMin, a * xMin + b, z,
xMax, a * xMax + b, z
);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.15,
depthWrite: false
})
);
grid.renderOrder = 3;
parent.add(grid);
}
// -------------------------------------------------------------------------
// Exact analytic section curves
// -------------------------------------------------------------------------
class EllipseSectionCurve extends THREE.Curve {
constructor(a, b) {
super();
this.a = a;
this.b = b;
const A = 1 - SLOPE * SLOPE * a * a;
this.A = A;
this.x0 =
(SLOPE * SLOPE * a * b) / A;
this.rx =
(SLOPE * b) / A;
this.rz =
(SLOPE * b) / Math.sqrt(A);
}
getPoint(t, target = new THREE.Vector3()) {
const angle = t * Math.PI * 2;
const x = this.x0 + this.rx * Math.cos(angle);
const z = this.rz * Math.sin(angle);
const y = this.a * x + this.b;
return target.set(x, y, z);
}
}
class ParabolaSectionCurve extends THREE.Curve {
constructor(b, zMin, zMax) {
super();
this.b = b;
this.zMin = zMin;
this.zMax = zMax;
}
getPoint(t, target = new THREE.Vector3()) {
const z = THREE.MathUtils.lerp(this.zMin, this.zMax, t);
const y =
this.b / 2 +
(z * z) / (2 * SLOPE * SLOPE * this.b);
const x =
-SLOPE * this.b / 2 +
(z * z) / (2 * SLOPE * this.b);
return target.set(x, y, z);
}
}
function addCurveGlow(parent, curve, accent, segments, closed = false) {
const glow = new THREE.Mesh(
new THREE.TubeGeometry(curve, segments, 0.095, 8, closed),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.13,
depthWrite: false,
toneMapped: false
})
);
glow.renderOrder = 5;
parent.add(glow);
const core = new THREE.Mesh(
new THREE.TubeGeometry(curve, segments, 0.042, 8, closed),
new THREE.MeshBasicMaterial({
color: accent,
toneMapped: false
})
);
core.renderOrder = 6;
parent.add(core);
return core;
}
function addPointMarkers(parent, curve, count, accent) {
const geometry = new THREE.SphereGeometry(0.064, 14, 10);
const material = new THREE.MeshBasicMaterial({
color: accent,
toneMapped: false
});
for (let i = 0; i < count; i++) {
const t = count === 1 ? 0.5 : i / (count - 1);
const marker = new THREE.Mesh(geometry, material);
marker.position.copy(curve.getPoint(t));
marker.renderOrder = 7;
parent.add(marker);
}
}
function addEllipseSection(parent) {
const curve = new EllipseSectionCurve(
ellipsePlane.a,
ellipsePlane.b
);
addCurveGlow(parent, curve, 0x5eead4, 240, true);
addPointMarkers(parent, curve, 9, 0xa7fff3);
}
function addParabolaSection(parent) {
const b = parabolaPlane.b;
// Intersection of the finite display cone with the plane.
const zAtConeTop = Math.sqrt(
2 *
SLOPE *
SLOPE *
b *
(CONE_HEIGHT - b / 2)
);
const solidCurve = new ParabolaSectionCurve(
b,
-zAtConeTop,
zAtConeTop
);
addCurveGlow(parent, solidCurve, 0xfb923c, 220, false);
addPointMarkers(parent, solidCurve, 7, 0xffc38d);
// Continue the mathematical curve beyond the finite solid.
const continuationSegments = [
[-PARABOLA_LIMIT, -zAtConeTop],
[zAtConeTop, PARABOLA_LIMIT]
];
for (const [zMin, zMax] of continuationSegments) {
const points = [];
const count = 90;
for (let i = 0; i <= count; i++) {
const z = THREE.MathUtils.lerp(zMin, zMax, i / count);
const y =
b / 2 +
(z * z) / (2 * SLOPE * SLOPE * b);
const x =
-SLOPE * b / 2 +
(z * z) / (2 * SLOPE * b);
points.push(new THREE.Vector3(x, y, z));
}
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineDashedMaterial({
color: 0xfb923c,
transparent: true,
opacity: 0.8,
dashSize: 0.17,
gapSize: 0.11,
depthWrite: false,
toneMapped: false
})
);
line.computeLineDistances();
line.renderOrder = 5;
parent.add(line);
}
// Parabola focus and directrix in the slicing plane.
//
// In the plane's local axis direction:
// z^2 = 4*p*u
//
// The directrix is parallel to z and lies one focal length
// behind the vertex.
const q = Math.sqrt(1 + SLOPE * SLOPE);
const p = (SLOPE * SLOPE * b) / (2 * q);
const vertex = new THREE.Vector3(
-SLOPE * b / 2,
b / 2,
0
);
const axisDirection = new THREE.Vector3(
SLOPE,
1,
0
).normalize();
const focus = vertex.clone().addScaledVector(axisDirection, p);
const directrixCenter =
vertex.clone().addScaledVector(axisDirection, -p);
const focusMaterial = new THREE.MeshBasicMaterial({
color: 0xfacc15,
toneMapped: false
});
const focusMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.09, 18, 12),
focusMaterial
);
focusMarker.position.copy(focus);
focusMarker.renderOrder = 8;
parent.add(focusMarker);
const focusGlow = new THREE.Mesh(
new THREE.SphereGeometry(0.18, 18, 12),
new THREE.MeshBasicMaterial({
color: 0xfacc15,
transparent: true,
opacity: 0.16,
depthWrite: false,
toneMapped: false
})
);
focusGlow.position.copy(focus);
focusGlow.renderOrder = 7;
parent.add(focusGlow);
const directrix = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
directrixCenter.clone().add(new THREE.Vector3(0, 0, -PARABOLA_LIMIT)),
directrixCenter.clone().add(new THREE.Vector3(0, 0, PARABOLA_LIMIT))
]),
new THREE.LineDashedMaterial({
color: 0xfacc15,
transparent: true,
opacity: 0.72,
dashSize: 0.13,
gapSize: 0.10,
depthWrite: false,
toneMapped: false
})
);
directrix.computeLineDistances();
directrix.renderOrder = 5;
parent.add(directrix);
}
// -------------------------------------------------------------------------
// View construction
// -------------------------------------------------------------------------
function createEllipseView() {
const panel = document.getElementById("ellipsePanel");
const { scene, background } = createScene(0x5eead4, 0x081522);
addCone(scene, 0x5eead4, false);
addPlane(
scene,
ellipsePlane.a,
ellipsePlane.b,
-2.8,
4.7,
-3.8,
3.8,
0x5eead4
);
addEllipseSection(scene);
const camera = new THREE.PerspectiveCamera(
42,
1,
0.1,
100
);
const orbit = new OrbitState(
camera,
new THREE.Vector3(0.55, 4.15, 0),
16.5,
-1.15,
1.06
);
return {
panel,
scene,
camera,
orbit,
background
};
}
function createParabolaView() {
const panel = document.getElementById("parabolaPanel");
const { scene, background } = createScene(0xfb923c, 0x10141e);
addCone(scene, 0xfb923c, true);
addPlane(
scene,
parabolaPlane.a,
parabolaPlane.b,
-2.1,
6.4,
-5.0,
5.0,
0xfb923c
);
addParabolaSection(scene);
const camera = new THREE.PerspectiveCamera(
42,
1,
0.1,
100
);
const orbit = new OrbitState(
camera,
new THREE.Vector3(1.25, 5.35, 0),
18.5,
-1.15,
1.08
);
return {
panel,
scene,
camera,
orbit,
background
};
}
views.push(createEllipseView());
views.push(createParabolaView());
// -------------------------------------------------------------------------
// Pointer, wheel, touch, and reset interaction
// -------------------------------------------------------------------------
const pointers = new Map();
let pinchStartDistance = 0;
let pinchStartRadius = 0;
function viewAt(clientX, clientY) {
for (const view of views) {
const rect = view.panel.getBoundingClientRect();
if (
clientX >= rect.left &&
clientX <= rect.right &&
clientY >= rect.top &&
clientY <= rect.bottom
) {
return view;
}
}
return null;
}
function currentPinch() {
const entries = [...pointers.values()];
if (entries.length < 2) {
return null;
}
const a = entries[0];
const b = entries[1];
return {
distance: Math.hypot(a.x - b.x, a.y - b.y),
view: a.view === b.view ? a.view : null
};
}
canvas.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) {
return;
}
const view = viewAt(event.clientX, event.clientY);
if (!view) {
return;
}
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
view
});
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
if (pointers.size === 2) {
const pinch = currentPinch();
if (pinch && pinch.view) {
pinchStartDistance = pinch.distance;
pinchStartRadius = pinch.view.orbit.radius;
}
}
});
canvas.addEventListener("pointermove", (event) => {
const pointer = pointers.get(event.pointerId);
if (!pointer) {
return;
}
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (pointers.size === 1) {
pointer.view.orbit.rotate(dx, dy);
} else if (pointers.size === 2) {
const pinch = currentPinch();
if (
pinch &&
pinch.view &&
pinchStartDistance > 0
) {
pinch.view.orbit.radius = THREE.MathUtils.clamp(
pinchStartRadius *
(pinchStartDistance / Math.max(pinch.distance, 0.001)),
9,
31
);
}
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
pinchStartDistance = 0;
pinchStartRadius = 0;
}
if (pointers.size === 0) {
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("lostpointercapture", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
const view = viewAt(event.clientX, event.clientY);
if (!view) {
return;
}
event.preventDefault();
const factor = Math.exp(event.deltaY * 0.001);
view.orbit.zoom(factor);
},
{ passive: false }
);
canvas.addEventListener("dblclick", (event) => {
const view = viewAt(event.clientX, event.clientY);
if (view) {
view.orbit.reset();
}
});
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
document.getElementById("reset").addEventListener("click", () => {
for (const view of views) {
view.orbit.reset();
}
});
// -------------------------------------------------------------------------
// Rendering into two scissored viewports
// -------------------------------------------------------------------------
function resizeRenderer() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
}
window.addEventListener("resize", resizeRenderer);
resizeRenderer();
function renderView(view) {
const rect = view.panel.getBoundingClientRect();
if (rect.width <= 0 || rect.height <= 0) {
return;
}
const x = Math.round(rect.left);
const y = Math.round(window.innerHeight - rect.bottom);
const width = Math.round(rect.width);
const height = Math.round(rect.height);
view.camera.aspect = width / height;
view.camera.updateProjectionMatrix();
view.orbit.update();
renderer.setViewport(x, y, width, height);
renderer.setScissor(x, y, width, height);
renderer.setClearColor(view.background, 1);
renderer.clear(true, true, true);
renderer.render(view.scene, view.camera);
}
function animate() {
requestAnimationFrame(animate);
renderer.setScissorTest(false);
renderer.setViewport(0, 0, window.innerWidth, window.innerHeight);
renderer.setClearColor(0x07111d, 1);
renderer.clear(true, true, true);
renderer.setScissorTest(true);
for (const view of views) {
renderView(view);
}
}
animate();
</script>
</body>
</html>
```