TransitionGenerator.js 3.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
const GlslTransitions = require("glsl-transitions");
const byName = {};
GlslTransitions.forEach(function (t) {
  byName[t.name] = t;
});
const transitions = [
  [ "cube", function () {
    return { persp: 0.9 - Math.random()*Math.random(), unzoom: Math.random()*Math.random() };
  } ],
  "undulating burn out",
  [ "CrossZoom", function () {
    return { strength: 0.5 * Math.random() };
  } ],
  "glitch displace",
  [ "Mosaic", function () {
    const dx = Math.round(Math.random() * 6 - 3), dy = Math.round(Math.random() * 6 - 3);
    if (dx===0 && dy===0) dy = -1;
    return { endx: dx, endy: dy };
  } ],
  [ "DoomScreenTransition", function () {
    return {
      barWidth: Math.round(6 + 20 * Math.random()),
      amplitude: 2 * Math.random(),
      noise: 0.5 * Math.random(),
      frequency: Math.random()
    };
  } ],
  [ "colourDistance", function () {
    return { interpolationPower: 6 * Math.random() };
  } ],
31
  "swap",
32 33 34 35 36 37 38 39 40
  [ "doorway", function () {
    return { perspective: Math.random() * Math.random(), depth: 1 + 10 * Math.random() * Math.random() };
  } ],
  "Star Wipe",
  "pinwheel",
  "SimpleFlip",
  "TilesScanline",
  "Dreamy",
  "Swirl",
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
41
  "burn",
42 43 44 45 46 47 48 49 50 51 52
  "Radial",
  [ "ripple", function () {
    return {
      amplitude: 200 * Math.random(),
      speed: 200 * Math.random()
    };
  } ],
  "morph",
  ["ButterflyWaveScrawler", function () {
    return {
      amplitude: Math.random(),
53
      waves: 50 * Math.random() * Math.random(),
54 55 56 57 58 59 60
      colorSeparation: 0.8 * Math.random() * Math.random()
    };
  } ],
  [ "flash", function () {
    return { flashIntensity: 4 * Math.random() };
  } ],
  [ "randomsquares", function () {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
61
    const size = Math.round(4 + 20 * Math.random());
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    return {
      size: [ size, size ],
      smoothness: Math.random()
    };
  } ],
  [ "flyeye", function () {
    return {
      size: Math.random() * Math.random(),
      zoom: 200 * Math.random() * Math.random(),
      colorSeparation: 0.8 * Math.random() * Math.random()
    };
  } ],
  "squeeze",
  [ "directionalwipe", function () {
    const angle = Math.random() * 2 * Math.PI;
    return {
      direction: [ Math.cos(angle), Math.sin(angle) ]
    };
  } ],
  "circleopen",
  [ "wind", function () {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
83
    return { size: 0.1+0.2 * Math.random() };
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  } ]
].map(function (obj) {
  let name, genUniforms;
  if (typeof obj === "string")
    name = obj;
  else {
    name = obj[0];
    genUniforms = obj[1];
  }
  if (!(name in byName)) throw new Error("no transition called "+name);
  const t = byName[name];
  return {
    transition: t,
    name: name,
    genUniforms: function () {
      return genUniforms ? {
        ...t.uniforms,
        ...genUniforms()
      } : t.uniforms;
    }
  };
});

function random () {
  const i = Math.floor(Math.random() * transitions.length);
  const t = transitions[i];
  const uniforms = t.genUniforms && t.genUniforms() || {};
  return {
    name: t.name,
    uniforms: uniforms
  };
}

const shaders = {};
transitions.forEach(function (o) {
  shaders[o.name] = { frag: o.transition.glsl };
});

module.exports = {
  shaders: shaders,
  random: random
};