(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "three", "../../core/Node.js", "../../inputs/ColorNode.js"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("three"), require("../../core/Node.js"), require("../../inputs/ColorNode.js"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.three, global.Node, global.ColorNode);
    global.SpriteNode = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _Node2, _ColorNode) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.SpriteNode = void 0;

  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

  function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }

  function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }

  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } Object.defineProperty(subClass, "prototype", { value: Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }), writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

  function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

  function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

  function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

  function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

  var SpriteNode = /*#__PURE__*/function (_Node) {
    _inherits(SpriteNode, _Node);

    var _super = _createSuper(SpriteNode);

    function SpriteNode() {
      var _this;

      _classCallCheck(this, SpriteNode);

      _this = _super.call(this);
      _this.color = new _ColorNode.ColorNode(0xEEEEEE);
      _this.spherical = true;
      return _this;
    }

    _createClass(SpriteNode, [{
      key: "build",
      value: function build(builder) {
        var output;
        builder.define('SPRITE');
        builder.requires.lights = false;
        builder.requires.transparent = this.alpha !== undefined;

        if (builder.isShader('vertex')) {
          var position = this.position ? this.position.analyzeAndFlow(builder, 'v3', {
            cache: 'position'
          }) : undefined;
          builder.mergeUniform(_three.UniformsUtils.merge([_three.UniformsLib.fog]));
          builder.addParsCode(
          /* glsl */
          "\n\t\t\t\t#include <fog_pars_vertex>\n\t\t\t\t#include <logdepthbuf_pars_vertex>\n\t\t\t\t#include <clipping_planes_pars_vertex>");
          output = ['#include <clipping_planes_fragment>', '#include <begin_vertex>'];

          if (position) {
            output.push(position.code, position.result ? 'transformed = ' + position.result + ';' : '');
          }

          output.push('#include <project_vertex>', '#include <fog_vertex>', 'mat4 modelViewMtx = modelViewMatrix;', 'mat4 modelMtx = modelMatrix;', // ignore position from modelMatrix (use vary position)
          'modelMtx[3][0] = 0.0;', 'modelMtx[3][1] = 0.0;', 'modelMtx[3][2] = 0.0;');

          if (!this.spherical) {
            output.push('modelMtx[1][1] = 1.0;');
          }

          output.push( // http://www.geeks3d.com/20140807/billboarding-vertex-shader-glsl/
          // First colunm.
          'modelViewMtx[0][0] = 1.0;', 'modelViewMtx[0][1] = 0.0;', 'modelViewMtx[0][2] = 0.0;');

          if (this.spherical) {
            output.push( // Second colunm.
            'modelViewMtx[1][0] = 0.0;', 'modelViewMtx[1][1] = 1.0;', 'modelViewMtx[1][2] = 0.0;');
          }

          output.push( // Thrid colunm.
          'modelViewMtx[2][0] = 0.0;', 'modelViewMtx[2][1] = 0.0;', 'modelViewMtx[2][2] = 1.0;', 'gl_Position = projectionMatrix * modelViewMtx * modelMtx * vec4( transformed, 1.0 );', '#include <logdepthbuf_vertex>', '#include <clipping_planes_vertex>', '#include <fog_vertex>');
        } else {
          builder.addParsCode(
          /* glsl */
          "\n\t\t\t\t#include <fog_pars_fragment>\n\t\t\t\t#include <logdepthbuf_pars_fragment>\n\t\t\t\t#include <clipping_planes_pars_fragment>");
          builder.addCode(
          /* glsl */
          "\n\t\t\t\t#include <clipping_planes_fragment>\n\t\t\t\t#include <logdepthbuf_fragment>"); // analyze all nodes to reuse generate codes

          if (this.mask) this.mask.analyze(builder);
          if (this.alpha) this.alpha.analyze(builder);
          this.color.analyze(builder, {
            slot: 'color'
          }); // build code

          var mask = this.mask ? this.mask.flow(builder, 'b') : undefined,
              alpha = this.alpha ? this.alpha.flow(builder, 'f') : undefined,
              color = this.color.flow(builder, 'c', {
            slot: 'color'
          });
          output = [];

          if (mask) {
            output.push(mask.code, 'if ( ! ' + mask.result + ' ) discard;');
          }

          if (alpha) {
            output.push(alpha.code, '#ifdef ALPHATEST', 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;', '#endif', color.code, 'gl_FragColor = vec4( ' + color.result + ', ' + alpha.result + ' );');
          } else {
            output.push(color.code, 'gl_FragColor = vec4( ' + color.result + ', 1.0 );');
          }

          output.push('#include <tonemapping_fragment>', '#include <encodings_fragment>', '#include <fog_fragment>');
        }

        return output.join('\n');
      }
    }, {
      key: "copy",
      value: function copy(source) {
        _get(_getPrototypeOf(SpriteNode.prototype), "copy", this).call(this, source); // vertex


        if (source.position) this.position = source.position; // fragment

        this.color = source.color;
        if (source.spherical !== undefined) this.spherical = source.spherical;
        if (source.mask) this.mask = source.mask;
        if (source.alpha) this.alpha = source.alpha;
        return this;
      }
    }, {
      key: "toJSON",
      value: function toJSON(meta) {
        var data = this.getJSONNode(meta);

        if (!data) {
          data = this.createJSONNode(meta); // vertex

          if (this.position) data.position = this.position.toJSON(meta).uuid; // fragment

          data.color = this.color.toJSON(meta).uuid;
          if (this.spherical === false) data.spherical = false;
          if (this.mask) data.mask = this.mask.toJSON(meta).uuid;
          if (this.alpha) data.alpha = this.alpha.toJSON(meta).uuid;
        }

        return data;
      }
    }]);

    return SpriteNode;
  }(_Node2.Node);

  _exports.SpriteNode = SpriteNode;
  SpriteNode.prototype.nodeType = 'Sprite';
});