CinematicCamera.js 6.0 KB

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