Prompt specification
Build or customize the Three.js 3D scene "Panel powering a small daytime fan" 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: Panel powering a small daytime fan
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/panel-powering-a-small-daytime-fan-x2534
Scene Description Prompt:
A solar-panel-and-fan cutaway changes fan speed with a movable light under a stated simplified relationship. One HTML file using Three.js from cdnjs.
Working Three.js Starter (panel-powering-a-small-daytime-fan-x2534):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Light → Solar → Fan</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 23, 39, 0.82);
--border: rgba(160, 205, 255, 0.16);
--text: #edf7ff;
--muted: #91a8bd;
--cyan: #55d9ff;
--amber: #ffc857;
--green: #75efb2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
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;
}
.brand {
position: fixed;
z-index: 5;
top: 24px;
left: 26px;
pointer-events: none;
}
.kicker {
margin-bottom: 5px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: var(--text);
font-size: clamp(25px, 3vw, 42px);
font-weight: 750;
letter-spacing: -0.045em;
line-height: 1;
}
.reset {
position: fixed;
z-index: 10;
top: 25px;
right: 25px;
padding: 10px 14px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(12, 29, 48, 0.76);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
backdrop-filter: blur(14px);
}
.reset:hover {
border-color: rgba(85, 217, 255, 0.5);
background: rgba(21, 47, 72, 0.9);
}
.readout {
position: fixed;
z-index: 6;
top: 108px;
left: 25px;
width: 315px;
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 70px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px);
}
.readout-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
}
.readout-title {
color: var(--text);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.12em;
}
.status {
display: flex;
align-items: center;
gap: 6px;
color: var(--green);
font-size: 11px;
font-weight: 700;
}
.status::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
content: "";
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.metric {
min-width: 0;
padding: 12px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 12px;
background: rgba(255, 255, 255, 0.035);
}
.metric.wide {
grid-column: 1 / -1;
}
.metric-label {
margin-bottom: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.metric-value {
color: var(--text);
font-size: 25px;
font-weight: 760;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.metric-value small {
margin-left: 4px;
color: var(--muted);
font-size: 11px;
font-weight: 650;
letter-spacing: 0;
}
.meter {
height: 5px;
margin-top: 10px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.09);
}
.meter-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--cyan), #8a7dff);
box-shadow: 0 0 14px rgba(85, 217, 255, 0.6);
transition: width 120ms linear;
}
.formula {
margin-top: 14px;
padding-top: 14px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.formula-title {
margin-bottom: 9px;
color: var(--text);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.equation {
margin: 5px 0;
color: #c9e7fa;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 12px;
line-height: 1.45;
}
.note {
margin: 10px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 13px;
margin-top: 13px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
}
.dot.amber {
background: var(--amber);
box-shadow: 0 0 8px var(--amber);
}
.dot.cyan {
background: var(--cyan);
box-shadow: 0 0 8px var(--cyan);
}
.hint {
position: fixed;
z-index: 5;
bottom: 20px;
left: 50%;
padding: 9px 14px;
border: 1px solid var(--border);
border-radius: 99px;
background: rgba(7, 17, 31, 0.72);
color: #b6c8d9;
font-size: 11px;
font-weight: 650;
white-space: nowrap;
pointer-events: none;
transform: translateX(-50%);
backdrop-filter: blur(12px);
}
.tag {
position: fixed;
z-index: 4;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 7px;
background: rgba(7, 17, 31, 0.76);
color: #d8e9f7;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
line-height: 1;
pointer-events: none;
text-transform: uppercase;
transform: translate(-50%, -100%);
white-space: nowrap;
backdrop-filter: blur(8px);
}
.tag::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 15px;
background: linear-gradient(rgba(216, 233, 247, 0.7), transparent);
content: "";
}
#tag-light {
color: var(--amber);
border-color: rgba(255, 200, 87, 0.3);
}
#tag-panel,
#tag-fan {
color: var(--cyan);
border-color: rgba(85, 217, 255, 0.28);
}
@media (max-width: 760px) {
.brand {
top: 18px;
left: 18px;
}
.kicker {
font-size: 9px;
}
h1 {
font-size: 25px;
}
.reset {
top: 18px;
right: 16px;
padding: 8px 10px;
font-size: 10px;
}
.readout {
top: 80px;
right: 12px;
left: 12px;
width: auto;
padding: 13px;
}
.metrics {
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.metric {
padding: 9px;
}
.metric.wide {
grid-column: auto;
}
.metric-value {
font-size: 20px;
}
.formula {
display: none;
}
.legend {
margin-top: 9px;
}
.hint {
bottom: 12px;
max-width: calc(100% - 20px);
overflow: hidden;
text-overflow: ellipsis;
}
.tag {
display: none;
}
}
</style>
</head>
<body>
<div class="brand">
<div class="kicker">Interactive Energy Cutaway</div>
<h1>Light → Solar → Fan</h1>
</div>
<button id="reset" class="reset" type="button">Reset view</button>
<aside class="readout">
<div class="readout-header">
<div class="readout-title">Live model</div>
<div id="status" class="status" aria-live="polite">Powering fan</div>
</div>
<div class="metrics">
<div class="metric">
<div class="metric-label">Distance</div>
<div class="metric-value"><span id="distance">0.0</span><small>m</small></div>
</div>
<div class="metric">
<div class="metric-label">Irradiance</div>
<div class="metric-value"><span id="irradiance">0</span><small>W/m²</small></div>
</div>
<div class="metric wide">
<div class="metric-label">Fan speed</div>
<div class="metric-value"><span id="rpm">0</span><small>rpm</small></div>
<div class="meter"><div id="rpmBar" class="meter-fill"></div></div>
</div>
</div>
<div class="formula">
<div class="formula-title">Simplified relationship</div>
<div class="equation">I = 100 / (1 + (d / 3 m)²) W/m²</div>
<div class="equation">n = 20 + 2I rpm</div>
<p class="note">
This distance-only teaching model omits incidence angle, panel heat,
wiring losses, motor load, and real inverter behavior.
</p>
</div>
<div class="legend">
<span><i class="dot amber"></i> Incident light</span>
<span><i class="dot cyan"></i> Electrical power</span>
</div>
</aside>
<div id="tag-panel" class="tag">Solar panel · cutaway</div>
<div id="tag-fan" class="tag">Fan · open shroud</div>
<div id="tag-light" class="tag">Drag light</div>
<div class="hint">
Drag the amber light along the rail · Drag background 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 $ = (id) => document.getElementById(id);
// -------------------------------------------------------------------------
// Renderer, scene, and camera
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.FogExp2(0x07111f, 0.032);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
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 solar panel and fan cutaway"
);
document.body.prepend(renderer.domElement);
const ambient = new THREE.HemisphereLight(0xb9dcff, 0x142033, 1.35);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xc8e5ff, 2.6);
keyLight.position.set(4, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x667dff, 1.4);
rimLight.position.set(-6, 4, -5);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Shared materials and geometry helpers
// -------------------------------------------------------------------------
const materials = {
platform: new THREE.MeshStandardMaterial({
color: 0x101e2d,
roughness: 0.78,
metalness: 0.15
}),
frame: new THREE.MeshStandardMaterial({
color: 0x9fb3c5,
roughness: 0.27,
metalness: 0.8
}),
darkMetal: new THREE.MeshStandardMaterial({
color: 0x172431,
roughness: 0.35,
metalness: 0.75
}),
cellA: new THREE.MeshStandardMaterial({
color: 0x0b4f87,
roughness: 0.23,
metalness: 0.42,
emissive: 0x031a31,
emissiveIntensity: 0.35
}),
cellB: new THREE.MeshStandardMaterial({
color: 0x1266a0,
roughness: 0.2,
metalness: 0.38,
emissive: 0x04213a,
emissiveIntensity: 0.35
}),
busbar: new THREE.MeshBasicMaterial({ color: 0xc7e8f8 }),
glass: new THREE.MeshPhysicalMaterial({
color: 0x8adfff,
transparent: true,
opacity: 0.22,
roughness: 0.08,
metalness: 0.05,
depthWrite: false
}),
backsheet: new THREE.MeshStandardMaterial({
color: 0xd9e6ec,
roughness: 0.85
}),
fan: new THREE.MeshStandardMaterial({
color: 0x64d7f4,
roughness: 0.27,
metalness: 0.48,
emissive: 0x062d3b,
emissiveIntensity: 0.4
}),
copper: new THREE.MeshStandardMaterial({
color: 0xffa34f,
roughness: 0.28,
metalness: 0.72
}),
amber: new THREE.MeshStandardMaterial({
color: 0xffc857,
emissive: 0xff9f23,
emissiveIntensity: 4,
roughness: 0.25
}),
black: new THREE.MeshStandardMaterial({
color: 0x080e15,
roughness: 0.48,
metalness: 0.55
})
};
function addMesh(geometry, material, parent = scene) {
const object = new THREE.Mesh(geometry, material);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function box(w, h, d, material, x = 0, y = 0, z = 0, parent = scene) {
const object = addMesh(
new THREE.BoxGeometry(w, h, d),
material,
parent
);
object.position.set(x, y, z);
return object;
}
function cylinder(radius, height, material, x = 0, y = 0, z = 0, parent = scene) {
const object = addMesh(
new THREE.CylinderGeometry(radius, radius, height, 32),
material,
parent
);
object.position.set(x, y, z);
return object;
}
function rod(start, end, radius, material, parent = scene) {
const direction = new THREE.Vector3().subVectors(end, start);
const midpoint = new THREE.Vector3()
.addVectors(start, end)
.multiplyScalar(0.5);
const object = cylinder(
radius,
direction.length(),
material,
midpoint.x,
midpoint.y,
midpoint.z,
parent
);
object.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return object;
}
// -------------------------------------------------------------------------
// Platform and grid
// -------------------------------------------------------------------------
const ground = addMesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0x07101b,
roughness: 0.96
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.25;
const platform = addMesh(
new THREE.CylinderGeometry(6.25, 6.25, 0.22, 96),
materials.platform
);
platform.position.y = -0.13;
const grid = new THREE.GridHelper(11.5, 23, 0x31506a, 0x20364b);
grid.position.y = 0.005;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
// -------------------------------------------------------------------------
// Solar panel with exposed glass / cell / backsheet layers
// -------------------------------------------------------------------------
const panel = new THREE.Group();
panel.position.set(-2.25, 1.5, -0.55);
panel.rotation.set(-0.1, 0.12, 0);
scene.add(panel);
// Aluminum frame rails.
box(3.8, 0.11, 0.18, materials.frame, 0, 1.18, 0, panel);
box(3.8, 0.11, 0.18, materials.frame, 0, -1.18, 0, panel);
box(0.11, 2.42, 0.18, materials.frame, -1.85, 0, 0, panel);
box(0.11, 2.42, 0.18, materials.frame, 1.85, 0, 0, panel);
// Main backing and glass.
box(
2.62,
2.18,
0.07,
materials.darkMetal,
-0.48,
0,
-0.05,
panel
);
const mainGlass = box(
2.62,
2.18,
0.035,
materials.glass,
-0.48,
0,
0.105,
panel
);
mainGlass.castShadow = false;
mainGlass.renderOrder = 2;
// Individual photovoltaic cells.
const cellGeometry = new THREE.BoxGeometry(0.39, 0.43, 0.025);
const busbarGeometry = new THREE.BoxGeometry(0.32, 0.009, 0.009);
for (let row = 0; row < 4; row++) {
for (let column = 0; column < 6; column++) {
const x = -1.62 + column * 0.43;
const y = -0.72 + row * 0.49;
addMesh(
cellGeometry,
(row + column) % 2 ? materials.cellA : materials.cellB,
panel
).position.set(x, y, 0.125);
addMesh(busbarGeometry, materials.busbar, panel).position.set(
x,
y,
0.142
);
}
}
// Exploded cutaway layers on the right side of the array.
box(
0.62,
2.08,
0.045,
materials.glass,
1.33,
0,
0.34,
panel
).castShadow = false;
box(
0.62,
2.02,
0.045,
materials.cellB,
1.33,
0,
0.52,
panel
);
box(
0.62,
2.08,
0.045,
materials.backsheet,
1.33,
0,
0.7,
panel
);
// Junction box on the back of the panel.
box(
0.55,
0.26,
0.12,
materials.black,
-0.55,
-1.0,
-0.17,
panel
);
// Panel supports.
rod(
new THREE.Vector3(-3.25, 0.05, -0.85),
new THREE.Vector3(-3.05, 1.05, -0.66),
0.055,
materials.frame
);
rod(
new THREE.Vector3(-1.25, 0.05, -0.85),
new THREE.Vector3(-1.48, 1.05, -0.66),
0.055,
materials.frame
);
rod(
new THREE.Vector3(-3.3, 0.05, -0.85),
new THREE.Vector3(-3.3, 0.05, -1.35),
0.07,
materials.darkMetal
);
rod(
new THREE.Vector3(-1.2, 0.05, -0.85),
new THREE.Vector3(-1.2, 0.05, -1.35),
0.07,
materials.darkMetal
);
// -------------------------------------------------------------------------
// Cutaway fan, exposed motor, blades, and guard
// -------------------------------------------------------------------------
const fan = new THREE.Group();
fan.position.set(2.15, 1.55, 0.55);
scene.add(fan);
// Open section of the circular shroud.
const shroud = addMesh(
new THREE.TorusGeometry(1.16, 0.13, 14, 90, Math.PI * 1.72),
materials.frame,
fan
);
shroud.rotation.z = 0.12;
// Rear motor can.
const motorCan = cylinder(
0.47,
0.65,
materials.darkMetal,
0,
0,
-0.47,
fan
);
motorCan.rotation.x = Math.PI / 2;
// Copper motor coils visible through the cutaway.
addMesh(
new THREE.TorusGeometry(0.42, 0.075, 10, 48),
materials.copper,
fan
).position.z = -0.16;
const bladeGroup = new THREE.Group();
fan.add(bladeGroup);
const bladeShape = new THREE.Shape();
bladeShape.moveTo(0.12, -0.08);
bladeShape.bezierCurveTo(0.38, -0.17, 0.94, 0.1, 1.02, 0.47);
bladeShape.bezierCurveTo(0.75, 0.8, 0.34, 0.66, 0.14, 0.29);
bladeShape.closePath();
const bladeGeometry = new THREE.ExtrudeGeometry(bladeShape, {
depth: 0.075,
bevelEnabled: true,
bevelSize: 0.025,
bevelThickness: 0.025,
bevelSegments: 2,
steps: 1
});
for (let i = 0; i < 5; i++) {
const blade = addMesh(bladeGeometry, materials.fan, bladeGroup);
blade.rotation.z = (i * Math.PI * 2) / 5;
blade.rotation.y = 0.08;
}
const hub = cylinder(0.25, 0.32, materials.frame, 0, 0, 0.15, fan);
hub.rotation.x = Math.PI / 2;
const hubCap = cylinder(
0.13,
0.36,
materials.copper,
0,
0,
0.19,
fan
);
hubCap.rotation.x = Math.PI / 2;
// Protective guard rings.
for (const radius of [0.48, 0.82]) {
addMesh(
new THREE.TorusGeometry(radius, 0.025, 8, 72),
materials.darkMetal,
fan
).position.z = 0.31;
}
const guardPositions = [];
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
guardPositions.push(
Math.cos(angle) * 0.23,
Math.sin(angle) * 0.23,
0.32,
Math.cos(angle) * 1.02,
Math.sin(angle) * 1.02,
0.32
);
}
const guardGeometry = new THREE.BufferGeometry();
guardGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(guardPositions, 3)
);
fan.add(
new THREE.LineSegments(
guardGeometry,
new THREE.LineBasicMaterial({
color: 0xaec4d5,
transparent: true,
opacity: 0.78
})
)
);
// Fan support.
rod(
new THREE.Vector3(2.15, 0.02, 0.16),
new THREE.Vector3(2.15, 1.05, 0.16),
0.085,
materials.frame
);
box(1.05, 0.09, 0.72, materials.darkMetal, 2.15, 0.04, 0.16);
// -------------------------------------------------------------------------
// Electrical cable and animated power particles
// -------------------------------------------------------------------------
const cableCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-0.67, 0.48, -0.31),
new THREE.Vector3(-0.18, 0.28, -0.16),
new THREE.Vector3(0.45, 0.22, 0.02),
new THREE.Vector3(1.08, 0.28, 0.12),
new THREE.Vector3(1.57, 0.53, 0.15),
new THREE.Vector3(2.15, 1.02, 0.08)
]);
const cable = addMesh(
new THREE.TubeGeometry(cableCurve, 80, 0.035, 8, false),
new THREE.MeshStandardMaterial({
color: 0x28485d,
roughness: 0.42,
metalness: 0.55,
emissive: 0x0a2735,
emissiveIntensity: 0.45
})
);
cable.castShadow = false;
// Small controller between panel and motor.
box(0.5, 0.25, 0.32, materials.black, 0.56, 0.23, 0.05);
box(
0.32,
0.12,
0.015,
new THREE.MeshBasicMaterial({ color: 0x55d9ff }),
0.56,
0.25,
0.222
);
const powerParticles = [];
const particleGeometry = new THREE.SphereGeometry(0.055, 10, 10);
const particleMaterial = new THREE.MeshBasicMaterial({ color: 0x76e8ff });
for (let i = 0; i < 12; i++) {
const particle = addMesh(
particleGeometry,
particleMaterial,
scene
);
particle.castShadow = false;
powerParticles.push(particle);
}
// -------------------------------------------------------------------------
// Movable light on an overhead rail
// -------------------------------------------------------------------------
const rail = box(
9.6,
0.09,
0.11,
materials.darkMetal,
0,
4.24,
2.25
);
rail.castShadow = false;
for (let x = -4.4; x <= 4.4; x += 0.8) {
box(
0.025,
0.07,
0.16,
materials.frame,
x,
4.24,
2.25
);
}
const lamp = new THREE.Group();
lamp.position.set(-0.45, 4.02, 2.25);
scene.add(lamp);
box(0.34, 0.18, 0.25, materials.frame, 0, 0.08, 0, lamp);
cylinder(0.28, 0.4, materials.darkMetal, 0, -0.13, 0, lamp);
const bulb = addMesh(
new THREE.SphereGeometry(0.19, 28, 20),
materials.amber,
lamp
);
bulb.position.y = -0.37;
addMesh(
new THREE.TorusGeometry(0.32, 0.035, 10, 40),
materials.frame,
lamp
).rotation.x = Math.PI / 2;
lamp.children[2].position.y = -0.37;
// A larger invisible sphere makes the lamp easy to grab.
const lampPick = addMesh(
new THREE.SphereGeometry(0.55, 16, 12),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
}),
lamp
);
lampPick.position.y = -0.37;
lampPick.castShadow = false;
lampPick.receiveShadow = false;
const panelTargetObject = new THREE.Object3D();
panelTargetObject.position.set(-0.48, 0, 0.15);
panel.add(panelTargetObject);
const spotlightTarget = new THREE.Object3D();
scene.add(spotlightTarget);
const spotlight = new THREE.SpotLight(
0xffd58a,
250,
18,
0.56,
0.56,
1.5
);
spotlight.castShadow = true;
spotlight.shadow.mapSize.set(1024, 1024);
spotlight.shadow.bias = -0.0002;
scene.add(spotlight);
scene.add(spotlight.target);
const bulbLight = new THREE.PointLight(0xffbf55, 9, 4.5, 2);
bulbLight.position.y = -0.37;
lamp.add(bulbLight);
// Transparent visual light cone.
const beam = addMesh(
new THREE.ConeGeometry(1, 1, 36, 1, true),
new THREE.MeshBasicMaterial({
color: 0xffd277,
transparent: true,
opacity: 0.045,
side: THREE.DoubleSide,
depthWrite: false
}),
scene
);
beam.castShadow = false;
beam.receiveShadow = false;
// -------------------------------------------------------------------------
// Screen-space labels
// -------------------------------------------------------------------------
const labels = [];
function createLabel(id, parent, position) {
const anchor = new THREE.Object3D();
anchor.position.copy(position);
parent.add(anchor);
labels.push({ element: $(id), anchor });
}
createLabel(
"tag-panel",
panel,
new THREE.Vector3(-0.55, 1.43, 0.2)
);
createLabel(
"tag-fan",
fan,
new THREE.Vector3(0, 1.43, 0.12)
);
createLabel(
"tag-light",
lamp,
new THREE.Vector3(0, -0.78, 0)
);
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of labels) {
label.anchor.getWorldPosition(projected);
projected.project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
label.element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// -------------------------------------------------------------------------
// Custom camera controls: orbit and zoom, implemented without add-ons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-0.15, 1.35, 0.15);
const spherical = new THREE.Spherical(10.4, 1.13, 0.75);
const cameraOffset = new THREE.Vector3();
function updateCamera() {
cameraOffset.setFromSpherical(spherical);
camera.position.copy(cameraTarget).add(cameraOffset);
camera.lookAt(cameraTarget);
}
updateCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const lampDragPlane = new THREE.Plane(
new THREE.Vector3(0, 0, 1),
-2.25
);
const planeIntersection = new THREE.Vector3();
let orbiting = false;
let draggingLamp = false;
let lampDragOffset = 0;
let lastPointerX = 0;
let lastPointerY = 0;
function updatePointer(event) {
pointer.x = (event.clientX / innerWidth) * 2 - 1;
pointer.y = -(event.clientY / innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function hitLamp(event) {
updatePointer(event);
return raycaster.intersectObject(lampPick, false).length > 0;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (hitLamp(event)) {
draggingLamp = true;
raycaster.ray.intersectPlane(lampDragPlane, planeIntersection);
lampDragOffset = lamp.position.x - planeIntersection.x;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
} else {
orbiting = true;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "move";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (draggingLamp) {
updatePointer(event);
if (raycaster.ray.intersectPlane(lampDragPlane, planeIntersection)) {
lamp.position.x = THREE.MathUtils.clamp(
planeIntersection.x + lampDragOffset,
-4.45,
4.45
);
}
} else if (orbiting) {
spherical.theta -= dx * 0.006;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - dy * 0.005,
0.3,
1.43
);
updateCamera();
} else {
renderer.domElement.style.cursor = hitLamp(event) ? "grab" : "default";
}
});
function endPointer(event) {
orbiting = false;
draggingLamp = false;
renderer.domElement.style.cursor = "default";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius + event.deltaY * 0.008,
5.4,
15
);
updateCamera();
},
{ passive: false }
);
$("reset").addEventListener("click", () => {
lamp.position.x = -0.45;
spherical.set(10.4, 1.13, 0.75);
updateCamera();
});
// -------------------------------------------------------------------------
// Simplified physical relationship and animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
let smoothedRPM = 20;
let smoothedIrradiance = 0;
const bulbWorld = new THREE.Vector3();
const targetWorld = new THREE.Vector3();
const beamDirection = new THREE.Vector3();
const particlePosition = new THREE.Vector3();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
scene.updateMatrixWorld(true);
bulb.getWorldPosition(bulbWorld);
panelTargetObject.getWorldPosition(targetWorld);
const distance = bulbWorld.distanceTo(targetWorld);
const irradiance = 100 / (1 + Math.pow(distance / 3, 2));
const targetRPM = 20 + 2 * irradiance;
smoothedIrradiance +=
(irradiance - smoothedIrradiance) * Math.min(1, delta * 7);
smoothedRPM += (targetRPM - smoothedRPM) * Math.min(1, delta * 7);
// Fan animation is visually slowed while preserving the RPM relationship.
bladeGroup.rotation.z -=
((smoothedRPM * Math.PI * 2) / 60) * 0.55 * delta;
// Light and beam.
spotlight.position.copy(bulbWorld);
spotlightTarget.position.copy(targetWorld);
spotlight.target = spotlightTarget;
spotlight.intensity = 25 + 430 * (smoothedIrradiance / 100);
bulbLight.intensity = 3 + 13 * (smoothedIrradiance / 100);
beamDirection.subVectors(targetWorld, bulbWorld);
const beamLength = beamDirection.length();
beamDirection.normalize();
const beamRadius = Math.tan(0.54) * beamLength;
beam.position.copy(bulbWorld).addScaledVector(beamDirection, beamLength * 0.5);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, -1, 0),
beamDirection
);
beam.scale.set(beamRadius, beamLength, beamRadius);
// Animated electricity particles moving from panel to motor.
const particleSpeed = 0.035 + smoothedRPM * 0.0014;
for (let i = 0; i < powerParticles.length; i++) {
const t = (elapsed * particleSpeed + i / powerParticles.length) % 1;
cableCurve.getPointAt(t, particlePosition);
powerParticles[i].position.copy(particlePosition);
powerParticles[i].scale.setScalar(
0.55 + 0.75 * (smoothedIrradiance / 100)
);
}
// Live readouts.
$("distance").textContent = distance.toFixed(1);
$("irradiance").textContent = Math.round(smoothedIrradiance);
$("rpm").textContent = Math.round(smoothedRPM);
$("rpmBar").style.width =
`${((smoothedRPM - 20) / 200) * 100}%`;
const status = $("status");
if (smoothedRPM > 160) {
status.textContent = "Strong output";
status.style.color = "#75efb2";
} else if (smoothedRPM > 90) {
status.textContent = "Powering fan";
status.style.color = "#55d9ff";
} else {
status.textContent = "Low output";
status.style.color = "#ffc857";
}
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
animate();
</script>
</body>
</html>
```