SSAOPass.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. 'use strict';
  2. /**
  3. * Screen-space ambient occlusion pass.
  4. *
  5. * Has the following parameters
  6. * - radius
  7. * - Ambient occlusion shadow radius (numeric value).
  8. * - onlyAO
  9. * - Display only ambient occlusion result (boolean value).
  10. * - aoClamp
  11. * - Ambient occlusion clamp (numeric value).
  12. * - lumInfluence
  13. * - Pixel luminosity influence in AO calculation (numeric value).
  14. *
  15. * To output to screen set renderToScreens true
  16. *
  17. * @author alteredq / http://alteredqualia.com/
  18. * @author tentone
  19. * @class SSAOPass
  20. */
  21. THREE.SSAOPass = function ( scene, camera, width, height ) {
  22. if ( THREE.SSAOShader === undefined ) {
  23. console.warn( 'THREE.SSAOPass depends on THREE.SSAOShader' );
  24. return new THREE.ShaderPass();
  25. }
  26. THREE.ShaderPass.call( this, THREE.SSAOShader );
  27. this.width = ( width !== undefined ) ? width : 512;
  28. this.height = ( height !== undefined ) ? height : 256;
  29. this.renderToScreen = false;
  30. this.camera2 = camera;
  31. this.scene2 = scene;
  32. //Depth material
  33. this.depthMaterial = new THREE.MeshDepthMaterial();
  34. this.depthMaterial.depthPacking = THREE.RGBADepthPacking;
  35. this.depthMaterial.blending = THREE.NoBlending;
  36. //Depth render target
  37. this.depthRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter } );
  38. //this.depthRenderTarget.texture.name = 'SSAOShader.rt';
  39. //Shader uniforms
  40. this.uniforms[ 'tDepth' ].value = this.depthRenderTarget.texture;
  41. this.uniforms[ 'size' ].value.set( this.width, this.height );
  42. this.uniforms[ 'cameraNear' ].value = this.camera2.near;
  43. this.uniforms[ 'cameraFar' ].value = this.camera2.far;
  44. this.uniforms[ 'radius' ].value = 4;
  45. this.uniforms[ 'onlyAO' ].value = false;
  46. this.uniforms[ 'aoClamp' ].value = 0.25;
  47. this.uniforms[ 'lumInfluence' ].value = 0.7;
  48. //Setters and getters for uniforms
  49. Object.defineProperties( this, {
  50. radius: {
  51. get: function () {
  52. return this.uniforms[ 'radius' ].value;
  53. },
  54. set: function ( value ) {
  55. this.uniforms[ 'radius' ].value = value;
  56. }
  57. },
  58. onlyAO: {
  59. get: function () {
  60. return this.uniforms[ 'onlyAO' ].value;
  61. },
  62. set: function ( value ) {
  63. this.uniforms[ 'onlyAO' ].value = value;
  64. }
  65. },
  66. aoClamp: {
  67. get: function () {
  68. return this.uniforms[ 'aoClamp' ].value;
  69. },
  70. set: function ( value ) {
  71. this.uniforms[ 'aoClamp' ].value = value;
  72. }
  73. },
  74. lumInfluence: {
  75. get: function () {
  76. return this.uniforms[ 'lumInfluence' ].value;
  77. },
  78. set: function ( value ) {
  79. this.uniforms[ 'lumInfluence' ].value = value;
  80. }
  81. },
  82. } );
  83. };
  84. THREE.SSAOPass.prototype = Object.create( THREE.ShaderPass.prototype );
  85. /**
  86. * Render using this pass.
  87. *
  88. * @method render
  89. * @param {WebGLRenderer} renderer
  90. * @param {WebGLRenderTarget} writeBuffer Buffer to write output.
  91. * @param {WebGLRenderTarget} readBuffer Input buffer.
  92. * @param {Number} delta Delta time in milliseconds.
  93. * @param {Boolean} maskActive Not used in this pass.
  94. */
  95. THREE.SSAOPass.prototype.render = function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  96. //Render depth into depthRenderTarget
  97. this.scene2.overrideMaterial = this.depthMaterial;
  98. renderer.render( this.scene2, this.camera2, this.depthRenderTarget, true );
  99. this.scene2.overrideMaterial = null;
  100. //SSAO shaderPass
  101. THREE.ShaderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta, maskActive );
  102. };
  103. /**
  104. * Change scene to be renderer by this render pass.
  105. *
  106. * @method setScene
  107. * @param {Scene} scene
  108. */
  109. THREE.SSAOPass.prototype.setScene = function ( scene ) {
  110. this.scene2 = scene;
  111. };
  112. /**
  113. * Set camera used by this render pass.
  114. *
  115. * @method setCamera
  116. * @param {Camera} camera
  117. */
  118. THREE.SSAOPass.prototype.setCamera = function ( camera ) {
  119. this.camera2 = camera;
  120. this.uniforms[ 'cameraNear' ].value = this.camera2.near;
  121. this.uniforms[ 'cameraFar' ].value = this.camera2.far;
  122. };
  123. /**
  124. * Set resolution of this render pass.
  125. *
  126. * @method setSize
  127. * @param {Number} width
  128. * @param {Number} height
  129. */
  130. THREE.SSAOPass.prototype.setSize = function ( width, height ) {
  131. this.width = width;
  132. this.height = height;
  133. this.uniforms[ 'size' ].value.set( this.width, this.height );
  134. this.depthRenderTarget.setSize( this.width, this.height );
  135. };