2
0

CinematicCamera.js 5.9 KB

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