"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.MODE = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _three = require("three");

var _Capabilities = _interopRequireDefault(require("../Core/System/Capabilities"));

/* babel-plugin-inline-import './Shader/PointsVS.glsl' */
var PointsVS = "precision highp float;\nprecision highp int;\n\n#include <logdepthbuf_pars_vertex>\n#define EPSILON 1e-6\n\nattribute vec3 position;\nuniform mat4 projectionMatrix;\nuniform mat4 modelViewMatrix;\nuniform float size;\n\nuniform bool pickingMode;\nuniform int mode;\nuniform float opacity;\nuniform vec4 overlayColor;\nattribute vec3 color;\nattribute vec4 unique_id;\nattribute float intensity;\n\n#if defined(NORMAL_OCT16)\nattribute vec2 oct16Normal;\n#elif defined(NORMAL_SPHEREMAPPED)\nattribute vec2 sphereMappedNormal;\n#else\nattribute vec3 normal;\n#endif\n\nvarying vec4 vColor;\n\n// see https://web.archive.org/web/20150303053317/http://lgdv.cs.fau.de/get/1602\n// and implementation in PotreeConverter (BINPointReader.cpp) and potree (BinaryDecoderWorker.js)\n#if defined(NORMAL_OCT16)\nvec3 decodeOct16Normal(vec2 encodedNormal) {\n    vec2 nNorm = 2. * (encodedNormal / 255.) - 1.;\n    vec3 n;\n    n.z = 1. - abs(nNorm.x) - abs(nNorm.y);\n    if (n.z >= 0.) {\n        n.x = nNorm.x;\n        n.y = nNorm.y;\n    } else {\n        n.x = sign(nNorm.x) - sign(nNorm.x) * sign(nNorm.y) * nNorm.y;\n        n.y = sign(nNorm.y) - sign(nNorm.y) * sign(nNorm.x) * nNorm.x;\n    }\n    return normalize(n);\n}\n#elif defined(NORMAL_SPHEREMAPPED)\n// see http://aras-p.info/texts/CompactNormalStorage.html method #4\n// or see potree's implementation in BINPointReader.cpp\nvec3 decodeSphereMappedNormal(vec2 encodedNormal) {\n    vec2 fenc = 2. * encodedNormal / 255. - 1.;\n    float f = dot(fenc,fenc);\n    float g = 2. * sqrt(1. - f);\n    vec3 n;\n    n.xy = fenc * g;\n    n.z = 1. - 2. * f;\n    return n;\n}\n#endif\n\nvoid main() {\n\n#if defined(NORMAL_OCT16)\n    vec3  normal = decodeOct16Normal(oct16Normal);\n#elif defined(NORMAL_SPHEREMAPPED)\n    vec3 normal = decodeSphereMappedNormal(sphereMappedNormal);\n#elif defined(NORMAL)\n    // nothing to do\n#else\n    // default to color\n    vec3 normal = color;\n#endif\n\n    if (pickingMode) {\n        vColor = unique_id;\n    } else if (mode == MODE_INTENSITY) {\n        vColor = vec4(intensity, intensity, intensity, opacity);\n    } else if (mode == MODE_NORMAL) {\n        vColor = vec4(abs(normal), opacity);\n    } else {\n        // default to color mode\n        vColor = vec4(mix(color, overlayColor.rgb, overlayColor.a), opacity);\n    }\n\n    gl_Position = projectionMatrix * (modelViewMatrix * vec4( position, 1.0 ));\n\n    if (size > 0.) {\n        gl_PointSize = size;\n    } else {\n        gl_PointSize = clamp(-size / gl_Position.w, 3.0, 10.0);\n    }\n\n    #include <logdepthbuf_vertex>\n}\n";

/* babel-plugin-inline-import './Shader/PointsFS.glsl' */
var PointsFS = "precision highp float;\nprecision highp int;\n\n#include <logdepthbuf_pars_fragment>\n\nvarying vec4 vColor;\n\nvoid main() {\n    // circular point rendering\n    if(length(gl_PointCoord - 0.5) > 0.5){\n        discard;\n    }\n\n    gl_FragColor = vColor;\n\n    #include <logdepthbuf_fragment>\n}\n";
var MODE = {
  COLOR: 0,
  INTENSITY: 1,
  CLASSIFICATION: 2,
  NORMAL: 3
};
exports.MODE = MODE;

var PointsMaterial =
/*#__PURE__*/
function (_RawShaderMaterial) {
  (0, _inherits2["default"])(PointsMaterial, _RawShaderMaterial);

  function PointsMaterial() {
    var _this;

    var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    (0, _classCallCheck2["default"])(this, PointsMaterial);
    _this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(PointsMaterial).call(this, options));
    _this.vertexShader = PointsVS;
    _this.fragmentShader = PointsFS;
    _this.size = options.size || 0;
    _this.scale = options.scale || 0.05 * 0.5 / Math.tan(1.0 / 2.0); // autosizing scale

    _this.overlayColor = options.overlayColor || new _three.Vector4(0, 0, 0, 0);
    _this.mode = options.mode || MODE.COLOR;
    _this.picking = false;

    for (var key in MODE) {
      if (Object.prototype.hasOwnProperty.call(MODE, key)) {
        _this.defines["MODE_".concat(key)] = MODE[key];
      }
    }

    _this.uniforms.size = new _three.Uniform(_this.size);
    _this.uniforms.mode = new _three.Uniform(_this.mode);
    _this.uniforms.pickingMode = new _three.Uniform(_this.picking);
    _this.uniforms.opacity = new _three.Uniform(_this.opacity);
    _this.uniforms.overlayColor = new _three.Uniform(_this.overlayColor);

    if (_Capabilities["default"].isLogDepthBufferSupported()) {
      _this.defines.USE_LOGDEPTHBUF = 1;
      _this.defines.USE_LOGDEPTHBUF_EXT = 1;
    }

    _this.updateUniforms();

    return _this;
  }

  (0, _createClass2["default"])(PointsMaterial, [{
    key: "enablePicking",
    value: function enablePicking(picking) {
      this.picking = picking;
      this.blending = picking ? _three.NoBlending : _three.NormalBlending;
      this.updateUniforms();
    }
  }, {
    key: "updateUniforms",
    value: function updateUniforms() {
      // if size is null, switch to autosizing using the canvas height
      this.uniforms.size.value = this.size > 0 ? this.size : -this.scale * window.innerHeight;
      this.uniforms.mode.value = this.mode;
      this.uniforms.pickingMode.value = this.picking;
      this.uniforms.opacity.value = this.opacity;
      this.uniforms.overlayColor.value = this.overlayColor;
    }
  }, {
    key: "update",
    value: function update(source) {
      this.visible = source.visible;
      this.opacity = source.opacity;
      this.transparent = source.transparent;
      this.size = source.size;
      this.mode = source.mode;
      this.picking = source.picking;
      this.scale = source.scale;
      this.overlayColor.copy(source.overlayColor);
      this.updateUniforms();
      Object.assign(this.defines, source.defines);
      return this;
    }
  }]);
  return PointsMaterial;
}(_three.RawShaderMaterial);

var _default = PointsMaterial;
exports["default"] = _default;