THE THREE.JS
DONUT PICNIC
CUBES, SHAPES, MATERIALS & GROUPS
By Shane Brumback · · Updated · Free Three.js tutorial · Cube Series Part 3
To build a Three.js scene from many shapes, make one Mesh per geometry, give each a material, collect related meshes in a THREE.Group, and move them every frame in the render loop. Here a glossy iced donut sits on a picnic table in a low-poly meadow while a teal cube, torus knot, sphere, icosahedron, cone and cylinder bob and spin around it.
You will use TorusGeometry, TorusKnotGeometry, metal and clearcoat materials, and THREE.Group. The scene behind this page is the live demo, and the full copy-paste source is below.
PART 3 OF THE BEGINNER SERIES
Prerequisite: finish Parts 1 and 2 first. This step assumes you can already make a scene, camera, renderer and spinning mesh (Part 1) and add lights, shadows, a ground and OrbitControls (Part 2). Now we put many shapes and materials together into one real scene.
LIVE DEMO
The donut picnic behind this page is a live Three.js scene. Launch the full-screen demo to drag to orbit and scroll to zoom around the donut and its floating shapes. Launch asks for a free Google sign-in first. Press Esc or Exit to return.
Free Google sign-in · Orbit: drag · Zoom: scroll · Esc to exit
What You Will Learn
In Part 1 you spun one cube. In Part 2 you lit it and gave it a floor. A real scene is many objects working together, so this step teaches the three ideas you need for that:
- More geometries: TorusGeometry (the donut), BoxGeometry (the teal cube), TorusKnotGeometry, SphereGeometry, IcosahedronGeometry, ConeGeometry and CylinderGeometry, plus BoxGeometry and CapsuleGeometry for the table and sprinkles.
- Mixing materials: MeshStandardMaterial with
metalnessandroughnessfor dough, gold and chrome; MeshPhysicalMaterial withclearcoatfor icing and a candy-shell knot. - Grouping and animation: THREE.Group to build trees, a table and a donut from parts, and a simple bob-and-spin loop that gives every object its own rhythm.
The meadow, sky, pine trees and rocks match Level 1 and Level 2 of the Cube Series — same hills and trees, with the picnic table and shapes added on top.
Step-by-Step Code Breakdown
Every excerpt below is copied exactly from the full code further down.
1. Set up the scene, camera and renderer
In plain words: we make an empty 3D world, a camera to look at it, and a renderer that draws it with soft shadows and warm filmic color. This is the same setup as steps 1 and 2 of the series, plus shadows and tone mapping. Create a Scene, a PerspectiveCamera and a WebGLRenderer, turn on PCF soft shadow maps and ACES tone mapping, and cap the pixel ratio on phones.
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 2000);
camera.position.set(0, 3.6, 10);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, isMobile ? 1.5 : 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 0.8;
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);
2. Paint the sky and float clouds
In plain words: the sky is the same soft blue as Levels 1 and 2. One Color fills scene.background and the Fog, so far hills melt into the sky. Low-poly cloud puffs drift as an InstancedMesh, matching Level 2.
const SKY = new THREE.Color(0x8ec5f0);
scene.background = SKY;
scene.fog = new THREE.Fog(SKY, 22, 60);
3. Light it with a soft sun
In plain words: a sky light fills everything softly and one strong sun throws the shadows — the same HemisphereLight and DirectionalLight setup as Level 2. Keep RoomEnvironment so the glossy icing and plate have something to reflect.
const pmrem = new THREE.PMREMGenerator(renderer);
scene.environment = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;
scene.add(new THREE.HemisphereLight(0xcfe6ff, 0x3f6b2a, 1.1));
const sun = new THREE.DirectionalLight(0xfff1d6, 2.6);
sun.position.set(-7, 13, 5);
sun.castShadow = true;
sun.shadow.mapSize.set(isMobile ? 1024 : 2048, isMobile ? 1024 : 2048);
sun.shadow.camera.left = sun.shadow.camera.bottom = -12;
sun.shadow.camera.right = sun.shadow.camera.top = 12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0005;
sun.shadow.radius = 3;
scene.add(sun);
4. Build the meadow: grass, hills and trees
In plain words: this is the same meadow as Levels 1 and 2. A PlaneGeometry with many segments is pushed up by heightAt so the middle stays flat for the picnic table and gentle hills rise toward the edges. Pine trees are a cylinder trunk with three stacked cones; rocks are dodecahedrons sitting on heightAt. The drifting cloud puffs are the same InstancedMesh helper as Level 2.
function heightAt(x, z) {
const d = Math.hypot(x, z);
const rise = THREE.MathUtils.smoothstep(d, 5, 22);
return rise * (2.5 + Math.sin(x * 0.35) * 1.6 + Math.cos(z * 0.3) * 1.4 + Math.sin((x + z) * 0.8) * 0.4);
}
const groundGeo = new THREE.PlaneGeometry(90, 90, 70, 70);
groundGeo.rotateX(-Math.PI / 2);
const pos = groundGeo.attributes.position;
for (let i = 0; i < pos.count; i++) {
pos.setY(i, heightAt(pos.getX(i), pos.getZ(i)));
}
groundGeo.computeVertexNormals();
const ground = new THREE.Mesh(
groundGeo,
new THREE.MeshStandardMaterial({ color: 0x5c9a3c, roughness: 1, flatShading: true })
);
ground.receiveShadow = true;
scene.add(ground);
const flat = (color) => new THREE.MeshStandardMaterial({ color, roughness: 0.9, flatShading: true });
const trunkMat = flat(0x6b4a2b);
const leafMat = flat(0x2f6b3a);
const rockMat = flat(0x8a8f96);
function addTree(x, z, s) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.15, 0.22, 1, 6), trunkMat);
trunk.position.y = 0.5;
tree.add(trunk);
for (let i = 0; i < 3; i++) {
const leaves = new THREE.Mesh(new THREE.ConeGeometry(1.1 - i * 0.25, 1.4, 7), leafMat);
leaves.position.y = 1.3 + i * 0.7;
tree.add(leaves);
}
tree.traverse((o) => { o.castShadow = true; });
tree.position.set(x, heightAt(x, z) - 0.05, z);
tree.scale.setScalar(s);
scene.add(tree);
}
[[-4, -3, 1.2], [-2, -6.5, 1.5], [2.5, -7, 1.4], [5.5, -5.5, 1.6], [-6.5, 1.5, 1.3],
[-5, 6, 1.4], [0, -11, 1.8], [-9, -7, 1.7], [9, -9, 1.9], [11, 2, 1.6]]
.forEach(([x, z, s]) => addTree(x, z, s));
function addRock(x, z, s) {
const rock = new THREE.Mesh(new THREE.DodecahedronGeometry(s, 0), rockMat);
rock.position.set(x, heightAt(x, z) + s * 0.35, z);
rock.rotation.set(x, z, x + z);
rock.castShadow = rock.receiveShadow = true;
scene.add(rock);
}
addRock(2.2, 1.6, 0.45);
addRock(2.8, 1.1, 0.25);
addRock(-2.4, 2.4, 0.55);
addRock(1.2, -2.8, 0.35);
const cloudMat = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1, flatShading: true, emissive: 0xffffff, emissiveIntensity: 0.35 });
const CLOUDS = isMobile ? 3 : 5;
const PUFFS = 5;
const clouds = new THREE.InstancedMesh(new THREE.IcosahedronGeometry(1, 1), cloudMat, CLOUDS * PUFFS);
clouds.frustumCulled = false;
scene.add(clouds);
const dummy = new THREE.Object3D();
function updateClouds(t) {
for (let c = 0; c < CLOUDS; c++) {
const x = ((-30 + c * 13 + (0.35 + (c % 3) * 0.12) * t + 32) % 64) - 32;
for (let p = 0; p < PUFFS; p++) {
const s = 2 - Math.abs(p - 2) * 0.4;
dummy.position.set(x + (p - 2) * 1.5, 7.5 + (c % 3) * 1.2 + (p % 2) * 0.3, -16 - (c % 2) * 6);
dummy.scale.set(s, s * 0.55, s);
dummy.updateMatrix();
clouds.setMatrixAt(c * PUFFS + p, dummy.matrix);
}
}
clouds.instanceMatrix.needsUpdate = true;
}
5. Set the picnic table
In plain words: a table is just boxes: one flat box for the top and four thin boxes for legs. A red cloth and a shiny plate finish it off, all inside one group so it moves as one piece. Group BoxGeometry parts into a table, add a thin box tablecloth and a CylinderGeometry plate with a clearcoat MeshPhysicalMaterial, then turn on castShadow and receiveShadow for every child with traverse().
const wood = new THREE.MeshStandardMaterial({ color: 0xb07a45, roughness: 0.75 });
const table = new THREE.Group();
const top = new THREE.Mesh(new THREE.BoxGeometry(4.4, 0.16, 2.2), wood);
top.position.y = 1.1;
table.add(top);
for (const sx of [-1.8, 1.8]) {
for (const sz of [-0.85, 0.85]) {
const leg = new THREE.Mesh(new THREE.BoxGeometry(0.14, 1.05, 0.14), wood);
leg.position.set(sx, 0.52, sz);
table.add(leg);
}
}
const cloth = new THREE.Mesh(
new THREE.BoxGeometry(2.4, 0.02, 2.24),
new THREE.MeshStandardMaterial({ color: 0xe8525f, roughness: 0.9 })
);
cloth.position.y = 1.19;
table.add(cloth);
const plate = new THREE.Mesh(
new THREE.CylinderGeometry(0.95, 0.8, 0.06, 32),
new THREE.MeshPhysicalMaterial({ color: 0xffffff, roughness: 0.2, clearcoat: 1 })
);
plate.position.y = 1.23;
table.add(plate);
table.traverse((o) => { o.castShadow = true; o.receiveShadow = true; });
scene.add(table);
6. Make the hero donut
In plain words: the donut is two rings stacked: a matte dough ring and a slightly fatter, flattened, glossy pink icing ring, sprinkled with tiny capsules. Grouping them lets us move and spin the whole donut at once. Combine two TorusGeometry meshes (MeshStandardMaterial dough, MeshPhysicalMaterial icing with clearcoat), add CapsuleGeometry sprinkles, tilt the donut group, and nest it in a hero group that the loop can bob.
const donut = new THREE.Group();
const dough = new THREE.Mesh(
new THREE.TorusGeometry(0.62, 0.3, 24, 64),
new THREE.MeshStandardMaterial({ color: 0xd9a25f, roughness: 0.65, metalness: 0 })
);
const icing = new THREE.Mesh(
new THREE.TorusGeometry(0.62, 0.315, 24, 64),
new THREE.MeshPhysicalMaterial({ color: 0xff7eb6, roughness: 0.35, clearcoat: 1, clearcoatRoughness: 0.1 })
);
icing.position.z = 0.07;
icing.scale.z = 0.8;
donut.add(dough, icing);
const sprinkleGeo = new THREE.CapsuleGeometry(0.02, 0.09, 2, 6);
const sprinkleColors = [0xffffff, 0xffd84a, 0x4ad6ff, 0x8a5cff, 0x5cff8a];
for (let i = 0; i < 46; i++) {
const a = Math.random() * Math.PI * 2, r = 0.62 + (Math.random() - 0.5) * 0.4;
const sprinkle = new THREE.Mesh(sprinkleGeo, new THREE.MeshStandardMaterial({ color: sprinkleColors[i % 5], roughness: 0.4 }));
sprinkle.position.set(Math.cos(a) * r, Math.sin(a) * r, 0.36);
sprinkle.rotation.set(Math.PI / 2, Math.random() * Math.PI, 0);
donut.add(sprinkle);
}
donut.rotation.x = -Math.PI / 2 + 0.35;
donut.traverse((o) => { o.castShadow = true; });
const hero = new THREE.Group();
hero.position.y = 1.85;
hero.add(donut);
scene.add(hero);
7. Add a floating geometry gallery
In plain words: around the donut float six more shapes, each with a different material: a glossy torus knot, a gold sphere, a faceted icosahedron, a cone, a chrome cylinder and the teal cube from Level 2. A small helper creates each one and remembers how it should move. Write an addFloater() helper that makes a Mesh, stores baseY, phase and speed in userData, and pushes it into an array. Call it with TorusKnotGeometry, SphereGeometry, IcosahedronGeometry, ConeGeometry, CylinderGeometry and BoxGeometry (the teal Level 2 cube) and mixed metalness, roughness and clearcoat values.
const floaters = [];
function addFloater(geometry, material, x, y, z, speed) {
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.position.set(x, y, z);
mesh.userData = { baseY: y, phase: Math.random() * Math.PI * 2, speed };
scene.add(mesh);
floaters.push(mesh);
return mesh;
}
addFloater(
new THREE.TorusKnotGeometry(0.42, 0.13, 128, 16),
new THREE.MeshPhysicalMaterial({ color: 0x7c4dff, metalness: 0.2, roughness: 0.2, clearcoat: 1, clearcoatRoughness: 0.05 }),
-2.6, 2.9, -1.2, 0.9
);
addFloater(
new THREE.SphereGeometry(0.42, 32, 16),
new THREE.MeshStandardMaterial({ color: 0xffc94a, metalness: 0.85, roughness: 0.25 }),
2.5, 2.7, -1.0, 1.1
);
addFloater(
new THREE.IcosahedronGeometry(0.45, 0),
new THREE.MeshStandardMaterial({ color: 0x2ad1d6, roughness: 0.4, flatShading: true }),
-1.4, 3.5, -2.4, 0.7
);
addFloater(
new THREE.ConeGeometry(0.35, 0.8, 24),
new THREE.MeshPhysicalMaterial({ color: 0xff8a3d, roughness: 0.3, clearcoat: 0.8 }),
1.3, 3.6, -2.3, 1.3
);
addFloater(
new THREE.CylinderGeometry(0.28, 0.28, 0.7, 32),
new THREE.MeshStandardMaterial({ color: 0xe8eef5, metalness: 0.9, roughness: 0.3 }),
0, 4.1, -3.0, 0.8
);
addFloater(
new THREE.BoxGeometry(0.6, 0.6, 0.6),
new THREE.MeshStandardMaterial({ color: 0x2ad1d6, roughness: 0.45, metalness: 0.05 }),
1.2, 2.75, 1.6, 1.0
);
8. Bob and spin every object
In plain words: every frame, each shape moves up and down on its own sine wave and turns a little, so the whole scene feels alive without any two shapes moving in step. Use a Clock with a capped delta, set position.y from Math.sin of elapsed time plus each object's phase, add to rotation scaled by delta, drift the clouds, update the controls and render.
const clock = new THREE.Clock();
function render() {
requestAnimationFrame(render);
const dt = Math.min(clock.getDelta(), 0.05);
const t = clock.elapsedTime;
hero.position.y = 1.85 + Math.sin(t * 1.6) * 0.12;
hero.rotation.y += dt * 0.6;
for (const m of floaters) {
const u = m.userData;
m.position.y = u.baseY + Math.sin(t * u.speed * 2 + u.phase) * 0.25;
m.rotation.x += dt * u.speed * 0.6;
m.rotation.y += dt * u.speed;
}
updateClouds(t);
controls.update();
renderer.render(scene, camera);
}
render();
Try it yourself: change the icing color, swap the gold sphere's metalness to 0, or add your own shape with one more addFloater() call. Want shapes you can click? Try raycaster selection.
The Full Code
Save this as index.html and open it, or press Launch to run it in a new tab. It loads Three.js 0.160.0 from unpkg, so no build tools are needed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Three.js Donut Picnic Scene</title>
<script type="importmap">
{"imports":{"three":"https://unpkg.com/three@0.160.0/build/three.module.js","three/addons/":"https://unpkg.com/three@0.160.0/examples/jsm/"}}
</script>
</head>
<body>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
const isMobile = matchMedia('(max-width: 768px)').matches;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 2000);
camera.position.set(0, 3.6, 10);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, isMobile ? 1.5 : 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 0.8;
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1.9, 0);
controls.enableDamping = true;
controls.autoRotate = true;
controls.autoRotateSpeed = 0.5;
controls.maxPolarAngle = Math.PI * 0.47;
controls.minDistance = 4;
controls.maxDistance = 24;
const SKY = new THREE.Color(0x8ec5f0);
scene.background = SKY;
scene.fog = new THREE.Fog(SKY, 22, 60);
const pmrem = new THREE.PMREMGenerator(renderer);
scene.environment = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;
scene.add(new THREE.HemisphereLight(0xcfe6ff, 0x3f6b2a, 1.1));
const sun = new THREE.DirectionalLight(0xfff1d6, 2.6);
sun.position.set(-7, 13, 5);
sun.castShadow = true;
sun.shadow.mapSize.set(isMobile ? 1024 : 2048, isMobile ? 1024 : 2048);
sun.shadow.camera.left = sun.shadow.camera.bottom = -12;
sun.shadow.camera.right = sun.shadow.camera.top = 12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0005;
sun.shadow.radius = 3;
scene.add(sun);
function heightAt(x, z) {
const d = Math.hypot(x, z);
const rise = THREE.MathUtils.smoothstep(d, 5, 22);
return rise * (2.5 + Math.sin(x * 0.35) * 1.6 + Math.cos(z * 0.3) * 1.4 + Math.sin((x + z) * 0.8) * 0.4);
}
const groundGeo = new THREE.PlaneGeometry(90, 90, 70, 70);
groundGeo.rotateX(-Math.PI / 2);
const pos = groundGeo.attributes.position;
for (let i = 0; i < pos.count; i++) {
pos.setY(i, heightAt(pos.getX(i), pos.getZ(i)));
}
groundGeo.computeVertexNormals();
const ground = new THREE.Mesh(
groundGeo,
new THREE.MeshStandardMaterial({ color: 0x5c9a3c, roughness: 1, flatShading: true })
);
ground.receiveShadow = true;
scene.add(ground);
const flat = (color) => new THREE.MeshStandardMaterial({ color, roughness: 0.9, flatShading: true });
const trunkMat = flat(0x6b4a2b);
const leafMat = flat(0x2f6b3a);
const rockMat = flat(0x8a8f96);
function addTree(x, z, s) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.15, 0.22, 1, 6), trunkMat);
trunk.position.y = 0.5;
tree.add(trunk);
for (let i = 0; i < 3; i++) {
const leaves = new THREE.Mesh(new THREE.ConeGeometry(1.1 - i * 0.25, 1.4, 7), leafMat);
leaves.position.y = 1.3 + i * 0.7;
tree.add(leaves);
}
tree.traverse((o) => { o.castShadow = true; });
tree.position.set(x, heightAt(x, z) - 0.05, z);
tree.scale.setScalar(s);
scene.add(tree);
}
[[-4, -3, 1.2], [-2, -6.5, 1.5], [2.5, -7, 1.4], [5.5, -5.5, 1.6], [-6.5, 1.5, 1.3],
[-5, 6, 1.4], [0, -11, 1.8], [-9, -7, 1.7], [9, -9, 1.9], [11, 2, 1.6]]
.forEach(([x, z, s]) => addTree(x, z, s));
function addRock(x, z, s) {
const rock = new THREE.Mesh(new THREE.DodecahedronGeometry(s, 0), rockMat);
rock.position.set(x, heightAt(x, z) + s * 0.35, z);
rock.rotation.set(x, z, x + z);
rock.castShadow = rock.receiveShadow = true;
scene.add(rock);
}
addRock(2.2, 1.6, 0.45);
addRock(2.8, 1.1, 0.25);
addRock(-2.4, 2.4, 0.55);
addRock(1.2, -2.8, 0.35);
const cloudMat = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1, flatShading: true, emissive: 0xffffff, emissiveIntensity: 0.35 });
const CLOUDS = isMobile ? 3 : 5;
const PUFFS = 5;
const clouds = new THREE.InstancedMesh(new THREE.IcosahedronGeometry(1, 1), cloudMat, CLOUDS * PUFFS);
clouds.frustumCulled = false;
scene.add(clouds);
const dummy = new THREE.Object3D();
function updateClouds(t) {
for (let c = 0; c < CLOUDS; c++) {
const x = ((-30 + c * 13 + (0.35 + (c % 3) * 0.12) * t + 32) % 64) - 32;
for (let p = 0; p < PUFFS; p++) {
const s = 2 - Math.abs(p - 2) * 0.4;
dummy.position.set(x + (p - 2) * 1.5, 7.5 + (c % 3) * 1.2 + (p % 2) * 0.3, -16 - (c % 2) * 6);
dummy.scale.set(s, s * 0.55, s);
dummy.updateMatrix();
clouds.setMatrixAt(c * PUFFS + p, dummy.matrix);
}
}
clouds.instanceMatrix.needsUpdate = true;
}
const wood = new THREE.MeshStandardMaterial({ color: 0xb07a45, roughness: 0.75 });
const table = new THREE.Group();
const top = new THREE.Mesh(new THREE.BoxGeometry(4.4, 0.16, 2.2), wood);
top.position.y = 1.1;
table.add(top);
for (const sx of [-1.8, 1.8]) {
for (const sz of [-0.85, 0.85]) {
const leg = new THREE.Mesh(new THREE.BoxGeometry(0.14, 1.05, 0.14), wood);
leg.position.set(sx, 0.52, sz);
table.add(leg);
}
}
const cloth = new THREE.Mesh(
new THREE.BoxGeometry(2.4, 0.02, 2.24),
new THREE.MeshStandardMaterial({ color: 0xe8525f, roughness: 0.9 })
);
cloth.position.y = 1.19;
table.add(cloth);
const plate = new THREE.Mesh(
new THREE.CylinderGeometry(0.95, 0.8, 0.06, 32),
new THREE.MeshPhysicalMaterial({ color: 0xffffff, roughness: 0.2, clearcoat: 1 })
);
plate.position.y = 1.23;
table.add(plate);
table.traverse((o) => { o.castShadow = true; o.receiveShadow = true; });
scene.add(table);
const donut = new THREE.Group();
const dough = new THREE.Mesh(
new THREE.TorusGeometry(0.62, 0.3, 24, 64),
new THREE.MeshStandardMaterial({ color: 0xd9a25f, roughness: 0.65, metalness: 0 })
);
const icing = new THREE.Mesh(
new THREE.TorusGeometry(0.62, 0.315, 24, 64),
new THREE.MeshPhysicalMaterial({ color: 0xff7eb6, roughness: 0.35, clearcoat: 1, clearcoatRoughness: 0.1 })
);
icing.position.z = 0.07;
icing.scale.z = 0.8;
donut.add(dough, icing);
const sprinkleGeo = new THREE.CapsuleGeometry(0.02, 0.09, 2, 6);
const sprinkleColors = [0xffffff, 0xffd84a, 0x4ad6ff, 0x8a5cff, 0x5cff8a];
for (let i = 0; i < 46; i++) {
const a = Math.random() * Math.PI * 2, r = 0.62 + (Math.random() - 0.5) * 0.4;
const sprinkle = new THREE.Mesh(sprinkleGeo, new THREE.MeshStandardMaterial({ color: sprinkleColors[i % 5], roughness: 0.4 }));
sprinkle.position.set(Math.cos(a) * r, Math.sin(a) * r, 0.36);
sprinkle.rotation.set(Math.PI / 2, Math.random() * Math.PI, 0);
donut.add(sprinkle);
}
donut.rotation.x = -Math.PI / 2 + 0.35;
donut.traverse((o) => { o.castShadow = true; });
const hero = new THREE.Group();
hero.position.y = 1.85;
hero.add(donut);
scene.add(hero);
const floaters = [];
function addFloater(geometry, material, x, y, z, speed) {
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.position.set(x, y, z);
mesh.userData = { baseY: y, phase: Math.random() * Math.PI * 2, speed };
scene.add(mesh);
floaters.push(mesh);
return mesh;
}
addFloater(
new THREE.TorusKnotGeometry(0.42, 0.13, 128, 16),
new THREE.MeshPhysicalMaterial({ color: 0x7c4dff, metalness: 0.2, roughness: 0.2, clearcoat: 1, clearcoatRoughness: 0.05 }),
-2.6, 2.9, -1.2, 0.9
);
addFloater(
new THREE.SphereGeometry(0.42, 32, 16),
new THREE.MeshStandardMaterial({ color: 0xffc94a, metalness: 0.85, roughness: 0.25 }),
2.5, 2.7, -1.0, 1.1
);
addFloater(
new THREE.IcosahedronGeometry(0.45, 0),
new THREE.MeshStandardMaterial({ color: 0x2ad1d6, roughness: 0.4, flatShading: true }),
-1.4, 3.5, -2.4, 0.7
);
addFloater(
new THREE.ConeGeometry(0.35, 0.8, 24),
new THREE.MeshPhysicalMaterial({ color: 0xff8a3d, roughness: 0.3, clearcoat: 0.8 }),
1.3, 3.6, -2.3, 1.3
);
addFloater(
new THREE.CylinderGeometry(0.28, 0.28, 0.7, 32),
new THREE.MeshStandardMaterial({ color: 0xe8eef5, metalness: 0.9, roughness: 0.3 }),
0, 4.1, -3.0, 0.8
);
addFloater(
new THREE.BoxGeometry(0.6, 0.6, 0.6),
new THREE.MeshStandardMaterial({ color: 0x2ad1d6, roughness: 0.45, metalness: 0.05 }),
1.2, 2.75, 1.6, 1.0
);
const clock = new THREE.Clock();
function render() {
requestAnimationFrame(render);
const dt = Math.min(clock.getDelta(), 0.05);
const t = clock.elapsedTime;
hero.position.y = 1.85 + Math.sin(t * 1.6) * 0.12;
hero.rotation.y += dt * 0.6;
for (const m of floaters) {
const u = m.userData;
m.position.y = u.baseY + Math.sin(t * u.speed * 2 + u.phase) * 0.25;
m.rotation.x += dt * u.speed * 0.6;
m.rotation.y += dt * u.speed;
}
updateClouds(t);
controls.update();
renderer.render(scene, camera);
}
render();
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
Frequently Asked Questions
How do I make a donut in Three.js?
Use new THREE.TorusGeometry(radius, tube, radialSegments, tubularSegments), for example TorusGeometry(0.62, 0.3, 24, 64), with a MeshStandardMaterial and add the Mesh to the scene. For icing, add a second, slightly fatter torus with a glossy MeshPhysicalMaterial, flatten it with scale.z and lift it a little.
What is the difference between TorusGeometry and TorusKnotGeometry?
TorusGeometry is a plain ring, like a donut. TorusKnotGeometry is a tube that winds around the ring several times to form a knot, controlled by its p and q parameters. Both take a radius and a tube thickness.
When should I use MeshPhysicalMaterial instead of MeshStandardMaterial?
MeshStandardMaterial covers most surfaces with color, metalness and roughness. Use MeshPhysicalMaterial when you need extra layers such as clearcoat, which adds a thin glossy coat like icing, candy or car paint. It costs a little more to render, so use it only on the hero objects.
What does THREE.Group do?
A Group is an empty container. Anything you add to it moves, rotates and scales with it. In this scene the donut, its icing and sprinkles share one group, so one rotation spins the whole donut, and each tree and the picnic table are groups of simple parts.
How do I make objects bob and spin?
In the render loop set position.y to a base height plus Math.sin(time * speed + phase) times a small amount, and add a little to rotation each frame scaled by the frame delta. Giving each object its own phase and speed keeps them from moving in step.
Do I need Part 1 and Part 2 first?
It helps. Part 1 builds your first spinning cube and Part 2 adds lights, shadows, a ground and OrbitControls. This Part 3 reuses all of that, brings back the teal Level 2 cube, and focuses on combining 7 geometries and mixed materials into one scene.
Why do I need to sign in to launch the live demo?
The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site, so I know who is learning from these tutorials.
Play It and Keep Learning
Low-poly scenes like this power Super Soldier Battle, free in your browser. Browse every Three.js tutorial or play more 3D games.
Need Three.js or WebGL work? I take freelance and contract work: contact me.