CinematicCamera.js 6.4 KB

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