Prompt specification
Build or customize the Three.js 3D scene "Jointed animal shadow puppet" 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: Jointed animal shadow puppet
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/jointed-animal-shadow-puppet-x0369
Scene Description Prompt:
A jointed voxel bird puppet between a lamp and screen, showing its projected silhouette. One HTML file, Three.js from cdnjs.
Working Three.js Starter (jointed-animal-shadow-puppet-x0369):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>The Jointed Shadow</title>
<style>
:root {
color-scheme: dark;
--paper: #e8dfcb;
--muted: rgba(232, 223, 203, 0.56);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #111215;
}
body {
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--paper);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.title {
position: fixed;
top: 30px;
left: 34px;
z-index: 10;
pointer-events: none;
user-select: none;
}
.eyebrow {
margin-bottom: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.28em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 4vw, 58px);
font-weight: 400;
line-height: 0.96;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 13px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
}
.status {
position: fixed;
top: 34px;
right: 34px;
z-index: 10;
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.22em;
text-transform: uppercase;
pointer-events: none;
}
.status::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: #e9a95d;
box-shadow: 0 0 13px #e9a95d;
content: "";
}
.hint {
position: fixed;
right: 34px;
bottom: 28px;
z-index: 10;
color: rgba(232, 223, 203, 0.38);
font-size: 9px;
letter-spacing: 0.18em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 700px) {
.title {
top: 22px;
left: 22px;
}
.status {
top: 24px;
right: 22px;
}
.subtitle {
max-width: 220px;
line-height: 1.5;
}
.hint {
right: 22px;
bottom: 20px;
}
}
</style>
</head>
<body>
<div class="title">
<div class="eyebrow">Light study · No. 03</div>
<h1>The Jointed<br />Shadow</h1>
<div class="subtitle">A wooden bird, a single lamp, and the shape of light.</div>
</div>
<div class="status">Live projection</div>
<div class="hint">Drag to orbit · Scroll to dolly</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x111215);
scene.fog = new THREE.FogExp2(0x111215, 0.026);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const controls = {
target: new THREE.Vector3(0.05, 0.18, -0.25),
radius: 11.25,
theta: 0.59,
phi: 1.22,
dragging: false,
previousX: 0,
previousY: 0
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
updateCamera();
renderer.domElement.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.previousX = event.clientX;
controls.previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const dx = event.clientX - controls.previousX;
const dy = event.clientY - controls.previousY;
controls.previousX = event.clientX;
controls.previousY = event.clientY;
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(controls.phi - dy * 0.005, 0.35, 1.46);
updateCamera();
});
function endDrag(event) {
controls.dragging = false;
renderer.domElement.style.cursor = "grab";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.style.cursor = "grab";
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius + event.deltaY * 0.008,
7.4,
16
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const floorY = -1.75;
function standard(color, roughness = 0.72, metalness = 0.02) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
const materials = {
body: standard(0x3d7274, 0.68),
bodyLight: standard(0x4c8583, 0.66),
bodyDark: standard(0x285456, 0.7),
cream: standard(0xd7c397, 0.72),
creamLight: standard(0xe8d6aa, 0.7),
beak: standard(0xc96743, 0.62),
joint: standard(0x9b7443, 0.45, 0.42),
wood: standard(0x332821, 0.74),
darkWood: standard(0x211c19, 0.78),
lamp: standard(0x32302d, 0.52, 0.38),
brass: standard(0x927044, 0.4, 0.55),
floor: standard(0x1d1b1b, 0.93)
};
function box(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(a, b, radius, material, radialSegments = 14) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
// -------------------------------------------------------------------------
// Screen and stage
// -------------------------------------------------------------------------
const screen = {
centerX: 0.6,
centerY: 0.6,
width: 5.6,
height: 4.7,
z: -2.7
};
const screenTextureCanvas = document.createElement("canvas");
screenTextureCanvas.width = 512;
screenTextureCanvas.height = 512;
const screenContext = screenTextureCanvas.getContext("2d");
const screenGradient = screenContext.createRadialGradient(
256,
225,
30,
256,
256,
360
);
screenGradient.addColorStop(0, "#e6d8b8");
screenGradient.addColorStop(0.6, "#d6c39d");
screenGradient.addColorStop(1, "#a99169");
screenContext.fillStyle = screenGradient;
screenContext.fillRect(0, 0, 512, 512);
for (let i = 0; i < 5200; i++) {
const alpha = Math.random() * 0.045;
screenContext.fillStyle = `rgba(55, 39, 24, ${alpha})`;
screenContext.fillRect(
Math.random() * 512,
Math.random() * 512,
Math.random() * 2 + 0.5,
Math.random() * 2 + 0.5
);
}
const screenTexture = new THREE.CanvasTexture(screenTextureCanvas);
screenTexture.colorSpace = THREE.SRGBColorSpace;
const screenMaterial = new THREE.MeshStandardMaterial({
map: screenTexture,
color: 0xffffff,
roughness: 0.98,
metalness: 0
});
const screenCanvas = new THREE.Mesh(
new THREE.PlaneGeometry(screen.width, screen.height),
screenMaterial
);
screenCanvas.position.set(screen.centerX, screen.centerY, screen.z);
screenCanvas.receiveShadow = true;
scene.add(screenCanvas);
const frameDepth = 0.18;
const frameThickness = 0.16;
box(
scene,
[screen.width + frameThickness * 2, frameThickness, frameDepth],
[screen.centerX, screen.centerY + screen.height / 2, screen.z + 0.04],
materials.wood
);
box(
scene,
[screen.width + frameThickness * 2, frameThickness, frameDepth],
[screen.centerX, screen.centerY - screen.height / 2, screen.z + 0.04],
materials.wood
);
box(
scene,
[frameThickness, screen.height, frameDepth],
[screen.centerX - screen.width / 2, screen.centerY, screen.z + 0.04],
materials.wood
);
box(
scene,
[frameThickness, screen.height, frameDepth],
[screen.centerX + screen.width / 2, screen.centerY, screen.z + 0.04],
materials.wood
);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
materials.floor
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = floorY;
floor.receiveShadow = true;
scene.add(floor);
const floorGlow = new THREE.Mesh(
new THREE.CircleGeometry(4.4, 64),
new THREE.MeshBasicMaterial({
color: 0x6d4529,
transparent: true,
opacity: 0.075,
depthWrite: false
})
);
floorGlow.rotation.x = -Math.PI / 2;
floorGlow.position.set(-0.2, floorY + 0.006, 0.25);
scene.add(floorGlow);
// -------------------------------------------------------------------------
// Lamp and light
// -------------------------------------------------------------------------
const lampPosition = new THREE.Vector3(-2.5, 0.05, 4.0);
const lightTargetPosition = new THREE.Vector3(0.6, 0.57, -2.68);
const beamDirection = lightTargetPosition.clone().sub(lampPosition);
const beamLength = beamDirection.length();
beamDirection.normalize();
cylinderBetween(
[-2.5, floorY + 0.12, 4.0],
[-2.5, -0.58, 4.0],
0.075,
materials.lamp
);
const lampBack = lampPosition.clone().addScaledVector(beamDirection, -0.37);
cylinderBetween(
[-2.5, -0.58, 4.0],
[lampBack.x, lampBack.y, lampBack.z],
0.075,
materials.lamp
);
const lampBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.52, 0.62, 0.14, 24),
materials.lamp
);
lampBase.position.set(-2.5, floorY + 0.08, 4.0);
lampBase.castShadow = true;
lampBase.receiveShadow = true;
scene.add(lampBase);
const shade = new THREE.Mesh(
new THREE.CylinderGeometry(0.17, 0.43, 0.68, 20, 1, true),
materials.lamp
);
shade.position.copy(lampPosition).addScaledVector(beamDirection, 0.2);
shade.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
beamDirection.clone().negate()
);
shade.castShadow = false;
scene.add(shade);
const shadeRim = new THREE.Mesh(
new THREE.TorusGeometry(0.425, 0.025, 8, 32),
materials.brass
);
shadeRim.position.copy(lampPosition).addScaledVector(beamDirection, 0.53);
shadeRim.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
beamDirection
);
scene.add(shadeRim);
const bulbMaterial = new THREE.MeshStandardMaterial({
color: 0xffd69a,
emissive: 0xffb34f,
emissiveIntensity: 5.5,
roughness: 0.3
});
const bulb = new THREE.Mesh(
new THREE.SphereGeometry(0.15, 18, 12),
bulbMaterial
);
bulb.position.copy(lampPosition).addScaledVector(beamDirection, 0.06);
scene.add(bulb);
const glowCanvas = document.createElement("canvas");
glowCanvas.width = 128;
glowCanvas.height = 128;
const glowContext = glowCanvas.getContext("2d");
const glowGradient = glowContext.createRadialGradient(64, 64, 0, 64, 64, 64);
glowGradient.addColorStop(0, "rgba(255,213,145,0.9)");
glowGradient.addColorStop(0.18, "rgba(255,176,82,0.38)");
glowGradient.addColorStop(1, "rgba(255,145,40,0)");
glowContext.fillStyle = glowGradient;
glowContext.fillRect(0, 0, 128, 128);
const glowTexture = new THREE.CanvasTexture(glowCanvas);
glowTexture.colorSpace = THREE.SRGBColorSpace;
const bulbGlow = new THREE.Sprite(
new THREE.SpriteMaterial({
map: glowTexture,
transparent: true,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
bulbGlow.position.copy(bulb.position);
bulbGlow.scale.set(0.95, 0.95, 0.95);
scene.add(bulbGlow);
const spotlight = new THREE.SpotLight(0xffc276, 230, 22, 0.44, 0.48, 1.45);
spotlight.position.copy(lampPosition);
spotlight.target.position.copy(lightTargetPosition);
spotlight.castShadow = true;
spotlight.shadow.mapSize.set(2048, 2048);
spotlight.shadow.camera.near = 0.15;
spotlight.shadow.camera.far = 18;
spotlight.shadow.bias = -0.00025;
spotlight.shadow.normalBias = 0.025;
scene.add(spotlight, spotlight.target);
const bulbLight = new THREE.PointLight(0xffa95a, 24, 4.5, 1.7);
bulbLight.position.copy(bulb.position);
scene.add(bulbLight);
const hemisphere = new THREE.HemisphereLight(0x8496a5, 0x33231d, 1.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xd9e4e6, 1.7);
keyLight.position.set(-4, 7, 6);
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;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5c8e96, 1.0);
rimLight.position.set(5, 3, -5);
scene.add(rimLight);
const beam = new THREE.Mesh(
new THREE.ConeGeometry(2.8, beamLength, 48, 1, true),
new THREE.MeshBasicMaterial({
color: 0xe9a55c,
transparent: true,
opacity: 0.024,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
beam.position.copy(lampPosition).addScaledVector(beamDirection, beamLength * 0.5);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, -1, 0),
beamDirection
);
beam.renderOrder = 1;
scene.add(beam);
// -------------------------------------------------------------------------
// Jointed voxel bird
// -------------------------------------------------------------------------
const trackedBirdParts = [];
const birdRoot = new THREE.Group();
birdRoot.position.set(-0.64, 0.15, 0);
birdRoot.scale.setScalar(0.85);
scene.add(birdRoot);
function voxel(parent, position, size, material, track = true) {
const mesh = box(parent, size, position, material, true);
if (track) trackedBirdParts.push(mesh);
return mesh;
}
const bodyGroup = new THREE.Group();
birdRoot.add(bodyGroup);
const bodyLayout = [
[-0.32, 0, 0],
[0, 0, 0],
[0.32, 0, 0],
[-0.32, 0.32, 0],
[0, 0.32, 0],
[0.32, 0.32, 0],
[-0.32, 0, 0.32],
[0, 0, 0.32],
[0.32, 0, 0.32]
];
bodyLayout.forEach((position, index) => {
const material =
index === 4 ? materials.bodyLight :
index % 3 === 0 ? materials.bodyDark :
materials.body;
voxel(bodyGroup, position, [0.295, 0.295, 0.295], material);
});
voxel(bodyGroup, [0.32, 0.14, 0.14], [0.28, 0.28, 0.28], materials.creamLight);
const neckPivot = new THREE.Group();
neckPivot.position.set(0.4, 0.32, 0);
bodyGroup.add(neckPivot);
const neckPositions = [
[0, 0, 0],
[0.32, 0.32, 0],
[0.32, 0.64, 0],
[0.64, 0.64, 0]
];
neckPositions.forEach((position, index) => {
voxel(
neckPivot,
position,
[0.29, 0.29, 0.29],
index % 2 ? materials.cream : materials.creamLight
);
});
const headPivot = new THREE.Group();
headPivot.position.set(0.64, 0.96, 0);
neckPivot.add(headPivot);
voxel(headPivot, [0, 0, 0], [0.31, 0.31, 0.31], materials.creamLight);
voxel(headPivot, [0.28, -0.02, 0], [0.27, 0.22, 0.22], materials.beak);
voxel(headPivot, [0.53, -0.04, 0], [0.24, 0.16, 0.16], materials.beak);
const frontWing = new THREE.Group();
frontWing.position.set(-0.02, 0.22, 0.36);
bodyGroup.add(frontWing);
[
[-0.28, 0, 0],
[-0.58, 0, 0],
[-0.88, 0, 0],
[-0.30, 0.29, 0],
[-0.60, 0.29, 0]
].forEach((position, index) => {
voxel(
frontWing,
position,
[0.29, 0.25, 0.25],
index % 2 ? materials.bodyDark : materials.body
);
});
const backWing = new THREE.Group();
backWing.position.set(-0.02, 0.22, -0.36);
bodyGroup.add(backWing);
[
[-0.28, 0, 0],
[-0.58, 0, 0],
[-0.88, 0, 0],
[-0.30, 0.29, 0],
[-0.60, 0.29, 0]
].forEach((position, index) => {
voxel(
backWing,
position,
[0.29, 0.25, 0.25],
index % 2 ? materials.body : materials.bodyDark
);
});
const tailPivot = new THREE.Group();
tailPivot.position.set(-0.68, 0.12, 0);
bodyGroup.add(tailPivot);
voxel(tailPivot, [-0.30, 0, 0], [0.32, 0.24, 0.26], materials.bodyDark);
voxel(tailPivot, [-0.60, 0, 0], [0.32, 0.22, 0.24], materials.body);
voxel(tailPivot, [-0.30, 0.27, 0], [0.28, 0.25, 0.24], materials.bodyLight);
// Thin wooden leg linkages and small square joints.
[-0.17, 0.17].forEach((x, legIndex) => {
const z = legIndex === 0 ? 0.18 : -0.18;
for (let i = 0; i < 4; i++) {
voxel(
birdRoot,
[x, -0.48 - i * 0.42, z],
[0.105, 0.39, 0.105],
materials.wood
);
}
voxel(birdRoot, [x, -0.48, z], [0.15, 0.15, 0.15], materials.joint);
voxel(birdRoot, [x, -1.34, z], [0.15, 0.15, 0.15], materials.joint);
voxel(birdRoot, [x + 0.17, -1.76, z], [0.43, 0.09, 0.13], materials.wood);
voxel(birdRoot, [x - 0.16, -1.76, z + 0.12], [0.13, 0.08, 0.36], materials.wood);
});
// Small decorative joint pins.
voxel(bodyGroup, [0, 0.29, 0.37], [0.12, 0.12, 0.1], materials.joint);
voxel(frontWing, [-0.28, 0, 0.14], [0.12, 0.12, 0.1], materials.joint);
// -------------------------------------------------------------------------
// Projected silhouette: project the bird's actual boxes from lamp to screen
// -------------------------------------------------------------------------
const silhouetteMaterial = new THREE.MeshBasicMaterial({
color: 0x211d1e,
side: THREE.DoubleSide,
depthWrite: false,
polygonOffset: true,
polygonOffsetFactor: -1,
polygonOffsetUnits: -1
});
const silhouetteMesh = new THREE.Mesh(
new THREE.BufferGeometry(),
silhouetteMaterial
);
silhouetteMesh.position.z = screen.z + 0.018;
silhouetteMesh.renderOrder = 4;
scene.add(silhouetteMesh);
const screenBounds = {
left: screen.centerX - screen.width / 2,
right: screen.centerX + screen.width / 2,
bottom: screen.centerY - screen.height / 2,
top: screen.centerY + screen.height / 2
};
function convexHull(points) {
points.sort((a, b) => a.x - b.x || a.y - b.y);
const cross = (o, a, b) =>
(a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
const lower = [];
for (const point of points) {
while (
lower.length >= 2 &&
cross(lower[lower.length - 2], lower[lower.length - 1], point) <= 0
) {
lower.pop();
}
lower.push(point);
}
const upper = [];
for (let i = points.length - 1; i >= 0; i--) {
const point = points[i];
while (
upper.length >= 2 &&
cross(upper[upper.length - 2], upper[upper.length - 1], point) <= 0
) {
upper.pop();
}
upper.push(point);
}
lower.pop();
upper.pop();
return lower.concat(upper);
}
function clipPolygon(polygon) {
const edges = [
{
inside: p => p.x >= screenBounds.left,
intersect: (a, b) => {
const t = (screenBounds.left - a.x) / (b.x - a.x);
return { x: screenBounds.left, y: a.y + (b.y - a.y) * t };
}
},
{
inside: p => p.x <= screenBounds.right,
intersect: (a, b) => {
const t = (screenBounds.right - a.x) / (b.x - a.x);
return { x: screenBounds.right, y: a.y + (b.y - a.y) * t };
}
},
{
inside: p => p.y >= screenBounds.bottom,
intersect: (a, b) => {
const t = (screenBounds.bottom - a.y) / (b.y - a.y);
return { x: a.x + (b.x - a.x) * t, y: screenBounds.bottom };
}
},
{
inside: p => p.y <= screenBounds.top,
intersect: (a, b) => {
const t = (screenBounds.top - a.y) / (b.y - a.y);
return { x: a.x + (b.x - a.x) * t, y: screenBounds.top };
}
}
];
let output = polygon;
for (const edge of edges) {
const input = output;
output = [];
for (let i = 0; i < input.length; i++) {
const current = input[i];
const previous = input[(i + input.length - 1) % input.length];
const currentInside = edge.inside(current);
const previousInside = edge.inside(previous);
if (currentInside) {
if (!previousInside) output.push(edge.intersect(previous, current));
output.push(current);
} else if (previousInside) {
output.push(edge.intersect(previous, current));
}
}
if (output.length === 0) break;
}
return output;
}
const corner = new THREE.Vector3();
function updateSilhouette() {
birdRoot.updateMatrixWorld(true);
const vertices = [];
for (const mesh of trackedBirdParts) {
if (!mesh.geometry.boundingBox) mesh.geometry.computeBoundingBox();
const boxInfo = mesh.geometry.boundingBox;
const projected = [];
for (let i = 0; i < 8; i++) {
corner.set(
i & 1 ? boxInfo.max.x : boxInfo.min.x,
i & 2 ? boxInfo.max.y : boxInfo.min.y,
i & 4 ? boxInfo.max.z : boxInfo.min.z
);
corner.applyMatrix4(mesh.matrixWorld);
const t =
(lampPosition.z - screen.z) /
(lampPosition.z - corner.z);
projected.push({
x: lampPosition.x + (corner.x - lampPosition.x) * t,
y: lampPosition.y + (corner.y - lampPosition.y) * t
});
}
const clipped = clipPolygon(convexHull(projected));
for (let i = 1; i < clipped.length - 1; i++) {
vertices.push(
clipped[0].x, clipped[0].y, 0,
clipped[i].x, clipped[i].y, 0,
clipped[i + 1].x, clipped[i + 1].y, 0
);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
silhouetteMesh.geometry.dispose();
silhouetteMesh.geometry = geometry;
}
// -------------------------------------------------------------------------
// Dust inside the beam
// -------------------------------------------------------------------------
const dustCount = 180;
const dustPositions = new Float32Array(dustCount * 3);
const dustBase = new Float32Array(dustCount * 3);
const right = new THREE.Vector3().crossVectors(
beamDirection,
new THREE.Vector3(0, 1, 0)
).normalize();
const up = new THREE.Vector3().crossVectors(right, beamDirection).normalize();
for (let i = 0; i < dustCount; i++) {
const t = 0.06 + Math.random() * 0.9;
const radius = Math.sqrt(Math.random()) * Math.tan(0.39) * beamLength * t;
const angle = Math.random() * Math.PI * 2;
const point = lampPosition
.clone()
.addScaledVector(beamDirection, beamLength * t)
.addScaledVector(right, Math.cos(angle) * radius)
.addScaledVector(up, Math.sin(angle) * radius);
dustPositions[i * 3] = point.x;
dustPositions[i * 3 + 1] = point.y;
dustPositions[i * 3 + 2] = point.z;
dustBase[i * 3] = point.x;
dustBase[i * 3 + 1] = point.y;
dustBase[i * 3 + 2] = point.z;
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0xffd39a,
size: 0.025,
transparent: true,
opacity: 0.28,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
scene.add(dust);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let frame = 0;
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
frame++;
birdRoot.position.y = 0.15 + Math.sin(elapsed * 1.15) * 0.022;
birdRoot.rotation.z = Math.sin(elapsed * 0.78) * 0.014;
frontWing.rotation.x = -0.08 + Math.sin(elapsed * 2.15) * 0.32;
backWing.rotation.x = 0.08 - Math.sin(elapsed * 2.15 + 0.4) * 0.28;
neckPivot.rotation.z = Math.sin(elapsed * 0.92) * 0.028;
headPivot.rotation.z = Math.sin(elapsed * 1.32 + 0.5) * 0.045;
tailPivot.rotation.z = Math.sin(elapsed * 0.87) * 0.055;
const dustAttribute = dustGeometry.attributes.position;
for (let i = 0; i < dustCount; i++) {
const index = i * 3;
dustAttribute.array[index] =
dustBase[index] + Math.sin(elapsed * 0.22 + i) * 0.018;
dustAttribute.array[index + 1] =
dustBase[index + 1] + Math.sin(elapsed * 0.4 + i * 0.7) * 0.035;
dustAttribute.array[index + 2] =
dustBase[index + 2] + Math.cos(elapsed * 0.18 + i) * 0.015;
}
dustAttribute.needsUpdate = true;
if (frame % 2 === 0) updateSilhouette();
renderer.render(scene, camera);
}
updateSilhouette();
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```