SAOPass.js 14 KB

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