(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "three"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("three"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.three);
    global.PackedPhongMaterial = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three) {
  "use strict";

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

  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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

  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 PackedPhongMaterial = /*#__PURE__*/function (_MeshPhongMaterial) {
    _inherits(PackedPhongMaterial, _MeshPhongMaterial);

    var _super = _createSuper(PackedPhongMaterial);

    function PackedPhongMaterial(parameters) {
      var _this;

      _classCallCheck(this, PackedPhongMaterial);

      _this = _super.call(this);
      _this.defines = {};
      _this.type = 'PackedPhongMaterial';
      _this.uniforms = _three.UniformsUtils.merge([_three.ShaderLib.phong.uniforms, {
        quantizeMatPos: {
          value: null
        },
        quantizeMatUV: {
          value: null
        }
      }]);
      _this.vertexShader = ['#define PHONG', 'varying vec3 vViewPosition;', _three.ShaderChunk.common, _three.ShaderChunk.uv_pars_vertex, _three.ShaderChunk.uv2_pars_vertex, _three.ShaderChunk.displacementmap_pars_vertex, _three.ShaderChunk.envmap_pars_vertex, _three.ShaderChunk.color_pars_vertex, _three.ShaderChunk.fog_pars_vertex, _three.ShaderChunk.normal_pars_vertex, _three.ShaderChunk.morphtarget_pars_vertex, _three.ShaderChunk.skinning_pars_vertex, _three.ShaderChunk.shadowmap_pars_vertex, _three.ShaderChunk.logdepthbuf_pars_vertex, _three.ShaderChunk.clipping_planes_pars_vertex, "#ifdef USE_PACKED_NORMAL\n\t\t\t\t\t#if USE_PACKED_NORMAL == 0\n\t\t\t\t\t\tvec3 decodeNormal(vec3 packedNormal)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfloat x = packedNormal.x * 2.0 - 1.0;\n\t\t\t\t\t\t\tfloat y = packedNormal.y * 2.0 - 1.0;\n\t\t\t\t\t\t\tvec2 scth = vec2(sin(x * PI), cos(x * PI));\n\t\t\t\t\t\t\tvec2 scphi = vec2(sqrt(1.0 - y * y), y);\n\t\t\t\t\t\t\treturn normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );\n\t\t\t\t\t\t}\n\t\t\t\t\t#endif\n\n\t\t\t\t\t#if USE_PACKED_NORMAL == 1\n\t\t\t\t\t\tvec3 decodeNormal(vec3 packedNormal)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvec3 v = vec3(packedNormal.xy, 1.0 - abs(packedNormal.x) - abs(packedNormal.y));\n\t\t\t\t\t\t\tif (v.z < 0.0)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tv.xy = (1.0 - abs(v.yx)) * vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn normalize(v);\n\t\t\t\t\t\t}\n\t\t\t\t\t#endif\n\n\t\t\t\t\t#if USE_PACKED_NORMAL == 2\n\t\t\t\t\t\tvec3 decodeNormal(vec3 packedNormal)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvec3 v = (packedNormal * 2.0) - 1.0;\n\t\t\t\t\t\t\treturn normalize(v);\n\t\t\t\t\t\t}\n\t\t\t\t\t#endif\n\t\t\t\t#endif", "#ifdef USE_PACKED_POSITION\n\t\t\t\t\t#if USE_PACKED_POSITION == 0\n\t\t\t\t\t\tuniform mat4 quantizeMatPos;\n\t\t\t\t\t#endif\n\t\t\t\t#endif", "#ifdef USE_PACKED_UV\n\t\t\t\t\t#if USE_PACKED_UV == 1\n\t\t\t\t\t\tuniform mat3 quantizeMatUV;\n\t\t\t\t\t#endif\n\t\t\t\t#endif", "#ifdef USE_PACKED_UV\n\t\t\t\t\t#if USE_PACKED_UV == 0\n\t\t\t\t\t\tvec2 decodeUV(vec2 packedUV)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvec2 uv = (packedUV * 2.0) - 1.0;\n\t\t\t\t\t\t\treturn uv;\n\t\t\t\t\t\t}\n\t\t\t\t\t#endif\n\n\t\t\t\t\t#if USE_PACKED_UV == 1\n\t\t\t\t\t\tvec2 decodeUV(vec2 packedUV)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvec2 uv = ( vec3(packedUV, 1.0) * quantizeMatUV ).xy;\n\t\t\t\t\t\t\treturn uv;\n\t\t\t\t\t\t}\n\t\t\t\t\t#endif\n\t\t\t\t#endif", 'void main() {', _three.ShaderChunk.uv_vertex, "#ifdef USE_UV\n\t\t\t\t\t#ifdef USE_PACKED_UV\n\t\t\t\t\t\tvUv = decodeUV(vUv);\n\t\t\t\t\t#endif\n\t\t\t\t#endif", _three.ShaderChunk.uv2_vertex, _three.ShaderChunk.color_vertex, _three.ShaderChunk.beginnormal_vertex, "#ifdef USE_PACKED_NORMAL\n\t\t\t\t\tobjectNormal = decodeNormal(objectNormal);\n\t\t\t\t#endif\n\n\t\t\t\t#ifdef USE_TANGENT\n\t\t\t\t\tvec3 objectTangent = vec3( tangent.xyz );\n\t\t\t\t#endif\n\t\t\t\t", _three.ShaderChunk.morphnormal_vertex, _three.ShaderChunk.skinbase_vertex, _three.ShaderChunk.skinnormal_vertex, _three.ShaderChunk.defaultnormal_vertex, _three.ShaderChunk.normal_vertex, _three.ShaderChunk.begin_vertex, "#ifdef USE_PACKED_POSITION\n\t\t\t\t\t#if USE_PACKED_POSITION == 0\n\t\t\t\t\t\ttransformed = ( vec4(transformed, 1.0) * quantizeMatPos ).xyz;\n\t\t\t\t\t#endif\n\t\t\t\t#endif", _three.ShaderChunk.morphtarget_vertex, _three.ShaderChunk.skinning_vertex, _three.ShaderChunk.displacementmap_vertex, _three.ShaderChunk.project_vertex, _three.ShaderChunk.logdepthbuf_vertex, _three.ShaderChunk.clipping_planes_vertex, 'vViewPosition = - mvPosition.xyz;', _three.ShaderChunk.worldpos_vertex, _three.ShaderChunk.envmap_vertex, _three.ShaderChunk.shadowmap_vertex, _three.ShaderChunk.fog_vertex, '}'].join('\n'); // Use the original MeshPhongMaterial's fragmentShader.

      _this.fragmentShader = ['#define PHONG', 'uniform vec3 diffuse;', 'uniform vec3 emissive;', 'uniform vec3 specular;', 'uniform float shininess;', 'uniform float opacity;', _three.ShaderChunk.common, _three.ShaderChunk.packing, _three.ShaderChunk.dithering_pars_fragment, _three.ShaderChunk.color_pars_fragment, _three.ShaderChunk.uv_pars_fragment, _three.ShaderChunk.uv2_pars_fragment, _three.ShaderChunk.map_pars_fragment, _three.ShaderChunk.alphamap_pars_fragment, _three.ShaderChunk.aomap_pars_fragment, _three.ShaderChunk.lightmap_pars_fragment, _three.ShaderChunk.emissivemap_pars_fragment, _three.ShaderChunk.envmap_common_pars_fragment, _three.ShaderChunk.envmap_pars_fragment, _three.ShaderChunk.cube_uv_reflection_fragment, _three.ShaderChunk.fog_pars_fragment, _three.ShaderChunk.bsdfs, _three.ShaderChunk.lights_pars_begin, _three.ShaderChunk.normal_pars_fragment, _three.ShaderChunk.lights_phong_pars_fragment, _three.ShaderChunk.shadowmap_pars_fragment, _three.ShaderChunk.bumpmap_pars_fragment, _three.ShaderChunk.normalmap_pars_fragment, _three.ShaderChunk.specularmap_pars_fragment, _three.ShaderChunk.logdepthbuf_pars_fragment, _three.ShaderChunk.clipping_planes_pars_fragment, 'void main() {', _three.ShaderChunk.clipping_planes_fragment, 'vec4 diffuseColor = vec4( diffuse, opacity );', 'ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );', 'vec3 totalEmissiveRadiance = emissive;', _three.ShaderChunk.logdepthbuf_fragment, _three.ShaderChunk.map_fragment, _three.ShaderChunk.color_fragment, _three.ShaderChunk.alphamap_fragment, _three.ShaderChunk.alphatest_fragment, _three.ShaderChunk.specularmap_fragment, _three.ShaderChunk.normal_fragment_begin, _three.ShaderChunk.normal_fragment_maps, _three.ShaderChunk.emissivemap_fragment, // accumulation
      _three.ShaderChunk.lights_phong_fragment, _three.ShaderChunk.lights_fragment_begin, _three.ShaderChunk.lights_fragment_maps, _three.ShaderChunk.lights_fragment_end, // modulation
      _three.ShaderChunk.aomap_fragment, 'vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;', _three.ShaderChunk.envmap_fragment, 'gl_FragColor = vec4( outgoingLight, diffuseColor.a );', _three.ShaderChunk.tonemapping_fragment, _three.ShaderChunk.encodings_fragment, _three.ShaderChunk.fog_fragment, _three.ShaderChunk.premultiplied_alpha_fragment, _three.ShaderChunk.dithering_fragment, '}'].join('\n');

      _this.setValues(parameters);

      return _this;
    }

    return _createClass(PackedPhongMaterial);
  }(_three.MeshPhongMaterial);

  _exports.PackedPhongMaterial = PackedPhongMaterial;
});