CinematicCamera.js 6.0 KB

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