(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three", "../../core/Node.js", "../../inputs/ColorNode.js", "../../inputs/FloatNode.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three"), require("../../core/Node.js"), require("../../inputs/ColorNode.js"), require("../../inputs/FloatNode.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.three, global.Node, global.ColorNode, global.FloatNode); global.PhongNode = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _Node2, _ColorNode, _FloatNode) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.PhongNode = 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 PhongNode = /*#__PURE__*/function (_Node) { _inherits(PhongNode, _Node); var _super = _createSuper(PhongNode); function PhongNode() { var _this; _classCallCheck(this, PhongNode); _this = _super.call(this); _this.color = new _ColorNode.ColorNode(0xEEEEEE); _this.specular = new _ColorNode.ColorNode(0x111111); _this.shininess = new _FloatNode.FloatNode(30); return _this; } _createClass(PhongNode, [{ key: "build", value: function build(builder) { var code; builder.define('PHONG'); builder.requires.lights = 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])); builder.addParsCode( /* glsl */ "\n\t\t\t\tvarying vec3 vViewPosition;\n\n\t\t\t\t//\"#include // encoding functions\n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include "); var output = ['#include ', '#include ', '#include ', '#include ', '#include ', '#include ', '#include ']; if (position) { output.push(position.code, position.result ? 'transformed = ' + position.result + ';' : ''); } output.push(' #include ', ' #include ', ' #include ', ' #include ', ' #include ', ' #include ', ' vViewPosition = - mvPosition.xyz;', ' #include ', ' #include ', ' #include '); code = output.join('\n'); } else { // analyze all nodes to reuse generate codes if (this.mask) this.mask.analyze(builder); this.color.analyze(builder, { slot: 'color' }); this.specular.analyze(builder); this.shininess.analyze(builder); if (this.alpha) this.alpha.analyze(builder); if (this.normal) this.normal.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) this.environment.analyze(builder, { slot: 'environment' }); if (this.environmentAlpha && this.environment) this.environmentAlpha.analyze(builder); // build code var mask = this.mask ? this.mask.flow(builder, 'b') : undefined; var color = this.color.flow(builder, 'c', { slot: 'color' }); var specular = this.specular.flow(builder, 'c'); var shininess = this.shininess.flow(builder, 'f'); var alpha = this.alpha ? this.alpha.flow(builder, 'f') : undefined; var normal = this.normal ? this.normal.flow(builder, 'v3') : 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 = this.environment ? this.environment.flow(builder, 'c', { slot: 'environment' }) : undefined; var environmentAlpha = this.environmentAlpha && this.environment ? this.environmentAlpha.flow(builder, 'f') : undefined; builder.requires.transparent = alpha !== undefined; builder.addParsCode( /* glsl */ "\n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include \n\t\t\t\t#include "); var _output = [// prevent undeclared normal '#include ', // prevent undeclared material ' BlinnPhongMaterial material;']; 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 ', specular.code, ' vec3 specular = ' + specular.result + ';', shininess.code, ' float shininess = max( 0.0001, ' + shininess.result + ' );', ' float specularStrength = 1.0;' // Ignored in MaterialNode ( replace to specular ) ); if (alpha) { _output.push(alpha.code, '#ifdef ALPHATEST', 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;', '#endif'); } if (normal) { _output.push(normal.code, 'normal = ' + normal.result + ';'); } // optimization for now _output.push('material.diffuseColor = ' + (light ? 'vec3( 1.0 )' : 'diffuseColor') + ';'); _output.push( // accumulation 'material.specularColor = specular;', 'material.specularShininess = shininess;', 'material.specularStrength = specularStrength;', '#include ', '#include '); if (light) { _output.push(light.code, 'reflectedLight.directDiffuse = ' + light.result + ';'); // apply color _output.push('reflectedLight.directDiffuse *= diffuseColor;', 'reflectedLight.indirectDiffuse *= diffuseColor;'); } if (ao) { _output.push(ao.code, 'reflectedLight.indirectDiffuse *= ' + ao.result + ';'); } 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 + ';'); } _output.push('vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular;'); if (environment) { _output.push(environment.code); if (environmentAlpha) { _output.push(environmentAlpha.code, 'outgoingLight = mix( outgoingLight, ' + environment.result + ', ' + environmentAlpha.result + ' );'); } else { _output.push('outgoingLight = ' + environment.result + ';'); } } /* switch( builder.material.combine ) { case ENVMAP_BLENDING_MULTIPLY: //output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" ); //outgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity ); break; } */ if (alpha) { _output.push('gl_FragColor = vec4( outgoingLight, ' + alpha.result + ' );'); } else { _output.push('gl_FragColor = vec4( outgoingLight, 1.0 );'); } _output.push('#include ', '#include ', '#include ', '#include '); code = _output.join('\n'); } return code; } }, { key: "copy", value: function copy(source) { _get(_getPrototypeOf(PhongNode.prototype), "copy", this).call(this, source); // vertex if (source.position) this.position = source.position; // fragment this.color = source.color; this.specular = source.specular; this.shininess = source.shininess; if (source.mask) this.mask = source.mask; if (source.alpha) this.alpha = source.alpha; if (source.normal) this.normal = source.normal; 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.environmentAlpha) this.environmentAlpha = source.environmentAlpha; 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.specular = this.specular.toJSON(meta).uuid; data.shininess = this.shininess.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.light) data.light = this.light.toJSON(meta).uuid; if (this.ao) data.ao = this.ao.toJSON(meta).uuid; if (this.ambient) data.ambient = this.ambient.toJSON(meta).uuid; if (this.shadow) data.shadow = this.shadow.toJSON(meta).uuid; if (this.emissive) data.emissive = this.emissive.toJSON(meta).uuid; if (this.environment) data.environment = this.environment.toJSON(meta).uuid; if (this.environmentAlpha) data.environmentAlpha = this.environmentAlpha.toJSON(meta).uuid; } return data; } }]); return PhongNode; }(_Node2.Node); _exports.PhongNode = PhongNode; PhongNode.prototype.nodeType = 'Phong'; });