(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three", "./Pass.js", "../shaders/SAOShader.js", "../shaders/DepthLimitedBlurShader.js", "../shaders/CopyShader.js", "../shaders/UnpackDepthRGBAShader.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three"), require("./Pass.js"), require("../shaders/SAOShader.js"), require("../shaders/DepthLimitedBlurShader.js"), require("../shaders/CopyShader.js"), require("../shaders/UnpackDepthRGBAShader.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.three, global.Pass, global.SAOShader, global.DepthLimitedBlurShader, global.CopyShader, global.UnpackDepthRGBAShader); global.SAOPass = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _Pass2, _SAOShader, _DepthLimitedBlurShader, _CopyShader, _UnpackDepthRGBAShader) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.SAOPass = 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); } /** * SAO implementation inspired from bhouston previous SAO work */ var SAOPass = /*#__PURE__*/function (_Pass) { _inherits(SAOPass, _Pass); var _super = _createSuper(SAOPass); function SAOPass(scene, camera) { var _this; var useDepthTexture = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var useNormals = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; var resolution = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : new _three.Vector2(256, 256); _classCallCheck(this, SAOPass); _this = _super.call(this); _this.scene = scene; _this.camera = camera; _this.clear = true; _this.needsSwap = false; _this.supportsDepthTextureExtension = useDepthTexture; _this.supportsNormalTexture = useNormals; _this.originalClearColor = new _three.Color(); _this._oldClearColor = new _three.Color(); _this.oldClearAlpha = 1; _this.params = { output: 0, saoBias: 0.5, saoIntensity: 0.18, saoScale: 1, saoKernelRadius: 100, saoMinResolution: 0, saoBlur: true, saoBlurRadius: 8, saoBlurStdDev: 4, saoBlurDepthCutoff: 0.01 }; _this.resolution = new _three.Vector2(resolution.x, resolution.y); _this.saoRenderTarget = new _three.WebGLRenderTarget(_this.resolution.x, _this.resolution.y, { minFilter: _three.LinearFilter, magFilter: _three.LinearFilter, format: _three.RGBAFormat }); _this.blurIntermediateRenderTarget = _this.saoRenderTarget.clone(); _this.beautyRenderTarget = _this.saoRenderTarget.clone(); _this.normalRenderTarget = new _three.WebGLRenderTarget(_this.resolution.x, _this.resolution.y, { minFilter: _three.NearestFilter, magFilter: _three.NearestFilter, format: _three.RGBAFormat }); _this.depthRenderTarget = _this.normalRenderTarget.clone(); var depthTexture; if (_this.supportsDepthTextureExtension) { depthTexture = new _three.DepthTexture(); depthTexture.type = _three.UnsignedShortType; _this.beautyRenderTarget.depthTexture = depthTexture; _this.beautyRenderTarget.depthBuffer = true; } _this.depthMaterial = new _three.MeshDepthMaterial(); _this.depthMaterial.depthPacking = _three.RGBADepthPacking; _this.depthMaterial.blending = _three.NoBlending; _this.normalMaterial = new _three.MeshNormalMaterial(); _this.normalMaterial.blending = _three.NoBlending; if (_SAOShader.SAOShader === undefined) { console.error('THREE.SAOPass relies on SAOShader'); } _this.saoMaterial = new _three.ShaderMaterial({ defines: Object.assign({}, _SAOShader.SAOShader.defines), fragmentShader: _SAOShader.SAOShader.fragmentShader, vertexShader: _SAOShader.SAOShader.vertexShader, uniforms: _three.UniformsUtils.clone(_SAOShader.SAOShader.uniforms) }); _this.saoMaterial.extensions.derivatives = true; _this.saoMaterial.defines['DEPTH_PACKING'] = _this.supportsDepthTextureExtension ? 0 : 1; _this.saoMaterial.defines['NORMAL_TEXTURE'] = _this.supportsNormalTexture ? 1 : 0; _this.saoMaterial.defines['PERSPECTIVE_CAMERA'] = _this.camera.isPerspectiveCamera ? 1 : 0; _this.saoMaterial.uniforms['tDepth'].value = _this.supportsDepthTextureExtension ? depthTexture : _this.depthRenderTarget.texture; _this.saoMaterial.uniforms['tNormal'].value = _this.normalRenderTarget.texture; _this.saoMaterial.uniforms['size'].value.set(_this.resolution.x, _this.resolution.y); _this.saoMaterial.uniforms['cameraInverseProjectionMatrix'].value.copy(_this.camera.projectionMatrixInverse); _this.saoMaterial.uniforms['cameraProjectionMatrix'].value = _this.camera.projectionMatrix; _this.saoMaterial.blending = _three.NoBlending; if (_DepthLimitedBlurShader.DepthLimitedBlurShader === undefined) { console.error('THREE.SAOPass relies on DepthLimitedBlurShader'); } _this.vBlurMaterial = new _three.ShaderMaterial({ uniforms: _three.UniformsUtils.clone(_DepthLimitedBlurShader.DepthLimitedBlurShader.uniforms), defines: Object.assign({}, _DepthLimitedBlurShader.DepthLimitedBlurShader.defines), vertexShader: _DepthLimitedBlurShader.DepthLimitedBlurShader.vertexShader, fragmentShader: _DepthLimitedBlurShader.DepthLimitedBlurShader.fragmentShader }); _this.vBlurMaterial.defines['DEPTH_PACKING'] = _this.supportsDepthTextureExtension ? 0 : 1; _this.vBlurMaterial.defines['PERSPECTIVE_CAMERA'] = _this.camera.isPerspectiveCamera ? 1 : 0; _this.vBlurMaterial.uniforms['tDiffuse'].value = _this.saoRenderTarget.texture; _this.vBlurMaterial.uniforms['tDepth'].value = _this.supportsDepthTextureExtension ? depthTexture : _this.depthRenderTarget.texture; _this.vBlurMaterial.uniforms['size'].value.set(_this.resolution.x, _this.resolution.y); _this.vBlurMaterial.blending = _three.NoBlending; _this.hBlurMaterial = new _three.ShaderMaterial({ uniforms: _three.UniformsUtils.clone(_DepthLimitedBlurShader.DepthLimitedBlurShader.uniforms), defines: Object.assign({}, _DepthLimitedBlurShader.DepthLimitedBlurShader.defines), vertexShader: _DepthLimitedBlurShader.DepthLimitedBlurShader.vertexShader, fragmentShader: _DepthLimitedBlurShader.DepthLimitedBlurShader.fragmentShader }); _this.hBlurMaterial.defines['DEPTH_PACKING'] = _this.supportsDepthTextureExtension ? 0 : 1; _this.hBlurMaterial.defines['PERSPECTIVE_CAMERA'] = _this.camera.isPerspectiveCamera ? 1 : 0; _this.hBlurMaterial.uniforms['tDiffuse'].value = _this.blurIntermediateRenderTarget.texture; _this.hBlurMaterial.uniforms['tDepth'].value = _this.supportsDepthTextureExtension ? depthTexture : _this.depthRenderTarget.texture; _this.hBlurMaterial.uniforms['size'].value.set(_this.resolution.x, _this.resolution.y); _this.hBlurMaterial.blending = _three.NoBlending; if (_CopyShader.CopyShader === undefined) { console.error('THREE.SAOPass relies on CopyShader'); } _this.materialCopy = new _three.ShaderMaterial({ uniforms: _three.UniformsUtils.clone(_CopyShader.CopyShader.uniforms), vertexShader: _CopyShader.CopyShader.vertexShader, fragmentShader: _CopyShader.CopyShader.fragmentShader, blending: _three.NoBlending }); _this.materialCopy.transparent = true; _this.materialCopy.depthTest = false; _this.materialCopy.depthWrite = false; _this.materialCopy.blending = _three.CustomBlending; _this.materialCopy.blendSrc = _three.DstColorFactor; _this.materialCopy.blendDst = _three.ZeroFactor; _this.materialCopy.blendEquation = _three.AddEquation; _this.materialCopy.blendSrcAlpha = _three.DstAlphaFactor; _this.materialCopy.blendDstAlpha = _three.ZeroFactor; _this.materialCopy.blendEquationAlpha = _three.AddEquation; if (_UnpackDepthRGBAShader.UnpackDepthRGBAShader === undefined) { console.error('THREE.SAOPass relies on UnpackDepthRGBAShader'); } _this.depthCopy = new _three.ShaderMaterial({ uniforms: _three.UniformsUtils.clone(_UnpackDepthRGBAShader.UnpackDepthRGBAShader.uniforms), vertexShader: _UnpackDepthRGBAShader.UnpackDepthRGBAShader.vertexShader, fragmentShader: _UnpackDepthRGBAShader.UnpackDepthRGBAShader.fragmentShader, blending: _three.NoBlending }); _this.fsQuad = new _Pass2.FullScreenQuad(null); return _this; } _createClass(SAOPass, [{ key: "render", value: function render(renderer, writeBuffer, readBuffer /*, deltaTime, maskActive*/ ) { // Rendering readBuffer first when rendering to screen if (this.renderToScreen) { this.materialCopy.blending = _three.NoBlending; this.materialCopy.uniforms['tDiffuse'].value = readBuffer.texture; this.materialCopy.needsUpdate = true; this.renderPass(renderer, this.materialCopy, null); } if (this.params.output === 1) { return; } renderer.getClearColor(this._oldClearColor); this.oldClearAlpha = renderer.getClearAlpha(); var oldAutoClear = renderer.autoClear; renderer.autoClear = false; renderer.setRenderTarget(this.depthRenderTarget); renderer.clear(); this.saoMaterial.uniforms['bias'].value = this.params.saoBias; this.saoMaterial.uniforms['intensity'].value = this.params.saoIntensity; this.saoMaterial.uniforms['scale'].value = this.params.saoScale; this.saoMaterial.uniforms['kernelRadius'].value = this.params.saoKernelRadius; this.saoMaterial.uniforms['minResolution'].value = this.params.saoMinResolution; this.saoMaterial.uniforms['cameraNear'].value = this.camera.near; this.saoMaterial.uniforms['cameraFar'].value = this.camera.far; // this.saoMaterial.uniforms['randomSeed'].value = Math.random(); var depthCutoff = this.params.saoBlurDepthCutoff * (this.camera.far - this.camera.near); this.vBlurMaterial.uniforms['depthCutoff'].value = depthCutoff; this.hBlurMaterial.uniforms['depthCutoff'].value = depthCutoff; this.vBlurMaterial.uniforms['cameraNear'].value = this.camera.near; this.vBlurMaterial.uniforms['cameraFar'].value = this.camera.far; this.hBlurMaterial.uniforms['cameraNear'].value = this.camera.near; this.hBlurMaterial.uniforms['cameraFar'].value = this.camera.far; this.params.saoBlurRadius = Math.floor(this.params.saoBlurRadius); if (this.prevStdDev !== this.params.saoBlurStdDev || this.prevNumSamples !== this.params.saoBlurRadius) { _DepthLimitedBlurShader.BlurShaderUtils.configure(this.vBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new _three.Vector2(0, 1)); _DepthLimitedBlurShader.BlurShaderUtils.configure(this.hBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new _three.Vector2(1, 0)); this.prevStdDev = this.params.saoBlurStdDev; this.prevNumSamples = this.params.saoBlurRadius; } // Rendering scene to depth texture renderer.setClearColor(0x000000); renderer.setRenderTarget(this.beautyRenderTarget); renderer.clear(); renderer.render(this.scene, this.camera); // Re-render scene if depth texture extension is not supported if (!this.supportsDepthTextureExtension) { // Clear rule : far clipping plane in both RGBA and Basic encoding this.renderOverride(renderer, this.depthMaterial, this.depthRenderTarget, 0x000000, 1.0); } if (this.supportsNormalTexture) { // Clear rule : default normal is facing the camera this.renderOverride(renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0); } // Rendering SAO texture this.renderPass(renderer, this.saoMaterial, this.saoRenderTarget, 0xffffff, 1.0); // Blurring SAO texture if (this.params.saoBlur) { this.renderPass(renderer, this.vBlurMaterial, this.blurIntermediateRenderTarget, 0xffffff, 1.0); this.renderPass(renderer, this.hBlurMaterial, this.saoRenderTarget, 0xffffff, 1.0); } var outputMaterial = this.materialCopy; // Setting up SAO rendering if (this.params.output === 3) { if (this.supportsDepthTextureExtension) { this.materialCopy.uniforms['tDiffuse'].value = this.beautyRenderTarget.depthTexture; this.materialCopy.needsUpdate = true; } else { this.depthCopy.uniforms['tDiffuse'].value = this.depthRenderTarget.texture; this.depthCopy.needsUpdate = true; outputMaterial = this.depthCopy; } } else if (this.params.output === 4) { this.materialCopy.uniforms['tDiffuse'].value = this.normalRenderTarget.texture; this.materialCopy.needsUpdate = true; } else { this.materialCopy.uniforms['tDiffuse'].value = this.saoRenderTarget.texture; this.materialCopy.needsUpdate = true; } // Blending depends on output, only want a CustomBlending when showing SAO if (this.params.output === 0) { outputMaterial.blending = _three.CustomBlending; } else { outputMaterial.blending = _three.NoBlending; } // Rendering SAOPass result on top of previous pass this.renderPass(renderer, outputMaterial, this.renderToScreen ? null : readBuffer); renderer.setClearColor(this._oldClearColor, this.oldClearAlpha); renderer.autoClear = oldAutoClear; } }, { key: "renderPass", value: function renderPass(renderer, passMaterial, renderTarget, clearColor, clearAlpha) { // save original state renderer.getClearColor(this.originalClearColor); var originalClearAlpha = renderer.getClearAlpha(); 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) { renderer.getClearColor(this.originalClearColor); var originalClearAlpha = renderer.getClearAlpha(); 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: "setSize", value: function setSize(width, height) { this.beautyRenderTarget.setSize(width, height); this.saoRenderTarget.setSize(width, height); this.blurIntermediateRenderTarget.setSize(width, height); this.normalRenderTarget.setSize(width, height); this.depthRenderTarget.setSize(width, height); this.saoMaterial.uniforms['size'].value.set(width, height); this.saoMaterial.uniforms['cameraInverseProjectionMatrix'].value.copy(this.camera.projectionMatrixInverse); this.saoMaterial.uniforms['cameraProjectionMatrix'].value = this.camera.projectionMatrix; this.saoMaterial.needsUpdate = true; this.vBlurMaterial.uniforms['size'].value.set(width, height); this.vBlurMaterial.needsUpdate = true; this.hBlurMaterial.uniforms['size'].value.set(width, height); this.hBlurMaterial.needsUpdate = true; } }]); return SAOPass; }(_Pass2.Pass); _exports.SAOPass = SAOPass; SAOPass.OUTPUT = { 'Beauty': 1, 'Default': 0, 'SAO': 2, 'Depth': 3, 'Normal': 4 }; });