CinematicCamera.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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, frameHeight, fNumber, coc ) {
  24. // In case of cinematicCamera, having a default lens set is important
  25. if ( focalLength === undefined ) focalLength = 35;
  26. THREE.PerspectiveCamera.prototype.setLens.call( this, focalLength, frameHeight );
  27. // if fnumber and coc are not provided, cinematicCamera tries to act as a basic PerspectiveCamera
  28. if ( fNumber === undefined ) fNumber = 8;
  29. if ( coc === undefined ) coc = 0.019;
  30. this.focalLength = focalLength;
  31. this.fNumber = fNumber;
  32. this.coc = coc;
  33. // fNumber is focalLength by aperture
  34. this.aperture = this.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 = ( this.focalLength * this.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. // distance from the camera (normal to frustrum) to focus on (unused)
  54. this.focusDistance = focusDistance;
  55. // the nearest point from the camera which is in focus (unused)
  56. this.nearPoint = ( this.hyperFocal * this.focusDistance ) / ( this.hyperFocal + ( this.focusDistance - this.focalLength ) );
  57. // the farthest point from the camera which is in focus (unused)
  58. this.farPoint = ( this.hyperFocal * this.focusDistance ) / ( this.hyperFocal - ( this.focusDistance - this.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.focusDistance );
  64. this.ldistance = this.linearize( 1 - this.sdistance );
  65. this.postprocessing.bokeh_uniforms[ 'focalDepth' ].value = this.ldistance;
  66. };
  67. THREE.CinematicCamera.prototype.initPostProcessing = function () {
  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. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  73. this.postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  74. this.postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  75. var bokeh_shader = THREE.BokehShader;
  76. this.postprocessing.bokeh_uniforms = THREE.UniformsUtils.clone( bokeh_shader.uniforms );
  77. this.postprocessing.bokeh_uniforms[ "tColor" ].value = this.postprocessing.rtTextureColor;
  78. this.postprocessing.bokeh_uniforms[ "tDepth" ].value = this.postprocessing.rtTextureDepth;
  79. this.postprocessing.bokeh_uniforms[ "manualdof" ].value = 0;
  80. this.postprocessing.bokeh_uniforms[ "shaderFocus" ].value = 0;
  81. this.postprocessing.bokeh_uniforms[ "fstop" ].value = 2.8;
  82. this.postprocessing.bokeh_uniforms[ "showFocus" ].value = 1;
  83. this.postprocessing.bokeh_uniforms[ "focalDepth" ].value = 0.1;
  84. console.log( this.postprocessing.bokeh_uniforms[ "focalDepth" ].value );
  85. this.postprocessing.bokeh_uniforms[ "znear" ].value = this.near;
  86. this.postprocessing.bokeh_uniforms[ "zfar" ].value = this.near;
  87. this.postprocessing.bokeh_uniforms[ "textureWidth" ].value = window.innerWidth;
  88. this.postprocessing.bokeh_uniforms[ "textureHeight" ].value = window.innerHeight;
  89. this.postprocessing.materialBokeh = new THREE.ShaderMaterial( {
  90. uniforms: this.postprocessing.bokeh_uniforms,
  91. vertexShader: bokeh_shader.vertexShader,
  92. fragmentShader: bokeh_shader.fragmentShader,
  93. defines: {
  94. RINGS: this.shaderSettings.rings,
  95. SAMPLES: this.shaderSettings.samples
  96. }
  97. } );
  98. this.postprocessing.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 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. THREE.CinematicCamera.prototype.renderCinematic = function ( scene, renderer ) {
  104. if ( this.postprocessing.enabled ) {
  105. renderer.clear();
  106. // Render scene into texture
  107. scene.overrideMaterial = null;
  108. renderer.render( scene, camera, this.postprocessing.rtTextureColor, true );
  109. // Render depth into texture
  110. scene.overrideMaterial = this.material_depth;
  111. renderer.render( scene, camera, this.postprocessing.rtTextureDepth, true );
  112. // Render bokeh composite
  113. renderer.render( this.postprocessing.scene, this.postprocessing.camera );
  114. }
  115. };