SSAOPass.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 = 32;
  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, deltaTime, maskActive */ ) {
  130. // render beauty and depth
  131. renderer.setRenderTarget( this.beautyRenderTarget );
  132. renderer.clear();
  133. renderer.render( this.scene, this.camera );
  134. // render normals
  135. this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
  136. // render SSAO
  137. this.ssaoMaterial.uniforms[ 'kernelRadius' ].value = this.kernelRadius;
  138. this.ssaoMaterial.uniforms[ 'minDistance' ].value = this.minDistance;
  139. this.ssaoMaterial.uniforms[ 'maxDistance' ].value = this.maxDistance;
  140. this.renderPass( renderer, this.ssaoMaterial, this.ssaoRenderTarget );
  141. // render blur
  142. this.renderPass( renderer, this.blurMaterial, this.blurRenderTarget );
  143. // output result to screen
  144. switch ( this.output ) {
  145. case THREE.SSAOPass.OUTPUT.SSAO:
  146. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.ssaoRenderTarget.texture;
  147. this.copyMaterial.blending = THREE.NoBlending;
  148. this.renderPass( renderer, this.copyMaterial, null );
  149. break;
  150. case THREE.SSAOPass.OUTPUT.Blur:
  151. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.blurRenderTarget.texture;
  152. this.copyMaterial.blending = THREE.NoBlending;
  153. this.renderPass( renderer, this.copyMaterial, null );
  154. break;
  155. case THREE.SSAOPass.OUTPUT.Beauty:
  156. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  157. this.copyMaterial.blending = THREE.NoBlending;
  158. this.renderPass( renderer, this.copyMaterial, null );
  159. break;
  160. case THREE.SSAOPass.OUTPUT.Depth:
  161. this.renderPass( renderer, this.depthRenderMaterial, null );
  162. break;
  163. case THREE.SSAOPass.OUTPUT.Normal:
  164. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
  165. this.copyMaterial.blending = THREE.NoBlending;
  166. this.renderPass( renderer, this.copyMaterial, null );
  167. break;
  168. case THREE.SSAOPass.OUTPUT.Default:
  169. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  170. this.copyMaterial.blending = THREE.NoBlending;
  171. this.renderPass( renderer, this.copyMaterial, null );
  172. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.blurRenderTarget.texture;
  173. this.copyMaterial.blending = THREE.CustomBlending;
  174. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  175. break;
  176. default:
  177. console.warn( 'THREE.SSAOPass: Unknown output type.' );
  178. }
  179. },
  180. renderPass: function ( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  181. // save original state
  182. this.originalClearColor.copy( renderer.getClearColor() );
  183. var originalClearAlpha = renderer.getClearAlpha();
  184. var originalAutoClear = renderer.autoClear;
  185. renderer.setRenderTarget( renderTarget );
  186. // setup pass state
  187. renderer.autoClear = false;
  188. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  189. renderer.setClearColor( clearColor );
  190. renderer.setClearAlpha( clearAlpha || 0.0 );
  191. renderer.clear();
  192. }
  193. this.quad.material = passMaterial;
  194. renderer.render( this.quadScene, this.quadCamera );
  195. // restore original state
  196. renderer.autoClear = originalAutoClear;
  197. renderer.setClearColor( this.originalClearColor );
  198. renderer.setClearAlpha( originalClearAlpha );
  199. },
  200. renderOverride: function ( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  201. this.originalClearColor.copy( renderer.getClearColor() );
  202. var originalClearAlpha = renderer.getClearAlpha();
  203. var originalAutoClear = renderer.autoClear;
  204. renderer.setRenderTarget( renderTarget );
  205. renderer.autoClear = false;
  206. clearColor = overrideMaterial.clearColor || clearColor;
  207. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  208. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  209. renderer.setClearColor( clearColor );
  210. renderer.setClearAlpha( clearAlpha || 0.0 );
  211. renderer.clear();
  212. }
  213. this.scene.overrideMaterial = overrideMaterial;
  214. renderer.render( this.scene, this.camera );
  215. this.scene.overrideMaterial = null;
  216. // restore original state
  217. renderer.autoClear = originalAutoClear;
  218. renderer.setClearColor( this.originalClearColor );
  219. renderer.setClearAlpha( originalClearAlpha );
  220. },
  221. setSize: function ( width, height ) {
  222. this.width = width;
  223. this.height = height;
  224. this.beautyRenderTarget.setSize( width, height );
  225. this.ssaoRenderTarget.setSize( width, height );
  226. this.normalRenderTarget.setSize( width, height );
  227. this.blurRenderTarget.setSize( width, height );
  228. this.ssaoMaterial.uniforms[ 'resolution' ].value.set( width, height );
  229. this.ssaoMaterial.uniforms[ 'cameraProjectionMatrix' ].value.copy( this.camera.projectionMatrix );
  230. this.ssaoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.getInverse( this.camera.projectionMatrix );
  231. this.blurMaterial.uniforms[ 'resolution' ].value.set( width, height );
  232. },
  233. generateSampleKernel: function () {
  234. var kernelSize = this.kernelSize;
  235. var kernel = this.kernel;
  236. for ( var i = 0; i < kernelSize; i ++ ) {
  237. var sample = new THREE.Vector3();
  238. sample.x = ( Math.random() * 2 ) - 1;
  239. sample.y = ( Math.random() * 2 ) - 1;
  240. sample.z = Math.random();
  241. sample.normalize();
  242. var scale = i / kernelSize;
  243. scale = THREE.Math.lerp( 0.1, 1, scale * scale );
  244. sample.multiplyScalar( scale );
  245. kernel.push( sample );
  246. }
  247. },
  248. generateRandomKernelRotations: function () {
  249. var width = 4, height = 4;
  250. if ( SimplexNoise === undefined ) {
  251. console.error( 'THREE.SSAOPass: The pass relies on THREE.SimplexNoise.' );
  252. }
  253. var simplex = new SimplexNoise();
  254. var size = width * height;
  255. var data = new Float32Array( size * 4 );
  256. for ( var i = 0; i < size; i ++ ) {
  257. var stride = i * 4;
  258. var x = ( Math.random() * 2 ) - 1;
  259. var y = ( Math.random() * 2 ) - 1;
  260. var z = 0;
  261. var noise = simplex.noise3d( x, y, z );
  262. data[ stride ] = noise;
  263. data[ stride + 1 ] = noise;
  264. data[ stride + 2 ] = noise;
  265. data[ stride + 3 ] = 1;
  266. }
  267. this.noiseTexture = new THREE.DataTexture( data, width, height, THREE.RGBAFormat, THREE.FloatType );
  268. this.noiseTexture.wrapS = THREE.RepeatWrapping;
  269. this.noiseTexture.wrapT = THREE.RepeatWrapping;
  270. this.noiseTexture.needsUpdate = true;
  271. }
  272. } );
  273. THREE.SSAOPass.OUTPUT = {
  274. 'Default': 0,
  275. 'SSAO': 1,
  276. 'Blur': 2,
  277. 'Beauty': 3,
  278. 'Depth': 4,
  279. 'Normal': 5
  280. };