THREE.JS COOKBOOK
ADVANCED TECHNIQUES

A recipe-style book by Jos Dirksen: short, ready-to-use solutions for advanced Three.js materials, dynamic textures, lighting and shadows, particle systems, physics and loading resources. Great when you know the basics and want answers to "how do I...?". See it on Amazon.

✓ FREE Book · Intermediate RecipesMaterials + lightingPhysics

Book by Jos Dirksen · Updated November 2, 2026 · Book guide · about 6 minute read

See it live

LIVE DEMO

The book on the right of this page is a live Three.js scene using its real cover as a texture. Launch it full screen to drag to turn it and scroll to zoom. 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

About This Book

Three.js Cookbook Advanced Techniques book cover

Create visually stunning 3D graphics and animations directly in your browser, with no plugins. This cookbook-style guide delivers practical, ready-to-use recipes for advanced Three.js techniques.

Learn how to use advanced materials, dynamic textures, realistic lighting and shadows, particle systems, physics and third-party libraries to take your 3D projects to the next level.

Buy on Amazon →

As an Amazon Associate I earn from qualifying purchases, at no extra cost to you.

Who This Book Is For

Developers who already know JavaScript and want to master advanced Three.js features quickly. Basic Three.js knowledge helps but is not required; each recipe is explained on its own.

What You Will Learn

  • Advanced materials, textures and dynamic textures
  • Realistic lighting, shadows and post-processing
  • Complex particle systems and animations
  • Physics simulation and collision detection
  • Keyboard controls, drag and drop, resource loading
  • Matrix transformations and geometry manipulation

A Note on Versions

Three.js changes quickly and this book was written for an older release, so some names have moved. Two common ones: THREE.Geometry was removed in favor of BufferGeometry, and examples now import addons as ES modules from three/addons/. The ideas behind each recipe still hold; the free tutorials on this site use the current pinned Three.js 0.160.0.

Step-by-Step Code Breakdown

Bonus recipe in the spirit of the book: how the floating 3D book at the top of this page is built. Every excerpt is copied exactly from the full code below.

1. Scene, camera and renderer

In plain words: a dark scene, a camera a few steps in front of the book, and a renderer. Tone mapping keeps the bright cover colors from washing out under the lights.

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07090f);

const camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.1, 100);
camera.position.set(0, 0.3, 9);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.outputColorSpace = THREE.SRGBColorSpace;
document.body.appendChild(renderer.domElement);

2. Light it like a product shot

In plain words: photographers light products with a soft key light from the front side and a colored rim light from behind that outlines the edges. We do the same with two directional lights plus a little ambient fill.

scene.add(new THREE.AmbientLight(0xffffff, 0.6));
const key = new THREE.DirectionalLight(0xffffff, 2.2);
key.position.set(3, 4, 6);
scene.add(key);
const rim = new THREE.DirectionalLight(0xffb347, 3);
rim.position.set(-5, 2, -4);
scene.add(rim);

3. Load the cover as a texture

In plain words: TextureLoader downloads the cover picture and turns it into a texture the graphics card can paint onto 3D shapes. setCrossOrigin('anonymous') is needed because the image lives on another website. Setting the color space to sRGB keeps the colors true to the original picture.

const loader = new THREE.TextureLoader();
loader.setCrossOrigin('anonymous');
const coverTex = loader.load('https://m.media-amazon.com/images/I/715LHF1+OsL._SY466_.jpg');
coverTex.colorSpace = THREE.SRGBColorSpace;
coverTex.anisotropy = renderer.capabilities.getMaxAnisotropy();

4. Build the book from a box with six materials

In plain words: a book is just a thin box. BoxGeometry accepts an array of six materials, one per face, in this order: right, left, top, bottom, front, back. The front gets the cover, the left side becomes the colored spine, and the other edges get a cream paper color so it reads as pages.

const paper = new THREE.MeshStandardMaterial({ color: 0xf1ead8, roughness: 0.9 });
const spineMat = new THREE.MeshStandardMaterial({ color: 0x2b2b2b, roughness: 0.5 });
const front = new THREE.MeshStandardMaterial({ map: coverTex, roughness: 0.45 });
const back = new THREE.MeshStandardMaterial({ color: 0x2b2b2b, roughness: 0.6 });
const book = new THREE.Mesh(
  new THREE.BoxGeometry(3, 4.6, 0.5),
  [paper, spineMat, paper, paper, front, back]   // right, left, top, bottom, front, back
);
scene.add(book);

5. Float the book and add drifting sparks

In plain words: a few hundred tiny points drift around the book like dust in a sunbeam. In the loop the book sways side to side with Math.sin, so you see the cover, the spine and the page edges catch the light.

const sparkPos = new Float32Array(400 * 3);
for (let i = 0; i < sparkPos.length; i++) sparkPos[i] = (Math.random() - 0.5) * 14;
const sparkGeo = new THREE.BufferGeometry();
sparkGeo.setAttribute('position', new THREE.BufferAttribute(sparkPos, 3));
const sparks = new THREE.Points(sparkGeo, new THREE.PointsMaterial({ color: 0xffb347, size: 0.05, transparent: true, opacity: 0.8 }));
scene.add(sparks);

const clock = new THREE.Clock();
function animate() {
  requestAnimationFrame(animate);
  const t = clock.getElapsedTime();
  book.rotation.y = Math.sin(t * 0.5) * 0.6;
  book.rotation.x = Math.sin(t * 0.3) * 0.08;
  book.position.y = Math.sin(t * 0.8) * 0.12;
  sparks.rotation.y = t * 0.03;
  controls.update();
  renderer.render(scene, camera);
}
animate();

Live Scene: The Full Code

Paste this into a file named index.html. It loads the cover image from Amazon, which allows cross-origin use, so it runs from anywhere. Launch Full Code opens this exact code as a standalone page in a new window.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Three.js 3D Book Cover Scene</title>
  <style>
    body { margin: 0; overflow: hidden; background: #000; font-family: system-ui, sans-serif; }
  </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';

// --- 1. Scene, camera and renderer ---
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07090f);

const camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.1, 100);
camera.position.set(0, 0.3, 9);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.outputColorSpace = THREE.SRGBColorSpace;
document.body.appendChild(renderer.domElement);

// Orbit controls: drag (mouse or touch) to orbit. Zoom is off on the page so
// vertical swipes still scroll on phones; the full-screen demo turns zoom on.
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0, 0);
controls.enableDamping = true;
controls.enableZoom = false;
controls.enablePan = false;
controls.autoRotate = false;
controls.autoRotateSpeed = 0;
renderer.domElement.style.touchAction = 'pan-y';

// --- 2. Light it like a product shot ---
scene.add(new THREE.AmbientLight(0xffffff, 0.6));
const key = new THREE.DirectionalLight(0xffffff, 2.2);
key.position.set(3, 4, 6);
scene.add(key);
const rim = new THREE.DirectionalLight(0xffb347, 3);
rim.position.set(-5, 2, -4);
scene.add(rim);

// --- 3. Load the cover as a texture ---
const loader = new THREE.TextureLoader();
loader.setCrossOrigin('anonymous');
const coverTex = loader.load('https://m.media-amazon.com/images/I/715LHF1+OsL._SY466_.jpg');
coverTex.colorSpace = THREE.SRGBColorSpace;
coverTex.anisotropy = renderer.capabilities.getMaxAnisotropy();

// --- 4. Build the book from a box with six materials ---
const paper = new THREE.MeshStandardMaterial({ color: 0xf1ead8, roughness: 0.9 });
const spineMat = new THREE.MeshStandardMaterial({ color: 0x2b2b2b, roughness: 0.5 });
const front = new THREE.MeshStandardMaterial({ map: coverTex, roughness: 0.45 });
const back = new THREE.MeshStandardMaterial({ color: 0x2b2b2b, roughness: 0.6 });
const book = new THREE.Mesh(
  new THREE.BoxGeometry(3, 4.6, 0.5),
  [paper, spineMat, paper, paper, front, back]   // right, left, top, bottom, front, back
);
scene.add(book);

// --- 5. Float the book and add drifting sparks ---
const sparkPos = new Float32Array(400 * 3);
for (let i = 0; i < sparkPos.length; i++) sparkPos[i] = (Math.random() - 0.5) * 14;
const sparkGeo = new THREE.BufferGeometry();
sparkGeo.setAttribute('position', new THREE.BufferAttribute(sparkPos, 3));
const sparks = new THREE.Points(sparkGeo, new THREE.PointsMaterial({ color: 0xffb347, size: 0.05, transparent: true, opacity: 0.8 }));
scene.add(sparks);

const clock = new THREE.Clock();
function animate() {
  requestAnimationFrame(animate);
  const t = clock.getElapsedTime();
  book.rotation.y = Math.sin(t * 0.5) * 0.6;
  book.rotation.x = Math.sin(t * 0.3) * 0.08;
  book.position.y = Math.sin(t * 0.8) * 0.12;
  sparks.rotation.y = t * 0.03;
  controls.update();
  renderer.render(scene, camera);
}
animate();

addEventListener('resize', () => {
  camera.aspect = innerWidth / innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>

What To Try Next

  • Swap in your own cover: change the URL in step 3 to any image on a server that allows cross-origin loading.
  • Add a back cover: load a second texture and use it for the last material in the array instead of the plain color.
  • Make it open: split the cover into its own thin box with its pivot on the spine and rotate it on click.
  • Practice the particle chapter with the free comet particle system tutorial.

Frequently Asked Questions

Who wrote the Three.js Cookbook?

Jos Dirksen wrote the Three.js Cookbook, published by Packt. He also wrote Learning Three.js.

Is the Three.js Cookbook good for beginners?

It is best once you know JavaScript and the basic Three.js scene, camera and renderer. Each recipe stands alone, so beginners can still follow individual recipes with a little patience.

Is the Three.js Cookbook still up to date?

The concepts are still useful, but it targets an older Three.js version. Expect to update some code, for example replacing THREE.Geometry with BufferGeometry and importing addons as ES modules.

Where can I buy the Three.js Cookbook?

It is available on Amazon in Kindle and paperback formats. The Buy on Amazon button on this page links to it.

Is the 3D book scene code on this page free?

Yes. The live 3D book scene code is free to copy and Launch Full Code opens it in a new window. Only the full-screen live demo asks for a free Google sign-in.