SSAOPass.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. /*
  3. * Screen-space ambient occlusion pass.
  4. *
  5. * Has the following parameters
  6. * - radius
  7. * - onlyAO
  8. * - aoClamp
  9. * - lumInfluence
  10. *
  11. * To output to screen set renderToScreens true
  12. *
  13. * @author alteredq / http://alteredqualia.com/
  14. * @author tentone
  15. */
  16. THREE.SSAOPass = function ( scene, camera, width, height ) {
  17. if ( THREE.SSAOShader === undefined)
  18. {
  19. console.warn('THREE.SSAOPass depends on THREE.SSAOShader');
  20. return new THREE.ShaderPass();
  21. }
  22. THREE.ShaderPass.call( this, THREE.SSAOShader );
  23. this.width = ( width !== undefined ) ? width : 512;
  24. this.height = ( height !== undefined ) ? height : 512;
  25. this.renderToScreen = false;
  26. this.camera2 = camera;
  27. this.scene2 = scene;
  28. //Depth material
  29. this.depthMaterial = new THREE.MeshDepthMaterial();
  30. this.depthMaterial.depthPacking = THREE.RGBADepthPacking;
  31. this.depthMaterial.blending = THREE.NoBlending;
  32. //Depth render target
  33. this.depthRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter } );
  34. this.depthRenderTarget.texture.name = 'SSAOShader.rt';
  35. //Shader uniforms
  36. this.uniforms[ 'tDepth' ].value = this.depthRenderTarget.texture;
  37. this.uniforms[ 'size' ].value.set( this.width, this.height );
  38. this.uniforms[ 'cameraNear' ].value = this.camera2.near;
  39. this.uniforms[ 'cameraFar' ].value = this.camera2.far;
  40. this.uniforms[ 'radius' ].value = 4;
  41. this.uniforms[ 'onlyAO' ].value = false;
  42. this.uniforms[ 'aoClamp' ].value = 0.25;
  43. this.uniforms[ 'lumInfluence' ].value = 0.7;
  44. //Setters and getters for uniforms
  45. var self = this;
  46. Object.defineProperties(this, {
  47. radius: {
  48. get: function() { return this.uniforms[ 'radius' ].value; },
  49. set: function(value) { this.uniforms[ 'radius' ].value = value; }
  50. },
  51. onlyAO: {
  52. get: function() { return this.uniforms[ 'onlyAO' ].value; },
  53. set: function(value) { this.uniforms[ 'onlyAO' ].value = value; }
  54. },
  55. aoClamp: {
  56. get: function() { return this.uniforms[ 'aoClamp' ].value; },
  57. set: function(value) { this.uniforms[ 'aoClamp' ].value = value; }
  58. },
  59. lumInfluence: {
  60. get: function() { return this.uniforms[ 'lumInfluence' ].value; },
  61. set: function(value) { this.uniforms[ 'lumInfluence' ].value = value; }
  62. },
  63. });
  64. }
  65. THREE.SSAOPass.prototype = Object.create( THREE.ShaderPass.prototype );
  66. THREE.SSAOPass.prototype.render = function( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  67. //Render depth into depthRenderTarget
  68. this.scene2.overrideMaterial = this.depthMaterial;
  69. renderer.render( this.scene2, this.camera2, this.depthRenderTarget, true );
  70. //Render renderPass and SSAO shaderPass
  71. this.scene2.overrideMaterial = null;
  72. THREE.ShaderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta, maskActive );
  73. };
  74. THREE.SSAOPass.prototype.setSize = function( width, height ) {
  75. this.width = width;
  76. this.height = height;
  77. this.uniforms[ 'size' ].value.set( this.width, this.height );
  78. this.depthRenderTarget.setSize( this.width, this.height );
  79. };