CinematicCamera.js 5.9 KB

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