SSAARenderPass.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /** (超级采样抗锯齿)
  2. *这是一种“暴力”但效果最好的方法。(性能最差)
  3. 它将场景渲染到一个比屏幕大得多的分辨率上,然后再降采样到屏幕大小,从而获得极其平滑的边缘
  4. * Supersample Anti-Aliasing Render Pass
  5. *
  6. * @author bhouston / http://clara.io/
  7. *
  8. * This manual approach to SSAA re-renders the scene ones for each sample with camera jitter and accumulates the results.
  9. *
  10. * References: https://en.wikipedia.org/wiki/Supersampling
  11. *
  12. */
  13. import * as THREE from "../../../../libs/three.js/build/three.module.js";
  14. import {Pass, ShaderPass} from './ShaderPass'
  15. import CopyShader from './CopyShader'
  16. //较为原始的一种抗锯齿 (超级采样抗锯齿)
  17. let SSAARenderPass = function ( clearColor, clearAlpha ) {
  18. Pass.call( this );
  19. //this.scene //= scene;
  20. //this.camera = camera;
  21. this.sampleLevel = 4; // specified as n, where the number of samples is 2^n, so sampleLevel = 4, is 2^4 samples, 16.
  22. this.unbiased = true;
  23. // as we need to clear the buffer in this pass, clearColor must be set to something, defaults to black.
  24. this.clearColor = ( clearColor !== undefined ) ? clearColor : 0x000000;
  25. this.clearAlpha = ( clearAlpha !== undefined ) ? clearAlpha : 0;
  26. this.renderUniforms = {
  27. bgTex : {value:null},
  28. outlineTex : {value:null},
  29. opacity : {value:1},
  30. }
  31. this.renderMat = new THREE.ShaderMaterial({
  32. uniforms: this.renderUniforms,
  33. vertexShader: CopyShader.vertexShader,
  34. /* fragmentShader: CopyShader.fragmentShader, */
  35. fragmentShader: `
  36. uniform sampler2D bgTex;
  37. uniform sampler2D outlineTex;
  38. uniform float opacity;
  39. varying vec2 vUv;
  40. void main() {
  41. vec4 color1 = texture2D( bgTex, vUv );
  42. vec4 color2 = texture2D( outlineTex, vUv );
  43. gl_FragColor = opacity * mix(color1, color2, color2.a) ;
  44. }
  45. `,
  46. premultipliedAlpha: true,
  47. blending: THREE.AdditiveBlending,
  48. depthTest: false,
  49. depthWrite: false,
  50. transparent: true
  51. })
  52. this.renderMat2 = new THREE.ShaderMaterial({
  53. uniforms: THREE.UniformsUtils.clone(CopyShader.uniforms) ,
  54. vertexShader: CopyShader.vertexShader,
  55. fragmentShader:`uniform float opacity;
  56. uniform sampler2D tDiffuse;
  57. varying vec2 vUv;
  58. void main() {
  59. vec4 texel = texture2D( tDiffuse, vUv );
  60. if(texel.r == 0.0 && texel.g == 0.0 && texel.b == 0.0){
  61. discard;
  62. }else{
  63. gl_FragColor = opacity * texel;
  64. }
  65. }
  66. ` ,
  67. depthTest: false,
  68. depthWrite: false,
  69. transparent: true
  70. })
  71. ////////////////////
  72. /* this.renderMat.blendSrc = THREE.OneFactor //即将写入缓冲区的颜色。
  73. this.renderMat.blendDst = THREE.OneFactor //缓冲区已经存在的颜色
  74. this.renderMat.blendEquation = THREE.AddEquation;
  75. this.renderMat.blendEquationAlpha = THREE.AddEquation;
  76. this.renderMat.blendDstAlpha = THREE.SrcAlphaFactor
  77. this.renderMat.blendSrcAlpha = THREE.SrcAlphaFactor */
  78. this.camera2 = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  79. this.scene2 = new THREE.Scene();
  80. this.quad2 = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), this.renderMat/* this.copyMaterial */ );
  81. this.quad2.frustumCulled = false; // Avoid getting clipped
  82. this.scene2.add( this.quad2 );
  83. this.copyPass = new ShaderPass( CopyShader );
  84. this.copyPass.renderToScreen = true
  85. };
  86. SSAARenderPass.prototype = Object.assign( Object.create( Pass.prototype ), {
  87. constructor: SSAARenderPass,
  88. dispose: function () {
  89. if ( this.sampleRenderTarget ) {
  90. this.sampleRenderTarget.dispose();
  91. this.sampleRenderTarget = null;
  92. }
  93. },
  94. setSize: function ( width, height ) {
  95. if ( this.sampleRenderTarget ) this.sampleRenderTarget.setSize( width, height );
  96. this.childPass && this.childPass.setSize(width, height)
  97. },
  98. addPass: function (pass){
  99. this.childPass = pass;
  100. },
  101. render: function (scene, camera, viewports, renderer, writeBuffer, readBuffer, maskActive, renderFun ) {
  102. if(this.useCopy ){
  103. scene = this.copyPass.scene; camera = this.copyPass.camera;
  104. }
  105. if ( ! this.sampleRenderTarget ) {
  106. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat } );
  107. this.sampleRenderTarget.texture.name = "SSAARenderPass.sample";
  108. }
  109. var jitterOffsets = SSAARenderPass.JitterVectors[ Math.max( 0, Math.min( this.sampleLevel, 5 ) ) ];
  110. var autoClear = renderer.autoClear;
  111. renderer.autoClear = false;
  112. var oldClearColor = renderer.getClearColor(new THREE.Color).getHex();
  113. var oldClearAlpha = renderer.getClearAlpha();
  114. renderer.setClearColor( this.clearColor, this.clearAlpha );
  115. var baseSampleWeight = 1.0 / jitterOffsets.length;
  116. var roundingRange = 1 / 32;
  117. //this.copyUniforms[ "tDiffuse" ].value = this.sampleRenderTarget.texture;
  118. let oldTarget = renderer.getRenderTarget();
  119. if(oldTarget){
  120. if(oldTarget.scissorTest){
  121. var width = oldTarget.scissor.w, height = oldTarget.scissor.z
  122. }else{
  123. var width = oldTarget.width, height = oldTarget.height;
  124. }
  125. }else{
  126. var width = readBuffer.width, height = readBuffer.height;
  127. }
  128. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  129. let opa = 0
  130. for ( var i = 0; i < jitterOffsets.length; i ++ ) {
  131. var jitterOffset = jitterOffsets[ i ];
  132. if ( camera.setViewOffset ) {
  133. camera.setViewOffset( width, height,
  134. jitterOffset[ 0 ] * 0.0625 , jitterOffset[ 1 ] * 0.0625 , // 0.0625 = 1 / 16
  135. width, height );
  136. }
  137. var sampleWeight = baseSampleWeight;
  138. if ( this.unbiased ) {//更柔和
  139. var uniformCenteredDistribution = ( - 0.5 + ( i + 0.5 ) / jitterOffsets.length );
  140. sampleWeight += roundingRange * uniformCenteredDistribution;
  141. }
  142. renderer.setRenderTarget(this.sampleRenderTarget)
  143. renderer.clear()
  144. if(this.useCopy){
  145. this.copyPass.render(scene,camera, null,renderer, writeBuffer, readBuffer )
  146. }else{
  147. if(renderFun){
  148. renderFun({target : this.sampleRenderTarget})
  149. }else{
  150. renderer.render( scene, camera );
  151. }
  152. }
  153. renderer.setRenderTarget(oldTarget)
  154. //---------------------
  155. //获取outline tex
  156. let hasOutline = this.childPass && this.childPass.render(scene, camera, renderer, writeBuffer, readBuffer, null, renderFun )
  157. //合成到该材质
  158. this.renderUniforms[ "bgTex" ].value = this.sampleRenderTarget.texture;
  159. this.renderUniforms[ "outlineTex" ].value = hasOutline ? readBuffer.texture : null
  160. this.renderUniforms[ "opacity" ].value = sampleWeight;
  161. /* console.log('sampleWeight', sampleWeight)
  162. opa += sampleWeight */
  163. if(!this.renderToScreen){
  164. renderer.setRenderTarget(writeBuffer)
  165. }
  166. if(i === 0 ){
  167. renderer.setClearColor( 0x000000, 0 ); //叠加前颜色必须0
  168. renderer.clear()
  169. }
  170. renderer.render( this.scene2, this.camera2); // , this.renderToScreen ? null : writeBuffer, ( i === 0 )
  171. if(!this.renderToScreen){
  172. renderer.setRenderTarget(oldTarget)
  173. }
  174. //if(i==2)break;
  175. }
  176. //console.log('sum:',opa)
  177. if ( camera.clearViewOffset )camera.clearViewOffset();
  178. //renderer.setRenderTarget(readBuffer)
  179. //renderer.setClearColor( 0x000000, 0 );
  180. //renderer.clear()
  181. /* this.quad2.material = this.renderMat2
  182. this.renderMat2.uniforms.tDiffuse.value = writeBuffer.texture;
  183. renderer.render( this.scene2, this.camera2);
  184. this.quad2.material = this.renderMat */
  185. //renderer.setRenderTarget(oldTarget)
  186. renderer.autoClear = autoClear;
  187. renderer.setClearColor( oldClearColor, oldClearAlpha );
  188. /* 试了好几次,测量线的透明度还是还原不了。 clearAlpha十分影响结果。
  189. 因为绘制测量线需要背景透明。 或许可以先全部绘制完后,再 copyshader中 抗锯齿?
  190. 另外会有黑边。
  191. */
  192. }
  193. } );
  194. // These jitter vectors are specified in integers because it is easier.
  195. // I am assuming a [-8,8) integer grid, but it needs to be mapped onto [-0.5,0.5)
  196. // before being used, thus these integers need to be scaled by 1/16.
  197. //
  198. // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
  199. SSAARenderPass.JitterVectors = [
  200. [
  201. [ 0, 0 ]
  202. ],
  203. [
  204. [ 4, 4 ], [ - 4, - 4 ]
  205. ],
  206. [
  207. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  208. ],
  209. [
  210. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  211. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  212. ],
  213. [
  214. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  215. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  216. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  217. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  218. ],
  219. [
  220. [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ],
  221. [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ],
  222. [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ],
  223. [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ],
  224. [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ],
  225. [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ],
  226. [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ],
  227. [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ]
  228. ]
  229. ];
  230. //锯齿效果见 https://threejs.org/examples/?q=ssaa#webgl_postprocessing_ssaa
  231. export default SSAARenderPass