RENDERING A
3D CUBE
By Shane Brumback · · Updated · Free Three.js tutorial · Level 2
The next step after your first cube: put a lit 3D cube on a low-poly outdoor landscape with rolling hills, trees, rocks, drifting clouds and soft shadows, animate it with THREE.Clock so it runs the same speed on every screen, and drag around it with OrbitControls.
The meadow behind this page is the live demo, built from the exact Full Code below.
LEVEL 1 → LEVEL 2
Start here first: Create Your First Cube
New to Three.js? Do the beginner Create Your First Cube tutorial first. It walks through the scene, camera, renderer, a teal cube on flat grass with one tree and one rock, and the animation loop line by line. This page assumes you know those and adds rolling hills, more trees and rocks, clouds, shadow tuning, Clock timing and OrbitControls on top.
Create Your First Cube (Level 1)LIVE DEMO
The landscape behind this page is the live demo. Click Live Demo to go full screen, then drag to orbit and scroll or pinch to zoom.
A free Google sign-in unlocks the interactive demo.
Introduction
Prerequisite: Create Your First Cube. If you've built that scene, you already know how a scene, camera and renderer fit together, why a MeshStandardMaterial needs light, and how requestAnimationFrame drives the loop. We won't repeat those basics here.
This Level 2 tutorial is about growing Level 1's simple scene into a small world that looks finished. In plain beginner words: Level 1 gave you flat grass, one pine tree and one rock. Here you add rolling hills, plant more trees and rocks on that ground, tune the sun's soft shadows, float a few clouds, keep the spin smooth with Clock, and drag around with OrbitControls. It's still one HTML file with no build tools, and it stays fast on phones.
What's New Compared to Your First Cube
- Hills, not a flat floor: vertices of a
PlaneGeometrypushed up by a height function. - Props that follow the ground: trees and rocks placed with the same height function.
- Shadow tuning: a sized shadow camera,
biasandradiusfor clean, soft shadows. - InstancedMesh clouds: 25 puffs in a single draw call.
- Clock timing: speed in radians per second with
getDelta(), not per frame. - Performance basics: capped
setPixelRatio, smaller shadow maps and fewer clouds on phones. - OrbitControls on
renderer.domElementwith damping and limits, plus a resize handler.
Frequently Asked Questions
Should I do Create Your First Cube before this tutorial?
Yes. This is the Level 2 follow-up to Create Your First Cube (threejs-spinning-cube-starter.html). That page builds a teal cube on flat grass with one pine tree, one rock, a blue sky and a soft shadow; this Level 2 keeps that same world and adds rolling hills, more trees and rocks, drifting clouds, tighter shadow tuning, Clock timing and OrbitControls.
Why use THREE.Clock instead of adding a fixed rotation each frame?
requestAnimationFrame runs at the screen's refresh rate, so cube.rotation.y += 0.01 spins twice as fast on a 120 Hz screen as on a 60 Hz one. Multiplying a speed by clock.getDelta(), the seconds since the last frame, makes the motion the same speed on every device.
How do I make low-poly hills in Three.js?
Create a PlaneGeometry with enough segments, rotate it flat, loop over its position attribute and set each vertex's Y from a height function, then call computeVertexNormals(). Use MeshStandardMaterial with flatShading: true for the faceted low-poly look.
Why are my Three.js shadows blurry or missing?
Enable renderer.shadowMap, set castShadow on the light and the objects, and receiveShadow on the ground. Then shrink the DirectionalLight's shadow camera (left, right, top, bottom) to just cover your scene; a huge shadow box spreads the shadow map thin and makes shadows blurry.
Why should setPixelRatio be capped?
Phones can report a devicePixelRatio of 3 or more, which makes the GPU draw up to nine times the pixels. Math.min(window.devicePixelRatio, 2) (or 1.5 on mobile) keeps the scene sharp while staying smooth and battery friendly.
Why attach OrbitControls to renderer.domElement?
OrbitControls only reacts to pointer events on the element you give it. Passing renderer.domElement means drags on the 3D canvas orbit the camera, while buttons and text elsewhere on the page keep working normally.
Watch: Three.js Helmet HUD: FPS Visor Overlay (Indie Forge Studio) · Play Super Soldier Battle and climb the leaderboard
Code Breakdown
Nine steps, each one copied straight from The Full Code. The code comes first, then what it does in plain words.
1. Renderer, camera and scene
// STEP 1: Renderer, camera and scene
const isMobile = matchMedia('(max-width: 768px)').matches;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, isMobile ? 1.5 : 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(6, 4.2, 9.5);
In plain words: this is the same setup you wrote in the first cube, with three upgrades. setPixelRatio keeps edges sharp on retina screens but is capped (1.5 on phones, 2 on desktop) so the GPU isn't drawing four times the pixels for nothing. shadowMap.enabled with PCFSoftShadowMap turns on soft-edged shadows, and ACESFilmicToneMapping keeps the bright sun from blowing out the colors. The camera starts up and back so you look down across the meadow.
2. Sky color and fog
// STEP 2: Sky color and fog const SKY = new THREE.Color(0x8ec5f0); scene.background = SKY; scene.fog = new THREE.Fog(SKY, 22, 60);
In plain words: one sky-blue color is used for both the background and the Fog. Things farther than 22 units start fading into that blue, and by 60 units they're gone, so the far hills melt into the sky instead of ending at a hard edge.
3. Sky light plus a sun that casts soft shadows
// STEP 3: Sky light plus a sun that casts soft shadows 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);
In plain words: a HemisphereLight gives blue light from above and green bounce light from below, so shadowed sides are never pitch black. The DirectionalLight is the sun. Its shadow camera is a box around the play area (24 by 24 units); keeping that box small is what makes the shadows crisp. bias stops striped "shadow acne" and radius softens the edge. Phones get a smaller shadow map to stay fast.
4. Grass ground with rolling hills
// STEP 4: Grass ground with rolling hills (flat in the middle)
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);
In plain words: the starter used a flat plane. Here we take a PlaneGeometry with 70 by 70 segments, lay it flat, and push each vertex up with heightAt(x, z). smoothstep keeps the middle flat (where the cube sits) and lets the hills rise toward the edges. computeVertexNormals() recalculates the lighting directions, and flatShading gives the faceted low-poly look.
5. Low-poly trees and rocks
// STEP 5: Low-poly trees and rocks
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);
In plain words: a tree is a Group: one 6-sided cylinder trunk and three stacked cones. A rock is a DodecahedronGeometry with detail 0, which already looks like a chipped stone. Both reuse a few shared materials (cheaper than one per object) and ask heightAt how high the ground is, so they sit on the hills instead of floating or sinking.
6. Drifting clouds in one draw call
// STEP 6: Drifting clouds (one InstancedMesh, one draw call)
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;
}
In plain words: every cloud is five squashed icosahedron "puffs". Instead of 25 separate meshes we use one InstancedMesh, so the GPU draws all of them in a single call. Each frame updateClouds(t) slides them along X using the elapsed time and wraps them back around when they drift off the edge.
7. The cube on the grass
// STEP 7: The cube, lit and casting a shadow on the grass
const cube = new THREE.Mesh(
new THREE.BoxGeometry(1.6, 1.6, 1.6),
new THREE.MeshStandardMaterial({ color: 0x2ad1d6, roughness: 0.45, metalness: 0.05 })
);
cube.position.y = 0.8; // half its height, so it rests on the grass
cube.rotation.y = Math.PI / 4; // start corner-on so two faces catch the light
cube.castShadow = true;
cube.receiveShadow = true;
scene.add(cube);
In plain words: the same BoxGeometry + MeshStandardMaterial mesh you already know, now 1.6 units wide. Lifting it by half its height (0.8) makes it rest on the grass, starting it at 45° shows two lit faces, and castShadow drops its shadow on the ground.
8. OrbitControls on the renderer's canvas
// STEP 8: OrbitControls on the renderer's canvas const controls = new OrbitControls(camera, renderer.domElement); controls.target.set(0, 1, 0); controls.enableDamping = true; controls.enablePan = false; controls.minDistance = 5; controls.maxDistance = 25; controls.maxPolarAngle = Math.PI * 0.47;
In plain words: OrbitControls listens on renderer.domElement (the canvas itself), so dragging the scene orbits the camera around target. Damping makes it glide, pan is off, zoom is clamped between 5 and 25 units, and maxPolarAngle stops the camera from dipping under the grass.
9. Time-based animation with Clock, plus a resize handler
// STEP 9: Time-based animation with Clock, plus a resize handler
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.1);
const t = clock.elapsedTime;
cube.rotation.y += 0.6 * dt; // 0.6 radians per second on any screen
updateClouds(t);
controls.update();
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
In plain words: clock.getDelta() returns the seconds since the last frame, so 0.6 * dt spins the cube 0.6 radians per second whether the screen runs at 30, 60 or 144 Hz (the first cube added a fixed amount per frame, which runs faster on fast screens). The delta is capped at 0.1 s so a hidden tab doesn't make the cube jump. elapsedTime drives the clouds, controls.update() applies damping, and the resize handler keeps the camera aspect and canvas size in step with the window.
The Full Code
Save this as index.html and open it in a modern browser, or press Launch to run it in a new tab. The landscape behind this page runs this exact file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>3D Cube on a Low-Poly Landscape - Three.js</title>
<link rel="icon" href="data:,">
<style>
body { margin: 0; overflow: hidden; background: #8ec5f0; }
canvas { display: block; }
</style>
<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';
// STEP 1: Renderer, camera and scene
const isMobile = matchMedia('(max-width: 768px)').matches;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, isMobile ? 1.5 : 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(6, 4.2, 9.5);
// STEP 2: Sky color and fog
const SKY = new THREE.Color(0x8ec5f0);
scene.background = SKY;
scene.fog = new THREE.Fog(SKY, 22, 60);
// STEP 3: Sky light plus a sun that casts soft shadows
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);
// STEP 4: Grass ground with rolling hills (flat in the middle)
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);
// STEP 5: Low-poly trees and rocks
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);
// STEP 6: Drifting clouds (one InstancedMesh, one draw call)
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;
}
// STEP 7: The cube, lit and casting a shadow on the grass
const cube = new THREE.Mesh(
new THREE.BoxGeometry(1.6, 1.6, 1.6),
new THREE.MeshStandardMaterial({ color: 0x2ad1d6, roughness: 0.45, metalness: 0.05 })
);
cube.position.y = 0.8; // half its height, so it rests on the grass
cube.rotation.y = Math.PI / 4; // start corner-on so two faces catch the light
cube.castShadow = true;
cube.receiveShadow = true;
scene.add(cube);
// STEP 8: OrbitControls on the renderer's canvas
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1, 0);
controls.enableDamping = true;
controls.enablePan = false;
controls.minDistance = 5;
controls.maxDistance = 25;
controls.maxPolarAngle = Math.PI * 0.47;
// STEP 9: Time-based animation with Clock, plus a resize handler
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.1);
const t = clock.elapsedTime;
cube.rotation.y += 0.6 * dt; // 0.6 radians per second on any screen
updateClouds(t);
controls.update();
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
What To Try Next
- Reshape the land: change the numbers inside
heightAtfor taller hills or a valley. - Plant a forest: add more
[x, z, scale]entries to the tree list. - Move the sun: change
sun.positionand watch the shadows swing. - Auto-orbit: set
controls.autoRotate = truefor a slow showcase spin. - Day to dusk: animate the sun and sky colors over time, like the next tutorial does.
Next up: Adding Lighting to a Three.js Scene (campfire, street lamp and a day-to-dusk cycle). Need a refresher on the basics? Go back to Create Your First Cube.
Play My Free Browser Games
SEE 3D AND 2D BROWSER GAMES IN ACTION
My free browser games are full Three.js 3D experiences — no download, they run right in your browser.