SuccessGraph
← Back to the class

Build the little world

This companion to the video assumes basic JavaScript familiarity. The working scene is world.js; all of its shapes and colors are editable. Run npm ci in the project root, then npm run lesson, and open the local URL printed by Vite.

1. Scene, camera, and renderer

makeWorld(canvas, width, height) creates the renderer, scene, and camera. The orthographic camera keeps parallel lines parallel, making the island look like a small set. Its left and right bounds use the canvas aspect ratio, while its position and lookAt define the composition. Try changing the camera position from (8, 7, 10) to (5, 5, 12).

The renderer uses sRGB output and ACES filmic tone mapping. Three.js works in a linear color space for lighting and converts for display. For color image textures, set the texture's colorSpace to THREE.SRGBColorSpace; data textures such as roughness and normal maps should not be marked as color. This project uses solid colors, so there are no textures to configure.

2. Island and props

A cylinder is a useful low-cost starting point for a round island. The taller tan cylinder forms the soil; a thin green cylinder sits on top. The helper mesh adds meshes to a parent and enables casting and receiving shadows. Trees combine a cylinder trunk with a scaled sphere. Flowers repeat a tiny stem and a colored top. Change the coordinate pairs in the flower loop to rearrange them.

3. Character

The character function creates a THREE.Group. It adds scaled spheres for body, head, ears, eyes, cheeks, arms, and feet. Coordinates are local to the group, so a change to the group's position, rotation, or scale transforms the complete character. The helper is called twice with different positions, colors, and scales. Try moving the eyes slightly apart or changing ear length: small changes have a large effect on expression.

4. Lighting and shadows

A hemisphere light gives the scene a broad sky/ground fill. A directional light supplies the warm key and shadow direction. Shadows require four settings: renderer.shadowMap.enabled, key.castShadow, a casting mesh's castShadow, and a receiving mesh's receiveShadow. The shadow camera's left, right, top, bottom, near, and far bounds limit the region covered by its map. Tight bounds improve useful shadow detail, but must still include both the objects and the surfaces receiving their shadows.

MeshStandardMaterial responds to light. The materials use high roughness for a soft, matte look. Start with lighting and composition before adding more effects.

5. Animation and responsiveness

render(t) offsets each group with sine waves. The friend uses a phase offset so the motion does not look mechanically synchronized. main.js converts the requestAnimationFrame timestamp from milliseconds to seconds and passes it to the scene. Time-based motion keeps its speed independent of frame rate.

The demo resizes the existing renderer and updates the orthographic camera bounds and projection. It expands the vertical view in portrait windows so the island stays in frame. Pixel ratio is fixed at one to bound rendering cost; test the quality and performance tradeoff on your target devices. The video uses a fixed-size renderer for deterministic frames.

6. Try a variation

  1. Change the grass and character colors in palette.
  2. Make one ear shorter and move the eyes to change the expression.
  3. Add a prop as another child of a character group.
  4. Reduce the sway amplitude and compare the mood.
  5. Check the scene at a smaller window size and watch for clipped shadows.

References

Follow the full video

The video uses this project as its starting point rather than asking you to type a complete application from an empty directory. Read world.js alongside the chapter examples; helper functions appear before their calls in that file. Here is the practical order:

  1. Open the project folder in a code editor. In a terminal in that folder, run npm ci, then npm run lesson. Open the local address printed by Vite. Stop the server with Ctrl-C when finished.
  2. In index.html, find the canvas and the module script. In main.js, trace how the canvas enters makeWorld and how tick supplies seconds.
  3. In world.js, find the renderer, output settings, scene, orthographic camera, and two lights. Try moving the key light. The camera uses bounds, not camera.aspect; resizing updates those bounds and calls camera.updateProjectionMatrix().
  4. Find mat, palette, mesh, and sphere. A geometry defines the shape, a material defines the surface, and a mesh joins them. The last argument to sphere is the parent group.
  5. Find the two cylinders and adjust the turf height by a small amount. A gap between them reveals how placement works. Restore the original value when finished.
  6. Find the character function. The body, head, ears, face, arms, and feet are children of one group. side mirrors the paired parts. Make one outer ear shorter by changing its vertical scale argument from .65 to (side < 0 ? .45 : .65); change the inner ear's vertical scale from .39 to (side < 0 ? .25 : .39) so it still fits. Save and compare.
  7. The hero and friend calls reuse the same builder with different positions, colors, and scales. Change the friend's color or scale and inspect the relationship between them.
  8. Find tree and the flower coordinate loop. Rearrange one flower without moving the others. Keep the face area unobstructed.
  9. Inspect shadow setup: the renderer must have shadows enabled, the light must cast, the caster mesh must cast, and the receiving mesh must receive. The mesh helper sets the last two flags. When shadows are absent, check all four conditions and whether the shadow camera covers the scene.
  10. In render, compare the hero's and friend's sine expressions. The +1 in the friend's vertical motion is a phase offset. Halve the amplitudes to make the movement quieter, then restore them if desired.
  11. Resize the browser to a tall shape. resize updates all four orthographic bounds, calls updateProjectionMatrix, and changes the renderer size without recreating the scene. The example uses pixel ratio 1 for predictable cost; higher ratios improve sharpness but can require much more GPU work.

The captions are a separate SRT file and can be edited independently. In the extended version, code examples on screen are short excerpts of the complete source, not standalone files; use the actual project for the surrounding declarations and helper functions.

References for the full class

The portrait-safe orthographic bounds used by the runnable scene are:

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);

The extra vertical room prevents the horizontal view becoming too narrow in portrait orientation.

Follow along with the beginner edition

Open the chapter list in CHAPTERS.txt at the project root beside the video. The companion project opens on the finished scene so you can confirm your tooling first. To rebuild it, follow BUILD-FROM-SCRATCH.md and keep the original world.js open for comparison.

Checkpoint What to do What you should see
Setup In the directory containing package.json, run npm ci, then npm run lesson. Open the exact Local URL and leave the terminal running. Two characters on a grassy island. If it is blank, open the browser console and fix the first reported error before proceeding.
First image In main.js, find the canvas, makeWorld, and the first requestAnimationFrame(tick). In world.js, find renderer.render(scene, camera). The render call draws the scene through the camera onto the canvas. The animation loop calls it again with a new time.
Character In the paired ear loop, change the outer ear's X scale from .22 to .27, then save. Both outer ears get wider. Position remains mirrored by side. Restore .22 before continuing.
Lighting In render, temporarily change the default lighting=true to lighting=false and reload. Restore it afterwards. The fill still shows the shapes but the directional light and cast shadows disappear.
Motion Find hero.position.y and change the amplitude from .045 to .0225. The hero's vertical travel halves. Timing and the friend's movement stay the same.
Resize Resize the browser from landscape to portrait. The island remains inside the frame. If a resize edit stretches it, check the orthographic bounds and the projection update.
Download the exercise ZIP Build from scratch