SAOPass.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import {
  2. AddEquation,
  3. Color,
  4. CustomBlending,
  5. DepthTexture,
  6. DstAlphaFactor,
  7. DstColorFactor,
  8. MeshDepthMaterial,
  9. MeshNormalMaterial,
  10. NearestFilter,
  11. NoBlending,
  12. RGBADepthPacking,
  13. ShaderMaterial,
  14. UniformsUtils,
  15. UnsignedShortType,
  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. import { UnpackDepthRGBAShader } from '../shaders/UnpackDepthRGBAShader.js';
  26. /**
  27. * SAO implementation inspired from bhouston previous SAO work
  28. */
  29. class SAOPass extends Pass {
  30. constructor( scene, camera, useDepthTexture = false, useNormals = false, resolution = new Vector2( 256, 256 ) ) {
  31. super();
  32. this.scene = scene;
  33. this.camera = camera;
  34. this.clear = true;
  35. this.needsSwap = false;
  36. this.supportsDepthTextureExtension = useDepthTexture;
  37. this.supportsNormalTexture = useNormals;
  38. this.originalClearColor = new Color();
  39. this._oldClearColor = new Color();
  40. this.oldClearAlpha = 1;
  41. this.params = {
  42. output: 0,
  43. saoBias: 0.5,
  44. saoIntensity: 0.18,
  45. saoScale: 1,
  46. saoKernelRadius: 100,
  47. saoMinResolution: 0,
  48. saoBlur: true,
  49. saoBlurRadius: 8,
  50. saoBlurStdDev: 4,
  51. saoBlurDepthCutoff: 0.01
  52. };
  53. this.resolution = new Vector2( resolution.x, resolution.y );
  54. this.saoRenderTarget = new WebGLRenderTarget( this.resolution.x, this.resolution.y );
  55. this.blurIntermediateRenderTarget = this.saoRenderTarget.clone();
  56. this.beautyRenderTarget = this.saoRenderTarget.clone();
  57. this.normalRenderTarget = new WebGLRenderTarget( this.resolution.x, this.resolution.y, {
  58. minFilter: NearestFilter,
  59. magFilter: NearestFilter
  60. } );
  61. this.depthRenderTarget = this.normalRenderTarget.clone();
  62. let depthTexture;
  63. if ( this.supportsDepthTextureExtension ) {
  64. depthTexture = new DepthTexture();
  65. depthTexture.type = UnsignedShortType;
  66. this.beautyRenderTarget.depthTexture = depthTexture;
  67. this.beautyRenderTarget.depthBuffer = true;
  68. }
  69. this.depthMaterial = new MeshDepthMaterial();
  70. this.depthMaterial.depthPacking = RGBADepthPacking;
  71. this.depthMaterial.blending = NoBlending;
  72. this.normalMaterial = new MeshNormalMaterial();
  73. this.normalMaterial.blending = NoBlending;
  74. this.saoMaterial = new ShaderMaterial( {
  75. defines: Object.assign( {}, SAOShader.defines ),
  76. fragmentShader: SAOShader.fragmentShader,
  77. vertexShader: SAOShader.vertexShader,
  78. uniforms: UniformsUtils.clone( SAOShader.uniforms )
  79. } );
  80. this.saoMaterial.extensions.derivatives = true;
  81. this.saoMaterial.defines[ 'DEPTH_PACKING' ] = this.supportsDepthTextureExtension ? 0 : 1;
  82. this.saoMaterial.defines[ 'NORMAL_TEXTURE' ] = this.supportsNormalTexture ? 1 : 0;
  83. this.saoMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  84. this.saoMaterial.uniforms[ 'tDepth' ].value = ( this.supportsDepthTextureExtension ) ? depthTexture : this.depthRenderTarget.texture;
  85. this.saoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
  86. this.saoMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  87. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  88. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  89. this.saoMaterial.blending = NoBlending;
  90. this.vBlurMaterial = 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.vBlurMaterial.defines[ 'DEPTH_PACKING' ] = this.supportsDepthTextureExtension ? 0 : 1;
  97. this.vBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  98. this.vBlurMaterial.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  99. this.vBlurMaterial.uniforms[ 'tDepth' ].value = ( this.supportsDepthTextureExtension ) ? depthTexture : this.depthRenderTarget.texture;
  100. this.vBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  101. this.vBlurMaterial.blending = NoBlending;
  102. this.hBlurMaterial = new ShaderMaterial( {
  103. uniforms: UniformsUtils.clone( DepthLimitedBlurShader.uniforms ),
  104. defines: Object.assign( {}, DepthLimitedBlurShader.defines ),
  105. vertexShader: DepthLimitedBlurShader.vertexShader,
  106. fragmentShader: DepthLimitedBlurShader.fragmentShader
  107. } );
  108. this.hBlurMaterial.defines[ 'DEPTH_PACKING' ] = this.supportsDepthTextureExtension ? 0 : 1;
  109. this.hBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  110. this.hBlurMaterial.uniforms[ 'tDiffuse' ].value = this.blurIntermediateRenderTarget.texture;
  111. this.hBlurMaterial.uniforms[ 'tDepth' ].value = ( this.supportsDepthTextureExtension ) ? depthTexture : this.depthRenderTarget.texture;
  112. this.hBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  113. this.hBlurMaterial.blending = NoBlending;
  114. this.materialCopy = new ShaderMaterial( {
  115. uniforms: UniformsUtils.clone( CopyShader.uniforms ),
  116. vertexShader: CopyShader.vertexShader,
  117. fragmentShader: CopyShader.fragmentShader,
  118. blending: NoBlending
  119. } );
  120. this.materialCopy.transparent = true;
  121. this.materialCopy.depthTest = false;
  122. this.materialCopy.depthWrite = false;
  123. this.materialCopy.blending = CustomBlending;
  124. this.materialCopy.blendSrc = DstColorFactor;
  125. this.materialCopy.blendDst = ZeroFactor;
  126. this.materialCopy.blendEquation = AddEquation;
  127. this.materialCopy.blendSrcAlpha = DstAlphaFactor;
  128. this.materialCopy.blendDstAlpha = ZeroFactor;
  129. this.materialCopy.blendEquationAlpha = AddEquation;
  130. this.depthCopy = new ShaderMaterial( {
  131. uniforms: UniformsUtils.clone( UnpackDepthRGBAShader.uniforms ),
  132. vertexShader: UnpackDepthRGBAShader.vertexShader,
  133. fragmentShader: UnpackDepthRGBAShader.fragmentShader,
  134. blending: NoBlending
  135. } );
  136. this.fsQuad = new FullScreenQuad( null );
  137. }
  138. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  139. // Rendering readBuffer first when rendering to screen
  140. if ( this.renderToScreen ) {
  141. this.materialCopy.blending = NoBlending;
  142. this.materialCopy.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  143. this.materialCopy.needsUpdate = true;
  144. this.renderPass( renderer, this.materialCopy, null );
  145. }
  146. if ( this.params.output === 1 ) {
  147. return;
  148. }
  149. renderer.getClearColor( this._oldClearColor );
  150. this.oldClearAlpha = renderer.getClearAlpha();
  151. const oldAutoClear = renderer.autoClear;
  152. renderer.autoClear = false;
  153. renderer.setRenderTarget( this.depthRenderTarget );
  154. renderer.clear();
  155. this.saoMaterial.uniforms[ 'bias' ].value = this.params.saoBias;
  156. this.saoMaterial.uniforms[ 'intensity' ].value = this.params.saoIntensity;
  157. this.saoMaterial.uniforms[ 'scale' ].value = this.params.saoScale;
  158. this.saoMaterial.uniforms[ 'kernelRadius' ].value = this.params.saoKernelRadius;
  159. this.saoMaterial.uniforms[ 'minResolution' ].value = this.params.saoMinResolution;
  160. this.saoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  161. this.saoMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  162. // this.saoMaterial.uniforms['randomSeed'].value = Math.random();
  163. const depthCutoff = this.params.saoBlurDepthCutoff * ( this.camera.far - this.camera.near );
  164. this.vBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  165. this.hBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  166. this.vBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  167. this.vBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  168. this.hBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  169. this.hBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  170. this.params.saoBlurRadius = Math.floor( this.params.saoBlurRadius );
  171. if ( ( this.prevStdDev !== this.params.saoBlurStdDev ) || ( this.prevNumSamples !== this.params.saoBlurRadius ) ) {
  172. BlurShaderUtils.configure( this.vBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new Vector2( 0, 1 ) );
  173. BlurShaderUtils.configure( this.hBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new Vector2( 1, 0 ) );
  174. this.prevStdDev = this.params.saoBlurStdDev;
  175. this.prevNumSamples = this.params.saoBlurRadius;
  176. }
  177. // Rendering scene to depth texture
  178. renderer.setClearColor( 0x000000 );
  179. renderer.setRenderTarget( this.beautyRenderTarget );
  180. renderer.clear();
  181. renderer.render( this.scene, this.camera );
  182. // Re-render scene if depth texture extension is not supported
  183. if ( ! this.supportsDepthTextureExtension ) {
  184. // Clear rule : far clipping plane in both RGBA and Basic encoding
  185. this.renderOverride( renderer, this.depthMaterial, this.depthRenderTarget, 0x000000, 1.0 );
  186. }
  187. if ( this.supportsNormalTexture ) {
  188. // Clear rule : default normal is facing the camera
  189. this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
  190. }
  191. // Rendering SAO texture
  192. this.renderPass( renderer, this.saoMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  193. // Blurring SAO texture
  194. if ( this.params.saoBlur ) {
  195. this.renderPass( renderer, this.vBlurMaterial, this.blurIntermediateRenderTarget, 0xffffff, 1.0 );
  196. this.renderPass( renderer, this.hBlurMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  197. }
  198. let outputMaterial = this.materialCopy;
  199. // Setting up SAO rendering
  200. if ( this.params.output === 3 ) {
  201. if ( this.supportsDepthTextureExtension ) {
  202. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.depthTexture;
  203. this.materialCopy.needsUpdate = true;
  204. } else {
  205. this.depthCopy.uniforms[ 'tDiffuse' ].value = this.depthRenderTarget.texture;
  206. this.depthCopy.needsUpdate = true;
  207. outputMaterial = this.depthCopy;
  208. }
  209. } else if ( this.params.output === 4 ) {
  210. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
  211. this.materialCopy.needsUpdate = true;
  212. } else {
  213. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  214. this.materialCopy.needsUpdate = true;
  215. }
  216. // Blending depends on output, only want a CustomBlending when showing SAO
  217. if ( this.params.output === 0 ) {
  218. outputMaterial.blending = CustomBlending;
  219. } else {
  220. outputMaterial.blending = NoBlending;
  221. }
  222. // Rendering SAOPass result on top of previous pass
  223. this.renderPass( renderer, outputMaterial, this.renderToScreen ? null : readBuffer );
  224. renderer.setClearColor( this._oldClearColor, this.oldClearAlpha );
  225. renderer.autoClear = oldAutoClear;
  226. }
  227. renderPass( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  228. // save original state
  229. renderer.getClearColor( this.originalClearColor );
  230. const originalClearAlpha = renderer.getClearAlpha();
  231. const originalAutoClear = renderer.autoClear;
  232. renderer.setRenderTarget( renderTarget );
  233. // setup pass state
  234. renderer.autoClear = false;
  235. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  236. renderer.setClearColor( clearColor );
  237. renderer.setClearAlpha( clearAlpha || 0.0 );
  238. renderer.clear();
  239. }
  240. this.fsQuad.material = passMaterial;
  241. this.fsQuad.render( renderer );
  242. // restore original state
  243. renderer.autoClear = originalAutoClear;
  244. renderer.setClearColor( this.originalClearColor );
  245. renderer.setClearAlpha( originalClearAlpha );
  246. }
  247. renderOverride( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  248. renderer.getClearColor( this.originalClearColor );
  249. const originalClearAlpha = renderer.getClearAlpha();
  250. const originalAutoClear = renderer.autoClear;
  251. renderer.setRenderTarget( renderTarget );
  252. renderer.autoClear = false;
  253. clearColor = overrideMaterial.clearColor || clearColor;
  254. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  255. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  256. renderer.setClearColor( clearColor );
  257. renderer.setClearAlpha( clearAlpha || 0.0 );
  258. renderer.clear();
  259. }
  260. this.scene.overrideMaterial = overrideMaterial;
  261. renderer.render( this.scene, this.camera );
  262. this.scene.overrideMaterial = null;
  263. // restore original state
  264. renderer.autoClear = originalAutoClear;
  265. renderer.setClearColor( this.originalClearColor );
  266. renderer.setClearAlpha( originalClearAlpha );
  267. }
  268. setSize( width, height ) {
  269. this.beautyRenderTarget.setSize( width, height );
  270. this.saoRenderTarget.setSize( width, height );
  271. this.blurIntermediateRenderTarget.setSize( width, height );
  272. this.normalRenderTarget.setSize( width, height );
  273. this.depthRenderTarget.setSize( width, height );
  274. this.saoMaterial.uniforms[ 'size' ].value.set( width, height );
  275. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  276. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  277. this.saoMaterial.needsUpdate = true;
  278. this.vBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  279. this.vBlurMaterial.needsUpdate = true;
  280. this.hBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  281. this.hBlurMaterial.needsUpdate = true;
  282. }
  283. dispose() {
  284. this.saoRenderTarget.dispose();
  285. this.blurIntermediateRenderTarget.dispose();
  286. this.beautyRenderTarget.dispose();
  287. this.normalRenderTarget.dispose();
  288. this.depthRenderTarget.dispose();
  289. this.depthMaterial.dispose();
  290. this.normalMaterial.dispose();
  291. this.saoMaterial.dispose();
  292. this.vBlurMaterial.dispose();
  293. this.hBlurMaterial.dispose();
  294. this.materialCopy.dispose();
  295. this.depthCopy.dispose();
  296. this.fsQuad.dispose();
  297. }
  298. }
  299. SAOPass.OUTPUT = {
  300. 'Beauty': 1,
  301. 'Default': 0,
  302. 'SAO': 2,
  303. 'Depth': 3,
  304. 'Normal': 4
  305. };
  306. export { SAOPass };