SAOPass.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * SAO implementation inspired from bhouston previous SAO work
  3. */
  4. THREE.SAOPass = function ( scene, camera, depthTexture, useNormals, resolution ) {
  5. THREE.Pass.call( this );
  6. this.scene = scene;
  7. this.camera = camera;
  8. this.clear = true;
  9. this.needsSwap = false;
  10. this.supportsDepthTextureExtension = ( depthTexture !== undefined ) ? depthTexture : false;
  11. this.supportsNormalTexture = ( useNormals !== undefined ) ? useNormals : false;
  12. this.originalClearColor = new THREE.Color();
  13. this._oldClearColor = new THREE.Color();
  14. this.oldClearAlpha = 1;
  15. this.params = {
  16. output: 0,
  17. saoBias: 0.5,
  18. saoIntensity: 0.18,
  19. saoScale: 1,
  20. saoKernelRadius: 100,
  21. saoMinResolution: 0,
  22. saoBlur: true,
  23. saoBlurRadius: 8,
  24. saoBlurStdDev: 4,
  25. saoBlurDepthCutoff: 0.01
  26. };
  27. this.resolution = ( resolution !== undefined ) ? new THREE.Vector2( resolution.x, resolution.y ) : new THREE.Vector2( 256, 256 );
  28. this.saoRenderTarget = new THREE.WebGLRenderTarget( this.resolution.x, this.resolution.y, {
  29. minFilter: THREE.LinearFilter,
  30. magFilter: THREE.LinearFilter,
  31. format: THREE.RGBAFormat
  32. } );
  33. this.blurIntermediateRenderTarget = this.saoRenderTarget.clone();
  34. this.beautyRenderTarget = this.saoRenderTarget.clone();
  35. this.normalRenderTarget = new THREE.WebGLRenderTarget( this.resolution.x, this.resolution.y, {
  36. minFilter: THREE.NearestFilter,
  37. magFilter: THREE.NearestFilter,
  38. format: THREE.RGBAFormat
  39. } );
  40. this.depthRenderTarget = this.normalRenderTarget.clone();
  41. if ( this.supportsDepthTextureExtension ) {
  42. var depthTexture = new THREE.DepthTexture();
  43. depthTexture.type = THREE.UnsignedShortType;
  44. this.beautyRenderTarget.depthTexture = depthTexture;
  45. this.beautyRenderTarget.depthBuffer = true;
  46. }
  47. this.depthMaterial = new THREE.MeshDepthMaterial();
  48. this.depthMaterial.depthPacking = THREE.RGBADepthPacking;
  49. this.depthMaterial.blending = THREE.NoBlending;
  50. this.normalMaterial = new THREE.MeshNormalMaterial();
  51. this.normalMaterial.blending = THREE.NoBlending;
  52. if ( THREE.SAOShader === undefined ) {
  53. console.error( 'THREE.SAOPass relies on THREE.SAOShader' );
  54. }
  55. this.saoMaterial = new THREE.ShaderMaterial( {
  56. defines: Object.assign( {}, THREE.SAOShader.defines ),
  57. fragmentShader: THREE.SAOShader.fragmentShader,
  58. vertexShader: THREE.SAOShader.vertexShader,
  59. uniforms: THREE.UniformsUtils.clone( THREE.SAOShader.uniforms )
  60. } );
  61. this.saoMaterial.extensions.derivatives = true;
  62. this.saoMaterial.defines[ 'DEPTH_PACKING' ] = this.supportsDepthTextureExtension ? 0 : 1;
  63. this.saoMaterial.defines[ 'NORMAL_TEXTURE' ] = this.supportsNormalTexture ? 1 : 0;
  64. this.saoMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  65. this.saoMaterial.uniforms[ 'tDepth' ].value = ( this.supportsDepthTextureExtension ) ? depthTexture : this.depthRenderTarget.texture;
  66. this.saoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
  67. this.saoMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  68. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  69. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  70. this.saoMaterial.blending = THREE.NoBlending;
  71. if ( THREE.DepthLimitedBlurShader === undefined ) {
  72. console.error( 'THREE.SAOPass relies on THREE.DepthLimitedBlurShader' );
  73. }
  74. this.vBlurMaterial = new THREE.ShaderMaterial( {
  75. uniforms: THREE.UniformsUtils.clone( THREE.DepthLimitedBlurShader.uniforms ),
  76. defines: Object.assign( {}, THREE.DepthLimitedBlurShader.defines ),
  77. vertexShader: THREE.DepthLimitedBlurShader.vertexShader,
  78. fragmentShader: THREE.DepthLimitedBlurShader.fragmentShader
  79. } );
  80. this.vBlurMaterial.defines[ 'DEPTH_PACKING' ] = this.supportsDepthTextureExtension ? 0 : 1;
  81. this.vBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  82. this.vBlurMaterial.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  83. this.vBlurMaterial.uniforms[ 'tDepth' ].value = ( this.supportsDepthTextureExtension ) ? depthTexture : this.depthRenderTarget.texture;
  84. this.vBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  85. this.vBlurMaterial.blending = THREE.NoBlending;
  86. this.hBlurMaterial = new THREE.ShaderMaterial( {
  87. uniforms: THREE.UniformsUtils.clone( THREE.DepthLimitedBlurShader.uniforms ),
  88. defines: Object.assign( {}, THREE.DepthLimitedBlurShader.defines ),
  89. vertexShader: THREE.DepthLimitedBlurShader.vertexShader,
  90. fragmentShader: THREE.DepthLimitedBlurShader.fragmentShader
  91. } );
  92. this.hBlurMaterial.defines[ 'DEPTH_PACKING' ] = this.supportsDepthTextureExtension ? 0 : 1;
  93. this.hBlurMaterial.defines[ 'PERSPECTIVE_CAMERA' ] = this.camera.isPerspectiveCamera ? 1 : 0;
  94. this.hBlurMaterial.uniforms[ 'tDiffuse' ].value = this.blurIntermediateRenderTarget.texture;
  95. this.hBlurMaterial.uniforms[ 'tDepth' ].value = ( this.supportsDepthTextureExtension ) ? depthTexture : this.depthRenderTarget.texture;
  96. this.hBlurMaterial.uniforms[ 'size' ].value.set( this.resolution.x, this.resolution.y );
  97. this.hBlurMaterial.blending = THREE.NoBlending;
  98. if ( THREE.CopyShader === undefined ) {
  99. console.error( 'THREE.SAOPass relies on THREE.CopyShader' );
  100. }
  101. this.materialCopy = new THREE.ShaderMaterial( {
  102. uniforms: THREE.UniformsUtils.clone( THREE.CopyShader.uniforms ),
  103. vertexShader: THREE.CopyShader.vertexShader,
  104. fragmentShader: THREE.CopyShader.fragmentShader,
  105. blending: THREE.NoBlending
  106. } );
  107. this.materialCopy.transparent = true;
  108. this.materialCopy.depthTest = false;
  109. this.materialCopy.depthWrite = false;
  110. this.materialCopy.blending = THREE.CustomBlending;
  111. this.materialCopy.blendSrc = THREE.DstColorFactor;
  112. this.materialCopy.blendDst = THREE.ZeroFactor;
  113. this.materialCopy.blendEquation = THREE.AddEquation;
  114. this.materialCopy.blendSrcAlpha = THREE.DstAlphaFactor;
  115. this.materialCopy.blendDstAlpha = THREE.ZeroFactor;
  116. this.materialCopy.blendEquationAlpha = THREE.AddEquation;
  117. if ( THREE.UnpackDepthRGBAShader === undefined ) {
  118. console.error( 'THREE.SAOPass relies on THREE.UnpackDepthRGBAShader' );
  119. }
  120. this.depthCopy = new THREE.ShaderMaterial( {
  121. uniforms: THREE.UniformsUtils.clone( THREE.UnpackDepthRGBAShader.uniforms ),
  122. vertexShader: THREE.UnpackDepthRGBAShader.vertexShader,
  123. fragmentShader: THREE.UnpackDepthRGBAShader.fragmentShader,
  124. blending: THREE.NoBlending
  125. } );
  126. this.fsQuad = new THREE.Pass.FullScreenQuad( null );
  127. };
  128. THREE.SAOPass.OUTPUT = {
  129. 'Beauty': 1,
  130. 'Default': 0,
  131. 'SAO': 2,
  132. 'Depth': 3,
  133. 'Normal': 4
  134. };
  135. THREE.SAOPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  136. constructor: THREE.SAOPass,
  137. render: function ( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  138. // Rendering readBuffer first when rendering to screen
  139. if ( this.renderToScreen ) {
  140. this.materialCopy.blending = THREE.NoBlending;
  141. this.materialCopy.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  142. this.materialCopy.needsUpdate = true;
  143. this.renderPass( renderer, this.materialCopy, null );
  144. }
  145. if ( this.params.output === 1 ) {
  146. return;
  147. }
  148. renderer.getClearColor( this._oldClearColor );
  149. this.oldClearAlpha = renderer.getClearAlpha();
  150. var oldAutoClear = renderer.autoClear;
  151. renderer.autoClear = false;
  152. renderer.setRenderTarget( this.depthRenderTarget );
  153. renderer.clear();
  154. this.saoMaterial.uniforms[ 'bias' ].value = this.params.saoBias;
  155. this.saoMaterial.uniforms[ 'intensity' ].value = this.params.saoIntensity;
  156. this.saoMaterial.uniforms[ 'scale' ].value = this.params.saoScale;
  157. this.saoMaterial.uniforms[ 'kernelRadius' ].value = this.params.saoKernelRadius;
  158. this.saoMaterial.uniforms[ 'minResolution' ].value = this.params.saoMinResolution;
  159. this.saoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  160. this.saoMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  161. // this.saoMaterial.uniforms['randomSeed'].value = Math.random();
  162. var depthCutoff = this.params.saoBlurDepthCutoff * ( this.camera.far - this.camera.near );
  163. this.vBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  164. this.hBlurMaterial.uniforms[ 'depthCutoff' ].value = depthCutoff;
  165. this.vBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  166. this.vBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  167. this.hBlurMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  168. this.hBlurMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  169. this.params.saoBlurRadius = Math.floor( this.params.saoBlurRadius );
  170. if ( ( this.prevStdDev !== this.params.saoBlurStdDev ) || ( this.prevNumSamples !== this.params.saoBlurRadius ) ) {
  171. THREE.BlurShaderUtils.configure( this.vBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new THREE.Vector2( 0, 1 ) );
  172. THREE.BlurShaderUtils.configure( this.hBlurMaterial, this.params.saoBlurRadius, this.params.saoBlurStdDev, new THREE.Vector2( 1, 0 ) );
  173. this.prevStdDev = this.params.saoBlurStdDev;
  174. this.prevNumSamples = this.params.saoBlurRadius;
  175. }
  176. // Rendering scene to depth texture
  177. renderer.setClearColor( 0x000000 );
  178. renderer.setRenderTarget( this.beautyRenderTarget );
  179. renderer.clear();
  180. renderer.render( this.scene, this.camera );
  181. // Re-render scene if depth texture extension is not supported
  182. if ( ! this.supportsDepthTextureExtension ) {
  183. // Clear rule : far clipping plane in both RGBA and Basic encoding
  184. this.renderOverride( renderer, this.depthMaterial, this.depthRenderTarget, 0x000000, 1.0 );
  185. }
  186. if ( this.supportsNormalTexture ) {
  187. // Clear rule : default normal is facing the camera
  188. this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
  189. }
  190. // Rendering SAO texture
  191. this.renderPass( renderer, this.saoMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  192. // Blurring SAO texture
  193. if ( this.params.saoBlur ) {
  194. this.renderPass( renderer, this.vBlurMaterial, this.blurIntermediateRenderTarget, 0xffffff, 1.0 );
  195. this.renderPass( renderer, this.hBlurMaterial, this.saoRenderTarget, 0xffffff, 1.0 );
  196. }
  197. var outputMaterial = this.materialCopy;
  198. // Setting up SAO rendering
  199. if ( this.params.output === 3 ) {
  200. if ( this.supportsDepthTextureExtension ) {
  201. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.depthTexture;
  202. this.materialCopy.needsUpdate = true;
  203. } else {
  204. this.depthCopy.uniforms[ 'tDiffuse' ].value = this.depthRenderTarget.texture;
  205. this.depthCopy.needsUpdate = true;
  206. outputMaterial = this.depthCopy;
  207. }
  208. } else if ( this.params.output === 4 ) {
  209. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
  210. this.materialCopy.needsUpdate = true;
  211. } else {
  212. this.materialCopy.uniforms[ 'tDiffuse' ].value = this.saoRenderTarget.texture;
  213. this.materialCopy.needsUpdate = true;
  214. }
  215. // Blending depends on output, only want a CustomBlending when showing SAO
  216. if ( this.params.output === 0 ) {
  217. outputMaterial.blending = THREE.CustomBlending;
  218. } else {
  219. outputMaterial.blending = THREE.NoBlending;
  220. }
  221. // Rendering SAOPass result on top of previous pass
  222. this.renderPass( renderer, outputMaterial, this.renderToScreen ? null : readBuffer );
  223. renderer.setClearColor( this._oldClearColor, this.oldClearAlpha );
  224. renderer.autoClear = oldAutoClear;
  225. },
  226. renderPass: function ( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  227. // save original state
  228. renderer.getClearColor( this.originalClearColor );
  229. var originalClearAlpha = renderer.getClearAlpha();
  230. var originalAutoClear = renderer.autoClear;
  231. renderer.setRenderTarget( renderTarget );
  232. // setup pass state
  233. renderer.autoClear = false;
  234. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  235. renderer.setClearColor( clearColor );
  236. renderer.setClearAlpha( clearAlpha || 0.0 );
  237. renderer.clear();
  238. }
  239. this.fsQuad.material = passMaterial;
  240. this.fsQuad.render( renderer );
  241. // restore original state
  242. renderer.autoClear = originalAutoClear;
  243. renderer.setClearColor( this.originalClearColor );
  244. renderer.setClearAlpha( originalClearAlpha );
  245. },
  246. renderOverride: function ( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  247. renderer.getClearColor( this.originalClearColor );
  248. var originalClearAlpha = renderer.getClearAlpha();
  249. var originalAutoClear = renderer.autoClear;
  250. renderer.setRenderTarget( renderTarget );
  251. renderer.autoClear = false;
  252. clearColor = overrideMaterial.clearColor || clearColor;
  253. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  254. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  255. renderer.setClearColor( clearColor );
  256. renderer.setClearAlpha( clearAlpha || 0.0 );
  257. renderer.clear();
  258. }
  259. this.scene.overrideMaterial = overrideMaterial;
  260. renderer.render( this.scene, this.camera );
  261. this.scene.overrideMaterial = null;
  262. // restore original state
  263. renderer.autoClear = originalAutoClear;
  264. renderer.setClearColor( this.originalClearColor );
  265. renderer.setClearAlpha( originalClearAlpha );
  266. },
  267. setSize: function ( width, height ) {
  268. this.beautyRenderTarget.setSize( width, height );
  269. this.saoRenderTarget.setSize( width, height );
  270. this.blurIntermediateRenderTarget.setSize( width, height );
  271. this.normalRenderTarget.setSize( width, height );
  272. this.depthRenderTarget.setSize( width, height );
  273. this.saoMaterial.uniforms[ 'size' ].value.set( width, height );
  274. this.saoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  275. this.saoMaterial.uniforms[ 'cameraProjectionMatrix' ].value = this.camera.projectionMatrix;
  276. this.saoMaterial.needsUpdate = true;
  277. this.vBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  278. this.vBlurMaterial.needsUpdate = true;
  279. this.hBlurMaterial.uniforms[ 'size' ].value.set( width, height );
  280. this.hBlurMaterial.needsUpdate = true;
  281. }
  282. } );