SAOPass.js 14 KB

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