CinematicCamera.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. //Inheriting PerspectiveCamera
  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. this.material_depth = new THREE.MeshDepthMaterial();
  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, frameHeight, fNumber, coc ) {
  25. //In case of cinematicCamera, having a default lens set is important
  26. if ( focalLength === undefined ) focalLength = 35;
  27. THREE.PerspectiveCamera.prototype.setLens.call( this, focalLength, frameHeight );
  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.focalLength = focalLength;
  32. this.fNumber = fNumber;
  33. this.coc = coc;
  34. //fNumber is focalLength by aperture
  35. this.aperture = this.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 = (this.focalLength*this.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. //distance from the camera (normal to frustrum) to focus on (unused)
  55. this.focusDistance = focusDistance;
  56. //the nearest point from the camera which is in focus (unused)
  57. this.nearPoint = (this.hyperFocal*this.focusDistance)/(this.hyperFocal+(this.focusDistance-this.focalLength));
  58. //the farthest point from the camera which is in focus (unused)
  59. this.farPoint = (this.hyperFocal*this.focusDistance)/(this.hyperFocal-(this.focusDistance-this.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.focusDistance);
  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. {
  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;
  80. this.postprocessing.bokeh_uniforms[ "tDepth" ].value = this.postprocessing.rtTextureDepth;
  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({uniforms: this.postprocessing.bokeh_uniforms, vertexShader: bokeh_shader.vertexShader, fragmentShader: bokeh_shader.fragmentShader, defines: { RINGS: this.shaderSettings.rings, SAMPLES: this.shaderSettings.samples } } );
  92. this.postprocessing.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight ), this.postprocessing.materialBokeh );
  93. this.postprocessing.quad.position.z = -500;
  94. this.postprocessing.scene.add( this.postprocessing.quad );
  95. }
  96. }
  97. THREE.CinematicCamera.prototype.renderCinematic = function (scene, renderer ){
  98. if ( this.postprocessing.enabled ) {
  99. renderer.clear();
  100. // Render scene into texture
  101. scene.overrideMaterial = null;
  102. renderer.render( scene, camera, this.postprocessing.rtTextureColor, true );
  103. // Render depth into texture
  104. scene.overrideMaterial = this.material_depth;
  105. renderer.render( scene, camera, this.postprocessing.rtTextureDepth, true );
  106. // Render bokeh composite
  107. renderer.render( this.postprocessing.scene, this.postprocessing.camera );
  108. }
  109. }