Prompt specification
Build or customize the Three.js 3D scene "Pottery fragment in a labeled soil square" 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: Pottery fragment in a labeled soil square
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pottery-fragment-in-a-labeled-soil-square-x2257
Scene Description Prompt:
Place a pottery fragment partly exposed in a small excavation square. Show its position within a soil layer and include a scale without inventing an exact date. Use Three.js from cdnjs in a single HTML file, with stylized solid shapes.
Working Three.js Starter (pottery-fragment-in-a-labeled-soil-square-x2257):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pottery Fragment — Excavation Cutaway</title>
<style>
:root {
--ink: #302820;
--muted: #756c61;
--paper: rgba(247, 242, 232, 0.92);
--line: rgba(61, 47, 35, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #ddd6ca;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--line);
background: var(--paper);
box-shadow: 0 12px 36px rgba(52, 39, 28, 0.11);
backdrop-filter: blur(12px);
}
.title-panel {
top: 24px;
left: 24px;
width: min(360px, calc(100vw - 48px));
padding: 18px 20px 17px;
border-radius: 4px;
}
.eyebrow {
margin-bottom: 9px;
color: #9a4f31;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 3vw, 35px);
font-weight: 500;
line-height: 1.04;
letter-spacing: -0.025em;
}
.subtitle {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.context-note {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 13px;
padding: 6px 8px;
border: 1px solid rgba(96, 69, 48, 0.14);
background: rgba(255, 255, 255, 0.42);
color: #68594c;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.context-note::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: #a96342;
content: "";
}
.legend {
left: 24px;
bottom: 24px;
width: 245px;
padding: 15px 17px 14px;
border-radius: 4px;
}
.legend-title {
margin-bottom: 10px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 18px 1fr;
align-items: center;
gap: 9px;
margin-top: 7px;
font-size: 11px;
}
.swatch {
width: 18px;
height: 9px;
border: 1px solid rgba(0, 0, 0, 0.09);
}
.swatch.topsoil {
background: #514033;
}
.swatch.upper {
background: #86583c;
}
.swatch.compact {
background: #ae754d;
}
.swatch.lower {
background: #c39a6d;
}
.legend-foot {
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.controls {
top: 24px;
right: 24px;
display: flex;
gap: 8px;
}
button {
min-height: 38px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 3px;
background: var(--paper);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
box-shadow: 0 8px 25px rgba(52, 39, 28, 0.08);
}
button:hover {
background: #fffaf1;
}
.hint {
right: 24px;
bottom: 24px;
padding: 10px 13px;
border-radius: 3px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.03em;
}
.world-label {
position: fixed;
z-index: 4;
pointer-events: none;
white-space: nowrap;
}
#artifact-label {
padding: 10px 12px 10px 14px;
border-left: 3px solid #a85e3d;
background: rgba(248, 243, 233, 0.95);
box-shadow: 0 8px 25px rgba(52, 39, 28, 0.13);
transform: translate(22px, -50%);
}
#artifact-label::before {
position: absolute;
top: 50%;
right: 100%;
width: 22px;
height: 1px;
background: #8c5a41;
content: "";
}
#artifact-label.flip {
border-right: 3px solid #a85e3d;
border-left: 0;
transform: translate(calc(-100% - 22px), -50%);
}
#artifact-label.flip::before {
right: auto;
left: 100%;
}
.label-title {
color: #3d3027;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.label-detail {
margin-top: 3px;
color: #7a695a;
font-size: 10px;
}
#scale-label {
padding: 6px 8px;
border: 1px solid rgba(52, 39, 28, 0.12);
background: rgba(247, 242, 232, 0.9);
color: #5f5146;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
transform: translate(-50%, -160%);
}
@media (max-width: 700px) {
.title-panel {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 14px 16px;
}
.subtitle {
display: none;
}
.legend {
left: 14px;
bottom: 14px;
width: 205px;
}
.legend-foot,
.hint {
display: none;
}
.controls {
top: auto;
right: 14px;
bottom: 14px;
}
#artifact-label {
max-width: 190px;
white-space: normal;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive three-dimensional excavation cutaway"></main>
<section class="panel title-panel">
<div class="eyebrow">Excavation cutaway · object 01</div>
<h1>Pottery fragment in stratigraphic context</h1>
<p class="subtitle">
A stylized section view showing the sherd partly exposed from the
upper subsoil layer.
</p>
<div class="context-note">Relative context · no date assigned</div>
</section>
<section class="panel legend" aria-label="Soil layer legend">
<div class="legend-title">Soil sequence</div>
<div class="legend-row">
<span class="swatch topsoil"></span>
<span>Topsoil</span>
</div>
<div class="legend-row">
<span class="swatch upper"></span>
<span>Upper subsoil</span>
</div>
<div class="legend-row">
<span class="swatch compact"></span>
<span>Compact clay</span>
</div>
<div class="legend-row">
<span class="swatch lower"></span>
<span>Lower deposit</span>
</div>
<div class="legend-foot">
The square boundary marks the excavation area. The open corner is a
schematic cutaway used to reveal the fragment’s depth.
</div>
</section>
<div class="controls">
<button id="reset-view" type="button">Reset view</button>
</div>
<div class="panel hint">Drag to orbit · scroll to zoom</div>
<div id="artifact-label" class="world-label">
<div class="label-title">Pottery fragment</div>
<div class="label-detail">Partly exposed · upper subsoil</div>
</div>
<div id="scale-label" class="world-label">0–40 cm scale</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.04;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Stylized 3D excavation square with a partly exposed pottery fragment"
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color("#ddd6ca");
scene.fog = new THREE.Fog("#ddd6ca", 5.5, 11);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.02,
50
);
const target = new THREE.Vector3(0, -0.16, 0);
const controls = {
theta: 0.78,
phi: 1.03,
radius: 2.25,
dragging: false,
pointerX: 0,
pointerY: 0
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
target.x + controls.radius * sinPhi * Math.sin(controls.theta),
target.y + controls.radius * Math.cos(controls.phi),
target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xfff7e8,
0x6d5a48,
2.15
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d7, 3.2);
keyLight.position.set(-2.8, 4.6, 3.4);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -2;
keyLight.shadow.camera.right = 2;
keyLight.shadow.camera.top = 2;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 9;
keyLight.shadow.bias = -0.0004;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xd8e3eb, 0.85);
fillLight.position.set(3, 2, -3);
scene.add(fillLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xd8d0c2,
roughness: 1
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(20, 20),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.475;
ground.receiveShadow = true;
scene.add(ground);
const layers = [
{
name: "Topsoil",
top: 0,
bottom: -0.075,
color: 0x514033
},
{
name: "Upper subsoil",
top: -0.075,
bottom: -0.21,
color: 0x86583c
},
{
name: "Compact clay",
top: -0.21,
bottom: -0.33,
color: 0xae754d
},
{
name: "Lower deposit",
top: -0.33,
bottom: -0.46,
color: 0xc39a6d
}
];
// Each layer has an L-shaped footprint. The missing front-right
// quadrant acts as a cutaway, revealing the internal soil sequence.
function addSoilLayer(layer) {
const height = layer.top - layer.bottom;
const material = new THREE.MeshStandardMaterial({
color: layer.color,
roughness: 0.98,
metalness: 0
});
const left = new THREE.Mesh(
new THREE.BoxGeometry(0.65, height, 1.3),
material
);
left.position.set(-0.325, (layer.top + layer.bottom) / 2, 0);
left.castShadow = true;
left.receiveShadow = true;
scene.add(left);
const backRight = new THREE.Mesh(
new THREE.BoxGeometry(0.65, height, 0.65),
material
);
backRight.position.set(0.325, (layer.top + layer.bottom) / 2, -0.325);
backRight.castShadow = true;
backRight.receiveShadow = true;
scene.add(backRight);
}
layers.forEach(addSoilLayer);
// Deterministic pseudo-random detail for clods and inclusions.
let seed = 4281;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
function randomBetween(min, max) {
return min + (max - min) * random();
}
const inclusionMaterials = [
new THREE.MeshStandardMaterial({
color: 0x49372d,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x6f5039,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb98255,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xd0a06e,
roughness: 1,
flatShading: true
})
];
const pebbleGeometry = new THREE.DodecahedronGeometry(1, 0);
// Low-poly clods scattered over the surviving top surface.
for (let i = 0; i < 44; i++) {
let x = randomBetween(-0.61, 0.61);
let z = randomBetween(-0.61, 0.61);
// Do not place objects in the cutaway or over the ruler.
if (x > 0 && z > 0) continue;
if (x < -0.08 && x > -0.58 && z > 0.25 && z < 0.39) continue;
const radius = randomBetween(0.012, 0.033);
const pebble = new THREE.Mesh(
pebbleGeometry,
inclusionMaterials[random() > 0.72 ? 1 : 0]
);
pebble.position.set(x, 0.004, z);
pebble.scale.set(
radius * randomBetween(0.8, 1.4),
radius * randomBetween(0.35, 0.65),
radius * randomBetween(0.8, 1.4)
);
pebble.rotation.set(
random() * Math.PI,
random() * Math.PI,
random() * Math.PI
);
pebble.castShadow = true;
pebble.receiveShadow = true;
scene.add(pebble);
}
// Small stones embedded in the two exposed section faces.
for (let i = 0; i < 38; i++) {
const onZFace = i % 2 === 0;
const y = randomBetween(-0.435, -0.09);
const radius = randomBetween(0.009, 0.023);
let layerIndex = 0;
if (y < -0.33) layerIndex = 3;
else if (y < -0.21) layerIndex = 2;
else if (y < -0.075) layerIndex = 1;
const pebble = new THREE.Mesh(
pebbleGeometry,
inclusionMaterials[layerIndex]
);
if (onZFace) {
const x = randomBetween(0.055, 0.605);
// Keep the artifact area visually distinct.
if (Math.abs(x - 0.25) < 0.105 && y > -0.215 && y < -0.065) {
continue;
}
pebble.position.set(x, y, 0.006);
pebble.scale.set(
radius * 1.35,
radius * 0.7,
radius * 0.42
);
} else {
const z = randomBetween(0.055, 0.605);
pebble.position.set(0.006, y, z);
pebble.scale.set(
radius * 0.42,
radius * 0.7,
radius * 1.35
);
}
pebble.rotation.set(
random() * Math.PI,
random() * Math.PI,
random() * Math.PI
);
pebble.castShadow = true;
scene.add(pebble);
}
// A slightly darker contact area around the partly exposed sherd.
const contactMaterial = new THREE.MeshStandardMaterial({
color: 0x493126,
roughness: 1,
flatShading: true
});
const contactArea = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.105, 1),
contactMaterial
);
contactArea.position.set(0.25, -0.142, 0.003);
contactArea.scale.set(1.18, 0.72, 0.07);
contactArea.rotation.set(0.1, 0.15, -0.08);
contactArea.receiveShadow = true;
scene.add(contactArea);
// Curved, broken pottery fragment.
function shardBounds(index, count) {
const bottom =
0.006 +
0.006 * Math.sin(index * 1.73) +
0.003 * Math.cos(index * 0.71);
const top =
0.11 -
0.006 +
0.007 * Math.sin(index * 2.21 + 0.6) +
0.004 * Math.cos(index * 1.11);
return { bottom, top };
}
function createShardGeometry(
radius = 0.115,
thickness = 0.012,
height = 0.11,
thetaSpan = 1.76,
nTheta = 16,
nY = 4
) {
const positions = [];
const outerIndices = [];
const innerIndices = [];
const edgeIndices = [];
const row = nTheta + 1;
const surfaceVertexCount = row * (nY + 1);
for (let surface = 0; surface < 2; surface++) {
const rr = surface === 0 ? radius : radius - thickness;
for (let j = 0; j <= nY; j++) {
const v = j / nY;
for (let i = 0; i <= nTheta; i++) {
const theta =
-thetaSpan / 2 + (thetaSpan * i) / nTheta;
const bounds = shardBounds(i, nTheta);
const y =
bounds.bottom + (bounds.top - bounds.bottom) * v;
const unevenRadius =
rr + 0.0012 * Math.sin(i * 1.87 + j * 0.63);
const x = unevenRadius * Math.sin(theta);
const z =
unevenRadius * Math.cos(theta) -
radius +
0.0015 * Math.sin(i * 1.3 + j * 0.8);
positions.push(x, y, z);
}
}
}
function index(surface, j, i) {
return (
surface * surfaceVertexCount +
j * row +
i
);
}
function pushQuad(target, a, b, c, d) {
target.push(a, b, c, a, c, d);
}
for (let j = 0; j < nY; j++) {
for (let i = 0; i < nTheta; i++) {
const a = index(0, j, i);
const b = index(0, j, i + 1);
const c = index(0, j + 1, i);
const d = index(0, j + 1, i + 1);
outerIndices.push(a, b, c, b, d, c);
const ia = index(1, j, i);
const ib = index(1, j, i + 1);
const ic = index(1, j + 1, i);
const id = index(1, j + 1, i + 1);
innerIndices.push(ia, ic, ib, ib, ic, id);
}
}
// Broken upper and lower edges.
for (let i = 0; i < nTheta; i++) {
pushQuad(
edgeIndices,
index(0, nY, i),
index(0, nY, i + 1),
index(1, nY, i + 1),
index(1, nY, i)
);
pushQuad(
edgeIndices,
index(1, 0, i),
index(1, 0, i + 1),
index(0, 0, i + 1),
index(0, 0, i)
);
}
// Broken side edges.
for (let j = 0; j < nY; j++) {
pushQuad(
edgeIndices,
index(0, j, 0),
index(0, j + 1, 0),
index(1, j + 1, 0),
index(1, j, 0)
);
pushQuad(
edgeIndices,
index(1, j, nTheta),
index(1, j + 1, nTheta),
index(0, j + 1, nTheta),
index(0, j, nTheta)
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex([
...outerIndices,
...innerIndices,
...edgeIndices
]);
geometry.addGroup(0, outerIndices.length, 0);
geometry.addGroup(
outerIndices.length,
innerIndices.length,
1
);
geometry.addGroup(
outerIndices.length + innerIndices.length,
edgeIndices.length,
2
);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
function createPotteryBandGeometry(
radius = 0.115,
thetaSpan = 1.76,
nTheta = 16
) {
const positions = [];
const indices = [];
for (let j = 0; j < 2; j++) {
const v = j === 0 ? 0.46 : 0.61;
for (let i = 0; i <= nTheta; i++) {
const theta =
-thetaSpan / 2 + (thetaSpan * i) / nTheta;
const bounds = shardBounds(i, nTheta);
const y =
bounds.bottom + (bounds.top - bounds.bottom) * v;
const rr = radius + 0.0018;
const x = rr * Math.sin(theta);
const z = rr * Math.cos(theta) - radius;
positions.push(x, y, z);
}
}
const row = nTheta + 1;
for (let i = 0; i < nTheta; i++) {
const a = i;
const b = i + 1;
const c = row + i;
const d = row + i + 1;
indices.push(a, b, c, b, d, c);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const potteryMaterials = [
new THREE.MeshStandardMaterial({
color: 0xa96543,
roughness: 0.88,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x70402f,
roughness: 0.95,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0xc17a55,
roughness: 0.94,
side: THREE.DoubleSide
})
];
const shard = new THREE.Mesh(
createShardGeometry(),
potteryMaterials
);
shard.castShadow = true;
shard.receiveShadow = true;
const shardGroup = new THREE.Group();
shardGroup.add(shard);
const paintedBand = new THREE.Mesh(
createPotteryBandGeometry(),
new THREE.MeshStandardMaterial({
color: 0x693b2d,
roughness: 0.96,
side: THREE.DoubleSide
})
);
paintedBand.castShadow = true;
shardGroup.add(paintedBand);
// The central face projects from the cut while the broken sides
// remain embedded in the upper subsoil.
shardGroup.position.set(0.25, -0.194, 0.043);
shardGroup.rotation.set(-0.025, 0.075, -0.035);
scene.add(shardGroup);
// Four corner stakes define the original square excavation.
const stakeMaterial = new THREE.MeshStandardMaterial({
color: 0x8d6747,
roughness: 0.9
});
const stringMaterial = new THREE.MeshStandardMaterial({
color: 0xd9c59f,
roughness: 1
});
const corner = 0.665;
const corners = [
new THREE.Vector3(-corner, 0, -corner),
new THREE.Vector3(corner, 0, -corner),
new THREE.Vector3(corner, 0, corner),
new THREE.Vector3(-corner, 0, corner)
];
function cylinderBetween(a, b, radius, material, radialSegments = 7) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments
),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
return mesh;
}
corners.forEach((point) => {
const stake = new THREE.Mesh(
new THREE.CylinderGeometry(0.009, 0.012, 0.145, 7),
stakeMaterial
);
stake.position.set(point.x, 0.065, point.z);
stake.rotation.z = randomBetween(-0.05, 0.05);
stake.castShadow = true;
scene.add(stake);
});
for (let i = 0; i < corners.length; i++) {
const a = corners[i].clone();
const b = corners[(i + 1) % corners.length].clone();
a.y = 0.135;
b.y = 0.135;
scene.add(
cylinderBetween(
a,
b,
0.0025,
stringMaterial,
6
)
);
}
// A 40 cm alternating scale bar placed directly on the top surface.
const rulerCream = new THREE.MeshStandardMaterial({
color: 0xf3ead7,
roughness: 0.85
});
const rulerDark = new THREE.MeshStandardMaterial({
color: 0x352f2a,
roughness: 0.9
});
const rulerStartX = -0.52;
const rulerZ = 0.315;
const segmentLength = 0.05;
for (let i = 0; i < 8; i++) {
const segment = new THREE.Mesh(
new THREE.BoxGeometry(segmentLength, 0.012, 0.043),
i % 2 === 0 ? rulerCream : rulerDark
);
segment.position.set(
rulerStartX + segmentLength * (i + 0.5),
0.013,
rulerZ
);
segment.castShadow = true;
segment.receiveShadow = true;
scene.add(segment);
}
// End ticks.
[-0.52, -0.12].forEach((x) => {
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.003, 0.018, 0.061),
rulerDark
);
tick.position.set(x, 0.017, rulerZ);
tick.castShadow = true;
scene.add(tick);
});
// Screen-space labels remain attached to their 3D anchor points.
const artifactLabel = document.querySelector("#artifact-label");
const scaleLabel = document.querySelector("#scale-label");
const artifactAnchor = new THREE.Vector3(0.25, -0.14, 0.09);
const scaleAnchor = new THREE.Vector3(-0.32, 0.035, 0.315);
const projected = new THREE.Vector3();
function positionLabel(element, worldPoint, isArtifact = false) {
projected.copy(worldPoint).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
if (isArtifact) {
artifactLabel.classList.toggle(
"flip",
x > window.innerWidth * 0.66
);
}
}
// Custom orbit controls implemented directly without addons.
renderer.domElement.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const dx = event.clientX - controls.pointerX;
const dy = event.clientY - controls.pointerY;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi + dy * 0.005,
0.38,
1.42
);
updateCamera();
});
function endDrag(event) {
controls.dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius + event.deltaY * 0.0014,
1.55,
4.1
);
updateCamera();
},
{ passive: false }
);
document.querySelector("#reset-view").addEventListener("click", () => {
controls.theta = 0.78;
controls.phi = 1.03;
controls.radius = 2.25;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
});
renderer.setAnimationLoop(() => {
positionLabel(artifactLabel, artifactAnchor, true);
positionLabel(scaleLabel, scaleAnchor);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```