three-stdlib
Version:
stand-alone library of threejs examples
1 lines • 10.1 kB
Source Map (JSON)
{"version":3,"file":"CinematicCamera.cjs","sources":["../../src/cameras/CinematicCamera.js"],"sourcesContent":["import {\n Mesh,\n OrthographicCamera,\n PerspectiveCamera,\n PlaneGeometry,\n Scene,\n ShaderMaterial,\n UniformsUtils,\n WebGLRenderTarget,\n} from 'three'\nimport { BokehShader2, BokehDepthShader } from '../shaders/BokehShader2'\n\nclass CinematicCamera extends PerspectiveCamera {\n constructor(fov, aspect, near, far) {\n super(fov, aspect, near, far)\n\n this.type = 'CinematicCamera'\n\n this.postprocessing = { enabled: true }\n this.shaderSettings = {\n rings: 3,\n samples: 4,\n }\n\n const depthShader = BokehDepthShader\n\n this.materialDepth = new ShaderMaterial({\n uniforms: depthShader.uniforms,\n vertexShader: depthShader.vertexShader,\n fragmentShader: depthShader.fragmentShader,\n })\n\n this.materialDepth.uniforms['mNear'].value = near\n this.materialDepth.uniforms['mFar'].value = far\n\n // In case of cinematicCamera, having a default lens set is important\n this.setLens()\n\n this.initPostProcessing()\n }\n\n // providing fnumber and coc(Circle of Confusion) as extra arguments\n setLens(focalLength, filmGauge, fNumber, coc) {\n // In case of cinematicCamera, having a default lens set is important\n if (focalLength === undefined) focalLength = 35\n if (filmGauge !== undefined) this.filmGauge = filmGauge\n\n this.setFocalLength(focalLength)\n\n // if fnumber and coc are not provided, cinematicCamera tries to act as a basic PerspectiveCamera\n if (fNumber === undefined) fNumber = 8\n if (coc === undefined) coc = 0.019\n\n this.fNumber = fNumber\n this.coc = coc\n\n // fNumber is focalLength by aperture\n this.aperture = focalLength / this.fNumber\n\n // hyperFocal is required to calculate depthOfField when a lens tries to focus at a distance with given fNumber and focalLength\n this.hyperFocal = (focalLength * focalLength) / (this.aperture * this.coc)\n }\n\n linearize(depth) {\n const zfar = this.far\n const znear = this.near\n return (-zfar * znear) / (depth * (zfar - znear) - zfar)\n }\n\n smoothstep(near, far, depth) {\n const x = this.saturate((depth - near) / (far - near))\n return x * x * (3 - 2 * x)\n }\n\n saturate(x) {\n return Math.max(0, Math.min(1, x))\n }\n\n // function for focusing at a distance from the camera\n focusAt(focusDistance) {\n if (focusDistance === undefined) focusDistance = 20\n\n const focalLength = this.getFocalLength()\n\n // distance from the camera (normal to frustrum) to focus on\n this.focus = focusDistance\n\n // the nearest point from the camera which is in focus (unused)\n this.nearPoint = (this.hyperFocal * this.focus) / (this.hyperFocal + (this.focus - focalLength))\n\n // the farthest point from the camera which is in focus (unused)\n this.farPoint = (this.hyperFocal * this.focus) / (this.hyperFocal - (this.focus - focalLength))\n\n // the gap or width of the space in which is everything is in focus (unused)\n this.depthOfField = this.farPoint - this.nearPoint\n\n // Considering minimum distance of focus for a standard lens (unused)\n if (this.depthOfField < 0) this.depthOfField = 0\n\n this.sdistance = this.smoothstep(this.near, this.far, this.focus)\n\n this.ldistance = this.linearize(1 - this.sdistance)\n\n this.postprocessing.bokeh_uniforms['focalDepth'].value = this.ldistance\n }\n\n initPostProcessing() {\n if (this.postprocessing.enabled) {\n this.postprocessing.scene = new Scene()\n\n this.postprocessing.camera = new OrthographicCamera(\n window.innerWidth / -2,\n window.innerWidth / 2,\n window.innerHeight / 2,\n window.innerHeight / -2,\n -10000,\n 10000,\n )\n\n this.postprocessing.scene.add(this.postprocessing.camera)\n\n this.postprocessing.rtTextureDepth = new WebGLRenderTarget(window.innerWidth, window.innerHeight)\n this.postprocessing.rtTextureColor = new WebGLRenderTarget(window.innerWidth, window.innerHeight)\n\n const bokeh_shader = BokehShader2\n\n this.postprocessing.bokeh_uniforms = UniformsUtils.clone(bokeh_shader.uniforms)\n\n this.postprocessing.bokeh_uniforms['tColor'].value = this.postprocessing.rtTextureColor.texture\n this.postprocessing.bokeh_uniforms['tDepth'].value = this.postprocessing.rtTextureDepth.texture\n\n this.postprocessing.bokeh_uniforms['manualdof'].value = 0\n this.postprocessing.bokeh_uniforms['shaderFocus'].value = 0\n\n this.postprocessing.bokeh_uniforms['fstop'].value = 2.8\n\n this.postprocessing.bokeh_uniforms['showFocus'].value = 1\n\n this.postprocessing.bokeh_uniforms['focalDepth'].value = 0.1\n\n //console.log( this.postprocessing.bokeh_uniforms[ \"focalDepth\" ].value );\n\n this.postprocessing.bokeh_uniforms['znear'].value = this.near\n this.postprocessing.bokeh_uniforms['zfar'].value = this.near\n\n this.postprocessing.bokeh_uniforms['textureWidth'].value = window.innerWidth\n\n this.postprocessing.bokeh_uniforms['textureHeight'].value = window.innerHeight\n\n this.postprocessing.materialBokeh = new ShaderMaterial({\n uniforms: this.postprocessing.bokeh_uniforms,\n vertexShader: bokeh_shader.vertexShader,\n fragmentShader: bokeh_shader.fragmentShader,\n defines: {\n RINGS: this.shaderSettings.rings,\n SAMPLES: this.shaderSettings.samples,\n DEPTH_PACKING: 1,\n },\n })\n\n this.postprocessing.quad = new Mesh(\n new PlaneGeometry(window.innerWidth, window.innerHeight),\n this.postprocessing.materialBokeh,\n )\n this.postprocessing.quad.position.z = -500\n this.postprocessing.scene.add(this.postprocessing.quad)\n }\n }\n\n renderCinematic(scene, renderer) {\n if (this.postprocessing.enabled) {\n const currentRenderTarget = renderer.getRenderTarget()\n\n renderer.clear()\n\n // Render scene into texture\n\n scene.overrideMaterial = null\n renderer.setRenderTarget(this.postprocessing.rtTextureColor)\n renderer.clear()\n renderer.render(scene, this)\n\n // Render depth into texture\n\n scene.overrideMaterial = this.materialDepth\n renderer.setRenderTarget(this.postprocessing.rtTextureDepth)\n renderer.clear()\n renderer.render(scene, this)\n\n // Render bokeh composite\n\n renderer.setRenderTarget(null)\n renderer.render(this.postprocessing.scene, this.postprocessing.camera)\n\n renderer.setRenderTarget(currentRenderTarget)\n }\n }\n}\n\nexport { CinematicCamera }\n"],"names":["PerspectiveCamera","BokehDepthShader","ShaderMaterial","Scene","OrthographicCamera","WebGLRenderTarget","BokehShader2","UniformsUtils","Mesh","PlaneGeometry"],"mappings":";;;;AAYA,MAAM,wBAAwBA,MAAAA,kBAAkB;AAAA,EAC9C,YAAY,KA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