SSAOPass.js 12 KB

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