(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three", "./Pass.js", "../math/SimplexNoise.js", "../shaders/SSAOShader.js", "../shaders/CopyShader.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three"), require("./Pass.js"), require("../math/SimplexNoise.js"), require("../shaders/SSAOShader.js"), require("../shaders/CopyShader.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.three, global.Pass, global.SimplexNoise, global.SSAOShader, global.CopyShader); global.SSAOPass = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _Pass2, _SimplexNoise, _SSAOShader, _CopyShader) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.SSAOPass = 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 SSAOPass = /*#__PURE__*/function (_Pass) { _inherits(SSAOPass, _Pass); var _super = _createSuper(SSAOPass); function SSAOPass(scene, camera, width, height) { var _this; _classCallCheck(this, SSAOPass); _this = _super.call(this); _this.width = width !== undefined ? width : 512; _this.height = height !== undefined ? height : 512; _this.clear = true; _this.camera = camera; _this.scene = scene; _this.kernelRadius = 8; _this.kernelSize = 32; _this.kernel = []; _this.noiseTexture = null; _this.output = 0; _this.minDistance = 0.005; _this.maxDistance = 0.1; _this._visibilityCache = new Map(); // _this.generateSampleKernel(); _this.generateRandomKernelRotations(); // beauty render target var depthTexture = new _three.DepthTexture(); depthTexture.type = _three.UnsignedShortType; _this.beautyRenderTarget = new _three.WebGLRenderTarget(_this.width, _this.height, { minFilter: _three.LinearFilter, magFilter: _three.LinearFilter, format: _three.RGBAFormat }); // normal render target with depth buffer _this.normalRenderTarget = new _three.WebGLRenderTarget(_this.width, _this.height, { minFilter: _three.NearestFilter, magFilter: _three.NearestFilter, format: _three.RGBAFormat, depthTexture: depthTexture }); // ssao render target _this.ssaoRenderTarget = new _three.WebGLRenderTarget(_this.width, _this.height, { minFilter: _three.LinearFilter, magFilter: _three.LinearFilter, format: _three.RGBAFormat }); _this.blurRenderTarget = _this.ssaoRenderTarget.clone(); // ssao material if (_SSAOShader.SSAOShader === undefined) { console.error('THREE.SSAOPass: The pass relies on SSAOShader.'); } _this.ssaoMaterial = new _three.ShaderMaterial({ defines: Object.assign({}, _SSAOShader.SSAOShader.defines), uniforms: _three.UniformsUtils.clone(_SSAOShader.SSAOShader.uniforms), vertexShader: _SSAOShader.SSAOShader.vertexShader, fragmentShader: _SSAOShader.SSAOShader.fragmentShader, blending: _three.NoBlending }); _this.ssaoMaterial.uniforms['tDiffuse'].value = _this.beautyRenderTarget.texture; _this.ssaoMaterial.uniforms['tNormal'].value = _this.normalRenderTarget.texture; _this.ssaoMaterial.uniforms['tDepth'].value = _this.normalRenderTarget.depthTexture; _this.ssaoMaterial.uniforms['tNoise'].value = _this.noiseTexture; _this.ssaoMaterial.uniforms['kernel'].value = _this.kernel; _this.ssaoMaterial.uniforms['cameraNear'].value = _this.camera.near; _this.ssaoMaterial.uniforms['cameraFar'].value = _this.camera.far; _this.ssaoMaterial.uniforms['resolution'].value.set(_this.width, _this.height); _this.ssaoMaterial.uniforms['cameraProjectionMatrix'].value.copy(_this.camera.projectionMatrix); _this.ssaoMaterial.uniforms['cameraInverseProjectionMatrix'].value.copy(_this.camera.projectionMatrixInverse); // normal material _this.normalMaterial = new _three.MeshNormalMaterial(); _this.normalMaterial.blending = _three.NoBlending; // blur material _this.blurMaterial = new _three.ShaderMaterial({ defines: Object.assign({}, _SSAOShader.SSAOBlurShader.defines), uniforms: _three.UniformsUtils.clone(_SSAOShader.SSAOBlurShader.uniforms), vertexShader: _SSAOShader.SSAOBlurShader.vertexShader, fragmentShader: _SSAOShader.SSAOBlurShader.fragmentShader }); _this.blurMaterial.uniforms['tDiffuse'].value = _this.ssaoRenderTarget.texture; _this.blurMaterial.uniforms['resolution'].value.set(_this.width, _this.height); // material for rendering the depth _this.depthRenderMaterial = new _three.ShaderMaterial({ defines: Object.assign({}, _SSAOShader.SSAODepthShader.defines), uniforms: _three.UniformsUtils.clone(_SSAOShader.SSAODepthShader.uniforms), vertexShader: _SSAOShader.SSAODepthShader.vertexShader, fragmentShader: _SSAOShader.SSAODepthShader.fragmentShader, blending: _three.NoBlending }); _this.depthRenderMaterial.uniforms['tDepth'].value = _this.normalRenderTarget.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.DstColorFactor, blendDst: _three.ZeroFactor, blendEquation: _three.AddEquation, blendSrcAlpha: _three.DstAlphaFactor, blendDstAlpha: _three.ZeroFactor, blendEquationAlpha: _three.AddEquation }); _this.fsQuad = new _Pass2.FullScreenQuad(null); _this.originalClearColor = new _three.Color(); return _this; } _createClass(SSAOPass, [{ key: "dispose", value: function dispose() { // dispose render targets this.beautyRenderTarget.dispose(); this.normalRenderTarget.dispose(); this.ssaoRenderTarget.dispose(); this.blurRenderTarget.dispose(); // dispose materials this.normalMaterial.dispose(); this.blurMaterial.dispose(); this.copyMaterial.dispose(); this.depthRenderMaterial.dispose(); // dipsose full screen quad this.fsQuad.dispose(); } }, { key: "render", value: function render(renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) { // render beauty renderer.setRenderTarget(this.beautyRenderTarget); renderer.clear(); renderer.render(this.scene, this.camera); // render normals and depth (honor only meshes, points and lines do not contribute to SSAO) this.overrideVisibility(); this.renderOverride(renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0); this.restoreVisibility(); // render SSAO this.ssaoMaterial.uniforms['kernelRadius'].value = this.kernelRadius; this.ssaoMaterial.uniforms['minDistance'].value = this.minDistance; this.ssaoMaterial.uniforms['maxDistance'].value = this.maxDistance; this.renderPass(renderer, this.ssaoMaterial, this.ssaoRenderTarget); // render blur this.renderPass(renderer, this.blurMaterial, this.blurRenderTarget); // output result to screen switch (this.output) { case SSAOPass.OUTPUT.SSAO: this.copyMaterial.uniforms['tDiffuse'].value = this.ssaoRenderTarget.texture; this.copyMaterial.blending = _three.NoBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; case SSAOPass.OUTPUT.Blur: this.copyMaterial.uniforms['tDiffuse'].value = this.blurRenderTarget.texture; this.copyMaterial.blending = _three.NoBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; case SSAOPass.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 SSAOPass.OUTPUT.Depth: this.renderPass(renderer, this.depthRenderMaterial, this.renderToScreen ? null : writeBuffer); break; case SSAOPass.OUTPUT.Normal: this.copyMaterial.uniforms['tDiffuse'].value = this.normalRenderTarget.texture; this.copyMaterial.blending = _three.NoBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; case SSAOPass.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.blurRenderTarget.texture; this.copyMaterial.blending = _three.CustomBlending; this.renderPass(renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer); break; default: console.warn('THREE.SSAOPass: Unknown output type.'); } } }, { 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.width = width; this.height = height; this.beautyRenderTarget.setSize(width, height); this.ssaoRenderTarget.setSize(width, height); this.normalRenderTarget.setSize(width, height); this.blurRenderTarget.setSize(width, height); this.ssaoMaterial.uniforms['resolution'].value.set(width, height); this.ssaoMaterial.uniforms['cameraProjectionMatrix'].value.copy(this.camera.projectionMatrix); this.ssaoMaterial.uniforms['cameraInverseProjectionMatrix'].value.copy(this.camera.projectionMatrixInverse); this.blurMaterial.uniforms['resolution'].value.set(width, height); } }, { key: "generateSampleKernel", value: function generateSampleKernel() { var kernelSize = this.kernelSize; var kernel = this.kernel; for (var i = 0; i < kernelSize; i++) { var sample = new _three.Vector3(); sample.x = Math.random() * 2 - 1; sample.y = Math.random() * 2 - 1; sample.z = Math.random(); sample.normalize(); var scale = i / kernelSize; scale = _three.MathUtils.lerp(0.1, 1, scale * scale); sample.multiplyScalar(scale); kernel.push(sample); } } }, { key: "generateRandomKernelRotations", value: function generateRandomKernelRotations() { var width = 4, height = 4; if (_SimplexNoise.SimplexNoise === undefined) { console.error('THREE.SSAOPass: The pass relies on SimplexNoise.'); } var simplex = new _SimplexNoise.SimplexNoise(); var size = width * height; var data = new Float32Array(size * 4); for (var i = 0; i < size; i++) { var stride = i * 4; var x = Math.random() * 2 - 1; var y = Math.random() * 2 - 1; var z = 0; var noise = simplex.noise3d(x, y, z); data[stride] = noise; data[stride + 1] = noise; data[stride + 2] = noise; data[stride + 3] = 1; } this.noiseTexture = new _three.DataTexture(data, width, height, _three.RGBAFormat, _three.FloatType); this.noiseTexture.wrapS = _three.RepeatWrapping; this.noiseTexture.wrapT = _three.RepeatWrapping; } }, { key: "overrideVisibility", value: function overrideVisibility() { var scene = this.scene; var cache = this._visibilityCache; scene.traverse(function (object) { cache.set(object, object.visible); if (object.isPoints || object.isLine) object.visible = false; }); } }, { key: "restoreVisibility", value: function restoreVisibility() { var scene = this.scene; var cache = this._visibilityCache; scene.traverse(function (object) { var visible = cache.get(object); object.visible = visible; }); cache.clear(); } }]); return SSAOPass; }(_Pass2.Pass); _exports.SSAOPass = SSAOPass; SSAOPass.OUTPUT = { 'Default': 0, 'SSAO': 1, 'Blur': 2, 'Beauty': 3, 'Depth': 4, 'Normal': 5 }; });