SAOPass.js 14 KB

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