SAOPass.js 14 KB

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