CinematicCamera.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. console.warn( "THREE.CinematicCamera: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author greggman / http://games.greggman.com/
  5. * @author zz85 / http://www.lab4games.net/zz85/blog
  6. * @author kaypiKun
  7. */
  8. THREE.CinematicCamera = function ( fov, aspect, near, far ) {
  9. THREE.PerspectiveCamera.call( this, fov, aspect, near, far );
  10. this.type = 'CinematicCamera';
  11. this.postprocessing = { enabled: true };
  12. this.shaderSettings = {
  13. rings: 3,
  14. samples: 4
  15. };
  16. var depthShader = THREE.BokehDepthShader;
  17. this.materialDepth = new THREE.ShaderMaterial( {
  18. uniforms: depthShader.uniforms,
  19. vertexShader: depthShader.vertexShader,
  20. fragmentShader: depthShader.fragmentShader
  21. } );
  22. this.materialDepth.uniforms[ 'mNear' ].value = near;
  23. this.materialDepth.uniforms[ 'mFar' ].value = far;
  24. // In case of cinematicCamera, having a default lens set is important
  25. this.setLens();
  26. this.initPostProcessing();
  27. };
  28. THREE.CinematicCamera.prototype = Object.create( THREE.PerspectiveCamera.prototype );
  29. THREE.CinematicCamera.prototype.constructor = THREE.CinematicCamera;
  30. // providing fnumber and coc(Circle of Confusion) as extra arguments
  31. THREE.CinematicCamera.prototype.setLens = function ( focalLength, filmGauge, fNumber, coc ) {
  32. // In case of cinematicCamera, having a default lens set is important
  33. if ( focalLength === undefined ) focalLength = 35;
  34. if ( filmGauge !== undefined ) this.filmGauge = filmGauge;
  35. this.setFocalLength( focalLength );
  36. // if fnumber and coc are not provided, cinematicCamera tries to act as a basic PerspectiveCamera
  37. if ( fNumber === undefined ) fNumber = 8;
  38. if ( coc === undefined ) coc = 0.019;
  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. THREE.CinematicCamera.prototype.linearize = function ( depth ) {
  47. var zfar = this.far;
  48. var znear = this.near;
  49. return - zfar * znear / ( depth * ( zfar - znear ) - zfar );
  50. };
  51. THREE.CinematicCamera.prototype.smoothstep = function ( near, far, depth ) {
  52. var x = this.saturate( ( depth - near ) / ( far - near ) );
  53. return x * x * ( 3 - 2 * x );
  54. };
  55. THREE.CinematicCamera.prototype.saturate = function ( x ) {
  56. return Math.max( 0, Math.min( 1, x ) );
  57. };
  58. // function for focusing at a distance from the camera
  59. THREE.CinematicCamera.prototype.focusAt = function ( focusDistance ) {
  60. if ( focusDistance === undefined ) focusDistance = 20;
  61. var 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. THREE.CinematicCamera.prototype.initPostProcessing = function () {
  77. if ( this.postprocessing.enabled ) {
  78. this.postprocessing.scene = new THREE.Scene();
  79. this.postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10000, 10000 );
  80. this.postprocessing.scene.add( this.postprocessing.camera );
  81. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  82. this.postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  83. this.postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  84. var bokeh_shader = THREE.BokehShader;
  85. this.postprocessing.bokeh_uniforms = THREE.UniformsUtils.clone( bokeh_shader.uniforms );
  86. this.postprocessing.bokeh_uniforms[ "tColor" ].value = this.postprocessing.rtTextureColor.texture;
  87. this.postprocessing.bokeh_uniforms[ "tDepth" ].value = this.postprocessing.rtTextureDepth.texture;
  88. this.postprocessing.bokeh_uniforms[ "manualdof" ].value = 0;
  89. this.postprocessing.bokeh_uniforms[ "shaderFocus" ].value = 0;
  90. this.postprocessing.bokeh_uniforms[ "fstop" ].value = 2.8;
  91. this.postprocessing.bokeh_uniforms[ "showFocus" ].value = 1;
  92. this.postprocessing.bokeh_uniforms[ "focalDepth" ].value = 0.1;
  93. //console.log( this.postprocessing.bokeh_uniforms[ "focalDepth" ].value );
  94. this.postprocessing.bokeh_uniforms[ "znear" ].value = this.near;
  95. this.postprocessing.bokeh_uniforms[ "zfar" ].value = this.near;
  96. this.postprocessing.bokeh_uniforms[ "textureWidth" ].value = window.innerWidth;
  97. this.postprocessing.bokeh_uniforms[ "textureHeight" ].value = window.innerHeight;
  98. this.postprocessing.materialBokeh = new THREE.ShaderMaterial( {
  99. uniforms: this.postprocessing.bokeh_uniforms,
  100. vertexShader: bokeh_shader.vertexShader,
  101. fragmentShader: bokeh_shader.fragmentShader,
  102. defines: {
  103. RINGS: this.shaderSettings.rings,
  104. SAMPLES: this.shaderSettings.samples,
  105. DEPTH_PACKING: 1
  106. }
  107. } );
  108. this.postprocessing.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight ), this.postprocessing.materialBokeh );
  109. this.postprocessing.quad.position.z = - 500;
  110. this.postprocessing.scene.add( this.postprocessing.quad );
  111. }
  112. };
  113. THREE.CinematicCamera.prototype.renderCinematic = function ( scene, renderer ) {
  114. if ( this.postprocessing.enabled ) {
  115. var currentRenderTarget = renderer.getRenderTarget();
  116. renderer.clear();
  117. // Render scene into texture
  118. scene.overrideMaterial = null;
  119. renderer.setRenderTarget( this.postprocessing.rtTextureColor );
  120. renderer.clear();
  121. renderer.render( scene, this );
  122. // Render depth into texture
  123. scene.overrideMaterial = this.materialDepth;
  124. renderer.setRenderTarget( this.postprocessing.rtTextureDepth );
  125. renderer.clear();
  126. renderer.render( scene, this );
  127. // Render bokeh composite
  128. renderer.setRenderTarget( null );
  129. renderer.render( this.postprocessing.scene, this.postprocessing.camera );
  130. renderer.setRenderTarget( currentRenderTarget );
  131. }
  132. };