CREATING & ANIMATING
SIMPLE 3D OBJECTS

By Shane Brumback · · Updated · Free Three.js tutorial · Level 1

To animate a simple 3D object in Three.js, put a Mesh in the scene and change its position, rotation and scale every frame from a render loop. This glossy blue cube cycles through a hop, a spin jump and three flips, with eased rotation and squash and stretch on every landing.

A beginner walkthrough of MeshPhysicalMaterial, a gradient sky dome, soft shadows and instanced wind-blown grass in Three.js. The scene behind this page is the live demo, and the full copy-paste source is below.

✓ FREE Level 1 · Beginner Keyframe-style animation Squash & stretch InstancedMesh grass
See it live

LIVE DEMO

The cube jumping behind this page is a live Three.js scene. Launch the full-screen demo to drag to orbit and scroll to zoom while it hops, spins and flips over the swaying grass. 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

READY FOR ENDLESS TERRAIN?

This Level 1 meadow is a small circle of grass. The Level 3 premium Terrain LOD tutorial streams endless hills with sand, grass, rock and snow blended in an onBeforeCompile shader, with LOD skirts, worker chunks and grass that follows you. Members unlock the full code; sign in to reveal it.

Three.js terrain LOD and splat shader premium tutorial
See the Level 3 Tutorial

How Animation Works in Three.js

Three.js is a JavaScript library that wraps WebGL, the browser API for GPU graphics. An animation is just a picture redrawn about 60 times a second, with an object a tiny bit different each time. You move a Mesh by changing its position, rotation and scale before each draw.

To make motion feel alive, this demo borrows two cartoon tricks: easing, so turns start and stop softly, and squash and stretch, so the cube flattens when it lands. New to Three.js? Start with the spinning cube starter or rendering a 3D cube. The grass comes from the endless procedural grass tutorial.

How It Works

  1. Set up the scene, camera and renderer. We make an empty 3D world, a camera to look at it, and a renderer that draws it with soft shadows and filmic color.
  2. Paint a gradient sky dome with a sun glow. We wrap the world in a giant inside-out ball and color it blue at the top, pale near the horizon, with a bright spot where the sun is.
  3. Float soft sprite clouds. We draw a few fuzzy white blobs on a little canvas and hang copies of it high in the sky.
  4. Light it with a shadow-casting sun. We add a sky light for soft fill and one strong sun light that casts the cube shadow onto the grass.
  5. Build a pretty blue physical cube. We make a rounded cube out of shiny blue car-paint style material and nest it in two groups so it can squash and flip separately.
  6. Write the grass wind shader. We write a tiny piece of GPU code that bends the top of every grass blade with a slow wave so the field sways.
  7. Plant grass with one InstancedMesh. We stamp one small blade shape tens of thousands of times in a single draw call, and let it catch the cube shadow.
  8. Choreograph jumps, spins and flips. We write a short playlist of moves: a hop, a spin jump, a front flip, a side flip and a twisting flip.
  9. Add squash and stretch on landing. The cube crouches before it jumps, stretches as it takes off, and wobbles flat when it lands, like a cartoon.
  10. Run the render loop. Every frame we advance time, move the cube, sway the grass and draw the picture.

The detailed code breakdown and the full copy-paste file are below. Every step is an exact excerpt of the full file, and the full file is the same code as the live demo.

Want the cube to react to clicks? Try raycaster selection, or swap the blue paint for a custom shader in the GLSL gradient cube tutorial.

Frequently Asked Questions

How do you animate a simple 3D object in Three.js?

Create a Mesh from a geometry and a material, add it to the scene, and change its position, rotation or scale inside a requestAnimationFrame render loop. Multiply changes by the frame delta from THREE.Clock so the motion runs at the same speed on every screen.

How do you make a cube jump and flip in Three.js?

Move the cube up and down with a parabola, 4 times p times 1 minus p, where p goes from 0 to 1 over the air time. At the same time ease its rotation from 0 to a full turn of Math.PI times 2 on the X or Z axis for a flip, or the Y axis for a spin.

What is squash and stretch?

It is a classic animation principle: an object flattens when it lands or crouches and stretches tall when it takes off. In Three.js you scale Y by a squash value and scale X and Z by one over its square root so the cube keeps the same volume.

Which material makes a cube look glossy?

MeshPhysicalMaterial with a little metalness, low roughness and a clearcoat layer gives a car-paint shine. It needs light to reflect, so add an environment map such as a PMREM RoomEnvironment and a directional sun.

How is the grass drawn so fast?

The grass is one InstancedMesh: the GPU gets one small blade shape and a list of matrices, then draws every copy in a single draw call. A wind function injected with onBeforeCompile bends each tip, and the demo uses fewer blades on phones.

Why do I need to sign in to launch the live demo?

The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site, so I know who is learning from these tutorials.

Play It and Keep Learning

Grass, sky and snappy animation like this power Super Soldier Battle, free in your browser. Browse every Three.js tutorial, read the articles, or search the site.

Need Three.js animation or WebGL work? I take freelance and contract work: contact me.

Play Game