SAOPass.js 14 KB

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