2
0

CinematicCamera.js 6.4 KB

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