text: import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  import _createClass from "@babel/runtime/helpers/esm/createClass";
  
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  
  import { Buffer, Transform, Framebuffer, Texture2D, readPixelsToArray } from '@luma.gl/core';
  import { padBuffer, getAttributeTypeFromSize, getSourceBufferAttribute, getAttributeBufferLength, cycleBuffers } from '../lib/attribute-transition-utils';
  import Attribute from '../lib/attribute';
  import Transition from './transition';
  
  var GPUSpringTransition = function () {
    function GPUSpringTransition(_ref) {
      var gl = _ref.gl,
          attribute = _ref.attribute,
          timeline = _ref.timeline;
  
      _classCallCheck(this, GPUSpringTransition);
  
      this.gl = gl;
      this.type = 'spring';
      this.transition = new Transition(timeline);
      this.attribute = attribute;
      this.attributeInTransition = new Attribute(gl, attribute.userData);
      this.currentBufferLayout = attribute.bufferLayout;
      this.currentLength = 0;
      this.texture = getTexture(gl);
      this.framebuffer = getFramebuffer(gl, this.texture);
      this.transform = getTransform(gl, attribute, this.framebuffer);
      var bufferOpts = {
        byteLength: 0,
        usage: 35050
      };
      this.buffers = [new Buffer(gl, bufferOpts), new Buffer(gl, bufferOpts), new Buffer(gl, bufferOpts)];
    }
  
    _createClass(GPUSpringTransition, [{
      key: "start",
      value: function start(transitionSettings, numInstances) {
        var gl = this.gl,
            buffers = this.buffers,
            attribute = this.attribute;
        var padBufferOpts = {
          numInstances: numInstances,
          attribute: attribute,
          fromLength: this.currentLength,
          fromBufferLayout: this.currentBufferLayout,
          getData: transitionSettings.enter
        };
        var _iteratorNormalCompletion = true;
        var _didIteratorError = false;
        var _iteratorError = undefined;
  
        try {
          for (var _iterator = buffers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            var buffer = _step.value;
            padBuffer(_objectSpread({
              buffer: buffer
            }, padBufferOpts));
          }
        } catch (err) {
          _didIteratorError = true;
          _iteratorError = err;
        } finally {
          try {
            if (!_iteratorNormalCompletion && _iterator["return"] != null) {
              _iterator["return"]();
            }
          } finally {
            if (_didIteratorError) {
              throw _iteratorError;
            }
          }
        }
  
        this.currentBufferLayout = attribute.bufferLayout;
        this.currentLength = getAttributeBufferLength(attribute, numInstances);
        this.attributeInTransition.update({
          buffer: buffers[1],
          value: attribute.value
        });
        this.transition.start(transitionSettings);
        this.transform.update({
          elementCount: Math.floor(this.currentLength / attribute.size),
          sourceBuffers: {
            aTo: getSourceBufferAttribute(gl, attribute)
          }
        });
      }
    }, {
      key: "update",
      value: function update() {
        var buffers = this.buffers,
            transform = this.transform,
            framebuffer = this.framebuffer,
            transition = this.transition;
        var updated = transition.update();
  
        if (!updated) {
          return false;
        }
  
        transform.update({
          sourceBuffers: {
            aPrev: buffers[0],
            aCur: buffers[1]
          },
          feedbackBuffers: {
            vNext: buffers[2]
          }
        });
        transform.run({
          framebuffer: framebuffer,
          discard: false,
          clearRenderTarget: true,
          uniforms: {
            stiffness: transition.settings.stiffness,
            damping: transition.settings.damping
          },
          parameters: {
            depthTest: false,
            blend: true,
            viewport: [0, 0, 1, 1],
            blendFunc: [1, 1],
            blendEquation: [32776, 32776]
          }
        });
        cycleBuffers(buffers);
        this.attributeInTransition.update({
          buffer: buffers[1]
        });
        var isTransitioning = readPixelsToArray(framebuffer)[0] > 0;
  
        if (!isTransitioning) {
          transition.end();
        }
  
        return true;
      }
    }, {
      key: "cancel",
      value: function cancel() {
        this.transition.cancel();
        this.transform["delete"]();
  
        while (this.buffers.length) {
          this.buffers.pop()["delete"]();
        }
  
        this.texture["delete"]();
        this.texture = null;
        this.framebuffer["delete"]();
        this.framebuffer = null;
      }
    }, {
      key: "inProgress",
      get: function get() {
        return this.transition.inProgress;
      }
    }]);
  
    return GPUSpringTransition;
  }();
  
  export { GPUSpringTransition as default };
  
  function getTransform(gl, attribute, framebuffer) {
    var attributeType = getAttributeTypeFromSize(attribute.size);
    return new Transform(gl, {
      framebuffer: framebuffer,
      vs: "\n#define SHADER_NAME spring-transition-vertex-shader\n\n#define EPSILON 0.00001\n\nuniform float stiffness;\nuniform float damping;\nattribute ATTRIBUTE_TYPE aPrev;\nattribute ATTRIBUTE_TYPE aCur;\nattribute ATTRIBUTE_TYPE aTo;\nvarying ATTRIBUTE_TYPE vNext;\nvarying float vIsTransitioningFlag;\n\nATTRIBUTE_TYPE getNextValue(ATTRIBUTE_TYPE cur, ATTRIBUTE_TYPE prev, ATTRIBUTE_TYPE dest) {\n  ATTRIBUTE_TYPE velocity = cur - prev;\n  ATTRIBUTE_TYPE delta = dest - cur;\n  ATTRIBUTE_TYPE spring = delta * stiffness;\n  ATTRIBUTE_TYPE damper = velocity * -1.0 * damping;\n  return spring + damper + velocity + cur;\n}\n\nvoid main(void) {\n  bool isTransitioning = length(aCur - aPrev) > EPSILON || length(aTo - aCur) > EPSILON;\n  vIsTransitioningFlag = isTransitioning ? 1.0 : 0.0;\n\n  vNext = getNextValue(aCur, aPrev, aTo);\n  gl_Position = vec4(0, 0, 0, 1);\n  gl_PointSize = 100.0;\n}\n",
      fs: "\n#define SHADER_NAME spring-transition-is-transitioning-fragment-shader\n\nvarying float vIsTransitioningFlag;\n\nvoid main(void) {\n  if (vIsTransitioningFlag == 0.0) {\n    discard;\n  }\n  gl_FragColor = vec4(1.0);\n}",
      defines: {
        ATTRIBUTE_TYPE: attributeType
      },
      varyings: ['vNext']
    });
  }
  
  function getTexture(gl) {
    return new Texture2D(gl, {
      data: new Uint8Array(4),
      format: 6408,
      type: 5121,
      border: 0,
      mipmaps: false,
      dataFormat: 6408,
      width: 1,
      height: 1
    });
  }
  
  function getFramebuffer(gl, texture) {
    return new Framebuffer(gl, {
      id: 'spring-transition-is-transitioning-framebuffer',
      width: 1,
      height: 1,
      attachments: _defineProperty({}, 36064, texture)
    });
  }
  //# sourceMappingURL=gpu-spring-transition.js.map