SAOPass.js 14 KB

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