SAOPass.js 14 KB

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