SAOPass.js 14 KB

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