SSAOPass.js 12 KB

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