Animating ThreeJS ShaderMaterial Uniforms

by Thanos Saringelos

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/87/three.js"></script>
<script data-name="orbitControls.js">
  /**
   * @author qiao / https://github.com/qiao
   * @author mrdoob / http://mrdoob.com
   * @author alteredq / http://alteredqualia.com/
   * @author WestLangley / http://github.com/WestLangley
   * @author erich666 / http://erichaines.com
   */

  // This set of controls performs orbiting, dollying (zooming), and panning.
  // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  //
  //    Orbit - left mouse / touch: one finger move
  //    Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
  //    Pan - right mouse, or arrow keys / touch: three finger swipe

  THREE.OrbitControls = function(object, domElement) {

    this.object = object;

    this.domElement = (domElement !== undefined) ? domElement : document;

    // Set to false to disable this control
    this.enabled = true;

    // "target" sets the location of focus, where the object orbits around
    this.target = new THREE.Vector3();

    // How far you can dolly in and out ( PerspectiveCamera only )
    this.minDistance = 0;
    this.maxDistance = Infinity;

    // How far you can zoom in and out ( OrthographicCamera only )
    this.minZoom = 0;
    this.maxZoom = Infinity;

    // How far you can orbit vertically, upper and lower limits.
    // Range is 0 to Math.PI radians.
    this.minPolarAngle = 0; // radians
    this.maxPolarAngle = Math.PI; // radians

    // How far you can orbit horizontally, upper and lower limits.
    // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
    this.minAzimuthAngle = -Infinity; // radians
    this.maxAzimuthAngle = Infinity; // radians

    // Set to true to enable damping (inertia)
    // If damping is enabled, you must call controls.update() in your animation loop
    this.enableDamping = false;
    this.dampingFactor = 0.25;

    // This option actually...

JavaScript

var container, stats;

var camera, scene, renderer, mesh, mixer;

var uniforms1, uniforms2;

var clock = new THREE.Clock();

init();
animate();

function init() {

  container = document.getElementById( 'container' );

  camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 3000 );
  camera.position.z = 4;

  scene = new THREE.Scene();

  var geometry = new THREE.PlaneGeometry( 2.3,2.3 );

  uniforms1 = {
    frame:      { value: 1.0 },
    resolution: { value: new THREE.Vector2() }
  };

  var material = new THREE.ShaderMaterial( {

    uniforms: uniforms1,
    vertexShader: document.getElementById( 'vertexShader' ).textContent,
    fragmentShader: document.getElementById( 'fragmentShader' ).textContent

  } );

  mesh = new THREE.Mesh( geometry, material );
	mesh.materialDotUniformsDotFrame = material.uniforms.frame;
  var track = new THREE.NumberKeyframeTrack( '.materialDotUniformsDotFrame[value]', [ 0, 1, 2 ], [ 1, 0, 1 ] );
  var clip = new THREE.AnimationClip( 'Action', 3, [ track ] );
  mixer = new THREE.AnimationMixer( mesh );
  var clipAction = mixer.clipAction( clip );
  clipAction.play();

	scene.add( mesh );

  renderer = new THREE.WebGLRenderer({alpha:true});
  renderer.setClearColor(0x000000,0)
  renderer.setPixelRatio( window.devicePixelRatio );
  container.appendChild( renderer.domElement );

  onWindowResize();

  window.addEventListener( 'resize', onWindowResize, false );
	  controls = new THREE.OrbitControls(camera, renderer.domElement);
    controls.addEventListener('change', render); 
}

function onWindowResize( event ) {

  uniforms1.resolution.value.x = window.innerWidth;
  uniforms1.resolution.value.y = window.innerHeight;

  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();

  renderer.setSize( window.innerWidth, window.innerHeight );

}

function animate() {

  requestAnimationFrame( animate );

  render();

}

function render() {

  var delta = clock.getDelta();

 ...