SSAOPass.js 12 KB

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