SAOPass.js 14 KB

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