1(function(Blotter) {
  2
  3  Blotter.SlidingDoorMaterial = function() {
  4    Blotter.Material.apply(this, arguments);
  5  };
  6
  7  Blotter.SlidingDoorMaterial.prototype = Object.create(Blotter.Material.prototype);
  8
  9  Blotter._extendWithGettersSetters(Blotter.SlidingDoorMaterial.prototype, (function () {
 10
 11    function _mainImageSrc () {
 12      var mainImageSrc = [
 13        Blotter.Assets.Shaders.PI,
 14
 15        "float easingForPositionWithDeadzoneWidth(float position, float deadzoneWidth) {",
 16        "    // Offset position buy 0.25 so that sin wave begins on a downslope at 0.0",
 17        "    position += 0.25;",
 18
 19        "    // Distance of adjusted position from 0.75, min of 0.0 and max of 0.5",
 20        "    float firstDist = distance(position, 0.75);",
 21
 22        "    // Divide deadzoneWidth by two, as we will be working it out in either direction from the center position.",
 23        "    float halfDeadzoneWidth = deadzoneWidth / 2.0;",
 24
 25        "    // Clamp distance of position from center (0.75) to something between 0.5 and the halfDeadzoneWidth from center.",
 26        "    float removedDistance = max(firstDist, halfDeadzoneWidth);",
 27
 28        "    // Find the percentage of removedDistance within the range of halfDeadzoneWidth..0.5",
 29        "    float distanceOfRange = (removedDistance - halfDeadzoneWidth) / (0.5 - halfDeadzoneWidth);",
 30
 31        "    // Convert distanceOfRange to a number between 0.0 and 0.5. This means that for any pixel +/- halfDeadzoneWidth from center, the value will be 0.5.",
 32        "    float offsetDist = (0.5 * (1.0 - (distanceOfRange)));",
 33
 34        "    if (position < 0.75) {",
 35        "        position = 0.25 + offsetDist;",
 36        "    } else {",
 37        "        position = 1.25 - offsetDist;",
 38        "    }",
 39
 40
 41        "    return sin((position) * PI * 2.0) / 2.0;",
 42        "}",
 43
 44
 45        "void mainImage( out vec4 mainImage, in vec2 fragCoord )",
 46        "{",
 47        "    // Setup ========================================================================",
 48
 49        "    vec2 uv = fragCoord.xy / uResolution.xy;",
 50
 51        "    float time = uGlobalTime * uSpeed;",
 52
 53        "    float directionalAdjustment = uFlipAnimationDirection > 0.0 ? -1.0 : 1.0;",
 54
 55
 56        "    if (uSpeed > 0.0) {",
 57
 58        "       // Define Axis-Based Striping ===================================================",
 59
 60        "       float divisions = uDivisions;",
 61        "       float effectPosition = fragCoord.y;",
 62        "       float effectDimension = uResolution.y;",
 63        "       if (uAnimateHorizontal > 0.0) {",
 64        "          effectPosition = fragCoord.x;",
 65        "          effectDimension = uResolution.x;",
 66        "          divisions = floor((divisions * (uResolution.x / uResolution.y)) + 0.5);",
 67        "       }",
 68        "       float stripe = floor(effectPosition / (effectDimension / divisions));",
 69
 70
 71        "       // Animate =====================================================================",
 72
 73        "       float timeAdjustedForStripe = time - ((uDivisionWidth / divisions) * stripe) * directionalAdjustment;",
 74        "       float offsetAtTime = mod(timeAdjustedForStripe, 1.0);",
 75
 76        "       // Divide sin output by 2 and add to 0.5 so that sin wave move between 0.0 and 1.0 rather than -1.0 and 1.0.",
 77        "       float easing = 0.5 + easingForPositionWithDeadzoneWidth(offsetAtTime, uDivisionWidth);",
 78
 79        "       // Mulptiply offsetAtTime by 2.0 and subtract from 1.0 so that position changes over a range of -1.0 to 1.0 rather than 0.0 to 1.0.",
 80        "       if (uAnimateHorizontal > 0.0) {",
 81        "          uv.x -= ((1.0 - (offsetAtTime * 2.0)) * easing) * directionalAdjustment;",
 82        "       } else {",
 83        "          uv.y -= ((1.0 - (offsetAtTime * 2.0)) * easing) * directionalAdjustment;",
 84        "       }",
 85        "    }",
 86
 87        "    mainImage = textTexture(uv);",
 88
 89        "}"
 90      ].join("\n");
 91
 92      return mainImageSrc;
 93    }
 94
 95    return {
 96
 97      constructor : Blotter.SlidingDoorMaterial,
 98
 99      init : function () {
100        this.mainImage = _mainImageSrc();
101        this.uniforms = {
102          uDivisions : { type : "1f", value : 5.0 },
103          uDivisionWidth : { type : "1f", value : 0.25 },
104          uAnimateHorizontal : { type : "1f", value : 0.0 },
105          uFlipAnimationDirection : { type : "1f", value : 0.0 },
106          uSpeed : { type : "1f", value : 0.2 }
107        };
108      }
109    };
110
111  })());
112
113})(
114  this.Blotter
115);