TransitionGenerator.js 3.17 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 31 32 33 34 35 36 37 38 39 40 41 42 43
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() };
  } ],
  [ "swap", function () {
    return { depth: 1 + 4 * Math.random(), perspective: 0.9 + Math.random() * Math.random() };
  } ],
  [ "doorway", function () {
    return { perspective: Math.random() * Math.random(), depth: 1 + 10 * Math.random() * Math.random() };
  } ],
  "Star Wipe",
  "pinwheel",
  "SimpleFlip",
  "TilesScanline",
  "Dreamy",
  "Swirl",
  "HSVfade",
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
44
  "burn",
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  "Radial",
  [ "ripple", function () {
    return {
      amplitude: 200 * Math.random(),
      speed: 200 * Math.random()
    };
  } ],
  "morph",
  ["ButterflyWaveScrawler", function () {
    return {
      amplitude: Math.random(),
      waves: 100 * Math.random() * Math.random(),
      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
64
    const size = Math.round(4 + 20 * Math.random());
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    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
86
    return { size: 0.1+0.2 * Math.random() };
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 126 127 128
  } ]
].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
};