SAOPass.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. console.warn( "THREE.SAOPass: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  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.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. depthTexture.minFilter = THREE.NearestFilter;
  46. depthTexture.maxFilter = THREE.NearestFilter;
  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.getInverse( this.camera.projectionMatrix );
  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.Pass.FullScreenQuad( null );
  130. };
  131. THREE.SAOPass.OUTPUT = {
  132. 'Beauty': 1,
  133. 'Default': 0,
  134. 'SAO': 2,
  135. 'Depth': 3,
  136. 'Normal': 4
  137. };
  138. THREE.SAOPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  139. constructor: THREE.SAOPass,
  140. render: function ( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  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. this.oldClearColor.copy( renderer.getClearColor() );
  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;
  164. // this.saoMaterial.uniforms['randomSeed'].value = Math.random();
  165. var depthCutoff = this.params.saoBlurDepthCutoff * ( this.camera.far - this.camera.near );
  166. this.vBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  167. this.hBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  168. this.vBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  169. this.vBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  170. this.hBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  171. this.hBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  172. this.params.saoBlurRadius = Math.floor( this.params.saoBlurRadius );
  173. if ( ( this.prevStdDev !== this.params.saoBlurStdDev ) || ( this.prevNumSamples !== this.params.saoBlurRadius ) ) {
  174. THREE.BlurShaderUtils.configure( this.vBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new THREE.Vector2( 0, 1 ) );
  175. THREE.BlurShaderUtils.configure( this.hBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new THREE.Vector2( 1, 0 ) );
  176. this.prevStdDev = this.params.saoBlurStdDev;
  177. this.prevNumSamples = this.params.saoBlurRadius;
  178. }
  179. // Rendering scene to depth texture
  180. renderer.setClearColor( 0x000000 );
  181. renderer.setRenderTarget( this.beautyRenderTarget );
  182. renderer.clear();
  183. renderer.render( this.scene, this.camera );
  184. // Re-render scene if depth texture extension is not supported
  185. if ( ! this.supportsDepthTextureExtension ) {
  186. // Clear rule : far clipping plane in both RGBA and Basic encoding
  187. this.renderOverride( renderer, this.depthMaterial, this.depthRenderTarget, 0x000000, 1.0 );
  188. }
  189. if ( this.supportsNormalTexture ) {
  190. // Clear rule : default normal is facing the camera
  191. this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
  192. }
  193. // Rendering SAO texture
  194. this.renderPass( renderer, this.saoMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  195. // Blurring SAO texture
  196. if ( this.params.saoBlur ) {
  197. this.renderPass( renderer, this.vBlurMaterial, this.blurIntermediateRenderTarget, 0xffffff, 1.0 );
  198. this.renderPass( renderer, this.hBlurMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  199. }
  200. var outputMaterial = this.materialCopy;
  201. // Setting up SAO rendering
  202. if ( this.params.output === 3 ) {
  203. if ( this.supportsDepthTextureExtension ) {
  204. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.depthTexture;
  205. this.materialCopy.needsUpdate = true;
  206. } else {
  207. this.depthCopy.uniforms[ 'tDiffuse' ].value = this.depthRenderTarget.texture;
  208. this.depthCopy.needsUpdate = true;
  209. outputMaterial = this.depthCopy;
  210. }
  211. } else if ( this.params.output === 4 ) {
  212. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
  213. this.materialCopy.needsUpdate = true;
  214. } else {
  215. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  216. this.materialCopy.needsUpdate = true;
  217. }
  218. // Blending depends on output, only want a CustomBlending when showing SAO
  219. if ( this.params.output === 0 ) {
  220. outputMaterial.blending = THREE.CustomBlending;
  221. } else {
  222. outputMaterial.blending = THREE.NoBlending;
  223. }
  224. // Rendering SAOPass result on top of previous pass
  225. this.renderPass( renderer, outputMaterial, this.renderToScreen ? null : readBuffer );
  226. renderer.setClearColor( this.oldClearColor, this.oldClearAlpha );
  227. renderer.autoClear = oldAutoClear;
  228. },
  229. renderPass: function ( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  230. // save original state
  231. this.originalClearColor.copy( renderer.getClearColor() );
  232. var originalClearAlpha = renderer.getClearAlpha();
  233. var originalAutoClear = renderer.autoClear;
  234. renderer.setRenderTarget( renderTarget );
  235. // setup pass state
  236. renderer.autoClear = false;
  237. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  238. renderer.setClearColor( clearColor );
  239. renderer.setClearAlpha( clearAlpha || 0.0 );
  240. renderer.clear();
  241. }
  242. this.fsQuad.material = passMaterial;
  243. this.fsQuad.render( renderer );
  244. // restore original state
  245. renderer.autoClear = originalAutoClear;
  246. renderer.setClearColor( this.originalClearColor );
  247. renderer.setClearAlpha( originalClearAlpha );
  248. },
  249. renderOverride: function ( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  250. this.originalClearColor.copy( renderer.getClearColor() );
  251. var originalClearAlpha = renderer.getClearAlpha();
  252. var originalAutoClear = renderer.autoClear;
  253. renderer.setRenderTarget( renderTarget );
  254. renderer.autoClear = false;
  255. clearColor = overrideMaterial.clearColor || clearColor;
  256. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  257. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  258. renderer.setClearColor( clearColor );
  259. renderer.setClearAlpha( clearAlpha || 0.0 );
  260. renderer.clear();
  261. }
  262. this.scene.overrideMaterial = overrideMaterial;
  263. renderer.render( this.scene, this.camera );
  264. this.scene.overrideMaterial = null;
  265. // restore original state
  266. renderer.autoClear = originalAutoClear;
  267. renderer.setClearColor( this.originalClearColor );
  268. renderer.setClearAlpha( originalClearAlpha );
  269. },
  270. setSize: function ( width, height ) {
  271. this.beautyRenderTarget.setSize( width, height );
  272. this.saoRenderTarget.setSize( width, height );
  273. this.blurIntermediateRenderTarget.setSize( width, height );
  274. this.normalRenderTarget.setSize( width, height );
  275. this.depthRenderTarget.setSize( width, height );
  276. this.saoMaterial.uniforms[ 'size' ].value.set( width, height );
  277. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.getInverse( this.camera.projectionMatrix );
  278. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  279. this.saoMaterial.needsUpdate = true;
  280. this.vBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  281. this.vBlurMaterial.needsUpdate = true;
  282. this.hBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  283. this.hBlurMaterial.needsUpdate = true;
  284. }
  285. } );