Prompt specification
Build or customize the Three.js 3D scene "Earth interior nesting model" 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: Earth interior nesting model
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/earth-interior-nesting-model-x1137
Scene Description Prompt:
Cut away a low-poly Earth to reveal crust, mantle, outer core, and inner core; label schematic thicknesses. Three.js from cdnjs, one HTML file.
Working Three.js Starter (earth-interior-nesting-model-x1137):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Earth — Planetary Section</title>
<style>
:root {
color-scheme: dark;
--ink: #eef7ff;
--muted: #9fb1c4;
--panel: rgba(8, 15, 25, 0.86);
--line: rgba(164, 199, 224, 0.18);
--crust: #68ddd5;
--mantle: #f0873e;
--outer: #ffc857;
--inner: #ffedae;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 38% 42%, #10273a 0%, #07111e 42%, #030711 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body::after {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(rgba(255,255,255,.018) 1px, transparent 1px),
linear-gradient(90deg, rgba(255,255,255,.018) 1px, transparent 1px);
background-size: 64px 64px;
mask-image: radial-gradient(circle, black, transparent 72%);
}
#viewport {
position: absolute;
inset: 0 330px 0 0;
overflow: hidden;
}
#scene {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene.dragging { cursor: grabbing; }
#leaders,
#callouts {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
#leaders { overflow: visible; }
.callout {
--accent: var(--crust);
position: absolute;
min-width: 112px;
padding: 8px 11px 8px 12px;
transform: translate(-50%, -50%);
border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
border-left: 3px solid var(--accent);
border-radius: 7px;
background: rgba(5, 11, 20, 0.84);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(10px);
transition: opacity 180ms ease;
white-space: nowrap;
}
.callout b {
display: block;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: .13em;
}
.callout span {
display: block;
margin-top: 2px;
color: #dce9f4;
font-size: 12px;
font-variant-numeric: tabular-nums;
}
#brand {
position: absolute;
z-index: 5;
top: 28px;
left: 30px;
pointer-events: none;
}
#brand .eyebrow {
margin: 0 0 7px;
color: #6bd8d0;
font-size: 10px;
font-weight: 800;
letter-spacing: .24em;
text-transform: uppercase;
}
#brand h1 {
margin: 0;
font-size: clamp(27px, 3vw, 46px);
font-weight: 560;
line-height: .98;
letter-spacing: -.045em;
}
#brand h1 span {
color: #8195a8;
font-weight: 350;
}
#brand p:last-child {
max-width: 310px;
margin: 12px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#controls {
position: absolute;
z-index: 10;
top: 26px;
right: 24px;
display: flex;
gap: 8px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 999px;
padding: 8px 13px;
background: rgba(8, 16, 27, 0.72);
color: #dceaf5;
font: inherit;
font-size: 11px;
letter-spacing: .025em;
cursor: pointer;
backdrop-filter: blur(10px);
transition: border-color 150ms ease, background 150ms ease;
}
button:hover {
border-color: rgba(104, 221, 213, .55);
background: rgba(15, 31, 46, .9);
}
button:focus-visible {
outline: 2px solid #68ddd5;
outline-offset: 2px;
}
#hint {
position: absolute;
z-index: 5;
left: 30px;
bottom: 24px;
color: #8196aa;
font-size: 10px;
letter-spacing: .06em;
pointer-events: none;
}
#hint span {
display: inline-block;
margin-right: 16px;
}
#hint span + span::before {
content: "•";
margin-right: 16px;
color: #456076;
}
#panel {
position: absolute;
z-index: 8;
top: 50%;
right: 24px;
width: 282px;
transform: translateY(-50%);
padding: 22px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 22px 70px rgba(0, 0, 0, .38);
backdrop-filter: blur(18px);
}
#panel .kicker {
margin: 0 0 7px;
color: #68ddd5;
font-size: 9px;
font-weight: 800;
letter-spacing: .2em;
text-transform: uppercase;
}
#panel h2 {
margin: 0;
font-size: 24px;
font-weight: 540;
letter-spacing: -.03em;
}
#panel .intro {
margin: 10px 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.scale-badge {
display: inline-block;
margin-left: 5px;
padding: 3px 6px;
border: 1px solid rgba(255, 200, 87, .28);
border-radius: 4px;
color: #ffd878;
font-size: 8px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
vertical-align: 2px;
}
#layerList {
display: grid;
gap: 3px;
}
.layer-row {
display: grid;
grid-template-columns: 12px 1fr auto;
gap: 10px;
align-items: center;
padding: 10px 0;
border-top: 1px solid rgba(164, 199, 224, .11);
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 14px currentColor;
}
.layer-row strong {
display: block;
font-size: 11px;
font-weight: 700;
letter-spacing: .035em;
}
.layer-row small {
display: block;
margin-top: 2px;
color: #8296aa;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.layer-row .depth {
color: #cbd8e4;
font-size: 10px;
font-variant-numeric: tabular-nums;
text-align: right;
}
#depthScale {
margin-top: 14px;
padding: 13px 14px;
border: 1px solid rgba(164, 199, 224, .12);
border-radius: 9px;
background: rgba(255,255,255,.025);
}
#depthScale .title {
margin-bottom: 9px;
color: #91a5b8;
font-size: 9px;
font-weight: 750;
letter-spacing: .12em;
text-transform: uppercase;
}
.scale-bar {
display: flex;
height: 7px;
overflow: hidden;
border-radius: 99px;
}
.scale-bar i { display: block; height: 100%; }
.scale-labels {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: #71869a;
font-size: 8px;
font-variant-numeric: tabular-nums;
}
#panel footer {
margin: 15px 0 0;
color: #72879b;
font-size: 9px;
line-height: 1.55;
}
#loading {
position: absolute;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #07111e;
color: #68ddd5;
font-size: 10px;
letter-spacing: .18em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.hidden { opacity: 0; pointer-events: none; }
@media (max-width: 900px) {
#viewport { inset: 0 0 258px 0; }
#panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
transform: none;
padding: 15px 17px;
max-height: 234px;
overflow: auto;
}
#panel h2 { font-size: 19px; }
#panel .intro { margin: 7px 0 10px; }
#layerList { grid-template-columns: 1fr 1fr; column-gap: 18px; }
.layer-row { padding: 7px 0; }
#depthScale, #panel footer { display: none; }
#brand { top: 18px; left: 18px; }
#brand h1 { font-size: 29px; }
#brand p:last-child { display: none; }
#controls { top: 18px; right: 14px; }
button { padding: 7px 10px; font-size: 10px; }
#hint { left: 18px; bottom: 12px; }
#hint span + span::before { margin-right: 8px; }
}
@media (max-width: 560px) {
#viewport { bottom: 272px; }
#brand .eyebrow { font-size: 8px; }
#brand h1 { font-size: 24px; }
#controls { top: 67px; }
#hint { font-size: 8px; }
.callout { min-width: 92px; padding: 6px 8px; }
.callout b { font-size: 8px; }
.callout span { font-size: 10px; }
}
</style>
</head>
<body>
<div id="viewport">
<canvas id="scene"></canvas>
<svg id="leaders" aria-hidden="true"></svg>
<div id="callouts" aria-hidden="true">
<div class="callout" id="label-crust" style="--accent: var(--crust)">
<b>CRUST</b><span>5–70 km</span>
</div>
<div class="callout" id="label-mantle" style="--accent: var(--mantle)">
<b>MANTLE</b><span>~2,890 km</span>
</div>
<div class="callout" id="label-outer" style="--accent: var(--outer)">
<b>OUTER CORE</b><span>~2,260 km</span>
</div>
<div class="callout" id="label-inner" style="--accent: var(--inner)">
<b>INNER CORE</b><span>r ≈ 1,220 km</span>
</div>
</div>
<header id="brand">
<p class="eyebrow">Earth / Structural section</p>
<h1>Inside a <span>living planet</span></h1>
<p>
A low-poly planetary cutaway showing the crust, mantle,
liquid outer core, and solid inner core.
</p>
</header>
<div id="controls">
<button id="reset" type="button">Reset view</button>
<button id="toggleLabels" type="button">Labels on</button>
</div>
<div id="hint">
<span>Drag to orbit</span>
<span>Scroll to zoom</span>
</div>
<div id="loading">Building planetary section</div>
</div>
<aside id="panel">
<p class="kicker">Interior model</p>
<h2>Earth, opened</h2>
<p class="intro">
Layer boundaries are enlarged and the cut angles are stepped
for visibility.
<span class="scale-badge">Not to scale</span>
</p>
<div id="layerList">
<div class="layer-row">
<i class="swatch" style="color:#68ddd5;background:#68ddd5"></i>
<div><strong>Crust</strong><small>Rock and ocean floor</small></div>
<div class="depth">5–70 km</div>
</div>
<div class="layer-row">
<i class="swatch" style="color:#f0873e;background:#f0873e"></i>
<div><strong>Mantle</strong><small>Silicate rock</small></div>
<div class="depth">~2,890 km</div>
</div>
<div class="layer-row">
<i class="swatch" style="color:#ffc857;background:#ffc857"></i>
<div><strong>Outer core</strong><small>Liquid iron–nickel</small></div>
<div class="depth">~2,260 km</div>
</div>
<div class="layer-row">
<i class="swatch" style="color:#ffedae;background:#ffedae"></i>
<div><strong>Inner core</strong><small>Solid iron–nickel</small></div>
<div class="depth">r ≈ 1,220 km</div>
</div>
</div>
<div id="depthScale">
<div class="title">Approximate depth from the surface</div>
<div class="scale-bar">
<i style="width:1.1%;background:#68ddd5"></i>
<i style="width:45.4%;background:#f0873e"></i>
<i style="width:35.5%;background:#ffc857"></i>
<i style="width:18%;background:#ffedae"></i>
</div>
<div class="scale-labels">
<span>0 km</span>
<span>2,890</span>
<span>5,150</span>
<span>6,371</span>
</div>
</div>
<footer>
Surface geography is diagrammatic. Radial thicknesses and cut
widths are exaggerated; numerical values are approximate.
</footer>
</aside>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const canvas = document.getElementById("scene");
const loading = document.getElementById("loading");
const leaders = document.getElementById("leaders");
const calloutLayer = document.getElementById("callouts");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x030711, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(37, 1, 0.1, 120);
const DEG = Math.PI / 180;
const model = new THREE.Group();
model.rotation.y = 0;
scene.add(model);
scene.add(new THREE.AmbientLight(0x8bb5d5, 1.25));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.3);
keyLight.position.set(-5, 8, 6);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x3b82ff, 2.4);
rimLight.position.set(7, 1, -6);
scene.add(rimLight);
const warmLight = new THREE.DirectionalLight(0xff9c52, 1.15);
warmLight.position.set(0, -5, 2);
scene.add(warmLight);
const coreLight = new THREE.PointLight(0xff9a45, 11, 8, 2);
scene.add(coreLight);
function seededRandom(seed) {
return function () {
seed |= 0;
seed = seed + 0x6D2B79F5 | 0;
let t = Math.imul(seed ^ seed >>> 15, 1 | seed);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
function addFacetColors(geometry, baseHex, variation = 0.045, seed = 1) {
const random = seededRandom(seed);
const base = new THREE.Color(baseHex);
const hsl = {};
base.getHSL(hsl);
const colors = [];
const count = geometry.attributes.position.count;
for (let i = 0; i < count; i += 3) {
const r = random() - 0.5;
const color = new THREE.Color().setHSL(
(hsl.h + (random() - 0.5) * 0.018 + 1) % 1,
THREE.MathUtils.clamp(hsl.s + (random() - 0.5) * 0.035, 0, 1),
THREE.MathUtils.clamp(hsl.l + r * variation, 0, 1)
);
for (let j = 0; j < 3; j++) {
colors.push(color.r, color.g, color.b);
}
}
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
}
/*
Builds an indexed, flat-shaded spherical sector.
Longitude 0 points toward +X; longitude 90° points toward +Z.
*/
function sphericalSector(radius, longitudeStart, longitudeSpan,
latitudeSegments = 20, longitudeSegments = 54) {
const positions = [];
const indices = [];
const ringStarts = [];
positions.push(0, -radius, 0);
for (let row = 1; row < latitudeSegments; row++) {
const latitude = -Math.PI / 2 + Math.PI * row / latitudeSegments;
const ringRadius = Math.cos(latitude) * radius;
ringStarts[row] = positions.length / 3;
for (let col = 0; col <= longitudeSegments; col++) {
const longitude =
longitudeStart + longitudeSpan * col / longitudeSegments;
positions.push(
ringRadius * Math.cos(longitude),
radius * Math.sin(latitude),
ringRadius * Math.sin(longitude)
);
}
}
const topIndex = positions.length / 3;
positions.push(0, radius, 0);
const firstRing = ringStarts[1];
for (let col = 0; col < longitudeSegments; col++) {
indices.push(
0,
firstRing + col,
firstRing + col + 1
);
}
for (let row = 1; row < latitudeSegments - 1; row++) {
for (let col = 0; col < longitudeSegments; col++) {
const a = ringStarts[row] + col;
const b = ringStarts[row + 1] + col;
const c = ringStarts[row + 1] + col + 1;
const d = ringStarts[row] + col + 1;
indices.push(a, b, d, b, c, d);
}
}
const lastRing = ringStarts[latitudeSegments - 1];
for (let col = 0; col < longitudeSegments; col++) {
indices.push(
lastRing + col,
topIndex,
lastRing + col + 1
);
}
const indexed = new THREE.BufferGeometry();
indexed.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
indexed.setIndex(indices);
const geometry = indexed.toNonIndexed();
indexed.dispose();
geometry.computeVertexNormals();
return geometry;
}
function meridianPoint(longitude, radius, angle) {
return new THREE.Vector3(
Math.cos(longitude) * radius * Math.cos(angle),
radius * Math.sin(angle),
Math.sin(longitude) * radius * Math.cos(angle)
);
}
/*
The exposed half-annulus on a meridian cut plane.
*/
function meridianCutFace(longitude, outerRadius, innerRadius, segments = 42) {
const positions = [];
for (let i = 0; i < segments; i++) {
const a = -Math.PI / 2 + Math.PI * i / segments;
const b = -Math.PI / 2 + Math.PI * (i + 1) / segments;
const outerA = meridianPoint(longitude, outerRadius, a);
const outerB = meridianPoint(longitude, outerRadius, b);
const innerA = meridianPoint(longitude, innerRadius, a);
const innerB = meridianPoint(longitude, innerRadius, b);
positions.push(
...outerA.toArray(),
...outerB.toArray(),
...innerA.toArray(),
...outerB.toArray(),
...innerB.toArray(),
...innerA.toArray()
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
function meridianDisk(longitude, radius, segments = 42) {
const positions = [];
for (let i = 0; i < segments; i++) {
const a = -Math.PI / 2 + Math.PI * i / segments;
const b = -Math.PI / 2 + Math.PI * (i + 1) / segments;
const outerA = meridianPoint(longitude, radius, a);
const outerB = meridianPoint(longitude, radius, b);
positions.push(
0, 0, 0,
...outerA.toArray(),
...outerB.toArray()
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
const layers = [
{
name: "Crust",
outerRadius: 3.00,
innerRadius: 2.88,
cutStart: -58 * DEG,
cutEnd: 42 * DEG,
surface: 0x226f89,
cut: 0x68ddd5,
emissive: 0x092d35,
emissiveIntensity: 0.07,
latitudeSegments: 22,
longitudeSegments: 60
},
{
name: "Mantle",
outerRadius: 2.88,
innerRadius: 1.48,
cutStart: -48 * DEG,
cutEnd: 32 * DEG,
surface: 0xc85631,
cut: 0xf0873e,
emissive: 0x481406,
emissiveIntensity: 0.12,
latitudeSegments: 21,
longitudeSegments: 54
},
{
name: "Outer core",
outerRadius: 1.48,
innerRadius: 0.72,
cutStart: -38 * DEG,
cutEnd: 22 * DEG,
surface: 0xf0a638,
cut: 0xffc857,
emissive: 0x7d3b05,
emissiveIntensity: 0.28,
latitudeSegments: 20,
longitudeSegments: 48
},
{
name: "Inner core",
outerRadius: 0.72,
innerRadius: 0,
cutStart: -28 * DEG,
cutEnd: 12 * DEG,
surface: 0xffdc78,
cut: 0xffedae,
emissive: 0x9c6811,
emissiveIntensity: 0.5,
latitudeSegments: 20,
longitudeSegments: 46
}
];
layers.forEach((layer, index) => {
const remainingSpan =
layer.cutStart + Math.PI * 2 - layer.cutEnd;
const surfaceGeometry = sphericalSector(
layer.outerRadius,
layer.cutEnd,
remainingSpan,
layer.latitudeSegments,
layer.longitudeSegments
);
addFacetColors(
surfaceGeometry,
layer.surface,
index === 0 ? 0.055 : 0.07,
100 + index * 31
);
const surfaceMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: index === 0 ? 0.92 : 0.78,
metalness: 0.02,
emissive: layer.emissive,
emissiveIntensity: layer.emissiveIntensity,
flatShading: true,
side: THREE.DoubleSide
});
const surfaceMesh = new THREE.Mesh(
surfaceGeometry,
surfaceMaterial
);
model.add(surfaceMesh);
if (index === 0) {
const wire = new THREE.LineSegments(
new THREE.WireframeGeometry(surfaceGeometry),
new THREE.LineBasicMaterial({
color: 0x9deaff,
transparent: true,
opacity: 0.07,
depthWrite: false
})
);
model.add(wire);
}
[layer.cutStart, layer.cutEnd].forEach((longitude, faceIndex) => {
const faceGeometry = layer.innerRadius > 0
? meridianCutFace(
longitude,
layer.outerRadius,
layer.innerRadius
)
: meridianDisk(longitude, layer.outerRadius);
addFacetColors(
faceGeometry,
layer.cut,
0.035,
400 + index * 17 + faceIndex
);
const faceMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.7,
metalness: 0.03,
emissive: layer.cut,
emissiveIntensity: index === 0 ? 0.035 : 0.09,
flatShading: true,
side: THREE.DoubleSide
});
model.add(new THREE.Mesh(faceGeometry, faceMaterial));
});
});
/*
Diagrammatic low-poly continents.
Polygons are triangulated, clipped around the cutaway, subdivided
in longitude/latitude, and then projected onto the crust.
*/
const continents = [
[
[-168,65],[-145,72],[-125,70],[-100,75],[-82,55],[-60,48],
[-68,30],[-82,25],[-97,18],[-108,30],[-118,32],[-125,48],[-150,58]
],
[
[-81,12],[-70,8],[-60,5],[-48,-2],[-35,-8],[-40,-22],
[-52,-35],[-67,-55],[-75,-45],[-79,-20]
],
[
[-10,36],[-8,58],[10,71],[30,70],[45,55],[40,40],[25,35],[10,37]
],
[
[-17,35],[8,37],[32,30],[50,12],[42,-12],[30,-35],
[18,-35],[8,-15],[-5,5],[-17,15]
],
[
[45,35],[60,70],[100,75],[140,65],[170,55],[160,35],
[130,20],[110,5],[95,20],[75,8],[60,25]
],
[
[113,-12],[123,-14],[136,-12],[153,-25],[146,-39],
[130,-36],[116,-34]
],
[
[-73,60],[-61,82],[-27,83],[-18,62],[-43,59]
]
];
function triangulateContour(points) {
const contour = points.map(
p => new THREE.Vector2(p[0], p[1])
);
let raw = [];
try {
raw = THREE.ShapeUtils.triangulateShape(contour, []);
} catch (_) {
raw = [];
}
const triangles = [];
// ShapeUtils can return a flat sequence of Vector2 vertices.
if (
raw.length &&
raw[0] &&
typeof raw[0].x === "number"
) {
for (let i = 0; i + 2 < raw.length; i += 3) {
triangles.push([raw[i], raw[i + 1], raw[i + 2]]);
}
} else if (
raw.length &&
Array.isArray(raw[0])
) {
raw.forEach(face => triangles.push(face.slice(0, 3)));
}
if (!triangles.length) {
const center = contour.reduce(
(sum, p) => sum.add(p),
new THREE.Vector2()
).multiplyScalar(1 / contour.length);
for (let i = 1; i < contour.length - 1; i++) {
triangles.push([center, contour[i], contour[i + 1]]);
}
}
return triangles;
}
function clipPolygon(polygon, boundary, keepGreater) {
if (!polygon.length) return [];
const output = [];
const inside = p => keepGreater
? p.x >= boundary
: p.x <= boundary;
const intersection = (a, b) => {
const t = (boundary - a.x) / (b.x - a.x);
return new THREE.Vector2(
boundary,
a.y + (b.y - a.y) * t
);
};
for (let i = 0; i < polygon.length; i++) {
const current = polygon[i];
const next = polygon[(i + 1) % polygon.length];
const currentInside = inside(current);
const nextInside = inside(next);
if (currentInside && nextInside) {
output.push(next.clone());
} else if (currentInside && !nextInside) {
output.push(intersection(current, next));
} else if (!currentInside && nextInside) {
output.push(intersection(current, next));
output.push(next.clone());
}
}
return output;
}
function subdivideTriangle(a, b, c, depth, result) {
const longest = Math.max(
a.distanceTo(b),
b.distanceTo(c),
c.distanceTo(a)
);
if (depth >= 6 || longest < 9) {
result.push([a, b, c]);
return;
}
const ab = new THREE.Vector2(
(a.x + b.x) / 2,
(a.y + b.y) / 2
);
const bc = new THREE.Vector2(
(b.x + c.x) / 2,
(b.y + c.y) / 2
);
const ca = new THREE.Vector2(
(c.x + a.x) / 2,
(c.y + a.y) / 2
);
subdivideTriangle(a, ab, ca, depth + 1, result);
subdivideTriangle(ab, b, bc, depth + 1, result);
subdivideTriangle(ca, bc, c, depth + 1, result);
subdivideTriangle(ab, bc, ca, depth + 1, result);
}
function spherePoint(lon, lat, radius) {
const longitude = lon * DEG;
const latitude = lat * DEG;
const ring = Math.cos(latitude) * radius;
return new THREE.Vector3(
ring * Math.cos(longitude),
radius * Math.sin(latitude),
ring * Math.sin(longitude)
);
}
function createLand() {
const positions = [];
const colors = [];
const random = seededRandom(90210);
continents.forEach(contour => {
const triangles = triangulateContour(contour);
triangles.forEach(triangle => {
const polygon = triangle.map(
p => new THREE.Vector2(p.x, p.y)
);
const pieces = [
clipPolygon(polygon, -58, false),
clipPolygon(polygon, 42, true)
].filter(piece => piece.length >= 3);
pieces.forEach(piece => {
const smallTriangles = [];
for (let i = 1; i < piece.length - 1; i++) {
subdivideTriangle(
piece[0],
piece[i],
piece[i + 1],
0,
smallTriangles
);
}
smallTriangles.forEach(tri => {
const color = new THREE.Color().setHSL(
0.285 + (random() - 0.5) * 0.045,
0.36 + random() * 0.12,
0.31 + (random() - 0.5) * 0.08
);
tri.forEach(vertex => {
const point = spherePoint(
vertex.x,
vertex.y,
3.018
);
positions.push(...point.toArray());
colors.push(color.r, color.g, color.b);
});
});
});
});
});
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.96,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
});
return new THREE.Mesh(geometry, material);
}
model.add(createLand());
// Thin atmospheric shell, also cut away.
const atmosphereGeometry = sphericalSector(
3.055,
layers[0].cutEnd,
layers[0].cutStart + Math.PI * 2 - layers[0].cutEnd,
14,
40
);
const atmosphere = new THREE.Mesh(
atmosphereGeometry,
new THREE.MeshBasicMaterial({
color: 0x5bcfff,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
model.add(atmosphere);
// Procedural star field.
const starCount = 1100;
const starPositions = new Float32Array(starCount * 3);
const starRandom = seededRandom(12345);
for (let i = 0; i < starCount; i++) {
const radius = 28 + starRandom() * 32;
const theta = starRandom() * Math.PI * 2;
const phi = Math.acos(2 * starRandom() - 1);
starPositions[i * 3] =
radius * Math.sin(phi) * Math.cos(theta);
starPositions[i * 3 + 1] =
radius * Math.cos(phi);
starPositions[i * 3 + 2] =
radius * Math.sin(phi) * Math.sin(theta);
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0xd7eaff,
size: 0.075,
transparent: true,
opacity: 0.72,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(stars);
/*
Direct camera controls: pointer orbit, wheel/pinch zoom.
No addons or example modules are used.
*/
let yaw = -8 * DEG;
let pitch = 16 * DEG;
let radius = 12.2;
let targetYaw = yaw;
let targetPitch = pitch;
let targetRadius = radius;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
targetYaw -= dx * 0.006;
targetPitch = THREE.MathUtils.clamp(
targetPitch + dy * 0.005,
-1.12,
1.12
);
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("wheel", event => {
event.preventDefault();
targetRadius = THREE.MathUtils.clamp(
targetRadius * Math.exp(event.deltaY * 0.001),
8.2,
18
);
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
function resetCamera() {
targetYaw = -8 * DEG;
targetPitch = 16 * DEG;
targetRadius = 12.2;
}
document.getElementById("reset").addEventListener("click", resetCamera);
let labelsVisible = true;
const labelToggle = document.getElementById("toggleLabels");
labelToggle.addEventListener("click", () => {
labelsVisible = !labelsVisible;
calloutLayer.style.display = labelsVisible ? "block" : "none";
leaders.style.display = labelsVisible ? "block" : "none";
labelToggle.textContent = labelsVisible ? "Labels on" : "Labels off";
});
window.addEventListener("keydown", event => {
if (event.key.toLowerCase() === "r") resetCamera();
if (event.key.toLowerCase() === "l") labelToggle.click();
});
/*
Screen-space labels with projected 3D leader anchors.
*/
const svgNS = "http://www.w3.org/2000/svg";
const callouts = [
{
element: document.getElementById("label-crust"),
anchor: meridianPoint(42 * DEG, 2.945, 50 * DEG),
offset: [-155, -65],
color: "#68ddd5"
},
{
element: document.getElementById("label-mantle"),
anchor: meridianPoint(32 * DEG, 2.23, 18 * DEG),
offset: [-165, 30],
color: "#f0873e"
},
{
element: document.getElementById("label-outer"),
anchor: meridianPoint(-38 * DEG, 1.12, 53 * DEG),
offset: [155, -65],
color: "#ffc857"
},
{
element: document.getElementById("label-inner"),
anchor: meridianPoint(-28 * DEG, 0.36, -32 * DEG),
offset: [145, 35],
color: "#ffedae"
}
];
callouts.forEach(item => {
item.line = document.createElementNS(svgNS, "line");
item.line.setAttribute("stroke", item.color);
item.line.setAttribute("stroke-width", "1");
item.line.setAttribute("stroke-opacity", "0.7");
item.dot = document.createElementNS(svgNS, "circle");
item.dot.setAttribute("r", "3.5");
item.dot.setAttribute("fill", item.color);
item.dot.setAttribute("stroke", "#07111e");
item.dot.setAttribute("stroke-width", "1.5");
leaders.appendChild(item.line);
leaders.appendChild(item.dot);
});
function updateLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const scale = width < 600 ? 0.64 : width < 900 ? 0.82 : 1;
callouts.forEach(item => {
const projected = item.anchor.clone().project(camera);
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
item.element.style.opacity = visible ? "1" : "0";
item.line.style.opacity = visible ? "1" : "0";
item.dot.style.opacity = visible ? "1" : "0";
if (!visible) return;
const sourceX = (projected.x * 0.5 + 0.5) * width;
const sourceY = (-projected.y * 0.5 + 0.5) * height;
const labelX = THREE.MathUtils.clamp(
sourceX + item.offset[0] * scale,
72,
width - 72
);
const labelY = THREE.MathUtils.clamp(
sourceY + item.offset[1] * scale,
58,
height - 58
);
item.element.style.left = `${labelX}px`;
item.element.style.top = `${labelY}px`;
item.line.setAttribute("x1", sourceX);
item.line.setAttribute("y1", sourceY);
item.line.setAttribute("x2", labelX);
item.line.setAttribute("y2", labelY);
item.dot.setAttribute("cx", sourceX);
item.dot.setAttribute("cy", sourceY);
});
}
function resize() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
leaders.setAttribute("viewBox", `0 0 ${width} ${height}`);
}
if ("ResizeObserver" in window) {
new ResizeObserver(resize).observe(viewport);
} else {
window.addEventListener("resize", resize);
}
resize();
function animate() {
requestAnimationFrame(animate);
yaw += (targetYaw - yaw) * 0.09;
pitch += (targetPitch - pitch) * 0.09;
radius += (targetRadius - radius) * 0.08;
const horizontal = Math.cos(pitch) * radius;
camera.position.set(
horizontal * Math.cos(yaw),
Math.sin(pitch) * radius,
horizontal * Math.sin(yaw)
);
camera.lookAt(0, 0, 0);
camera.updateMatrixWorld();
stars.rotation.y += 0.00012;
coreLight.intensity = 11 + Math.sin(performance.now() * 0.0014) * 0.65;
renderer.render(scene, camera);
updateLabels();
}
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 450);
});
</script>
</body>
</html>
```