CinematicCamera.js 6.6 KB

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