SAOPass.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. ( function () {
  2. /**
  3. * SAO implementation inspired from bhouston previous SAO work
  4. */
  5. class SAOPass extends THREE.Pass {
  6. constructor( scene, camera, depthTexture, useNormals, resolution ) {
  7. super();
  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. const depthTexture = new THREE.DepthTexture();
  45. depthTexture.type = THREE.UnsignedShortType;
  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.copy( this.camera.projectionMatrixInverse );
  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.UnpackDepthRGBAShader === 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.fsQuad = new THREE.FullScreenQuad( null );
  129. }
  130. render( renderer, writeBuffer, readBuffer
  131. /*, deltaTime, maskActive*/
  132. ) {
  133. // Rendering readBuffer first when rendering to screen
  134. if ( this.renderToScreen ) {
  135. this.materialCopy.blending = THREE.NoBlending;
  136. this.materialCopy.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  137. this.materialCopy.needsUpdate = true;
  138. this.renderPass( renderer, this.materialCopy, null );
  139. }
  140. if ( this.params.output === 1 ) {
  141. return;
  142. }
  143. renderer.getClearColor( this._oldClearColor );
  144. this.oldClearAlpha = renderer.getClearAlpha();
  145. const oldAutoClear = renderer.autoClear;
  146. renderer.autoClear = false;
  147. renderer.setRenderTarget( this.depthRenderTarget );
  148. renderer.clear();
  149. this.saoMaterial.uniforms[ 'bias' ].value = this.params.saoBias;
  150. this.saoMaterial.uniforms[ 'intensity' ].value = this.params.saoIntensity;
  151. this.saoMaterial.uniforms[ 'scale' ].value = this.params.saoScale;
  152. this.saoMaterial.uniforms[ 'kernelRadius' ].value = this.params.saoKernelRadius;
  153. this.saoMaterial.uniforms[ 'minResolution' ].value = this.params.saoMinResolution;
  154. this.saoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  155. this.saoMaterial.uniforms[ 'cameraFar' ].value = this.camera.far; // this.saoMaterial.uniforms['randomSeed'].value = Math.random();
  156. const depthCutoff = this.params.saoBlurDepthCutoff * ( this.camera.far - this.camera.near );
  157. this.vBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  158. this.hBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  159. this.vBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  160. this.vBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  161. this.hBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  162. this.hBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  163. this.params.saoBlurRadius = Math.floor( this.params.saoBlurRadius );
  164. if ( this.prevStdDev !== this.params.saoBlurStdDev || this.prevNumSamples !== this.params.saoBlurRadius ) {
  165. THREE.BlurShaderUtils.configure( this.vBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new THREE.Vector2( 0, 1 ) );
  166. THREE.BlurShaderUtils.configure( this.hBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new THREE.Vector2( 1, 0 ) );
  167. this.prevStdDev = this.params.saoBlurStdDev;
  168. this.prevNumSamples = this.params.saoBlurRadius;
  169. } // Rendering scene to depth texture
  170. renderer.setClearColor( 0x000000 );
  171. renderer.setRenderTarget( this.beautyRenderTarget );
  172. renderer.clear();
  173. renderer.render( this.scene, this.camera ); // Re-render scene if depth texture extension is not supported
  174. if ( ! this.supportsDepthTextureExtension ) {
  175. // Clear rule : far clipping plane in both RGBA and Basic encoding
  176. this.renderOverride( renderer, this.depthMaterial, this.depthRenderTarget, 0x000000, 1.0 );
  177. }
  178. if ( this.supportsNormalTexture ) {
  179. // Clear rule : default normal is facing the camera
  180. this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
  181. } // Rendering SAO texture
  182. this.renderPass( renderer, this.saoMaterial, this.saoRenderTarget, 0xffffff, 1.0 ); // Blurring SAO texture
  183. if ( this.params.saoBlur ) {
  184. this.renderPass( renderer, this.vBlurMaterial, this.blurIntermediateRenderTarget, 0xffffff, 1.0 );
  185. this.renderPass( renderer, this.hBlurMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  186. }
  187. let outputMaterial = this.materialCopy; // Setting up SAO rendering
  188. if ( this.params.output === 3 ) {
  189. if ( this.supportsDepthTextureExtension ) {
  190. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.depthTexture;
  191. this.materialCopy.needsUpdate = true;
  192. } else {
  193. this.depthCopy.uniforms[ 'tDiffuse' ].value = this.depthRenderTarget.texture;
  194. this.depthCopy.needsUpdate = true;
  195. outputMaterial = this.depthCopy;
  196. }
  197. } else if ( this.params.output === 4 ) {
  198. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
  199. this.materialCopy.needsUpdate = true;
  200. } else {
  201. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  202. this.materialCopy.needsUpdate = true;
  203. } // Blending depends on output, only want a THREE.CustomBlending when showing SAO
  204. if ( this.params.output === 0 ) {
  205. outputMaterial.blending = THREE.CustomBlending;
  206. } else {
  207. outputMaterial.blending = THREE.NoBlending;
  208. } // Rendering SAOPass result on top of previous pass
  209. this.renderPass( renderer, outputMaterial, this.renderToScreen ? null : readBuffer );
  210. renderer.setClearColor( this._oldClearColor, this.oldClearAlpha );
  211. renderer.autoClear = oldAutoClear;
  212. }
  213. renderPass( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  214. // save original state
  215. renderer.getClearColor( this.originalClearColor );
  216. const originalClearAlpha = renderer.getClearAlpha();
  217. const originalAutoClear = renderer.autoClear;
  218. renderer.setRenderTarget( renderTarget ); // setup pass state
  219. renderer.autoClear = false;
  220. if ( clearColor !== undefined && clearColor !== null ) {
  221. renderer.setClearColor( clearColor );
  222. renderer.setClearAlpha( clearAlpha || 0.0 );
  223. renderer.clear();
  224. }
  225. this.fsQuad.material = passMaterial;
  226. this.fsQuad.render( renderer ); // restore original state
  227. renderer.autoClear = originalAutoClear;
  228. renderer.setClearColor( this.originalClearColor );
  229. renderer.setClearAlpha( originalClearAlpha );
  230. }
  231. renderOverride( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  232. renderer.getClearColor( this.originalClearColor );
  233. const originalClearAlpha = renderer.getClearAlpha();
  234. const originalAutoClear = renderer.autoClear;
  235. renderer.setRenderTarget( renderTarget );
  236. renderer.autoClear = false;
  237. clearColor = overrideMaterial.clearColor || clearColor;
  238. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  239. if ( clearColor !== undefined && clearColor !== null ) {
  240. renderer.setClearColor( clearColor );
  241. renderer.setClearAlpha( clearAlpha || 0.0 );
  242. renderer.clear();
  243. }
  244. this.scene.overrideMaterial = overrideMaterial;
  245. renderer.render( this.scene, this.camera );
  246. this.scene.overrideMaterial = null; // restore original state
  247. renderer.autoClear = originalAutoClear;
  248. renderer.setClearColor( this.originalClearColor );
  249. renderer.setClearAlpha( originalClearAlpha );
  250. }
  251. setSize( width, height ) {
  252. this.beautyRenderTarget.setSize( width, height );
  253. this.saoRenderTarget.setSize( width, height );
  254. this.blurIntermediateRenderTarget.setSize( width, height );
  255. this.normalRenderTarget.setSize( width, height );
  256. this.depthRenderTarget.setSize( width, height );
  257. this.saoMaterial.uniforms[ 'size' ].value.set( width, height );
  258. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  259. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  260. this.saoMaterial.needsUpdate = true;
  261. this.vBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  262. this.vBlurMaterial.needsUpdate = true;
  263. this.hBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  264. this.hBlurMaterial.needsUpdate = true;
  265. }
  266. }
  267. SAOPass.OUTPUT = {
  268. 'Beauty': 1,
  269. 'Default': 0,
  270. 'SAO': 2,
  271. 'Depth': 3,
  272. 'Normal': 4
  273. };
  274. THREE.SAOPass = SAOPass;
  275. } )();