(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three", "./Pass.js", "../shaders/CopyShader.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three"), require("./Pass.js"), require("../shaders/CopyShader.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.three, global.Pass, global.CopyShader); global.SSAARenderPass = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _Pass2, _CopyShader) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.SSAARenderPass = 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); } /** * * Supersample Anti-Aliasing Render Pass * * This manual approach to SSAA re-renders the scene ones for each sample with camera jitter and accumulates the results. * * References: https://en.wikipedia.org/wiki/Supersampling * */ var SSAARenderPass = /*#__PURE__*/function (_Pass) { _inherits(SSAARenderPass, _Pass); var _super = _createSuper(SSAARenderPass); function SSAARenderPass(scene, camera, clearColor, clearAlpha) { var _this; _classCallCheck(this, SSAARenderPass); _this = _super.call(this); _this.scene = scene; _this.camera = camera; _this.sampleLevel = 4; // specified as n, where the number of samples is 2^n, so sampleLevel = 4, is 2^4 samples, 16. _this.unbiased = true; // as we need to clear the buffer in this pass, clearColor must be set to something, defaults to black. _this.clearColor = clearColor !== undefined ? clearColor : 0x000000; _this.clearAlpha = clearAlpha !== undefined ? clearAlpha : 0; _this._oldClearColor = new _three.Color(); if (_CopyShader.CopyShader === undefined) console.error('THREE.SSAARenderPass relies on CopyShader'); var copyShader = _CopyShader.CopyShader; _this.copyUniforms = _three.UniformsUtils.clone(copyShader.uniforms); _this.copyMaterial = new _three.ShaderMaterial({ uniforms: _this.copyUniforms, vertexShader: copyShader.vertexShader, fragmentShader: copyShader.fragmentShader, premultipliedAlpha: true, transparent: true, blending: _three.AdditiveBlending, depthTest: false, depthWrite: false }); _this.fsQuad = new _Pass2.FullScreenQuad(_this.copyMaterial); return _this; } _createClass(SSAARenderPass, [{ key: "dispose", value: function dispose() { if (this.sampleRenderTarget) { this.sampleRenderTarget.dispose(); this.sampleRenderTarget = null; } } }, { key: "setSize", value: function setSize(width, height) { if (this.sampleRenderTarget) this.sampleRenderTarget.setSize(width, height); } }, { key: "render", value: function render(renderer, writeBuffer, readBuffer) { if (!this.sampleRenderTarget) { this.sampleRenderTarget = new _three.WebGLRenderTarget(readBuffer.width, readBuffer.height, { minFilter: _three.LinearFilter, magFilter: _three.LinearFilter, format: _three.RGBAFormat }); this.sampleRenderTarget.texture.name = 'SSAARenderPass.sample'; } var jitterOffsets = _JitterVectors[Math.max(0, Math.min(this.sampleLevel, 5))]; var autoClear = renderer.autoClear; renderer.autoClear = false; renderer.getClearColor(this._oldClearColor); var oldClearAlpha = renderer.getClearAlpha(); var baseSampleWeight = 1.0 / jitterOffsets.length; var roundingRange = 1 / 32; this.copyUniforms['tDiffuse'].value = this.sampleRenderTarget.texture; var viewOffset = { fullWidth: readBuffer.width, fullHeight: readBuffer.height, offsetX: 0, offsetY: 0, width: readBuffer.width, height: readBuffer.height }; var originalViewOffset = Object.assign({}, this.camera.view); if (originalViewOffset.enabled) Object.assign(viewOffset, originalViewOffset); // render the scene multiple times, each slightly jitter offset from the last and accumulate the results. for (var i = 0; i < jitterOffsets.length; i++) { var jitterOffset = jitterOffsets[i]; if (this.camera.setViewOffset) { this.camera.setViewOffset(viewOffset.fullWidth, viewOffset.fullHeight, viewOffset.offsetX + jitterOffset[0] * 0.0625, viewOffset.offsetY + jitterOffset[1] * 0.0625, // 0.0625 = 1 / 16 viewOffset.width, viewOffset.height); } var sampleWeight = baseSampleWeight; if (this.unbiased) { // the theory is that equal weights for each sample lead to an accumulation of rounding errors. // The following equation varies the sampleWeight per sample so that it is uniformly distributed // across a range of values whose rounding errors cancel each other out. var uniformCenteredDistribution = -0.5 + (i + 0.5) / jitterOffsets.length; sampleWeight += roundingRange * uniformCenteredDistribution; } this.copyUniforms['opacity'].value = sampleWeight; renderer.setClearColor(this.clearColor, this.clearAlpha); renderer.setRenderTarget(this.sampleRenderTarget); renderer.clear(); renderer.render(this.scene, this.camera); renderer.setRenderTarget(this.renderToScreen ? null : writeBuffer); if (i === 0) { renderer.setClearColor(0x000000, 0.0); renderer.clear(); } this.fsQuad.render(renderer); } if (this.camera.setViewOffset && originalViewOffset.enabled) { this.camera.setViewOffset(originalViewOffset.fullWidth, originalViewOffset.fullHeight, originalViewOffset.offsetX, originalViewOffset.offsetY, originalViewOffset.width, originalViewOffset.height); } else if (this.camera.clearViewOffset) { this.camera.clearViewOffset(); } renderer.autoClear = autoClear; renderer.setClearColor(this._oldClearColor, oldClearAlpha); } }]); return SSAARenderPass; }(_Pass2.Pass); // These jitter vectors are specified in integers because it is easier. // I am assuming a [-8,8) integer grid, but it needs to be mapped onto [-0.5,0.5) // before being used, thus these integers need to be scaled by 1/16. // // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 _exports.SSAARenderPass = SSAARenderPass; var _JitterVectors = [[[0, 0]], [[4, 4], [-4, -4]], [[-2, -6], [6, -2], [-6, 2], [2, 6]], [[1, -3], [-1, 3], [5, 1], [-3, -5], [-5, 5], [-7, -1], [3, 7], [7, -7]], [[1, 1], [-1, -3], [-3, 2], [4, -1], [-5, -2], [2, 5], [5, 3], [3, -5], [-2, 6], [0, -7], [-4, -6], [-6, 4], [-8, 0], [7, -4], [6, 7], [-7, -8]], [[-4, -7], [-7, -5], [-3, -5], [-5, -4], [-1, -4], [-2, -2], [-6, -1], [-4, 0], [-7, 1], [-1, 2], [-6, 3], [-3, 3], [-7, 6], [-3, 6], [-5, 7], [-1, 7], [5, -7], [1, -6], [6, -5], [4, -4], [2, -3], [7, -2], [1, -1], [4, -1], [2, 1], [6, 2], [0, 4], [4, 4], [2, 5], [7, 5], [5, 6], [3, 7]]]; });