SuccessGraph
← Back to the class

Build the cartoon world from scratch

The video walks through the finished project. This companion lets you reconstruct the actual runnable scene without guessing omitted lines. Work in the project root, run npm ci and npm run lesson, then open the local URL. The existing lesson/index.html and lesson/main.js provide the canvas and animation loop. Keep the server running.

Create lesson/world-built.js and paste the following blocks into it in order. The intermediate file will be incomplete until the last block closes the function. No other missing code is required.

1. Set up the renderer and camera

Start a new file called lesson/world-built.js. Paste this first block. The function deliberately remains open; the next blocks go after it.

import * as THREE from 'three';

// A small, self-contained scene. All geometry is procedural and editable.
export function makeWorld(canvas, width = 1000, height = 800) {
  const renderer = new THREE.WebGLRenderer({canvas, antialias: true, alpha: true, preserveDrawingBuffer: true});
  renderer.setSize(width, height, false);
  renderer.setPixelRatio(1);
  renderer.outputColorSpace = THREE.SRGBColorSpace;
  renderer.toneMapping = THREE.ACESFilmicToneMapping;
  renderer.toneMappingExposure = 1.12;
  renderer.shadowMap.enabled = true;
  renderer.shadowMap.type = THREE.PCFShadowMap;
  const scene = new THREE.Scene();
  const camera = new THREE.OrthographicCamera(-5.1 * width / height, 5.1 * width / height, 5.1, -5.1, .1, 100);
  camera.position.set(8, 7, 10); camera.lookAt(0, 1, 0);

2. Add the lights

The hemisphere light fills dark surfaces, while the directional light provides the key and shadow source. Paste inside the still-open function.

  const fill = new THREE.HemisphereLight(0xcdeaff, 0x9f8274, 2.1);
  scene.add(fill);
  const key = new THREE.DirectionalLight(0xffe8c8, 3.1);
  key.position.set(-3, 8, 5); key.castShadow = true;
  key.shadow.mapSize.set(2048, 2048);
  Object.assign(key.shadow.camera, {left:-8,right:8,top:8,bottom:-8,near:.5,far:30});
  key.shadow.bias = -0.0002; scene.add(key);

3. Add the palette, mesh helpers, and island

These helpers create real meshes and set shadow flags. The two shallow cylinders make the layered island.

  const mat = (color, roughness = .82) => new THREE.MeshStandardMaterial({color, roughness});
  const palette = {grass:mat('#9ccc9c'), edge:mat('#d5b38e'), cream:mat('#fff3d6'), peach:mat('#ef9475'), dark:mat('#3b3536'), pink:mat('#ed9d9d'), leaf:mat('#719e7a'), flower:mat('#ffdf8c')};
  const mesh = (geo, material, x=0,y=0,z=0, parent=scene) => {const m = new THREE.Mesh(geo,material);m.position.set(x,y,z);m.castShadow=true;m.receiveShadow=true;parent.add(m);return m;};
  const sphere=(material,x,y,z,sx,sy,sz,parent=scene)=>{const m=mesh(new THREE.SphereGeometry(1,32,24),material,x,y,z,parent);m.scale.set(sx,sy,sz);return m;};
  const base=mesh(new THREE.CylinderGeometry(4.15,4.15,.48,64),palette.edge,0,-.14,0);
  const turf=mesh(new THREE.CylinderGeometry(4.16,4.16,.12,64),palette.grass,0,.15,0);

4. Build the character function

A group holds all body parts. Each scaled sphere uses group-local coordinates. The loop mirrors paired features.

  const character=(x,z,bodyColor, scale=1)=>{
    const group=new THREE.Group(); group.position.set(x,.2,z); group.scale.setScalar(scale);scene.add(group);
    sphere(bodyColor,0,1.05,0,.8,.95,.65,group);
    sphere(bodyColor,0,2.1,.06,.76,.7,.64,group);
    for(const side of [-1,1]) {
      const ear=sphere(bodyColor,side*.5,2.75,0,.22,.65,.22,group);ear.rotation.z=-side*.2;
      const inner=sphere(palette.pink,side*.5,2.78,.195,.105,.39,.035,group);inner.rotation.z=-side*.2;
      sphere(palette.dark,side*.25,2.17,.63,.07,.09,.04,group);
      sphere(palette.pink,side*.43,1.99,.59,.14,.08,.025,group);
      const arm=sphere(bodyColor,side*.77,1.12,.02,.2,.53,.23,group);arm.rotation.z=side*.53;
      sphere(bodyColor,side*.35,.22,.22,.31,.19,.38,group);
    }
    sphere(palette.dark,0,2.01,.685,.075,.05,.035,group);
    return group;
  };

5. Place characters and environment

Call the builder twice, then create two trees and the small flowers. Position values are in world units.

  const hero=character(-1.05,.15,palette.cream,1);
  const friend=character(1.25,-.2,palette.peach,.77);
  const beforeDetails = new Set(scene.children);
  // Simple, repeated motifs establish scale without distracting from the characters.
  const tree=(x,z,s)=>{const g=new THREE.Group();g.position.set(x,.2,z);g.scale.setScalar(s);scene.add(g);mesh(new THREE.CylinderGeometry(.13,.17,1.2,12),palette.edge,0,.6,0,g);sphere(palette.leaf,0,1.55,0,.7,.88,.7,g);};
  tree(-2.6,-1.8,1); tree(2.5,-2.0,.85);
  for (const [x,z] of [[-2.8,1.4],[-2,2.5],[.3,2.7],[2.5,1.6],[3,.2],[-.3,-2.9]]) {
    const stem=mesh(new THREE.CylinderGeometry(.025,.025,.28,8),palette.leaf,x,.35,z);
    sphere(palette.flower,x,.52,z,.13,.08,.13);
  }

6. Render the animated scene

The optional stage, reveal, and lighting arguments support the instructional video; ordinary browser use calls render(t) with their defaults. The animation uses seconds.

  const details = scene.children.filter(o => !beforeDetails.has(o));
  function render(t=0, stage=0, animate=true, lighting=true, reveal=4, showFriend=true){key.visible=lighting; fill.intensity=lighting ? 2.1 : 1.6; renderer.shadowMap.enabled=lighting; hero.visible = stage === 0 || stage >= 3;
    friend.visible = hero.visible && showFriend;
    const level = [1,1,2,2,3,3,4,4,2,2,3,3,4,4,3];
    for (const character of [hero, friend]) character.children.forEach((part, index) => { part.visible = level[index] <= reveal; });
    details.forEach(o => {o.visible = stage === 0 || stage >= 2}); t = animate ? t : 0; hero.position.y=.2 + Math.sin(t*2)*.045;hero.rotation.z=Math.sin(t*1.3)*.025;friend.position.y=.2 + (animate ? Math.sin(t*2+1)*.04 : 0);friend.rotation.z=-Math.sin(t*1.3)*.035;renderer.render(scene,camera);}

7. Resize without rebuilding

Update the orthographic bounds and projection before resizing the existing canvas.

  function resize(width, height) {
    const aspect = width / Math.max(1, height);
    const halfHeight = 5.1 * Math.max(1, 1 / aspect);
    camera.left = -halfHeight * aspect;
    camera.right = halfHeight * aspect;
    camera.top = halfHeight;
    camera.bottom = -halfHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(width, height, false);
  }

8. Clean up and return the API

This final block disposes resources, returns the public methods, and closes makeWorld.

  function dispose(){scene.traverse(o=>{if(o.isMesh){o.geometry.dispose(); if(Array.isArray(o.material))o.material.forEach(m=>m.dispose()); else o.material.dispose();}});renderer.dispose();}
  return {scene,camera,renderer,hero,friend,render,resize,dispose,base,turf,details,fill,key,palette};
}

9. Run your reconstruction

In lesson/main.js, temporarily change the import from ./world.js to ./world-built.js. Save both files. Vite reloads the browser, where you should see the same two animated characters and island as the original. Change the import back to ./world.js whenever you want the original again.

If the canvas is blank, check the browser console and terminal. Common errors include a missing closing brace, a block pasted out of order, or opening the HTML file directly instead of using Vite. If shadows are missing, verify the renderer, key light, casting mesh, and receiving mesh flags.

10. Experiment

Change one value at a time: the palette, a sphere scale, an ear tilt, or the wave amplitude. Resize to a portrait window and compare the camera framing. To make one ear shorter, change the vertical scales of both the outer and inner spheres in the loop as described in GUIDE.md. Save and observe each change.

Download the exercise ZIP Read the companion guide