(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three", "../shaders/BokehShader2.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three"), require("../shaders/BokehShader2.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.three, global.BokehShader2); global.CinematicCamera = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _BokehShader) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.CinematicCamera = 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 CinematicCamera = /*#__PURE__*/function (_PerspectiveCamera) { _inherits(CinematicCamera, _PerspectiveCamera); var _super = _createSuper(CinematicCamera); function CinematicCamera(fov, aspect, near, far) { var _this; _classCallCheck(this, CinematicCamera); _this = _super.call(this, fov, aspect, near, far); _this.type = 'CinematicCamera'; _this.postprocessing = { enabled: true }; _this.shaderSettings = { rings: 3, samples: 4 }; var depthShader = _BokehShader.BokehDepthShader; _this.materialDepth = new _three.ShaderMaterial({ uniforms: depthShader.uniforms, vertexShader: depthShader.vertexShader, fragmentShader: depthShader.fragmentShader }); _this.materialDepth.uniforms['mNear'].value = near; _this.materialDepth.uniforms['mFar'].value = far; // In case of cinematicCamera, having a default lens set is important _this.setLens(); _this.initPostProcessing(); return _this; } // providing fnumber and coc(Circle of Confusion) as extra arguments // In case of cinematicCamera, having a default lens set is important // if fnumber and coc are not provided, cinematicCamera tries to act as a basic PerspectiveCamera _createClass(CinematicCamera, [{ key: "setLens", value: function setLens() { var focalLength = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 35; var filmGauge = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 35; var fNumber = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 8; var coc = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0.019; this.filmGauge = filmGauge; this.setFocalLength(focalLength); this.fNumber = fNumber; this.coc = coc; // fNumber is focalLength by aperture this.aperture = focalLength / this.fNumber; // hyperFocal is required to calculate depthOfField when a lens tries to focus at a distance with given fNumber and focalLength this.hyperFocal = focalLength * focalLength / (this.aperture * this.coc); } }, { key: "linearize", value: function linearize(depth) { var zfar = this.far; var znear = this.near; return -zfar * znear / (depth * (zfar - znear) - zfar); } }, { key: "smoothstep", value: function smoothstep(near, far, depth) { var x = this.saturate((depth - near) / (far - near)); return x * x * (3 - 2 * x); } }, { key: "saturate", value: function saturate(x) { return Math.max(0, Math.min(1, x)); } // function for focusing at a distance from the camera }, { key: "focusAt", value: function focusAt() { var focusDistance = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 20; var focalLength = this.getFocalLength(); // distance from the camera (normal to frustrum) to focus on this.focus = focusDistance; // the nearest point from the camera which is in focus (unused) this.nearPoint = this.hyperFocal * this.focus / (this.hyperFocal + (this.focus - focalLength)); // the farthest point from the camera which is in focus (unused) this.farPoint = this.hyperFocal * this.focus / (this.hyperFocal - (this.focus - focalLength)); // the gap or width of the space in which is everything is in focus (unused) this.depthOfField = this.farPoint - this.nearPoint; // Considering minimum distance of focus for a standard lens (unused) if (this.depthOfField < 0) this.depthOfField = 0; this.sdistance = this.smoothstep(this.near, this.far, this.focus); this.ldistance = this.linearize(1 - this.sdistance); this.postprocessing.bokeh_uniforms['focalDepth'].value = this.ldistance; } }, { key: "initPostProcessing", value: function initPostProcessing() { if (this.postprocessing.enabled) { this.postprocessing.scene = new _three.Scene(); this.postprocessing.camera = new _three.OrthographicCamera(window.innerWidth / -2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / -2, -10000, 10000); this.postprocessing.scene.add(this.postprocessing.camera); var pars = { minFilter: _three.LinearFilter, magFilter: _three.LinearFilter, format: _three.RGBFormat }; this.postprocessing.rtTextureDepth = new _three.WebGLRenderTarget(window.innerWidth, window.innerHeight, pars); this.postprocessing.rtTextureColor = new _three.WebGLRenderTarget(window.innerWidth, window.innerHeight, pars); var bokeh_shader = _BokehShader.BokehShader; this.postprocessing.bokeh_uniforms = _three.UniformsUtils.clone(bokeh_shader.uniforms); this.postprocessing.bokeh_uniforms['tColor'].value = this.postprocessing.rtTextureColor.texture; this.postprocessing.bokeh_uniforms['tDepth'].value = this.postprocessing.rtTextureDepth.texture; this.postprocessing.bokeh_uniforms['manualdof'].value = 0; this.postprocessing.bokeh_uniforms['shaderFocus'].value = 0; this.postprocessing.bokeh_uniforms['fstop'].value = 2.8; this.postprocessing.bokeh_uniforms['showFocus'].value = 1; this.postprocessing.bokeh_uniforms['focalDepth'].value = 0.1; //console.log( this.postprocessing.bokeh_uniforms[ "focalDepth" ].value ); this.postprocessing.bokeh_uniforms['znear'].value = this.near; this.postprocessing.bokeh_uniforms['zfar'].value = this.near; this.postprocessing.bokeh_uniforms['textureWidth'].value = window.innerWidth; this.postprocessing.bokeh_uniforms['textureHeight'].value = window.innerHeight; this.postprocessing.materialBokeh = new _three.ShaderMaterial({ uniforms: this.postprocessing.bokeh_uniforms, vertexShader: bokeh_shader.vertexShader, fragmentShader: bokeh_shader.fragmentShader, defines: { RINGS: this.shaderSettings.rings, SAMPLES: this.shaderSettings.samples, DEPTH_PACKING: 1 } }); this.postprocessing.quad = new _three.Mesh(new _three.PlaneGeometry(window.innerWidth, window.innerHeight), this.postprocessing.materialBokeh); this.postprocessing.quad.position.z = -500; this.postprocessing.scene.add(this.postprocessing.quad); } } }, { key: "renderCinematic", value: function renderCinematic(scene, renderer) { if (this.postprocessing.enabled) { var currentRenderTarget = renderer.getRenderTarget(); renderer.clear(); // Render scene into texture scene.overrideMaterial = null; renderer.setRenderTarget(this.postprocessing.rtTextureColor); renderer.clear(); renderer.render(scene, this); // Render depth into texture scene.overrideMaterial = this.materialDepth; renderer.setRenderTarget(this.postprocessing.rtTextureDepth); renderer.clear(); renderer.render(scene, this); // Render bokeh composite renderer.setRenderTarget(null); renderer.render(this.postprocessing.scene, this.postprocessing.camera); renderer.setRenderTarget(currentRenderTarget); } } }]); return CinematicCamera; }(_three.PerspectiveCamera); _exports.CinematicCamera = CinematicCamera; });