(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three", "./Pass.js", "../shaders/SSRrShader.js", "../shaders/CopyShader.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three"), require("./Pass.js"), require("../shaders/SSRrShader.js"), require("../shaders/CopyShader.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.three, global.Pass, global.SSRrShader, global.CopyShader); global.SSRrPass = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _Pass2, _SSRrShader, _CopyShader) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.SSRrPass = 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 _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 SSRrPass = /*#__PURE__*/function (_Pass) { _inherits(SSRrPass, _Pass); var _super = _createSuper(SSRrPass); function SSRrPass(_ref) { var _this; var renderer = _ref.renderer, scene = _ref.scene, camera = _ref.camera, width = _ref.width, height = _ref.height, selects = _ref.selects; _classCallCheck(this, SSRrPass); _this = _super.call(this); _this.width = width !== undefined ? width : 512; _this.height = height !== undefined ? height : 512; _this.clear = true; _this.renderer = renderer; _this.scene = scene; _this.camera = camera; _this.output = 0; // this.output = 1; _this.ior = _SSRrShader.SSRrShader.uniforms.ior.value; _this.maxDistance = _SSRrShader.SSRrShader.uniforms.maxDistance.value; _this.surfDist = _SSRrShader.SSRrShader.uniforms.surfDist.value; _this.tempColor = new _three.Color(); _this.selects = selects; _this._specular = _SSRrShader.SSRrShader.defines.SPECULAR; Object.defineProperty(_assertThisInitialized(_this), 'specular', { get: function get() { return this._specular; }, set: function set(val) { if (this._specular === val) return; this._specular = val; this.ssrrMaterial.defines.SPECULAR = val; this.ssrrMaterial.needsUpdate = true; } }); _this._fillHole = _SSRrShader.SSRrShader.defines.FILL_HOLE; Object.defineProperty(_assertThisInitialized(_this), 'fillHole', { get: function get() { return this._fillHole; }, set: function set(val) { if (this._fillHole === val) return; this._fillHole = val; this.ssrrMaterial.defines.FILL_HOLE = val; this.ssrrMaterial.needsUpdate = true; } }); _this._infiniteThick = _SSRrShader.SSRrShader.defines.INFINITE_THICK; Object.defineProperty(_assertThisInitialized(_this), 'infiniteThick', { get: function get() { return this._infiniteThick; }, set: function set(val) { if (this._infiniteThick === val) return; this._infiniteThick = val; this.ssrrMaterial.defines.INFINITE_THICK = val; this.ssrrMaterial.needsUpdate = true; } }); // beauty render target with depth buffer var depthTexture = new _three.DepthTexture(); depthTexture.type = _three.UnsignedShortType; depthTexture.minFilter = _three.NearestFilter; depthTexture.magFilter = _three.NearestFilter; _this.beautyRenderTarget = new _three.WebGLRenderTarget(_this.width, _this.height, { minFilter: _three.NearestFilter, magFilter: _three.NearestFilter, format: _three.RGBAFormat, depthTexture: depthTexture, depthBuffer: true }); _this.specularRenderTarget = new _three.WebGLRenderTarget(_this.width, _this.height, { // TODO: Can merge with refractiveRenderTarget? minFilter: _three.NearestFilter, magFilter: _three.NearestFilter, format: _three.RGBAFormat }); // normalSelects render target var depthTextureSelects = new _three.DepthTexture(); depthTextureSelects.type = _three.UnsignedShortType; depthTextureSelects.minFilter = _three.NearestFilter; depthTextureSelects.magFilter = _three.NearestFilter; _this.normalSelectsRenderTarget = new _three.WebGLRenderTarget(_this.width, _this.height, { minFilter: _three.NearestFilter, magFilter: _three.NearestFilter, format: _three.RGBAFormat, type: _three.HalfFloatType, depthTexture: depthTextureSelects, depthBuffer: true }); // refractive render target _this.refractiveRenderTarget = new _three.WebGLRenderTarget(_this.width, _this.height, { minFilter: _three.NearestFilter, magFilter: _three.NearestFilter, format: _three.RGBAFormat }); // ssrr render target _this.ssrrRenderTarget = new _three.WebGLRenderTarget(_this.width, _this.height, { minFilter: _three.NearestFilter, magFilter: _three.NearestFilter, format: _three.RGBAFormat }); // ssrr material if (_SSRrShader.SSRrShader === undefined) { console.error('THREE.SSRrPass: The pass relies on SSRrShader.'); } _this.ssrrMaterial = new _three.ShaderMaterial({ defines: Object.assign({}, _SSRrShader.SSRrShader.defines, { MAX_STEP: Math.sqrt(_this.width * _this.width + _this.height * _this.height) }), uniforms: _three.UniformsUtils.clone(_SSRrShader.SSRrShader.uniforms), vertexShader: _SSRrShader.SSRrShader.vertexShader, fragmentShader: _SSRrShader.SSRrShader.fragmentShader, blending: _three.NoBlending }); _this.ssrrMaterial.uniforms['tDiffuse'].value = _this.beautyRenderTarget.texture; _this.ssrrMaterial.uniforms['tSpecular'].value = _this.specularRenderTarget.texture; _this.ssrrMaterial.uniforms['tNormalSelects'].value = _this.normalSelectsRenderTarget.texture; _this.ssrrMaterial.needsUpdate = true; _this.ssrrMaterial.uniforms['tRefractive'].value = _this.refractiveRenderTarget.texture; _this.ssrrMaterial.uniforms['tDepth'].value = _this.beautyRenderTarget.depthTexture; _this.ssrrMaterial.uniforms['tDepthSelects'].value = _this.normalSelectsRenderTarget.depthTexture; _this.ssrrMaterial.uniforms['cameraNear'].value = _this.camera.near; _this.ssrrMaterial.uniforms['cameraFar'].value = _this.camera.far; _this.ssrrMaterial.uniforms['resolution'].value.set(_this.width, _this.height); _this.ssrrMaterial.uniforms['cameraProjectionMatrix'].value.copy(_this.camera.projectionMatrix); _this.ssrrMaterial.uniforms['cameraInverseProjectionMatrix'].value.copy(_this.camera.projectionMatrixInverse); // normal material _this.normalMaterial = new _three.MeshNormalMaterial(); _this.normalMaterial.blending = _three.NoBlending; // refractiveOn material _this.refractiveOnMaterial = new _three.MeshBasicMaterial({ color: 'white' }); // refractiveOff material _this.refractiveOffMaterial = new _three.MeshBasicMaterial({ color: 'black' }); // specular material _this.specularMaterial = new _three.MeshStandardMaterial({ color: 'black', metalness: 0, roughness: .2 }); // material for rendering the depth _this.depthRenderMaterial = new _three.ShaderMaterial({ defines: Object.assign({}, _SSRrShader.SSRrDepthShader.defines), uniforms: _three.UniformsUtils.clone(_SSRrShader.SSRrDepthShader.uniforms), vertexShader: _SSRrShader.SSRrDepthShader.vertexShader, fragmentShader: _SSRrShader.SSRrDepthShader.fragmentShader, blending: _three.NoBlending }); _this.depthRenderMaterial.uniforms['tDepth'].value = _this.beautyRenderTarget.depthTexture; _this.depthRenderMaterial.uniforms['cameraNear'].value = _this.camera.near; _this.depthRenderMaterial.uniforms['cameraFar'].value = _this.camera.far; // material for rendering the content of a render target _this.copyMaterial = new _three.ShaderMaterial({ uniforms: _three.UniformsUtils.clone(_CopyShader.CopyShader.uniforms), vertexShader: _CopyShader.CopyShader.vertexShader, fragmentShader: _CopyShader.CopyShader.fragmentShader, transparent: true, depthTest: false, depthWrite: false, blendSrc: _three.SrcAlphaFactor, blendDst: _three.OneMinusSrcAlphaFactor, blendEquation: _three.AddEquation, blendSrcAlpha: _three.SrcAlphaFactor, blendDstAlpha: _three.OneMinusSrcAlphaFactor, blendEquationAlpha: _three.AddEquation // premultipliedAlpha:true, }); _this.fsQuad = new _Pass2.FullScreenQuad(null); _this.originalClearColor = new _three.Color(); return _this; } _createClass(SSRrPass, [{ key: "dispose", value: function dispose() { // dispose render targets this.beautyRenderTarget.dispose(); this.specularRenderTarget.dispose(); this.normalSelectsRenderTarget.dispose(); this.refractiveRenderTarget.dispose(); this.ssrrRenderTarget.dispose(); // dispose materials this.normalMaterial.dispose(); this.refractiveOnMaterial.dispose(); this.refractiveOffMaterial.dispose(); this.copyMaterial.dispose(); this.depthRenderMaterial.dispose(); // dipsose full screen quad this.fsQuad.dispose(); } }, { key: "render", value: function render(renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) { var _this2 = this; // render beauty and depth renderer.setRenderTarget(this.beautyRenderTarget); renderer.clear(); this.scene.children.forEach(function (child) { if (_this2.selects.includes(child)) { child.visible = false; } else { child.visible = true; } }); renderer.render(this.scene, this.camera); renderer.setRenderTarget(this.specularRenderTarget); renderer.clear(); this.scene.children.forEach(function (child) { if (_this2.selects.includes(child)) { child.visible = true; child._SSRrPassBackupMaterial = child.material; child.material = _this2.specularMaterial; } else if (!child.isLight) { child.visible = false; } }); renderer.render(this.scene, this.camera); this.scene.children.forEach(function (child) { if (_this2.selects.includes(child)) { child.material = child._SSRrPassBackupMaterial; } }); // render normalSelectss this.scene.children.forEach(function (child) { if (_this2.selects.includes(child)) { child.visible = true; } else { child.visible = false; } }); this.renderOverride(renderer, this.normalMaterial, this.normalSelectsRenderTarget, 0, 0); this.renderRefractive(renderer, this.refractiveOnMaterial, this.refractiveRenderTarget, 0, 0); // render SSRr this.ssrrMaterial.uniforms['ior'].value = this.ior; this.ssrrMaterial.uniforms['maxDistance'].value = this.maxDistance; this.ssrrMaterial.uniforms['surfDist'].value = this.surfDist; this.ssrrMaterial.uniforms['tSpecular'].value = this.specularRenderTarget.texture; this.renderPass(renderer, this.ssrrMaterial, this.ssrrRenderTarget); // output result to screen switch (this.output) { case SSRrPass.OUTPUT.Default: this.copyMaterial.uniforms['tDiffuse'].value = this.beautyRenderTarget.texture; this.copyMaterial.blending = _three.NoBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); this.copyMaterial.uniforms['tDiffuse'].value = this.ssrrRenderTarget.texture; this.copyMaterial.blending = _three.NormalBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; case SSRrPass.OUTPUT.SSRr: this.copyMaterial.uniforms['tDiffuse'].value = this.ssrrRenderTarget.texture; this.copyMaterial.blending = _three.NoBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; case SSRrPass.OUTPUT.Beauty: this.copyMaterial.uniforms['tDiffuse'].value = this.beautyRenderTarget.texture; this.copyMaterial.blending = _three.NoBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; case SSRrPass.OUTPUT.Depth: this.depthRenderMaterial.uniforms['tDepth'].value = this.beautyRenderTarget.depthTexture; this.renderPass(renderer, this.depthRenderMaterial, this.renderToScreen ? null : writeBuffer); break; case SSRrPass.OUTPUT.DepthSelects: this.depthRenderMaterial.uniforms['tDepth'].value = this.normalSelectsRenderTarget.depthTexture; this.renderPass(renderer, this.depthRenderMaterial, this.renderToScreen ? null : writeBuffer); break; case SSRrPass.OUTPUT.NormalSelects: this.copyMaterial.uniforms['tDiffuse'].value = this.normalSelectsRenderTarget.texture; this.copyMaterial.blending = _three.NoBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; case SSRrPass.OUTPUT.Refractive: this.copyMaterial.uniforms['tDiffuse'].value = this.refractiveRenderTarget.texture; this.copyMaterial.blending = _three.NoBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; case SSRrPass.OUTPUT.Specular: this.copyMaterial.uniforms['tDiffuse'].value = this.specularRenderTarget.texture; this.copyMaterial.blending = _three.NoBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; default: console.warn('THREE.SSRrPass: Unknown output type.'); } } }, { key: "renderPass", value: function renderPass(renderer, passMaterial, renderTarget, clearColor, clearAlpha) { // save original state this.originalClearColor.copy(renderer.getClearColor(this.tempColor)); var originalClearAlpha = renderer.getClearAlpha(this.tempColor); var originalAutoClear = renderer.autoClear; renderer.setRenderTarget(renderTarget); // setup pass state renderer.autoClear = false; if (clearColor !== undefined && clearColor !== null) { renderer.setClearColor(clearColor); renderer.setClearAlpha(clearAlpha || 0.0); renderer.clear(); } this.fsQuad.material = passMaterial; this.fsQuad.render(renderer); // restore original state renderer.autoClear = originalAutoClear; renderer.setClearColor(this.originalClearColor); renderer.setClearAlpha(originalClearAlpha); } }, { key: "renderOverride", value: function renderOverride(renderer, overrideMaterial, renderTarget, clearColor, clearAlpha) { this.originalClearColor.copy(renderer.getClearColor(this.tempColor)); var originalClearAlpha = renderer.getClearAlpha(this.tempColor); var originalAutoClear = renderer.autoClear; renderer.setRenderTarget(renderTarget); renderer.autoClear = false; clearColor = overrideMaterial.clearColor || clearColor; clearAlpha = overrideMaterial.clearAlpha || clearAlpha; if (clearColor !== undefined && clearColor !== null) { renderer.setClearColor(clearColor); renderer.setClearAlpha(clearAlpha || 0.0); renderer.clear(); } this.scene.overrideMaterial = overrideMaterial; renderer.render(this.scene, this.camera); this.scene.overrideMaterial = null; // restore original state renderer.autoClear = originalAutoClear; renderer.setClearColor(this.originalClearColor); renderer.setClearAlpha(originalClearAlpha); } }, { key: "renderRefractive", value: function renderRefractive(renderer, overrideMaterial, renderTarget, clearColor, clearAlpha) { var _this3 = this; this.originalClearColor.copy(renderer.getClearColor(this.tempColor)); var originalClearAlpha = renderer.getClearAlpha(this.tempColor); var originalAutoClear = renderer.autoClear; renderer.setRenderTarget(renderTarget); renderer.autoClear = false; clearColor = overrideMaterial.clearColor || clearColor; clearAlpha = overrideMaterial.clearAlpha || clearAlpha; if (clearColor !== undefined && clearColor !== null) { renderer.setClearColor(clearColor); renderer.setClearAlpha(clearAlpha || 0.0); renderer.clear(); } this.scene.children.forEach(function (child) { child.visible = true; }); this.scene.traverse(function (child) { child._SSRrPassBackupMaterial = child.material; if (_this3.selects.includes(child)) { child.material = _this3.refractiveOnMaterial; } else { child.material = _this3.refractiveOffMaterial; } }); this.scene._SSRrPassBackupBackground = this.scene.background; this.scene.background = null; this.scene._SSRrPassBackupFog = this.scene.fog; this.scene.fog = null; renderer.render(this.scene, this.camera); this.scene.fog = this.scene._SSRrPassBackupFog; this.scene.background = this.scene._SSRrPassBackupBackground; this.scene.traverse(function (child) { child.material = child._SSRrPassBackupMaterial; }); // restore original state renderer.autoClear = originalAutoClear; renderer.setClearColor(this.originalClearColor); renderer.setClearAlpha(originalClearAlpha); } }, { key: "setSize", value: function setSize(width, height) { this.width = width; this.height = height; this.ssrrMaterial.defines.MAX_STEP = Math.sqrt(width * width + height * height); this.ssrrMaterial.needsUpdate = true; this.beautyRenderTarget.setSize(width, height); this.specularRenderTarget.setSize(width, height); this.ssrrRenderTarget.setSize(width, height); this.normalSelectsRenderTarget.setSize(width, height); this.refractiveRenderTarget.setSize(width, height); this.ssrrMaterial.uniforms['resolution'].value.set(width, height); this.ssrrMaterial.uniforms['cameraProjectionMatrix'].value.copy(this.camera.projectionMatrix); this.ssrrMaterial.uniforms['cameraInverseProjectionMatrix'].value.copy(this.camera.projectionMatrixInverse); } }]); return SSRrPass; }(_Pass2.Pass); _exports.SSRrPass = SSRrPass; SSRrPass.OUTPUT = { 'Default': 0, 'SSRr': 1, 'Beauty': 3, 'Depth': 4, 'DepthSelects': 9, 'NormalSelects': 5, 'Refractive': 7, 'Specular': 8 }; });