WebGL Animation With three.js

NickCoutsos
GitHubgithub.com/nickcoutsos
Twitternull
Instagramnull

Presenting now! Follow along remotely:

https://nickcoutsos.github.io/threejs-animation-lunch-and-learn/

Introduction

By drawing it more frequently in more positions the movement is smoother and the animation nicer to watch.

See: requestAnimationFrame()

Tweening

Tweening allows us to define simple animations using interpolation to fill in the blanks.

Tweening: continued

So what else can we tween?

More Tweening:

What about material properties?

Time

Thus far we've interpolated values linearly with a fixed number of steps.

Pretend we have an infinite number of steps

Pretend that this set of steps is time. Because it is.

Rendering

const start = performance.now()
const duration = 5000

function renderFrame(now) {
  const delta = now - start
  const t = delta / duration

  myAnimation(t)

  if (delta <= duration) {
    requestAnimationFrame(renderFrame)
  }
}

Timing functions

Now our animated properties are functions of time.

A timing function:

That input represents time, or, completed percentage of an animation a known duration.

Scaling property changes with respect to elapsed time means they are animated independently of the framerate.

Timing functions: examples

Timing functions: complified

As long as you take a number and return a number...

const bounce = t => {
  if (t < 1/2.75) {
    return 7.5625*t*t
  } else if (t < (2/2.75)) {
    return 7.5625*(t-=(1.5/2.75))*t + .75;
  } else if (t < (2.5/2.75)) {
    return 7.5625*(t-=(2.25/2.75))*t + .9375
  } else {
    return 7.5625*(t-=(2.625/2.75))*t + .984375
  }
}

... you can take it as far as you need to.

Timing functions: simplified

npm install easing-utils
import { bounce } from 'easing-utils'

Part three dot js

What is it?

Object3Ds all the way down

The scene graph is a hierarchy of objects

Example: dodecahedron net

Example: keyboard layout morph

Let's have some fun

Q & A

Re-sync with presenter