SAOPass.js 14 KB

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