SSAARenderPass.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import {
  2. AdditiveBlending,
  3. Color,
  4. HalfFloatType,
  5. ShaderMaterial,
  6. UniformsUtils,
  7. WebGLRenderTarget
  8. } from 'three';
  9. import { Pass, FullScreenQuad } from './Pass.js';
  10. import { CopyShader } from '../shaders/CopyShader.js';
  11. /**
  12. * Supersample Anti-Aliasing Render Pass.
  13. *
  14. * This manual approach to SSAA re-renders the scene ones for each sample with camera jitter and accumulates the results.
  15. *
  16. * ```js
  17. * const ssaaRenderPass = new SSAARenderPass( scene, camera );
  18. * ssaaRenderPass.sampleLevel = 3;
  19. * composer.addPass( ssaaRenderPass );
  20. * ```
  21. *
  22. * @augments Pass
  23. */
  24. class SSAARenderPass extends Pass {
  25. /**
  26. * Constructs a new SSAA render pass.
  27. *
  28. * @param {Scene} scene - The scene to render.
  29. * @param {Camera} camera - The camera.
  30. * @param {?(number|Color|string)} [clearColor=0x000000] - The clear color of the render pass.
  31. * @param {?number} [clearAlpha=0] - The clear alpha of the render pass.
  32. */
  33. constructor( scene, camera, clearColor = 0x000000, clearAlpha = 0 ) {
  34. super();
  35. /**
  36. * The scene to render.
  37. *
  38. * @type {Scene}
  39. */
  40. this.scene = scene;
  41. /**
  42. * The camera.
  43. *
  44. * @type {Camera}
  45. */
  46. this.camera = camera;
  47. /**
  48. * The sample level. Specified as n, where the number of
  49. * samples is 2^n, so sampleLevel = 4, is 2^4 samples, 16.
  50. *
  51. * @type {number}
  52. * @default 4
  53. */
  54. this.sampleLevel = 4;
  55. /**
  56. * Whether the pass should be unbiased or not. This property has the most
  57. * visible effect when rendering to a RGBA8 buffer because it mitigates
  58. * rounding errors. By default RGBA16F is used.
  59. *
  60. * @type {boolean}
  61. * @default true
  62. */
  63. this.unbiased = true;
  64. /**
  65. * Whether to use a stencil buffer or not. This property can't
  66. * be changed after the first render.
  67. *
  68. * @type {boolean}
  69. * @default false
  70. */
  71. this.stencilBuffer = false;
  72. /**
  73. * The clear color of the render pass.
  74. *
  75. * @type {?(number|Color|string)}
  76. * @default 0x000000
  77. */
  78. this.clearColor = clearColor;
  79. /**
  80. * The clear alpha of the render pass.
  81. *
  82. * @type {?number}
  83. * @default 0
  84. */
  85. this.clearAlpha = clearAlpha;
  86. // internals
  87. this._sampleRenderTarget = null;
  88. this._oldClearColor = new Color();
  89. this._copyUniforms = UniformsUtils.clone( CopyShader.uniforms );
  90. this._copyMaterial = new ShaderMaterial( {
  91. uniforms: this._copyUniforms,
  92. vertexShader: CopyShader.vertexShader,
  93. fragmentShader: CopyShader.fragmentShader,
  94. transparent: true,
  95. depthTest: false,
  96. depthWrite: false,
  97. premultipliedAlpha: true,
  98. blending: AdditiveBlending
  99. } );
  100. this._fsQuad = new FullScreenQuad( this._copyMaterial );
  101. }
  102. /**
  103. * Frees the GPU-related resources allocated by this instance. Call this
  104. * method whenever the pass is no longer used in your app.
  105. */
  106. dispose() {
  107. if ( this._sampleRenderTarget ) {
  108. this._sampleRenderTarget.dispose();
  109. this._sampleRenderTarget = null;
  110. }
  111. this._copyMaterial.dispose();
  112. this._fsQuad.dispose();
  113. }
  114. /**
  115. * Sets the size of the pass.
  116. *
  117. * @param {number} width - The width to set.
  118. * @param {number} height - The width to set.
  119. */
  120. setSize( width, height ) {
  121. if ( this._sampleRenderTarget ) this._sampleRenderTarget.setSize( width, height );
  122. }
  123. /**
  124. * Performs the SSAA render pass.
  125. *
  126. * @param {WebGLRenderer} renderer - The renderer.
  127. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  128. * destination for the pass.
  129. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  130. * previous pass from this buffer.
  131. * @param {number} deltaTime - The delta time in seconds.
  132. * @param {boolean} maskActive - Whether masking is active or not.
  133. */
  134. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive */ ) {
  135. if ( ! this._sampleRenderTarget ) {
  136. this._sampleRenderTarget = new WebGLRenderTarget( readBuffer.width, readBuffer.height, { type: HalfFloatType, stencilBuffer: this.stencilBuffer } );
  137. this._sampleRenderTarget.texture.name = 'SSAARenderPass.sample';
  138. }
  139. const jitterOffsets = _JitterVectors[ Math.max( 0, Math.min( this.sampleLevel, 5 ) ) ];
  140. const autoClear = renderer.autoClear;
  141. renderer.autoClear = false;
  142. renderer.getClearColor( this._oldClearColor );
  143. const oldClearAlpha = renderer.getClearAlpha();
  144. const baseSampleWeight = 1.0 / jitterOffsets.length;
  145. const roundingRange = 1 / 32;
  146. this._copyUniforms[ 'tDiffuse' ].value = this._sampleRenderTarget.texture;
  147. const viewOffset = {
  148. fullWidth: readBuffer.width,
  149. fullHeight: readBuffer.height,
  150. offsetX: 0,
  151. offsetY: 0,
  152. width: readBuffer.width,
  153. height: readBuffer.height
  154. };
  155. const originalViewOffset = Object.assign( {}, this.camera.view );
  156. if ( originalViewOffset.enabled ) Object.assign( viewOffset, originalViewOffset );
  157. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  158. for ( let i = 0; i < jitterOffsets.length; i ++ ) {
  159. const jitterOffset = jitterOffsets[ i ];
  160. if ( this.camera.setViewOffset ) {
  161. this.camera.setViewOffset(
  162. viewOffset.fullWidth, viewOffset.fullHeight,
  163. viewOffset.offsetX + jitterOffset[ 0 ] * 0.0625, viewOffset.offsetY + jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  164. viewOffset.width, viewOffset.height
  165. );
  166. }
  167. let sampleWeight = baseSampleWeight;
  168. if ( this.unbiased ) {
  169. // the theory is that equal weights for each sample lead to an accumulation of rounding errors.
  170. // The following equation varies the sampleWeight per sample so that it is uniformly distributed
  171. // across a range of values whose rounding errors cancel each other out.
  172. const uniformCenteredDistribution = ( - 0.5 + ( i + 0.5 ) / jitterOffsets.length );
  173. sampleWeight += roundingRange * uniformCenteredDistribution;
  174. }
  175. this._copyUniforms[ 'opacity' ].value = sampleWeight;
  176. renderer.setClearColor( this.clearColor, this.clearAlpha );
  177. renderer.setRenderTarget( this._sampleRenderTarget );
  178. renderer.clear();
  179. renderer.render( this.scene, this.camera );
  180. renderer.setRenderTarget( this.renderToScreen ? null : writeBuffer );
  181. if ( i === 0 ) {
  182. renderer.setClearColor( 0x000000, 0.0 );
  183. renderer.clear();
  184. }
  185. this._fsQuad.render( renderer );
  186. }
  187. if ( this.camera.setViewOffset && originalViewOffset.enabled ) {
  188. this.camera.setViewOffset(
  189. originalViewOffset.fullWidth, originalViewOffset.fullHeight,
  190. originalViewOffset.offsetX, originalViewOffset.offsetY,
  191. originalViewOffset.width, originalViewOffset.height
  192. );
  193. } else if ( this.camera.clearViewOffset ) {
  194. this.camera.clearViewOffset();
  195. }
  196. renderer.autoClear = autoClear;
  197. renderer.setClearColor( this._oldClearColor, oldClearAlpha );
  198. }
  199. }
  200. // These jitter vectors are specified in integers because it is easier.
  201. // I am assuming a [-8,8) integer grid, but it needs to be mapped onto [-0.5,0.5)
  202. // before being used, thus these integers need to be scaled by 1/16.
  203. //
  204. // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
  205. const _JitterVectors = [
  206. [
  207. [ 0, 0 ]
  208. ],
  209. [
  210. [ 4, 4 ], [ - 4, - 4 ]
  211. ],
  212. [
  213. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  214. ],
  215. [
  216. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  217. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  218. ],
  219. [
  220. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  221. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  222. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  223. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  224. ],
  225. [
  226. [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ],
  227. [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ],
  228. [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ],
  229. [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ],
  230. [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ],
  231. [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ],
  232. [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ],
  233. [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ]
  234. ]
  235. ];
  236. export { SSAARenderPass };
粤ICP备19079148号