SAOPass.js 14 KB

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