SSAOPass.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import {
  2. AddEquation,
  3. Color,
  4. CustomBlending,
  5. DataTexture,
  6. DepthTexture,
  7. DstAlphaFactor,
  8. DstColorFactor,
  9. FloatType,
  10. LinearFilter,
  11. MathUtils,
  12. MeshNormalMaterial,
  13. NearestFilter,
  14. NoBlending,
  15. RGBAFormat,
  16. RepeatWrapping,
  17. ShaderMaterial,
  18. UniformsUtils,
  19. UnsignedShortType,
  20. Vector3,
  21. WebGLRenderTarget,
  22. ZeroFactor
  23. } from '../../../build/three.module.js';
  24. import { Pass } from '../postprocessing/Pass.js';
  25. import { SimplexNoise } from '../math/SimplexNoise.js';
  26. import { SSAOShader } from '../shaders/SSAOShader.js';
  27. import { SSAOBlurShader } from '../shaders/SSAOShader.js';
  28. import { SSAODepthShader } from '../shaders/SSAOShader.js';
  29. import { CopyShader } from '../shaders/CopyShader.js';
  30. var SSAOPass = function ( scene, camera, width, height ) {
  31. Pass.call( this );
  32. this.width = ( width !== undefined ) ? width : 512;
  33. this.height = ( height !== undefined ) ? height : 512;
  34. this.clear = true;
  35. this.camera = camera;
  36. this.scene = scene;
  37. this.kernelRadius = 8;
  38. this.kernelSize = 32;
  39. this.kernel = [];
  40. this.noiseTexture = null;
  41. this.output = 0;
  42. this.minDistance = 0.005;
  43. this.maxDistance = 0.1;
  44. this._visibilityCache = new Map();
  45. //
  46. this.generateSampleKernel();
  47. this.generateRandomKernelRotations();
  48. // beauty render target
  49. var depthTexture = new DepthTexture();
  50. depthTexture.type = UnsignedShortType;
  51. this.beautyRenderTarget = new WebGLRenderTarget( this.width, this.height, {
  52. minFilter: LinearFilter,
  53. magFilter: LinearFilter,
  54. format: RGBAFormat
  55. } );
  56. // normal render target with depth buffer
  57. this.normalRenderTarget = new WebGLRenderTarget( this.width, this.height, {
  58. minFilter: NearestFilter,
  59. magFilter: NearestFilter,
  60. format: RGBAFormat,
  61. depthTexture: depthTexture
  62. } );
  63. // ssao render target
  64. this.ssaoRenderTarget = new WebGLRenderTarget( this.width, this.height, {
  65. minFilter: LinearFilter,
  66. magFilter: LinearFilter,
  67. format: RGBAFormat
  68. } );
  69. this.blurRenderTarget = this.ssaoRenderTarget.clone();
  70. // ssao material
  71. if ( SSAOShader === undefined ) {
  72. console.error( 'THREE.SSAOPass: The pass relies on SSAOShader.' );
  73. }
  74. this.ssaoMaterial = new ShaderMaterial( {
  75. defines: Object.assign( {}, SSAOShader.defines ),
  76. uniforms: UniformsUtils.clone( SSAOShader.uniforms ),
  77. vertexShader: SSAOShader.vertexShader,
  78. fragmentShader: SSAOShader.fragmentShader,
  79. blending: NoBlending
  80. } );
  81. this.ssaoMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  82. this.ssaoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
  83. this.ssaoMaterial.uniforms[ 'tDepth' ].value = this.normalRenderTarget.depthTexture;
  84. this.ssaoMaterial.uniforms[ 'tNoise' ].value = this.noiseTexture;
  85. this.ssaoMaterial.uniforms[ 'kernel' ].value = this.kernel;
  86. this.ssaoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  87. this.ssaoMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  88. this.ssaoMaterial.uniforms[ 'resolution' ].value.set( this.width, this.height );
  89. this.ssaoMaterial.uniforms[ 'cameraProjectionMatrix' ].value.copy( this.camera.projectionMatrix );
  90. this.ssaoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  91. // normal material
  92. this.normalMaterial = new MeshNormalMaterial();
  93. this.normalMaterial.blending = NoBlending;
  94. // blur material
  95. this.blurMaterial = new ShaderMaterial( {
  96. defines: Object.assign( {}, SSAOBlurShader.defines ),
  97. uniforms: UniformsUtils.clone( SSAOBlurShader.uniforms ),
  98. vertexShader: SSAOBlurShader.vertexShader,
  99. fragmentShader: SSAOBlurShader.fragmentShader
  100. } );
  101. this.blurMaterial.uniforms[ 'tDiffuse' ].value = this.ssaoRenderTarget.texture;
  102. this.blurMaterial.uniforms[ 'resolution' ].value.set( this.width, this.height );
  103. // material for rendering the depth
  104. this.depthRenderMaterial = new ShaderMaterial( {
  105. defines: Object.assign( {}, SSAODepthShader.defines ),
  106. uniforms: UniformsUtils.clone( SSAODepthShader.uniforms ),
  107. vertexShader: SSAODepthShader.vertexShader,
  108. fragmentShader: SSAODepthShader.fragmentShader,
  109. blending: NoBlending
  110. } );
  111. this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.normalRenderTarget.depthTexture;
  112. this.depthRenderMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  113. this.depthRenderMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  114. // material for rendering the content of a render target
  115. this.copyMaterial = new ShaderMaterial( {
  116. uniforms: UniformsUtils.clone( CopyShader.uniforms ),
  117. vertexShader: CopyShader.vertexShader,
  118. fragmentShader: CopyShader.fragmentShader,
  119. transparent: true,
  120. depthTest: false,
  121. depthWrite: false,
  122. blendSrc: DstColorFactor,
  123. blendDst: ZeroFactor,
  124. blendEquation: AddEquation,
  125. blendSrcAlpha: DstAlphaFactor,
  126. blendDstAlpha: ZeroFactor,
  127. blendEquationAlpha: AddEquation
  128. } );
  129. this.fsQuad = new Pass.FullScreenQuad( null );
  130. this.originalClearColor = new Color();
  131. };
  132. SSAOPass.prototype = Object.assign( Object.create( Pass.prototype ), {
  133. constructor: SSAOPass,
  134. dispose: function () {
  135. // dispose render targets
  136. this.beautyRenderTarget.dispose();
  137. this.normalRenderTarget.dispose();
  138. this.ssaoRenderTarget.dispose();
  139. this.blurRenderTarget.dispose();
  140. // dispose materials
  141. this.normalMaterial.dispose();
  142. this.blurMaterial.dispose();
  143. this.copyMaterial.dispose();
  144. this.depthRenderMaterial.dispose();
  145. // dipsose full screen quad
  146. this.fsQuad.dispose();
  147. },
  148. render: function ( renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) {
  149. // render beauty
  150. renderer.setRenderTarget( this.beautyRenderTarget );
  151. renderer.clear();
  152. renderer.render( this.scene, this.camera );
  153. // render normals and depth (honor only meshes, points and lines do not contribute to SSAO)
  154. this.overrideVisibility();
  155. this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
  156. this.restoreVisibility();
  157. // render SSAO
  158. this.ssaoMaterial.uniforms[ 'kernelRadius' ].value = this.kernelRadius;
  159. this.ssaoMaterial.uniforms[ 'minDistance' ].value = this.minDistance;
  160. this.ssaoMaterial.uniforms[ 'maxDistance' ].value = this.maxDistance;
  161. this.renderPass( renderer, this.ssaoMaterial, this.ssaoRenderTarget );
  162. // render blur
  163. this.renderPass( renderer, this.blurMaterial, this.blurRenderTarget );
  164. // output result to screen
  165. switch ( this.output ) {
  166. case SSAOPass.OUTPUT.SSAO:
  167. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.ssaoRenderTarget.texture;
  168. this.copyMaterial.blending = NoBlending;
  169. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  170. break;
  171. case SSAOPass.OUTPUT.Blur:
  172. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.blurRenderTarget.texture;
  173. this.copyMaterial.blending = NoBlending;
  174. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  175. break;
  176. case SSAOPass.OUTPUT.Beauty:
  177. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  178. this.copyMaterial.blending = NoBlending;
  179. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  180. break;
  181. case SSAOPass.OUTPUT.Depth:
  182. this.renderPass( renderer, this.depthRenderMaterial, this.renderToScreen ? null : writeBuffer );
  183. break;
  184. case SSAOPass.OUTPUT.Normal:
  185. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
  186. this.copyMaterial.blending = NoBlending;
  187. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  188. break;
  189. case SSAOPass.OUTPUT.Default:
  190. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  191. this.copyMaterial.blending = NoBlending;
  192. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  193. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.blurRenderTarget.texture;
  194. this.copyMaterial.blending = CustomBlending;
  195. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  196. break;
  197. default:
  198. console.warn( 'THREE.SSAOPass: Unknown output type.' );
  199. }
  200. },
  201. renderPass: function ( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  202. // save original state
  203. renderer.getClearColor( this.originalClearColor );
  204. var originalClearAlpha = renderer.getClearAlpha();
  205. var originalAutoClear = renderer.autoClear;
  206. renderer.setRenderTarget( renderTarget );
  207. // setup pass state
  208. renderer.autoClear = false;
  209. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  210. renderer.setClearColor( clearColor );
  211. renderer.setClearAlpha( clearAlpha || 0.0 );
  212. renderer.clear();
  213. }
  214. this.fsQuad.material = passMaterial;
  215. this.fsQuad.render( renderer );
  216. // restore original state
  217. renderer.autoClear = originalAutoClear;
  218. renderer.setClearColor( this.originalClearColor );
  219. renderer.setClearAlpha( originalClearAlpha );
  220. },
  221. renderOverride: function ( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  222. renderer.getClearColor( this.originalClearColor );
  223. var originalClearAlpha = renderer.getClearAlpha();
  224. var originalAutoClear = renderer.autoClear;
  225. renderer.setRenderTarget( renderTarget );
  226. renderer.autoClear = false;
  227. clearColor = overrideMaterial.clearColor || clearColor;
  228. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  229. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  230. renderer.setClearColor( clearColor );
  231. renderer.setClearAlpha( clearAlpha || 0.0 );
  232. renderer.clear();
  233. }
  234. this.scene.overrideMaterial = overrideMaterial;
  235. renderer.render( this.scene, this.camera );
  236. this.scene.overrideMaterial = null;
  237. // restore original state
  238. renderer.autoClear = originalAutoClear;
  239. renderer.setClearColor( this.originalClearColor );
  240. renderer.setClearAlpha( originalClearAlpha );
  241. },
  242. setSize: function ( width, height ) {
  243. this.width = width;
  244. this.height = height;
  245. this.beautyRenderTarget.setSize( width, height );
  246. this.ssaoRenderTarget.setSize( width, height );
  247. this.normalRenderTarget.setSize( width, height );
  248. this.blurRenderTarget.setSize( width, height );
  249. this.ssaoMaterial.uniforms[ 'resolution' ].value.set( width, height );
  250. this.ssaoMaterial.uniforms[ 'cameraProjectionMatrix' ].value.copy( this.camera.projectionMatrix );
  251. this.ssaoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  252. this.blurMaterial.uniforms[ 'resolution' ].value.set( width, height );
  253. },
  254. generateSampleKernel: function () {
  255. var kernelSize = this.kernelSize;
  256. var kernel = this.kernel;
  257. for ( var i = 0; i < kernelSize; i ++ ) {
  258. var sample = new Vector3();
  259. sample.x = ( Math.random() * 2 ) - 1;
  260. sample.y = ( Math.random() * 2 ) - 1;
  261. sample.z = Math.random();
  262. sample.normalize();
  263. var scale = i / kernelSize;
  264. scale = MathUtils.lerp( 0.1, 1, scale * scale );
  265. sample.multiplyScalar( scale );
  266. kernel.push( sample );
  267. }
  268. },
  269. generateRandomKernelRotations: function () {
  270. var width = 4, height = 4;
  271. if ( SimplexNoise === undefined ) {
  272. console.error( 'THREE.SSAOPass: The pass relies on SimplexNoise.' );
  273. }
  274. var simplex = new SimplexNoise();
  275. var size = width * height;
  276. var data = new Float32Array( size * 4 );
  277. for ( var i = 0; i < size; i ++ ) {
  278. var stride = i * 4;
  279. var x = ( Math.random() * 2 ) - 1;
  280. var y = ( Math.random() * 2 ) - 1;
  281. var z = 0;
  282. var noise = simplex.noise3d( x, y, z );
  283. data[ stride ] = noise;
  284. data[ stride + 1 ] = noise;
  285. data[ stride + 2 ] = noise;
  286. data[ stride + 3 ] = 1;
  287. }
  288. this.noiseTexture = new DataTexture( data, width, height, RGBAFormat, FloatType );
  289. this.noiseTexture.wrapS = RepeatWrapping;
  290. this.noiseTexture.wrapT = RepeatWrapping;
  291. },
  292. overrideVisibility: function () {
  293. var scene = this.scene;
  294. var cache = this._visibilityCache;
  295. scene.traverse( function ( object ) {
  296. cache.set( object, object.visible );
  297. if ( object.isPoints || object.isLine ) object.visible = false;
  298. } );
  299. },
  300. restoreVisibility: function () {
  301. var scene = this.scene;
  302. var cache = this._visibilityCache;
  303. scene.traverse( function ( object ) {
  304. var visible = cache.get( object );
  305. object.visible = visible;
  306. } );
  307. cache.clear();
  308. }
  309. } );
  310. SSAOPass.OUTPUT = {
  311. 'Default': 0,
  312. 'SSAO': 1,
  313. 'Blur': 2,
  314. 'Beauty': 3,
  315. 'Depth': 4,
  316. 'Normal': 5
  317. };
  318. export { SSAOPass };