Software Cube Dance

Cube Dance

A ring of cubes spins around a center cube that is waiting for a cue. Click an outer cube and the center cube picks up its rotation. Scale, speed, cube count, and colors are all live.

Your browser does not support the HTML5 canvas element.

Click an outer cube

How it works

There is exactly one cube model: eight vertices arranged into 36 triangle vertices with a different color on each face. Every cube on screen is an instance of that model, drawn with its own transformation matrix.

  • Instancing. Each frame, JavaScript builds a model-view matrix per cube (translate, rotate about an arbitrary axis, scale) and uploads it as a uniform before drawing the shared mesh.
  • The ring. Outer cubes sit evenly on a circle of radius 0.75. Each one rotates about the line from the origin through its own center.
  • Picking by ray. A click is converted to normalized device coordinates, unprojected through the inverse view-projection matrix into a ray, and intersected with the plane the cubes live on. The nearest cube within range is selected.
  • Depth and perspective. A perspective camera looks at the scene with the depth buffer enabled, so faces of overlapping cubes are resolved correctly.
  • Extras. The outer ring can grow from 4 to 20 cubes, and the face colors can be re-rolled with random hues.

Concepts

  • Model instancing
  • Model-view matrices
  • Rotation about an axis
  • Depth buffering
  • Ray picking
  • Perspective projection

Built with

  • WebGL
  • GLSL
  • JavaScript

Background

Written for CSE 470, Introduction to Computer Graphics, at Arizona State University. Uses the course-supplied webgl-utils and MV.js helper libraries and unit-cube vertex data.