SAOPass.js 14 KB

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