SSAOPass.js 12 KB

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