SAOPass.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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.extensions.derivatives = true;
  71. this.saoMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  72. this.saoMaterial.uniforms[ 'tDepth' ].value = depthTexture;
  73. this.saoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
  74. this.saoMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  75. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  76. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  77. this.saoMaterial.blending = NoBlending;
  78. this.vBlurMaterial = new ShaderMaterial( {
  79. uniforms: UniformsUtils.clone( DepthLimitedBlurShader.uniforms ),
  80. defines: Object.assign( {}, DepthLimitedBlurShader.defines ),
  81. vertexShader: DepthLimitedBlurShader.vertexShader,
  82. fragmentShader: DepthLimitedBlurShader.fragmentShader
  83. } );
  84. this.vBlurMaterial.defines[ 'DEPTH_PACKING' ] = 0;
  85. this.vBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  86. this.vBlurMaterial.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  87. this.vBlurMaterial.uniforms[ 'tDepth' ].value = depthTexture;
  88. this.vBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  89. this.vBlurMaterial.blending = NoBlending;
  90. this.hBlurMaterial = new ShaderMaterial( {
  91. uniforms: UniformsUtils.clone( DepthLimitedBlurShader.uniforms ),
  92. defines: Object.assign( {}, DepthLimitedBlurShader.defines ),
  93. vertexShader: DepthLimitedBlurShader.vertexShader,
  94. fragmentShader: DepthLimitedBlurShader.fragmentShader
  95. } );
  96. this.hBlurMaterial.defines[ 'DEPTH_PACKING' ] = 0;
  97. this.hBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  98. this.hBlurMaterial.uniforms[ 'tDiffuse' ].value = this.blurIntermediateRenderTarget.texture;
  99. this.hBlurMaterial.uniforms[ 'tDepth' ].value = depthTexture;
  100. this.hBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  101. this.hBlurMaterial.blending = NoBlending;
  102. this.materialCopy = new ShaderMaterial( {
  103. uniforms: UniformsUtils.clone( CopyShader.uniforms ),
  104. vertexShader: CopyShader.vertexShader,
  105. fragmentShader: CopyShader.fragmentShader,
  106. blending: NoBlending
  107. } );
  108. this.materialCopy.transparent = true;
  109. this.materialCopy.depthTest = false;
  110. this.materialCopy.depthWrite = false;
  111. this.materialCopy.blending = CustomBlending;
  112. this.materialCopy.blendSrc = DstColorFactor;
  113. this.materialCopy.blendDst = ZeroFactor;
  114. this.materialCopy.blendEquation = AddEquation;
  115. this.materialCopy.blendSrcAlpha = DstAlphaFactor;
  116. this.materialCopy.blendDstAlpha = ZeroFactor;
  117. this.materialCopy.blendEquationAlpha = AddEquation;
  118. this.fsQuad = new FullScreenQuad( null );
  119. }
  120. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  121. // Rendering readBuffer first when rendering to screen
  122. if ( this.renderToScreen ) {
  123. this.materialCopy.blending = NoBlending;
  124. this.materialCopy.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  125. this.materialCopy.needsUpdate = true;
  126. this.renderPass( renderer, this.materialCopy, null );
  127. }
  128. renderer.getClearColor( this._oldClearColor );
  129. this.oldClearAlpha = renderer.getClearAlpha();
  130. const oldAutoClear = renderer.autoClear;
  131. renderer.autoClear = false;
  132. this.saoMaterial.uniforms[ 'bias' ].value = this.params.saoBias;
  133. this.saoMaterial.uniforms[ 'intensity' ].value = this.params.saoIntensity;
  134. this.saoMaterial.uniforms[ 'scale' ].value = this.params.saoScale;
  135. this.saoMaterial.uniforms[ 'kernelRadius' ].value = this.params.saoKernelRadius;
  136. this.saoMaterial.uniforms[ 'minResolution' ].value = this.params.saoMinResolution;
  137. this.saoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  138. this.saoMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  139. // this.saoMaterial.uniforms['randomSeed'].value = Math.random();
  140. const depthCutoff = this.params.saoBlurDepthCutoff * ( this.camera.far - this.camera.near );
  141. this.vBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  142. this.hBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  143. this.vBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  144. this.vBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  145. this.hBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  146. this.hBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  147. this.params.saoBlurRadius = Math.floor( this.params.saoBlurRadius );
  148. if ( ( this.prevStdDev !== this.params.saoBlurStdDev ) || ( this.prevNumSamples !== this.params.saoBlurRadius ) ) {
  149. BlurShaderUtils.configure( this.vBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new Vector2( 0, 1 ) );
  150. BlurShaderUtils.configure( this.hBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new Vector2( 1, 0 ) );
  151. this.prevStdDev = this.params.saoBlurStdDev;
  152. this.prevNumSamples = this.params.saoBlurRadius;
  153. }
  154. // render normal and depth
  155. this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
  156. // Rendering SAO texture
  157. this.renderPass( renderer, this.saoMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  158. // Blurring SAO texture
  159. if ( this.params.saoBlur ) {
  160. this.renderPass( renderer, this.vBlurMaterial, this.blurIntermediateRenderTarget, 0xffffff, 1.0 );
  161. this.renderPass( renderer, this.hBlurMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  162. }
  163. const outputMaterial = this.materialCopy;
  164. // Setting up SAO rendering
  165. if ( this.params.output === SAOPass.OUTPUT.Normal ) {
  166. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
  167. this.materialCopy.needsUpdate = true;
  168. } else {
  169. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  170. this.materialCopy.needsUpdate = true;
  171. }
  172. // Blending depends on output
  173. if ( this.params.output === SAOPass.OUTPUT.Default ) {
  174. outputMaterial.blending = CustomBlending;
  175. } else {
  176. outputMaterial.blending = NoBlending;
  177. }
  178. // Rendering SAOPass result on top of previous pass
  179. this.renderPass( renderer, outputMaterial, this.renderToScreen ? null : readBuffer );
  180. renderer.setClearColor( this._oldClearColor, this.oldClearAlpha );
  181. renderer.autoClear = oldAutoClear;
  182. }
  183. renderPass( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  184. // save original state
  185. renderer.getClearColor( this.originalClearColor );
  186. const originalClearAlpha = renderer.getClearAlpha();
  187. const originalAutoClear = renderer.autoClear;
  188. renderer.setRenderTarget( renderTarget );
  189. // setup pass state
  190. renderer.autoClear = false;
  191. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  192. renderer.setClearColor( clearColor );
  193. renderer.setClearAlpha( clearAlpha || 0.0 );
  194. renderer.clear();
  195. }
  196. this.fsQuad.material = passMaterial;
  197. this.fsQuad.render( renderer );
  198. // restore original state
  199. renderer.autoClear = originalAutoClear;
  200. renderer.setClearColor( this.originalClearColor );
  201. renderer.setClearAlpha( originalClearAlpha );
  202. }
  203. renderOverride( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  204. renderer.getClearColor( this.originalClearColor );
  205. const originalClearAlpha = renderer.getClearAlpha();
  206. const originalAutoClear = renderer.autoClear;
  207. renderer.setRenderTarget( renderTarget );
  208. renderer.autoClear = false;
  209. clearColor = overrideMaterial.clearColor || clearColor;
  210. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  211. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  212. renderer.setClearColor( clearColor );
  213. renderer.setClearAlpha( clearAlpha || 0.0 );
  214. renderer.clear();
  215. }
  216. this.scene.overrideMaterial = overrideMaterial;
  217. renderer.render( this.scene, this.camera );
  218. this.scene.overrideMaterial = null;
  219. // restore original state
  220. renderer.autoClear = originalAutoClear;
  221. renderer.setClearColor( this.originalClearColor );
  222. renderer.setClearAlpha( originalClearAlpha );
  223. }
  224. setSize( width, height ) {
  225. this.saoRenderTarget.setSize( width, height );
  226. this.blurIntermediateRenderTarget.setSize( width, height );
  227. this.normalRenderTarget.setSize( width, height );
  228. this.saoMaterial.uniforms[ 'size' ].value.set( width, height );
  229. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  230. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  231. this.saoMaterial.needsUpdate = true;
  232. this.vBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  233. this.vBlurMaterial.needsUpdate = true;
  234. this.hBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  235. this.hBlurMaterial.needsUpdate = true;
  236. }
  237. dispose() {
  238. this.saoRenderTarget.dispose();
  239. this.blurIntermediateRenderTarget.dispose();
  240. this.normalRenderTarget.dispose();
  241. this.normalMaterial.dispose();
  242. this.saoMaterial.dispose();
  243. this.vBlurMaterial.dispose();
  244. this.hBlurMaterial.dispose();
  245. this.materialCopy.dispose();
  246. this.fsQuad.dispose();
  247. }
  248. }
  249. SAOPass.OUTPUT = {
  250. 'Default': 0,
  251. 'SAO': 1,
  252. 'Normal': 2
  253. };
  254. export { SAOPass };