YOUR FIRST THREE.JS SCENE
SPINNING CUBE

LEVEL 1By Shane Brumback · Updated · Free beginner Three.js tutorial

To make your first Three.js scene, create a scene, a camera and a renderer, give it a blue sky and a sun, lay down a flat grass ground, add one pine tree and one rock, set a teal cube on the grass with a soft shadow, and turn the cube slowly on one axis in an animation loop. That is the whole scene behind this page, in six small steps.

Beginner Three.js Spinning cube First 3D scene

Press Live Demo to drag the scene and look around, then follow the steps and copy the full code.

See it live

LIVE DEMO

The teal cube turning slowly on the meadow behind this page is the live demo. Press the button to go full screen — the page hides so you can drag to look around the cube.

Press Esc or the X to exit.

Introduction

Background reading: What Is Three.js? — a plain-English overview before you write the scene.

Three.js is a free JavaScript library that draws 3D graphics in a web page. In this beginner tutorial you build one small, good-looking scene: a teal cube sitting on a flat grass ground with one pine tree and one grey rock, under a blue sky with soft shadows, turning slowly like a display stand.

We keep it simple on purpose. The cube turns on one axis only, and every step adds just one idea. If you can write a little HTML and JavaScript, you are ready — no installs or build tools, everything loads from a CDN.

Frequently Asked Questions

How do I make my first Three.js scene?

Create three things: a THREE.Scene to hold your objects, a PerspectiveCamera to look at them, and a WebGLRenderer that draws onto a canvas. Add a mesh such as a cube, add a light, and call renderer.render(scene, camera) inside a requestAnimationFrame loop. This tutorial does it in one short HTML file.

How do I make a blue sky in Three.js?

Set scene.background to a THREE.Color such as 0x8ec5f0, and add a THREE.Fog with the same color. Far-away ground then fades into the sky instead of ending at a hard edge.

How do I put a cube on grass in Three.js?

Make the ground from a PlaneGeometry, lay it flat with rotateX(-Math.PI / 2) and give it a green MeshStandardMaterial. A BoxGeometry is centered on its middle, so set cube.position.y to half the cube's height (0.8 for a 1.6-unit cube) and it will sit exactly on the grass.

How do I add a shadow in Three.js?

Turn on renderer.shadowMap.enabled, set castShadow on a DirectionalLight, the cube, the tree and the rock, and set receiveShadow on the ground. PCFSoftShadowMap, a 2048 shadow map and a small shadow camera box give soft, clean shadows.

How do I rotate a cube slowly on one axis?

In the animation loop change only cube.rotation.y, for example cube.rotation.y += 0.5 * clock.getDelta(). Using the time since the last frame keeps the speed steady on fast and slow screens, and leaving rotation.x and rotation.z alone keeps the motion simple.

Is this Three.js beginner tutorial free?

Yes. The tutorial and the full code are free on this page: copy it into an .html file or press Launch to run it in a new tab. The full-screen live demo just asks for a quick free Google sign-in.

How to Build It, Step by Step

Each step below is a piece of the same file. Put them together in order (or grab the full code) and you get exactly the scene behind this page.

Step 1: Set up the scene, camera and renderer

Every Three.js project starts with the same three things. The scene is an empty world you put things in. The camera is your eye: we lift it up and back, then point it at the cube with lookAt. The renderer draws what the camera sees onto a <canvas> on the page. We switch shadows on here, because the renderer is what draws them, and ACESFilmicToneMapping keeps the bright sun from washing out the colors.

// STEP 1: Setup - a scene, a camera and a renderer
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);            // up and back, looking down at the cube
camera.lookAt(0, 0.8, 0);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;           // turn shadows on
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild(renderer.domElement);

Step 2: Add a blue sky, fog and a sun

One sky-blue color is used for both scene.background and a Fog, so the far ground fades into the sky instead of ending at a hard edge. A HemisphereLight gives soft blue light from above and green bounce light from below, so nothing is pitch black. One DirectionalLight acts as the sun and casts the shadows; its shadow box (24 by 24 units) is kept small so the shadows stay crisp, and radius softens the edge.

// STEP 2: Blue sky, fog, and a sun that casts soft shadows
const SKY = new THREE.Color(0x8ec5f0);
scene.background = SKY;
scene.fog = new THREE.Fog(SKY, 22, 60);      // far ground fades into the sky

scene.add(new THREE.HemisphereLight(0xcfe6ff, 0x3f6b2a, 1.1)); // blue from above, green from below

const sun = new THREE.DirectionalLight(0xfff1d6, 2.6);
sun.position.set(-7, 13, 5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = sun.shadow.camera.bottom = -12;
sun.shadow.camera.right = sun.shadow.camera.top = 12;
sun.shadow.bias = -0.0005;
sun.shadow.radius = 3;                       // blur the shadow edge
scene.add(sun);

Step 3: Make a flat grass ground

The ground is a PlaneGeometry laid flat with rotateX. We keep it simple: one segment each way, no hills yet. flatShading gives the faceted low-poly look, and receiveShadow lets the cube's soft shadow land on the grass. Level 2 will add rolling hills on top of this same ground.

const groundGeo = new THREE.PlaneGeometry(90, 90, 1, 1);
groundGeo.rotateX(-Math.PI / 2);             // lay the plane flat
const ground = new THREE.Mesh(
    groundGeo,
    new THREE.MeshStandardMaterial({ color: 0x5c9a3c, roughness: 1, flatShading: true })
);
ground.receiveShadow = true;
scene.add(ground);

Step 4: Add one pine tree and one rock

A pine tree is a Group: a 6-sided cylinder trunk with three stacked cones on top. A rock is a DodecahedronGeometry, which already looks like a chipped grey stone. We plant just one tree and one rock so the scene stays easy to read. They share three materials, cast shadows, and use the same shapes Level 2 will reuse when you add more.

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, -0.05, z);           // flat ground, so y stays near 0
    tree.scale.setScalar(s);
    scene.add(tree);
}
addTree(-4, -3, 1.2);                        // just one tree for Level 1

function addRock(x, z, s) {
    const rock = new THREE.Mesh(new THREE.DodecahedronGeometry(s, 0), rockMat);
    rock.position.set(x, 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);                     // just one rock for Level 1

Step 5: Put a teal cube on the grass

A visible object in Three.js is a Mesh: a shape (BoxGeometry, 1.6 units on every side) plus a material (MeshStandardMaterial, which reacts to light). The cube's center is its middle, so we raise it by half its height (0.8) to make it sit on the grass. castShadow drops its soft shadow on the meadow.

// STEP 5: A teal cube sitting 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 })
);
cube.position.y = 0.8;                       // half its height, so it sits ON the grass
cube.castShadow = true;
cube.receiveShadow = true;
scene.add(cube);

Step 6: Turn it slowly on one axis

The animation loop runs about 60 times a second with requestAnimationFrame. Each frame we turn the cube a tiny bit around the Y axis only (the up-and-down axis), like a display stand. Multiplying by clock.getDelta() (the seconds since the last frame) keeps the speed the same on every screen: 0.5 radians per second, about one full turn every 12 seconds. The OrbitControls lines are optional: they let you drag to look around, with zoom off and the camera kept above the grass. The resize listener keeps the picture sharp when the window changes size.

// STEP 6: Turn it slowly on the Y axis only
// Optional: drag to look around (zoom off, never under the grass)
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0.8, 0);
controls.enableZoom = false;
controls.enablePan = false;
controls.maxPolarAngle = Math.PI * 0.47;
controls.enableDamping = true;

const clock = new THREE.Clock();
function animate() {
    requestAnimationFrame(animate);
    cube.rotation.y += 0.5 * clock.getDelta();  // 0.5 radians per second, Y axis only
    controls.update();
    renderer.render(scene, camera);
}
animate();

// Keep it sharp when the window changes size
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
});

The Full Code

Copy this into a file named index.html and open it in any modern browser, or press Launch to run it in a new tab right now. It is the same scene that is running behind this page.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My First Three.js Scene: Cube on a Meadow</title>
    <style> body { margin: 0; overflow: hidden; background: #8ec5f0; } canvas { display: block; touch-action: none; } </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: Setup - a scene, a camera and a renderer
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);            // up and back, looking down at the cube
camera.lookAt(0, 0.8, 0);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;           // turn shadows on
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild(renderer.domElement);

// STEP 2: Blue sky, fog, and a sun that casts soft shadows
const SKY = new THREE.Color(0x8ec5f0);
scene.background = SKY;
scene.fog = new THREE.Fog(SKY, 22, 60);      // far ground fades into the sky

scene.add(new THREE.HemisphereLight(0xcfe6ff, 0x3f6b2a, 1.1)); // blue from above, green from below

const sun = new THREE.DirectionalLight(0xfff1d6, 2.6);
sun.position.set(-7, 13, 5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = sun.shadow.camera.bottom = -12;
sun.shadow.camera.right = sun.shadow.camera.top = 12;
sun.shadow.bias = -0.0005;
sun.shadow.radius = 3;                       // blur the shadow edge
scene.add(sun);

// STEP 3: A flat grass ground (no hills yet — Level 2 adds those)
const groundGeo = new THREE.PlaneGeometry(90, 90, 1, 1);
groundGeo.rotateX(-Math.PI / 2);             // lay the plane flat
const ground = new THREE.Mesh(
    groundGeo,
    new THREE.MeshStandardMaterial({ color: 0x5c9a3c, roughness: 1, flatShading: true })
);
ground.receiveShadow = true;
scene.add(ground);

// STEP 4: One pine tree and one grey rock (same pieces Level 2 will reuse)
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, -0.05, z);           // flat ground, so y stays near 0
    tree.scale.setScalar(s);
    scene.add(tree);
}
addTree(-4, -3, 1.2);                        // just one tree for Level 1

function addRock(x, z, s) {
    const rock = new THREE.Mesh(new THREE.DodecahedronGeometry(s, 0), rockMat);
    rock.position.set(x, 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);                     // just one rock for Level 1

// STEP 5: A teal cube sitting 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 })
);
cube.position.y = 0.8;                       // half its height, so it sits ON the grass
cube.castShadow = true;
cube.receiveShadow = true;
scene.add(cube);

// STEP 6: Turn it slowly on the Y axis only
// Optional: drag to look around (zoom off, never under the grass)
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0.8, 0);
controls.enableZoom = false;
controls.enablePan = false;
controls.maxPolarAngle = Math.PI * 0.47;
controls.enableDamping = true;

const clock = new THREE.Clock();
function animate() {
    requestAnimationFrame(animate);
    cube.rotation.y += 0.5 * clock.getDelta();  // 0.5 radians per second, Y axis only
    controls.update();
    renderer.render(scene, camera);
}
animate();

// Keep it sharp when the window changes size
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
Rendering a 3D Cube on a Low-Poly Landscape, Level 2 Three.js tutorial
NEXT STEP · LEVEL 2

Rendering a 3D Cube on a Low-Poly Landscape

You've built your first scene. Level 2 puts your cube into a bigger low-poly world. Go to Level 2 →

What To Try Next

  • Change the cube color: try 0xff7a59 for orange.
  • Add more trees in Level 2, or call addTree(x, z, size) again here to practice.
  • Make it turn faster or slower by changing the 0.5 in the loop.
  • Swap BoxGeometry for SphereGeometry(0.8, 32, 16) and keep position.y = 0.8.
  • Ready for more? Learn lights in Adding Lighting to a Three.js Scene.

Beginner Three.js Tutorials

Keep learning: more beginner-friendly Three.js tutorials, one small idea at a time.