CinematicCamera.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. console.warn( "THREE.CinematicCamera: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.CinematicCamera = function ( fov, aspect, near, far ) {
  3. THREE.PerspectiveCamera.call( this, fov, aspect, near, far );
  4. this.type = 'CinematicCamera';
  5. this.postprocessing = { enabled: true };
  6. this.shaderSettings = {
  7. rings: 3,
  8. samples: 4
  9. };
  10. var depthShader = THREE.BokehDepthShader;
  11. this.materialDepth = new THREE.ShaderMaterial( {
  12. uniforms: depthShader.uniforms,
  13. vertexShader: depthShader.vertexShader,
  14. fragmentShader: depthShader.fragmentShader
  15. } );
  16. this.materialDepth.uniforms[ 'mNear' ].value = near;
  17. this.materialDepth.uniforms[ 'mFar' ].value = far;
  18. // In case of cinematicCamera, having a default lens set is important
  19. this.setLens();
  20. this.initPostProcessing();
  21. };
  22. THREE.CinematicCamera.prototype = Object.create( THREE.PerspectiveCamera.prototype );
  23. THREE.CinematicCamera.prototype.constructor = THREE.CinematicCamera;
  24. // providing fnumber and coc(Circle of Confusion) as extra arguments
  25. THREE.CinematicCamera.prototype.setLens = function ( focalLength, filmGauge, fNumber, coc ) {
  26. // In case of cinematicCamera, having a default lens set is important
  27. if ( focalLength === undefined ) focalLength = 35;
  28. if ( filmGauge !== undefined ) this.filmGauge = filmGauge;
  29. this.setFocalLength( focalLength );
  30. // if fnumber and coc are not provided, cinematicCamera tries to act as a basic PerspectiveCamera
  31. if ( fNumber === undefined ) fNumber = 8;
  32. if ( coc === undefined ) coc = 0.019;
  33. this.fNumber = fNumber;
  34. this.coc = coc;
  35. // fNumber is focalLength by aperture
  36. this.aperture = focalLength / this.fNumber;
  37. // hyperFocal is required to calculate depthOfField when a lens tries to focus at a distance with given fNumber and focalLength
  38. this.hyperFocal = ( focalLength * focalLength ) / ( this.aperture * this.coc );
  39. };
  40. THREE.CinematicCamera.prototype.linearize = function ( depth ) {
  41. var zfar = this.far;
  42. var znear = this.near;
  43. return - zfar * znear / ( depth * ( zfar - znear ) - zfar );
  44. };
  45. THREE.CinematicCamera.prototype.smoothstep = function ( near, far, depth ) {
  46. var x = this.saturate( ( depth - near ) / ( far - near ) );
  47. return x * x * ( 3 - 2 * x );
  48. };
  49. THREE.CinematicCamera.prototype.saturate = function ( x ) {
  50. return Math.max( 0, Math.min( 1, x ) );
  51. };
  52. // function for focusing at a distance from the camera
  53. THREE.CinematicCamera.prototype.focusAt = function ( focusDistance ) {
  54. if ( focusDistance === undefined ) focusDistance = 20;
  55. var focalLength = this.getFocalLength();
  56. // distance from the camera (normal to frustrum) to focus on
  57. this.focus = focusDistance;
  58. // the nearest point from the camera which is in focus (unused)
  59. this.nearPoint = ( this.hyperFocal * this.focus ) / ( this.hyperFocal + ( this.focus - focalLength ) );
  60. // the farthest point from the camera which is in focus (unused)
  61. this.farPoint = ( this.hyperFocal * this.focus ) / ( this.hyperFocal - ( this.focus - focalLength ) );
  62. // the gap or width of the space in which is everything is in focus (unused)
  63. this.depthOfField = this.farPoint - this.nearPoint;
  64. // Considering minimum distance of focus for a standard lens (unused)
  65. if ( this.depthOfField < 0 ) this.depthOfField = 0;
  66. this.sdistance = this.smoothstep( this.near, this.far, this.focus );
  67. this.ldistance = this.linearize( 1 - this.sdistance );
  68. this.postprocessing.bokeh_uniforms[ 'focalDepth' ].value = this.ldistance;
  69. };
  70. THREE.CinematicCamera.prototype.initPostProcessing = function () {
  71. if ( this.postprocessing.enabled ) {
  72. this.postprocessing.scene = new THREE.Scene();
  73. this.postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10000, 10000 );
  74. this.postprocessing.scene.add( this.postprocessing.camera );
  75. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  76. this.postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  77. this.postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  78. var bokeh_shader = THREE.BokehShader;
  79. this.postprocessing.bokeh_uniforms = THREE.UniformsUtils.clone( bokeh_shader.uniforms );
  80. this.postprocessing.bokeh_uniforms[ "tColor" ].value = this.postprocessing.rtTextureColor.texture;
  81. this.postprocessing.bokeh_uniforms[ "tDepth" ].value = this.postprocessing.rtTextureDepth.texture;
  82. this.postprocessing.bokeh_uniforms[ "manualdof" ].value = 0;
  83. this.postprocessing.bokeh_uniforms[ "shaderFocus" ].value = 0;
  84. this.postprocessing.bokeh_uniforms[ "fstop" ].value = 2.8;
  85. this.postprocessing.bokeh_uniforms[ "showFocus" ].value = 1;
  86. this.postprocessing.bokeh_uniforms[ "focalDepth" ].value = 0.1;
  87. //console.log( this.postprocessing.bokeh_uniforms[ "focalDepth" ].value );
  88. this.postprocessing.bokeh_uniforms[ "znear" ].value = this.near;
  89. this.postprocessing.bokeh_uniforms[ "zfar" ].value = this.near;
  90. this.postprocessing.bokeh_uniforms[ "textureWidth" ].value = window.innerWidth;
  91. this.postprocessing.bokeh_uniforms[ "textureHeight" ].value = window.innerHeight;
  92. this.postprocessing.materialBokeh = new THREE.ShaderMaterial( {
  93. uniforms: this.postprocessing.bokeh_uniforms,
  94. vertexShader: bokeh_shader.vertexShader,
  95. fragmentShader: bokeh_shader.fragmentShader,
  96. defines: {
  97. RINGS: this.shaderSettings.rings,
  98. SAMPLES: this.shaderSettings.samples,
  99. DEPTH_PACKING: 1
  100. }
  101. } );
  102. this.postprocessing.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight ), this.postprocessing.materialBokeh );
  103. this.postprocessing.quad.position.z = - 500;
  104. this.postprocessing.scene.add( this.postprocessing.quad );
  105. }
  106. };
  107. THREE.CinematicCamera.prototype.renderCinematic = function ( scene, renderer ) {
  108. if ( this.postprocessing.enabled ) {
  109. var currentRenderTarget = renderer.getRenderTarget();
  110. renderer.clear();
  111. // Render scene into texture
  112. scene.overrideMaterial = null;
  113. renderer.setRenderTarget( this.postprocessing.rtTextureColor );
  114. renderer.clear();
  115. renderer.render( scene, this );
  116. // Render depth into texture
  117. scene.overrideMaterial = this.materialDepth;
  118. renderer.setRenderTarget( this.postprocessing.rtTextureDepth );
  119. renderer.clear();
  120. renderer.render( scene, this );
  121. // Render bokeh composite
  122. renderer.setRenderTarget( null );
  123. renderer.render( this.postprocessing.scene, this.postprocessing.camera );
  124. renderer.setRenderTarget( currentRenderTarget );
  125. }
  126. };