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

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.StandardNode = 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 StandardNode = /*#__PURE__*/function (_Node) {
    _inherits(StandardNode, _Node);

    var _super = _createSuper(StandardNode);

    function StandardNode() {
      var _this;

      _classCallCheck(this, StandardNode);

      _this = _super.call(this);
      _this.color = new _ColorNode.ColorNode(0xFFFFFF);
      _this.roughness = new _FloatNode.FloatNode(1);
      _this.metalness = new _FloatNode.FloatNode(0);
      return _this;
    }

    _createClass(StandardNode, [{
      key: "build",
      value: function build(builder) {
        var code;
        builder.define('STANDARD');
        var useClearcoat = this.clearcoat || this.clearcoatRoughness || this.clearCoatNormal;

        if (useClearcoat) {
          builder.define('CLEARCOAT');
        }

        builder.requires.lights = true;
        builder.extensions.derivatives = true;
        builder.extensions.shaderTextureLOD = true;

        if (builder.isShader('vertex')) {
          var position = this.position ? this.position.analyzeAndFlow(builder, 'v3', {
            cache: 'position'
          }) : undefined;
          builder.mergeUniform(_three.UniformsUtils.merge([_three.UniformsLib.fog, _three.UniformsLib.lights]));

          if (_three.UniformsLib.LTC_1) {
            // add ltc data textures to material uniforms
            builder.uniforms.ltc_1 = {
              value: undefined
            };
            builder.uniforms.ltc_2 = {
              value: undefined
            };
          }

          builder.addParsCode(
          /* glsl */
          "\n\t\t\t\tvarying vec3 vViewPosition;\n\n\t\t\t\t#ifndef FLAT_SHADED\n\n\t\t\t\t\tvarying vec3 vNormal;\n\n\t\t\t\t#endif\n\n\t\t\t\t//\"#include <encodings_pars_fragment> // encoding functions\n\t\t\t\t#include <fog_pars_vertex>\n\t\t\t\t#include <morphtarget_pars_vertex>\n\t\t\t\t#include <skinning_pars_vertex>\n\t\t\t\t#include <shadowmap_pars_vertex>\n\t\t\t\t#include <logdepthbuf_pars_vertex>\n\t\t\t\t#include <clipping_planes_pars_vertex>");
          var output = ['#include <beginnormal_vertex>', '#include <morphnormal_vertex>', '#include <skinbase_vertex>', '#include <skinnormal_vertex>', '#include <defaultnormal_vertex>', '#ifndef FLAT_SHADED', // Normal computed with derivatives when FLAT_SHADED
          '	vNormal = normalize( transformedNormal );', '#endif', '#include <begin_vertex>'];

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

          output.push('#include <morphtarget_vertex>', '#include <skinning_vertex>', '#include <project_vertex>', '#include <fog_vertex>', '#include <logdepthbuf_vertex>', '#include <clipping_planes_vertex>', '	vViewPosition = - mvPosition.xyz;', '#include <worldpos_vertex>', '#include <shadowmap_vertex>');
          code = output.join('\n');
        } else {
          var roughnessNode = new _ExpressionNode.ExpressionNode('material.roughness', 'f');
          var clearcoatRoughnessNode = new _ExpressionNode.ExpressionNode('material.clearcoatRoughness', 'f');
          var contextEnvironment = {
            roughness: roughnessNode,
            bias: new _SpecularMIPLevelNode.SpecularMIPLevelNode(roughnessNode),
            viewNormal: new _ExpressionNode.ExpressionNode('normal', 'v3'),
            worldNormal: new _ExpressionNode.ExpressionNode('inverseTransformDirection( geometry.normal, viewMatrix )', 'v3'),
            gamma: true
          };
          var contextGammaOnly = {
            gamma: true
          };
          var contextClearcoatEnvironment = {
            roughness: clearcoatRoughnessNode,
            bias: new _SpecularMIPLevelNode.SpecularMIPLevelNode(clearcoatRoughnessNode),
            viewNormal: new _ExpressionNode.ExpressionNode('clearcoatNormal', 'v3'),
            worldNormal: new _ExpressionNode.ExpressionNode('inverseTransformDirection( geometry.clearcoatNormal, viewMatrix )', 'v3'),
            gamma: true
          }; // analyze all nodes to reuse generate codes

          if (this.mask) this.mask.analyze(builder);
          this.color.analyze(builder, {
            slot: 'color',
            context: contextGammaOnly
          });
          this.roughness.analyze(builder);
          this.metalness.analyze(builder);
          if (this.alpha) this.alpha.analyze(builder);
          if (this.normal) this.normal.analyze(builder);
          if (this.clearcoat) this.clearcoat.analyze(builder);
          if (this.clearcoatRoughness) this.clearcoatRoughness.analyze(builder);
          if (this.clearcoatNormal) this.clearcoatNormal.analyze(builder);
          if (this.reflectivity) this.reflectivity.analyze(builder);
          if (this.light) this.light.analyze(builder, {
            cache: 'light'
          });
          if (this.ao) this.ao.analyze(builder);
          if (this.ambient) this.ambient.analyze(builder);
          if (this.shadow) this.shadow.analyze(builder);
          if (this.emissive) this.emissive.analyze(builder, {
            slot: 'emissive'
          });

          if (this.environment) {
            // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
            // environment.analyze will detect if there is a need of calculate irradiance
            this.environment.analyze(builder, {
              cache: 'radiance',
              context: contextEnvironment,
              slot: 'radiance'
            });

            if (builder.requires.irradiance) {
              this.environment.analyze(builder, {
                cache: 'irradiance',
                context: contextEnvironment,
                slot: 'irradiance'
              });
            }
          }

          if (this.sheenColor) this.sheenColor.analyze(builder); // build code

          var mask = this.mask ? this.mask.flow(builder, 'b') : undefined;
          var color = this.color.flow(builder, 'c', {
            slot: 'color',
            context: contextGammaOnly
          });
          var roughness = this.roughness.flow(builder, 'f');
          var metalness = this.metalness.flow(builder, 'f');
          var alpha = this.alpha ? this.alpha.flow(builder, 'f') : undefined;
          var normal = this.normal ? this.normal.flow(builder, 'v3') : undefined;
          var clearcoat = this.clearcoat ? this.clearcoat.flow(builder, 'f') : undefined;
          var clearcoatRoughness = this.clearcoatRoughness ? this.clearcoatRoughness.flow(builder, 'f') : undefined;
          var clearcoatNormal = this.clearcoatNormal ? this.clearcoatNormal.flow(builder, 'v3') : undefined;
          var reflectivity = this.reflectivity ? this.reflectivity.flow(builder, 'f') : undefined;
          var light = this.light ? this.light.flow(builder, 'v3', {
            cache: 'light'
          }) : undefined;
          var ao = this.ao ? this.ao.flow(builder, 'f') : undefined;
          var ambient = this.ambient ? this.ambient.flow(builder, 'c') : undefined;
          var shadow = this.shadow ? this.shadow.flow(builder, 'c') : undefined;
          var emissive = this.emissive ? this.emissive.flow(builder, 'c', {
            slot: 'emissive'
          }) : undefined;
          var environment;

          if (this.environment) {
            environment = {
              radiance: this.environment.flow(builder, 'c', {
                cache: 'radiance',
                context: contextEnvironment,
                slot: 'radiance'
              })
            };

            if (builder.requires.irradiance) {
              environment.irradiance = this.environment.flow(builder, 'c', {
                cache: 'irradiance',
                context: contextEnvironment,
                slot: 'irradiance'
              });
            }
          }

          var clearcoatEnv = useClearcoat && environment ? this.environment.flow(builder, 'c', {
            cache: 'clearcoat',
            context: contextClearcoatEnvironment,
            slot: 'environment'
          }) : undefined;
          var sheenColor = this.sheenColor ? this.sheenColor.flow(builder, 'c') : undefined;
          builder.requires.transparent = alpha !== undefined;
          builder.addParsCode(
          /* glsl */
          "\n\t\t\t\tvarying vec3 vViewPosition;\n\n\t\t\t\t#define NODE_MAXIMUM_SPECULAR_COEFFICIENT 0.16\n\t\t\t\t#define NODE_DEFAULT_SPECULAR_COEFFICIENT 0.04\n\n\t\t\t\t#ifndef FLAT_SHADED\n\n\t\t\t\t\tvarying vec3 vNormal;\n\n\t\t\t\t#endif\n\n\t\t\t\t#include <dithering_pars_fragment>\n\t\t\t\t#include <fog_pars_fragment>\n\t\t\t\t#include <bsdfs>\n\t\t\t\t#include <lights_pars_begin>\n\t\t\t\t#include <lights_physical_pars_fragment>\n\t\t\t\t#include <shadowmap_pars_fragment>\n\t\t\t\t#include <logdepthbuf_pars_fragment>");
          var _output = ['#include <clipping_planes_fragment>', // add before: prevent undeclared normal
          '	#include <normal_fragment_begin>', '	#include <clearcoat_normal_fragment_begin>', // add before: prevent undeclared material
          '	PhysicalMaterial material;', '	material.diffuseColor = vec3( 1.0 );'];

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

          _output.push(color.code, '	vec3 diffuseColor = ' + color.result + ';', '	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );', '#include <logdepthbuf_fragment>', roughness.code, '	float roughnessFactor = ' + roughness.result + ';', metalness.code, '	float metalnessFactor = ' + metalness.result + ';');

          if (alpha) {
            _output.push(alpha.code, '#ifdef ALPHATEST', '	if ( ' + alpha.result + ' <= ALPHATEST ) discard;', '#endif');
          }

          if (normal) {
            _output.push(normal.code, 'normal = ' + normal.result + ';');
          }

          if (clearcoatNormal) {
            _output.push(clearcoatNormal.code, 'clearcoatNormal = ' + clearcoatNormal.result + ';');
          } // anti-aliasing code by @elalish


          _output.push('vec3 dxy = max( abs( dFdx( geometryNormal ) ), abs( dFdy( geometryNormal ) ) );', 'float geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );'); // optimization for now


          _output.push('material.diffuseColor = ' + (light ? 'vec3( 1.0 )' : 'diffuseColor * ( 1.0 - metalnessFactor )') + ';', 'material.roughness = max( roughnessFactor, 0.0525 );', 'material.roughness += geometryRoughness;', 'material.roughness = min( material.roughness, 1.0 );', 'material.roughness = clamp( roughnessFactor, 0.04, 1.0 );');

          if (clearcoat) {
            _output.push(clearcoat.code, 'material.clearcoat = saturate( ' + clearcoat.result + ' );' // Burley clearcoat model
            );
          } else if (useClearcoat) {
            _output.push('material.clearcoat = 0.0;');
          }

          if (clearcoatRoughness) {
            _output.push(clearcoatRoughness.code, 'material.clearcoatRoughness = max( ' + clearcoatRoughness.result + ', 0.0525 );', 'material.clearcoatRoughness += geometryRoughness;', 'material.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 );');
          } else if (useClearcoat) {
            _output.push('material.clearcoatRoughness = 0.0;');
          }

          if (sheenColor) {
            _output.push('material.sheenColor = ' + sheenColor.result + ';');
          }

          if (reflectivity) {
            _output.push(reflectivity.code, 'material.specularColor = mix( vec3( NODE_MAXIMUM_SPECULAR_COEFFICIENT * pow2( ' + reflectivity.result + ' ) ), diffuseColor, metalnessFactor );');
          } else {
            _output.push('material.specularColor = mix( vec3( NODE_DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor, metalnessFactor );');
          }

          _output.push('#include <lights_fragment_begin>');

          if (light) {
            _output.push(light.code, 'reflectedLight.directDiffuse = ' + light.result + ';'); // apply color


            _output.push('diffuseColor *= 1.0 - metalnessFactor;', 'reflectedLight.directDiffuse *= diffuseColor;', 'reflectedLight.indirectDiffuse *= diffuseColor;');
          }

          if (ao) {
            _output.push(ao.code, 'reflectedLight.indirectDiffuse *= ' + ao.result + ';', 'float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );', 'reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ' + ao.result + ', material.roughness );');
          }

          if (ambient) {
            _output.push(ambient.code, 'reflectedLight.indirectDiffuse += ' + ambient.result + ';');
          }

          if (shadow) {
            _output.push(shadow.code, 'reflectedLight.directDiffuse *= ' + shadow.result + ';', 'reflectedLight.directSpecular *= ' + shadow.result + ';');
          }

          if (emissive) {
            _output.push(emissive.code, 'reflectedLight.directDiffuse += ' + emissive.result + ';');
          }

          if (environment) {
            _output.push(environment.radiance.code);

            if (builder.requires.irradiance) {
              _output.push(environment.irradiance.code);
            }

            if (clearcoatEnv) {
              _output.push(clearcoatEnv.code, 'clearcoatRadiance += ' + clearcoatEnv.result + ';');
            }

            _output.push('radiance += ' + environment.radiance.result + ';');

            if (builder.requires.irradiance) {
              _output.push('iblIrradiance += PI * ' + environment.irradiance.result + ';');
            }
          }

          _output.push('#include <lights_fragment_end>');

          _output.push('vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;');

          if (alpha) {
            _output.push('gl_FragColor = vec4( outgoingLight, ' + alpha.result + ' );');
          } else {
            _output.push('gl_FragColor = vec4( outgoingLight, 1.0 );');
          }

          _output.push('#include <tonemapping_fragment>', '#include <encodings_fragment>', '#include <fog_fragment>', '#include <premultiplied_alpha_fragment>', '#include <dithering_fragment>');

          code = _output.join('\n');
        }

        return code;
      }
    }, {
      key: "copy",
      value: function copy(source) {
        _get(_getPrototypeOf(StandardNode.prototype), "copy", this).call(this, source); // vertex


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

        this.color = source.color;
        this.roughness = source.roughness;
        this.metalness = source.metalness;
        if (source.mask) this.mask = source.mask;
        if (source.alpha) this.alpha = source.alpha;
        if (source.normal) this.normal = source.normal;
        if (source.clearcoat) this.clearcoat = source.clearcoat;
        if (source.clearcoatRoughness) this.clearcoatRoughness = source.clearcoatRoughness;
        if (source.clearcoatNormal) this.clearcoatNormal = source.clearcoatNormal;
        if (source.reflectivity) this.reflectivity = source.reflectivity;
        if (source.light) this.light = source.light;
        if (source.shadow) this.shadow = source.shadow;
        if (source.ao) this.ao = source.ao;
        if (source.emissive) this.emissive = source.emissive;
        if (source.ambient) this.ambient = source.ambient;
        if (source.environment) this.environment = source.environment;
        if (source.sheenColor) this.sheenColor = source.sheenColor;
        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;
          data.roughness = this.roughness.toJSON(meta).uuid;
          data.metalness = this.metalness.toJSON(meta).uuid;
          if (this.mask) data.mask = this.mask.toJSON(meta).uuid;
          if (this.alpha) data.alpha = this.alpha.toJSON(meta).uuid;
          if (this.normal) data.normal = this.normal.toJSON(meta).uuid;
          if (this.clearcoat) data.clearcoat = this.clearcoat.toJSON(meta).uuid;
          if (this.clearcoatRoughness) data.clearcoatRoughness = this.clearcoatRoughness.toJSON(meta).uuid;
          if (this.clearcoatNormal) data.clearcoatNormal = this.clearcoatNormal.toJSON(meta).uuid;
          if (this.reflectivity) data.reflectivity = this.reflectivity.toJSON(meta).uuid;
          if (this.light) data.light = this.light.toJSON(meta).uuid;
          if (this.shadow) data.shadow = this.shadow.toJSON(meta).uuid;
          if (this.ao) data.ao = this.ao.toJSON(meta).uuid;
          if (this.emissive) data.emissive = this.emissive.toJSON(meta).uuid;
          if (this.ambient) data.ambient = this.ambient.toJSON(meta).uuid;
          if (this.environment) data.environment = this.environment.toJSON(meta).uuid;
          if (this.sheenColor) data.sheenColor = this.sheenColor.toJSON(meta).uuid;
        }

        return data;
      }
    }]);

    return StandardNode;
  }(_Node2.Node);

  _exports.StandardNode = StandardNode;
  StandardNode.prototype.nodeType = 'Standard';
});