Comp Sci A-Level • University ●●○ Intermediate ★ Free

3D Cube (Three.js)

Welcome to the third dimension. Drag to rotate. This scene is rendered using WebGL via the Three.js library.

Color:
Shape:
Rotation Speed: 5
Simulation running

3D Graphics: The Mathematics Behind the Cube

🏴 WebGL and the Rendering Pipeline

This simulator uses Three.js, a high-level wrapper around WebGL � the browser API that communicates directly with the GPU. Every frame passes through a pipeline: vertex shader (transforms each point in 3D), rasterisation (converts triangles to pixels), and fragment shader (applies lighting and colour to each pixel). A cube has 8 vertices and 12 triangles (2 per face � 6 faces).

📈 Rotation Matrices

3D rotation is performed by multiplying a vertex column vector by a 3×3 rotation matrix. Rotation around the Y-axis by angle θ maps (x, y, z) to (x cosθ + z sinθ, y, −x sinθ + z cosθ). Combining rotations requires multiplying matrices: Rtotal = RZ RY RX. Matrix multiplication is not commutative � order matters and produces different orientations.

📷 Perspective Projection

In a perspective camera, objects farther from the camera appear smaller. A vertex at position (x, y, z) is projected to screen coordinates roughly as (x/z, y/z) � dividing by the z-distance creates the perspective effect. The view frustum is a truncated pyramid: everything outside it is clipped. The frustum is defined by field-of-view (FOV), aspect ratio, near plane, and far plane.

🛠️ Quaternions and Gimbal Lock

Euler angles (three angle values for X, Y, Z axes) are intuitive but suffer from gimbal lock � when two axes align, you lose a degree of rotational freedom. Three.js internally uses quaternions (a 4-component number q = w + xi + yj + zk) which represent orientations without singularities. Quaternion slerp (spherical linear interpolation) gives smooth rotational animation.

Key Transformations & Formulas

TransformMatrix / FormulaEffect
Rotation XRX(θ): [1,0,0 / 0,cosθ,−sinθ / 0,sinθ,cosθ]Rotates around the X-axis (pitch)
Rotation YRY(θ): [cosθ,0,sinθ / 0,1,0 / −sinθ,0,cosθ]Rotates around the Y-axis (yaw)
Rotation ZRZ(θ): [cosθ,−sinθ,0 / sinθ,cosθ,0 / 0,0,1]Rotates around the Z-axis (roll)
Perspective dividex' = f·x/z, y' = f·y/zSmaller = further
Quaternion rotationq = (cos½θ, sin½θ·&hat;n)Axis-angle, no gimbal lock
Quaternion compositionp·q (Hamilton product)Combines two rotations
Normal vectorn⃗ = (vB−vA) × (vC−vA)Perpendicular to triangle face
Dot product (lighting)I = n⃗ · L⃗Lambertian diffuse shading

Curriculum Links

LevelSubjectTopic Covered
GCSE MathsGeometry2D/3D coordinates; reflections; rotations on a grid
A-Level MathsCore Pure / FurtherVectors; transformations; dot and cross product; matrices
A-Level CSComputer GraphicsRasterisation; GPU pipeline; coordinate systems
IB HL MathsVectors (Topic 3)3D bearings; matrix transformations; determinants
AP CSP / CSData and AlgorithmsAbstraction in rendering; coordinate geometry
University Year 1Linear Algebra / CGHomogeneous coordinates; view / projection matrices; quaternion algebra

Experiments to Try

Wireframe Analysis

Enable Wireframe and count the triangles on each geometry. A cube has 12, a sphere (32×32 segments) has ~2048. Notice how more triangles create a smoother silhouette.

Rotation vs. Spin Axis

Enable auto-rotation and drag to reorient the camera. Observe how the object still spins around its own Y-axis even as you change viewpoint � this is the difference between object space and world space.

Compare Geometries

Switch between dodecahedron (12 pentagonal faces) and icosahedron (20 triangular faces). Both have the same number of polygon faces as Platonic solids but completely different visual appearances.

Colour and Lighting

Change the colour and observe how the Phong lighting model shades each face. Darker edges indicate faces pointing away from the light source � this is the dot product of surface normal and light direction.

🔒 Unlock Premium Features

Unlock HD screenshot export, custom GLSL shader editor, animation recording, scene JSON export, and all 32 simulations with MySimulator Premium.

Share this simulation

Send this page to students or colleagues.

Related simulations

Continue with similar experiments.