SSAOPass.js 11 KB

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