SAOPass.js 14 KB

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