Prompt specification
Build or customize the Three.js 3D scene "Linear versus nonlinear device explorer" 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: Linear versus nonlinear device explorer
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/linear-versus-nonlinear-device-explorer-x1660
Scene Description Prompt:
Compare an ideal resistor's straight voltage-current relationship with a clearly defined nonlinear toy device; show why one constant resistance does not describe every component. Build this with Three.js from cdnjs as a low-poly scene in one self-contained HTML file.
Working Three.js Starter (linear-versus-nonlinear-device-explorer-x1660):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Interactive I–V Lab: Resistor vs. Nonlinear Toy Device</title>
<style>
:root {
color-scheme: dark;
--bg: #07121c;
--panel: #0d1c29;
--panel-2: #102534;
--line: #263f51;
--text: #e9f3f7;
--muted: #91a9b8;
--ideal: #ffc857;
--toy: #4ee1c1;
--accent: #76a9ff;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button {
font: inherit;
}
.app {
height: 100vh;
display: grid;
grid-template-rows: auto minmax(0, 1fr);
}
.header {
min-height: 92px;
padding: 18px 24px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
border-bottom: 1px solid var(--line);
background: rgba(7, 18, 28, 0.94);
position: relative;
z-index: 2;
}
.heading {
display: flex;
align-items: center;
gap: 16px;
min-width: 0;
}
.mark {
width: 45px;
height: 45px;
flex: 0 0 auto;
border: 1px solid #36536a;
background: linear-gradient(145deg, #17364a, #0b1b28);
clip-path: polygon(0 0, 72% 0, 100% 28%, 100% 100%, 28% 100%, 0 72%);
display: grid;
place-items: center;
color: var(--accent);
font-size: 22px;
font-weight: 800;
}
.eyebrow {
margin: 0 0 3px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(19px, 2.1vw, 28px);
line-height: 1.12;
letter-spacing: -0.025em;
}
.subtitle {
margin: 5px 0 0;
color: var(--muted);
font-size: 13px;
}
.header-actions {
display: flex;
align-items: center;
gap: 12px;
flex: 0 0 auto;
}
.view-button {
border: 1px solid #345169;
border-radius: 9px;
padding: 9px 13px;
color: var(--text);
background: #102536;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease;
}
.view-button:hover {
background: #173247;
border-color: #527894;
}
.view-button:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
main {
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
}
#stage {
min-width: 0;
min-height: 0;
position: relative;
overflow: hidden;
background:
radial-gradient(circle at 50% 38%, rgba(30, 67, 89, 0.28), transparent 45%),
var(--bg);
}
#stage canvas {
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#stage canvas:active {
cursor: grabbing;
}
#stage canvas:focus-visible {
outline: 2px solid var(--accent);
outline-offset: -3px;
}
.legend {
position: absolute;
top: 17px;
left: 18px;
z-index: 3;
display: flex;
flex-wrap: wrap;
gap: 8px;
pointer-events: none;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(70, 100, 122, 0.55);
border-radius: 999px;
background: rgba(7, 18, 28, 0.76);
backdrop-filter: blur(8px);
color: #bfd0da;
font-size: 11px;
font-weight: 700;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.ideal-color {
color: var(--ideal);
}
.toy-color {
color: var(--toy);
}
.hint {
position: absolute;
left: 50%;
bottom: 15px;
transform: translateX(-50%);
z-index: 3;
padding: 8px 13px;
border: 1px solid rgba(65, 94, 115, 0.65);
border-radius: 999px;
background: rgba(7, 18, 28, 0.8);
backdrop-filter: blur(8px);
color: var(--muted);
font-size: 11px;
white-space: nowrap;
pointer-events: none;
}
aside {
min-height: 0;
overflow-y: auto;
padding: 18px;
border-left: 1px solid var(--line);
background: #091621;
scrollbar-color: #345269 #091621;
}
.card {
position: relative;
overflow: hidden;
margin-bottom: 14px;
padding: 18px;
border: 1px solid var(--line);
border-radius: 15px;
background:
linear-gradient(145deg, rgba(20, 42, 57, 0.82), rgba(11, 27, 39, 0.94));
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
}
.card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 4px;
background: var(--card-accent, var(--accent));
}
.card.ideal {
--card-accent: var(--ideal);
}
.card.toy {
--card-accent: var(--toy);
}
.card.conclusion {
--card-accent: var(--accent);
}
.card-label {
margin: 0 0 6px;
color: var(--card-accent);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.card h2 {
margin: 0 0 9px;
font-size: 19px;
letter-spacing: -0.02em;
}
.card p {
margin: 9px 0;
color: #abc0cc;
font-size: 13px;
line-height: 1.55;
}
.formula {
margin: 14px 0;
padding: 12px 13px;
border: 1px solid rgba(78, 111, 135, 0.45);
border-radius: 10px;
background: rgba(3, 11, 18, 0.54);
color: #f2f8fb;
font-family:
"SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 14px;
line-height: 1.6;
overflow-wrap: anywhere;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.metric {
padding: 10px;
border: 1px solid rgba(66, 98, 121, 0.4);
border-radius: 9px;
background: rgba(7, 18, 28, 0.45);
}
.metric strong {
display: block;
color: var(--card-accent);
font-size: 17px;
}
.metric span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
line-height: 1.3;
}
.small-note {
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.key-idea {
padding: 11px 12px;
border-left: 3px solid var(--accent);
background: rgba(118, 169, 255, 0.07);
color: #c6d9ea;
font-size: 13px;
line-height: 1.55;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100vh;
}
main {
grid-template-columns: 1fr;
}
#stage {
height: 64vh;
min-height: 470px;
}
aside {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
.header {
padding: 15px 17px;
}
.subtitle {
display: none;
}
}
@media (max-width: 540px) {
.header {
min-height: 78px;
}
.mark {
width: 38px;
height: 38px;
}
.header-actions .view-button {
padding: 8px 10px;
font-size: 12px;
}
#stage {
min-height: 430px;
}
.hint {
max-width: calc(100% - 24px);
overflow: hidden;
text-overflow: ellipsis;
}
aside {
padding: 13px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="header">
<div class="heading">
<div class="mark" aria-hidden="true">I–V</div>
<div>
<p class="eyebrow">Interactive characteristics lab</p>
<h1>One straight line versus a nonlinear device</h1>
<p class="subtitle">
Compare how voltage and current reveal whether a component has one constant resistance.
</p>
</div>
</div>
<div class="header-actions">
<button id="resetView" class="view-button" type="button">Reset view</button>
</div>
</header>
<main>
<section
id="stage"
aria-label="Interactive 3D comparison of ideal resistor and nonlinear toy device current-voltage graphs"
>
<div class="legend" aria-hidden="true">
<div class="legend-item ideal-color">
<span class="dot"></span>
Ideal resistor: I = V / 2
</div>
<div class="legend-item toy-color">
<span class="dot"></span>
Toy device: I = V/2 + V³/32
</div>
</div>
<div class="hint">
Drag to orbit · Scroll to zoom · Double-click to reset
</div>
</section>
<aside aria-label="Explanation of the voltage-current comparison">
<section class="card ideal">
<p class="card-label">Straight line · constant ratio</p>
<h2>Ideal resistor</h2>
<p>
By definition, an ideal resistor obeys
<strong>V = IR</strong>. Here its fixed resistance is
<strong>2 Ω</strong>.
</p>
<div class="formula">
I(V) = V / R<br>
I(V) = V / 2
</div>
<div class="metrics">
<div class="metric">
<strong>2.00 Ω</strong>
<span>Resistance at every point</span>
</div>
<div class="metric">
<strong>0.50 A/V</strong>
<span>Constant slope ΔI/ΔV</span>
</div>
</div>
<p>
The graph is straight through the origin, and the ratio
<strong>V/I</strong> never changes.
</p>
</section>
<section class="card toy">
<p class="card-label">Curved line · operating-point dependent</p>
<h2>Defined nonlinear toy device</h2>
<p>
This is a memoryless, nonlinear one-port. For
<strong>0 ≤ V ≤ 4 V</strong>, it produces current
<strong>I in amperes</strong> according to:
</p>
<div class="formula">
I(V) = V/2 + V³/32
</div>
<div class="metrics">
<div class="metric">
<strong>1.60 Ω</strong>
<span>V/I at V = 2 V, I = 1.25 A</span>
</div>
<div class="metric">
<strong>1.00 Ω</strong>
<span>V/I at V = 4 V, I = 4 A</span>
</div>
</div>
<p>
The effective resistance changes with the operating point.
Even the local slope changes because
<strong>dI/dV = 1/2 + 3V²/32</strong>.
</p>
<p class="small-note">
“Toy device” means this response is mathematically defined,
not a claim that every real nonlinear component follows this
exact cubic law.
</p>
</section>
<section class="card conclusion">
<p class="card-label">Conclusion</p>
<h2>Why one constant R is not universal</h2>
<div class="key-idea">
A single constant resistance describes the ideal resistor
because <strong>V/I</strong> is constant. The toy device is
not one fixed resistor: its equivalent resistance depends
on the applied voltage.
</div>
<p>
Real diodes, LEDs, transistors, and other nonlinear devices
likewise generally require a voltage-current law or an
operating-point model rather than one universal resistance.
</p>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const resetButton = document.getElementById("resetView");
const COLORS = {
background: 0x07121c,
ground: 0x081722,
board: 0x102735,
boardEdge: 0x315269,
grid: 0x274252,
axis: 0x91adbd,
ideal: 0xffc857,
toy: 0x4ee1c1,
text: 0xd7e8f1,
muted: 0x7894a6
};
const V_MAX = 4;
const I_MAX = 4;
const R_IDEAL = 2;
// ------------------------------------------------------------
// Renderer, camera, and lighting
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(COLORS.background, 1);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"3D graph. Drag to orbit, scroll to zoom, and use arrow keys to rotate."
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.background);
scene.fog = new THREE.Fog(COLORS.background, 17, 36);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const hemisphere = new THREE.HemisphereLight(
0xb9ddff,
0x081019,
2.1
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(-5, 11, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4b9cff, 1.6);
rimLight.position.set(8, 5, -7);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xffc857, 10, 14, 2);
warmFill.position.set(-5, 4, 5);
scene.add(warmFill);
const toyFill = new THREE.PointLight(0x4ee1c1, 10, 14, 2);
toyFill.position.set(5, 4, 5);
scene.add(toyFill);
// ------------------------------------------------------------
// Low-poly environment
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(42, 32),
new THREE.MeshStandardMaterial({
color: COLORS.ground,
roughness: 0.96,
metalness: 0.02,
flatShading: true
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const floorGrid = new THREE.GridHelper(
30,
30,
0x315267,
0x17303f
);
floorGrid.position.y = 0.008;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.42;
scene.add(floorGrid);
function makeBox(
width,
height,
depth,
color,
options = {}
) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.75,
metalness: options.metalness ?? 0.08,
flatShading: true,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
return mesh;
}
function makeLabel(
text,
worldWidth,
color = "#d7e8f1",
options = {}
) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.textAlign = "center";
context.textBaseline = "middle";
context.font = `${options.weight ?? 700} ${
options.fontSize ?? 72
}px ui-sans-serif, system-ui, sans-serif`;
context.fillStyle = color;
context.fillText(text, 512, 130, 980);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(
worldWidth,
worldWidth * canvas.height / canvas.width,
1
);
sprite.renderOrder = 20;
return sprite;
}
function addSegment(
parent,
start,
end,
radius,
color,
options = {}
) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const material = new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.42,
metalness: options.metalness ?? 0.12,
emissive: options.emissive ?? color,
emissiveIntensity: options.emissiveIntensity ?? 0.2,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
flatShading: true
});
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
options.radialSegments ?? 6,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = options.castShadow ?? false;
parent.add(mesh);
return mesh;
}
function addPolyline(parent, points, color, radius = 0.045) {
for (let i = 0; i < points.length - 1; i++) {
addSegment(
parent,
points[i],
points[i + 1],
radius,
color,
{
emissiveIntensity: 0.38,
radialSegments: 6
}
);
}
}
function addMarker(parent, position, color, scale = 1) {
const marker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.12 * scale, 0),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.75,
roughness: 0.3,
flatShading: true
})
);
marker.position.copy(position);
marker.castShadow = true;
parent.add(marker);
return marker;
}
// ------------------------------------------------------------
// Graph construction
// ------------------------------------------------------------
const GRAPH = {
originX: -1.65,
originY: 1.0,
width: 3.3,
height: 3.3,
gridZ: 0.055,
axisZ: 0.11,
curveZ: 0.22
};
function graphPoint(voltage, current, z = GRAPH.curveZ) {
return new THREE.Vector3(
GRAPH.originX + GRAPH.width * voltage / V_MAX,
GRAPH.originY + GRAPH.height * current / I_MAX,
z
);
}
function createPanel({
x,
title,
accent,
kind
}) {
const panel = new THREE.Group();
panel.position.x = x;
scene.add(panel);
// Pedestal and vertical support.
const pedestal = makeBox(4.65, 0.48, 1.1, 0x0d2231);
pedestal.position.set(0, 0.24, 0);
panel.add(pedestal);
const support = makeBox(3.7, 0.26, 0.48, 0x122d3e);
support.position.set(0, 0.58, -0.05);
panel.add(support);
const pedestalStripe = makeBox(
3.8,
0.045,
0.045,
accent,
{
emissive: accent,
emissiveIntensity: 0.55,
castShadow: false
}
);
pedestalStripe.position.set(0, 0.24, 0.575);
panel.add(pedestalStripe);
// Main board.
const board = makeBox(4.35, 4.35, 0.14, COLORS.board, {
roughness: 0.88,
metalness: 0.04
});
board.position.set(0, 2.65, -0.08);
panel.add(board);
// Low-poly frame.
const frameColor = COLORS.boardEdge;
const topFrame = makeBox(4.45, 0.09, 0.19, frameColor);
topFrame.position.set(0, 4.82, -0.01);
panel.add(topFrame);
const bottomFrame = makeBox(4.45, 0.09, 0.19, frameColor);
bottomFrame.position.set(0, 0.48, -0.01);
panel.add(bottomFrame);
const leftFrame = makeBox(0.09, 4.35, 0.19, frameColor);
leftFrame.position.set(-2.18, 2.65, -0.01);
panel.add(leftFrame);
const rightFrame = makeBox(0.09, 4.35, 0.19, frameColor);
rightFrame.position.set(2.18, 2.65, -0.01);
panel.add(rightFrame);
// Title and accent underline.
const titleSprite = makeLabel(title, 2.8, `#${accent.toString(16).padStart(6, "0")}`, {
fontSize: 78,
weight: 850
});
titleSprite.position.set(0, 4.55, 0.18);
panel.add(titleSprite);
const titleRule = makeBox(
2.8,
0.045,
0.035,
accent,
{
emissive: accent,
emissiveIntensity: 0.5,
castShadow: false
}
);
titleRule.position.set(0, 4.28, 0.08);
panel.add(titleRule);
// Graph grid.
for (let i = 0; i <= 4; i++) {
const x = GRAPH.originX + GRAPH.width * i / 4;
const y = GRAPH.originY + GRAPH.height * i / 4;
const vertical = makeBox(
0.014,
GRAPH.height,
0.018,
COLORS.grid,
{
transparent: true,
opacity: 0.72,
castShadow: false,
receiveShadow: false
}
);
vertical.position.set(x, GRAPH.originY + GRAPH.height / 2, GRAPH.gridZ);
panel.add(vertical);
const horizontal = makeBox(
GRAPH.width,
0.014,
0.018,
COLORS.grid,
{
transparent: true,
opacity: 0.72,
castShadow: false,
receiveShadow: false
}
);
horizontal.position.set(
GRAPH.originX + GRAPH.width / 2,
y,
GRAPH.gridZ
);
panel.add(horizontal);
}
// Axes.
const axisMaterial = {
roughness: 0.45,
metalness: 0.1,
emissiveIntensity: 0.12
};
addSegment(
panel,
new THREE.Vector3(
GRAPH.originX,
GRAPH.originY,
GRAPH.axisZ
),
new THREE.Vector3(
GRAPH.originX + GRAPH.width,
GRAPH.originY,
GRAPH.axisZ
),
0.025,
COLORS.axis,
axisMaterial
);
addSegment(
panel,
new THREE.Vector3(
GRAPH.originX,
GRAPH.originY,
GRAPH.axisZ
),
new THREE.Vector3(
GRAPH.originX,
GRAPH.originY + GRAPH.height,
GRAPH.axisZ
),
0.025,
COLORS.axis,
axisMaterial
);
// Tick marks.
for (let i = 0; i <= 4; i++) {
const x = GRAPH.originX + GRAPH.width * i / 4;
const y = GRAPH.originY + GRAPH.height * i / 4;
const xTick = makeBox(
0.035,
0.13,
0.035,
COLORS.axis,
{
castShadow: false,
emissive: COLORS.axis,
emissiveIntensity: 0.08
}
);
xTick.position.set(x, GRAPH.originY - 0.045, GRAPH.axisZ + 0.01);
panel.add(xTick);
const yTick = makeBox(
0.13,
0.035,
0.035,
COLORS.axis,
{
castShadow: false,
emissive: COLORS.axis,
emissiveIntensity: 0.08
}
);
yTick.position.set(GRAPH.originX - 0.045, y, GRAPH.axisZ + 0.01);
panel.add(yTick);
}
// Axis tick labels.
for (let i = 0; i <= 4; i++) {
const x = GRAPH.originX + GRAPH.width * i / 4;
const y = GRAPH.originY + GRAPH.height * i / 4;
const voltageLabel = makeLabel(`${i} V`, 0.58, "#8da8b8", {
fontSize: 65
});
voltageLabel.position.set(x, GRAPH.originY - 0.26, 0.2);
panel.add(voltageLabel);
const currentLabel = makeLabel(`${i} A`, 0.58, "#8da8b8", {
fontSize: 65
});
currentLabel.position.set(GRAPH.originX - 0.29, y, 0.2);
panel.add(currentLabel);
}
const voltageTitle = makeLabel("VOLTAGE V", 1.75, "#b7ceda", {
fontSize: 62,
weight: 750
});
voltageTitle.position.set(0, 0.68, 0.22);
panel.add(voltageTitle);
const currentTitle = makeLabel("CURRENT A", 1.75, "#b7ceda", {
fontSize: 62,
weight: 750
});
currentTitle.position.set(-1.95, 2.65, 0.22);
currentTitle.rotation.z = -Math.PI / 2;
panel.add(currentTitle);
if (kind === "ideal") {
const points = [];
for (let i = 0; i <= 20; i++) {
const voltage = V_MAX * i / 20;
const current = voltage / R_IDEAL;
points.push(graphPoint(voltage, current));
}
addPolyline(panel, points, COLORS.ideal, 0.052);
addMarker(panel, graphPoint(0, 0), COLORS.ideal, 0.9);
addMarker(panel, graphPoint(2, 1), COLORS.ideal, 1.0);
addMarker(panel, graphPoint(4, 2), COLORS.ideal, 1.0);
const resistanceLabel = makeLabel(
"R = 2 Ω",
1.15,
"#ffc857",
{
fontSize: 78,
weight: 850
}
);
resistanceLabel.position.set(0.5, 1.93, 0.29);
panel.add(resistanceLabel);
const endpointLabel = makeLabel(
"4 V → 2 A",
1.45,
"#ffe1a1",
{
fontSize: 62
}
);
endpointLabel.position.set(1.05, 2.52, 0.29);
panel.add(endpointLabel);
} else {
const points = [];
for (let i = 0; i <= 48; i++) {
const voltage = V_MAX * i / 48;
const current = voltage / 2 + Math.pow(voltage, 3) / 32;
points.push(graphPoint(voltage, current));
}
addPolyline(panel, points, COLORS.toy, 0.052);
addMarker(panel, graphPoint(0, 0), COLORS.toy, 0.9);
addMarker(panel, graphPoint(2, 1.25), COLORS.toy, 1.0);
addMarker(panel, graphPoint(4, 4), COLORS.toy, 1.0);
const firstLabel = makeLabel(
"Rₑq = 1.60 Ω",
1.65,
"#7ff0da",
{
fontSize: 67,
weight: 850
}
);
firstLabel.position.set(-0.55, 2.14, 0.29);
panel.add(firstLabel);
const secondLabel = makeLabel(
"Rₑq = 1.00 Ω",
1.65,
"#7ff0da",
{
fontSize: 67,
weight: 850
}
);
secondLabel.position.set(0.72, 3.77, 0.29);
panel.add(secondLabel);
const curveNote = makeLabel(
"changing slope",
1.5,
"#a4c8c0",
{
fontSize: 61,
weight: 650
}
);
curveNote.position.set(0.85, 2.85, 0.29);
curveNote.rotation.z = -0.48;
panel.add(curveNote);
}
return panel;
}
createPanel({
x: -2.45,
title: "IDEAL RESISTOR",
accent: COLORS.ideal,
kind: "ideal"
});
createPanel({
x: 2.45,
title: "NONLINEAR TOY",
accent: COLORS.toy,
kind: "toy"
});
// Small low-poly posts in the background add depth without clutter.
const postMaterial = new THREE.MeshStandardMaterial({
color: 0x102634,
roughness: 0.9,
flatShading: true
});
[
[-7.4, 0.65, -5.2, 0.42],
[7.6, 0.9, -5.8, 0.5],
[-8.5, 0.4, 3.6, 0.34],
[8.4, 0.5, 3.1, 0.38]
].forEach(([x, y, z, scale]) => {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(scale, scale * 1.3, y * 2, 6),
postMaterial
);
post.position.set(x, y, z);
post.castShadow = true;
post.receiveShadow = true;
scene.add(post);
});
// ------------------------------------------------------------
// Direct camera controls — no external control module
// ------------------------------------------------------------
const orbit = {
theta: 0.02,
phi: 1.28,
radius: 12.6,
target: new THREE.Vector3(0, 2.55, 0),
userMoved: false
};
function fitRadius() {
const aspect = stage.clientWidth / Math.max(1, stage.clientHeight);
return THREE.MathUtils.clamp(13.7 / Math.max(aspect, 0.45), 12.2, 22);
}
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * cosPhi,
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.theta = 0.02;
orbit.phi = 1.28;
orbit.radius = fitRadius();
orbit.userMoved = false;
updateCamera();
}
const canvas = renderer.domElement;
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
orbit.userMoved = true;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.62,
1.52
);
updateCamera();
});
function endDrag(event) {
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();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.012,
9,
22
);
orbit.userMoved = true;
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
resetButton.addEventListener("click", resetCamera);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
orbit.theta += 0.1;
break;
case "ArrowRight":
orbit.theta -= 0.1;
break;
case "ArrowUp":
orbit.phi = THREE.MathUtils.clamp(orbit.phi - 0.08, 0.62, 1.52);
break;
case "ArrowDown":
orbit.phi = THREE.MathUtils.clamp(orbit.phi + 0.08, 0.62, 1.52);
break;
case "+":
case "=":
orbit.radius = Math.max(9, orbit.radius - 0.5);
break;
case "-":
case "_":
orbit.radius = Math.min(22, orbit.radius + 0.5);
break;
case "0":
resetCamera();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
orbit.userMoved = true;
updateCamera();
}
});
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
if (!orbit.userMoved) {
orbit.radius = fitRadius();
updateCamera();
}
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
resetCamera();
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```