CinematicCamera.js 6.1 KB

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