SAOPass.js 11 KB

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