SAOPass.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import {
  2. AddEquation,
  3. Color,
  4. CustomBlending,
  5. DepthTexture,
  6. DstAlphaFactor,
  7. DstColorFactor,
  8. HalfFloatType,
  9. MeshNormalMaterial,
  10. NearestFilter,
  11. NoBlending,
  12. ShaderMaterial,
  13. UniformsUtils,
  14. DepthStencilFormat,
  15. UnsignedInt248Type,
  16. Vector2,
  17. WebGLRenderTarget,
  18. ZeroFactor
  19. } from 'three';
  20. import { Pass, FullScreenQuad } from './Pass.js';
  21. import { SAOShader } from '../shaders/SAOShader.js';
  22. import { DepthLimitedBlurShader } from '../shaders/DepthLimitedBlurShader.js';
  23. import { BlurShaderUtils } from '../shaders/DepthLimitedBlurShader.js';
  24. import { CopyShader } from '../shaders/CopyShader.js';
  25. /**
  26. * SAO implementation inspired from bhouston previous SAO work
  27. */
  28. class SAOPass extends Pass {
  29. constructor( scene, camera, resolution = new Vector2( 256, 256 ) ) {
  30. super();
  31. this.scene = scene;
  32. this.camera = camera;
  33. this.clear = true;
  34. this.needsSwap = false;
  35. this.originalClearColor = new Color();
  36. this._oldClearColor = new Color();
  37. this.oldClearAlpha = 1;
  38. this.params = {
  39. output: 0,
  40. saoBias: 0.5,
  41. saoIntensity: 0.18,
  42. saoScale: 1,
  43. saoKernelRadius: 100,
  44. saoMinResolution: 0,
  45. saoBlur: true,
  46. saoBlurRadius: 8,
  47. saoBlurStdDev: 4,
  48. saoBlurDepthCutoff: 0.01
  49. };
  50. this.resolution = new Vector2( resolution.x, resolution.y );
  51. this.saoRenderTarget = new WebGLRenderTarget( this.resolution.x, this.resolution.y, { type: HalfFloatType } );
  52. this.blurIntermediateRenderTarget = this.saoRenderTarget.clone();
  53. const depthTexture = new DepthTexture();
  54. depthTexture.format = DepthStencilFormat;
  55. depthTexture.type = UnsignedInt248Type;
  56. this.normalRenderTarget = new WebGLRenderTarget( this.resolution.x, this.resolution.y, {
  57. minFilter: NearestFilter,
  58. magFilter: NearestFilter,
  59. type: HalfFloatType,
  60. depthTexture: depthTexture
  61. } );
  62. this.normalMaterial = new MeshNormalMaterial();
  63. this.normalMaterial.blending = NoBlending;
  64. this.saoMaterial = new ShaderMaterial( {
  65. defines: Object.assign( {}, SAOShader.defines ),
  66. fragmentShader: SAOShader.fragmentShader,
  67. vertexShader: SAOShader.vertexShader,
  68. uniforms: UniformsUtils.clone( SAOShader.uniforms )
  69. } );
  70. this.saoMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  71. this.saoMaterial.uniforms[ 'tDepth' ].value = depthTexture;
  72. this.saoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
  73. this.saoMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  74. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  75. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  76. this.saoMaterial.blending = NoBlending;
  77. this.vBlurMaterial = new ShaderMaterial( {
  78. uniforms: UniformsUtils.clone( DepthLimitedBlurShader.uniforms ),
  79. defines: Object.assign( {}, DepthLimitedBlurShader.defines ),
  80. vertexShader: DepthLimitedBlurShader.vertexShader,
  81. fragmentShader: DepthLimitedBlurShader.fragmentShader
  82. } );
  83. this.vBlurMaterial.defines[ 'DEPTH_PACKING' ] = 0;
  84. this.vBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  85. this.vBlurMaterial.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  86. this.vBlurMaterial.uniforms[ 'tDepth' ].value = depthTexture;
  87. this.vBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  88. this.vBlurMaterial.blending = NoBlending;
  89. this.hBlurMaterial = new ShaderMaterial( {
  90. uniforms: UniformsUtils.clone( DepthLimitedBlurShader.uniforms ),
  91. defines: Object.assign( {}, DepthLimitedBlurShader.defines ),
  92. vertexShader: DepthLimitedBlurShader.vertexShader,
  93. fragmentShader: DepthLimitedBlurShader.fragmentShader
  94. } );
  95. this.hBlurMaterial.defines[ 'DEPTH_PACKING' ] = 0;
  96. this.hBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  97. this.hBlurMaterial.uniforms[ 'tDiffuse' ].value = this.blurIntermediateRenderTarget.texture;
  98. this.hBlurMaterial.uniforms[ 'tDepth' ].value = depthTexture;
  99. this.hBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  100. this.hBlurMaterial.blending = NoBlending;
  101. this.materialCopy = new ShaderMaterial( {
  102. uniforms: UniformsUtils.clone( CopyShader.uniforms ),
  103. vertexShader: CopyShader.vertexShader,
  104. fragmentShader: CopyShader.fragmentShader,
  105. blending: NoBlending
  106. } );
  107. this.materialCopy.transparent = true;
  108. this.materialCopy.depthTest = false;
  109. this.materialCopy.depthWrite = false;
  110. this.materialCopy.blending = CustomBlending;
  111. this.materialCopy.blendSrc = DstColorFactor;
  112. this.materialCopy.blendDst = ZeroFactor;
  113. this.materialCopy.blendEquation = AddEquation;
  114. this.materialCopy.blendSrcAlpha = DstAlphaFactor;
  115. this.materialCopy.blendDstAlpha = ZeroFactor;
  116. this.materialCopy.blendEquationAlpha = AddEquation;
  117. this.fsQuad = new FullScreenQuad( null );
  118. }
  119. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  120. // Rendering readBuffer first when rendering to screen
  121. if ( this.renderToScreen ) {
  122. this.materialCopy.blending = NoBlending;
  123. this.materialCopy.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  124. this.materialCopy.needsUpdate = true;
  125. this.renderPass( renderer, this.materialCopy, null );
  126. }
  127. renderer.getClearColor( this._oldClearColor );
  128. this.oldClearAlpha = renderer.getClearAlpha();
  129. const oldAutoClear = renderer.autoClear;
  130. renderer.autoClear = false;
  131. this.saoMaterial.uniforms[ 'bias' ].value = this.params.saoBias;
  132. this.saoMaterial.uniforms[ 'intensity' ].value = this.params.saoIntensity;
  133. this.saoMaterial.uniforms[ 'scale' ].value = this.params.saoScale;
  134. this.saoMaterial.uniforms[ 'kernelRadius' ].value = this.params.saoKernelRadius;
  135. this.saoMaterial.uniforms[ 'minResolution' ].value = this.params.saoMinResolution;
  136. this.saoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  137. this.saoMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  138. // this.saoMaterial.uniforms['randomSeed'].value = Math.random();
  139. const depthCutoff = this.params.saoBlurDepthCutoff * ( this.camera.far - this.camera.near );
  140. this.vBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  141. this.hBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  142. this.vBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  143. this.vBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  144. this.hBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  145. this.hBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  146. this.params.saoBlurRadius = Math.floor( this.params.saoBlurRadius );
  147. if ( ( this.prevStdDev !== this.params.saoBlurStdDev ) || ( this.prevNumSamples !== this.params.saoBlurRadius ) ) {
  148. BlurShaderUtils.configure( this.vBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new Vector2( 0, 1 ) );
  149. BlurShaderUtils.configure( this.hBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new Vector2( 1, 0 ) );
  150. this.prevStdDev = this.params.saoBlurStdDev;
  151. this.prevNumSamples = this.params.saoBlurRadius;
  152. }
  153. // render normal and depth
  154. this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
  155. // Rendering SAO texture
  156. this.renderPass( renderer, this.saoMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  157. // Blurring SAO texture
  158. if ( this.params.saoBlur ) {
  159. this.renderPass( renderer, this.vBlurMaterial, this.blurIntermediateRenderTarget, 0xffffff, 1.0 );
  160. this.renderPass( renderer, this.hBlurMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  161. }
  162. const outputMaterial = this.materialCopy;
  163. // Setting up SAO rendering
  164. if ( this.params.output === SAOPass.OUTPUT.Normal ) {
  165. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
  166. this.materialCopy.needsUpdate = true;
  167. } else {
  168. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  169. this.materialCopy.needsUpdate = true;
  170. }
  171. // Blending depends on output
  172. if ( this.params.output === SAOPass.OUTPUT.Default ) {
  173. outputMaterial.blending = CustomBlending;
  174. } else {
  175. outputMaterial.blending = NoBlending;
  176. }
  177. // Rendering SAOPass result on top of previous pass
  178. this.renderPass( renderer, outputMaterial, this.renderToScreen ? null : readBuffer );
  179. renderer.setClearColor( this._oldClearColor, this.oldClearAlpha );
  180. renderer.autoClear = oldAutoClear;
  181. }
  182. renderPass( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  183. // save original state
  184. renderer.getClearColor( this.originalClearColor );
  185. const originalClearAlpha = renderer.getClearAlpha();
  186. const originalAutoClear = renderer.autoClear;
  187. renderer.setRenderTarget( renderTarget );
  188. // setup pass state
  189. renderer.autoClear = false;
  190. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  191. renderer.setClearColor( clearColor );
  192. renderer.setClearAlpha( clearAlpha || 0.0 );
  193. renderer.clear();
  194. }
  195. this.fsQuad.material = passMaterial;
  196. this.fsQuad.render( renderer );
  197. // restore original state
  198. renderer.autoClear = originalAutoClear;
  199. renderer.setClearColor( this.originalClearColor );
  200. renderer.setClearAlpha( originalClearAlpha );
  201. }
  202. renderOverride( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  203. renderer.getClearColor( this.originalClearColor );
  204. const originalClearAlpha = renderer.getClearAlpha();
  205. const originalAutoClear = renderer.autoClear;
  206. renderer.setRenderTarget( renderTarget );
  207. renderer.autoClear = false;
  208. clearColor = overrideMaterial.clearColor || clearColor;
  209. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  210. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  211. renderer.setClearColor( clearColor );
  212. renderer.setClearAlpha( clearAlpha || 0.0 );
  213. renderer.clear();
  214. }
  215. this.scene.overrideMaterial = overrideMaterial;
  216. renderer.render( this.scene, this.camera );
  217. this.scene.overrideMaterial = null;
  218. // restore original state
  219. renderer.autoClear = originalAutoClear;
  220. renderer.setClearColor( this.originalClearColor );
  221. renderer.setClearAlpha( originalClearAlpha );
  222. }
  223. setSize( width, height ) {
  224. this.saoRenderTarget.setSize( width, height );
  225. this.blurIntermediateRenderTarget.setSize( width, height );
  226. this.normalRenderTarget.setSize( width, height );
  227. this.saoMaterial.uniforms[ 'size' ].value.set( width, height );
  228. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  229. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  230. this.saoMaterial.needsUpdate = true;
  231. this.vBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  232. this.vBlurMaterial.needsUpdate = true;
  233. this.hBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  234. this.hBlurMaterial.needsUpdate = true;
  235. }
  236. dispose() {
  237. this.saoRenderTarget.dispose();
  238. this.blurIntermediateRenderTarget.dispose();
  239. this.normalRenderTarget.dispose();
  240. this.normalMaterial.dispose();
  241. this.saoMaterial.dispose();
  242. this.vBlurMaterial.dispose();
  243. this.hBlurMaterial.dispose();
  244. this.materialCopy.dispose();
  245. this.fsQuad.dispose();
  246. }
  247. }
  248. SAOPass.OUTPUT = {
  249. 'Default': 0,
  250. 'SAO': 1,
  251. 'Normal': 2
  252. };
  253. export { SAOPass };