SSAAPassNode.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import { AdditiveBlending, Color, Vector2, PassNode, QuadMesh, NodeMaterial } from 'three/webgpu';
  2. import { uniform, mrt, texture, getTextureIndex, unpremultiplyAlpha } from 'three/tsl';
  3. const _size = /*@__PURE__*/ new Vector2();
  4. const _clearColor = /*@__PURE__*/ new Color();
  5. /**
  6. * A special render pass node that renders the scene with SSAA (Supersampling Anti-Aliasing).
  7. * This manual SSAA approach re-renders the scene ones for each sample with camera jitter and accumulates the results.
  8. *
  9. * This node produces a high-quality anti-aliased output but is also extremely expensive because of
  10. * its brute-force approach of re-rendering the entire scene multiple times.
  11. *
  12. * Reference: {@link https://en.wikipedia.org/wiki/Supersampling}
  13. *
  14. * @augments PassNode
  15. * @three_import import { ssaaPass } from 'three/addons/tsl/display/SSAAPassNode.js';
  16. */
  17. class SSAAPassNode extends PassNode {
  18. static get type() {
  19. return 'SSAAPassNode';
  20. }
  21. /**
  22. * Constructs a new SSAA pass node.
  23. *
  24. * @param {Scene} scene - The scene to render.
  25. * @param {Camera} camera - The camera to render the scene with.
  26. */
  27. constructor( scene, camera ) {
  28. super( PassNode.COLOR, scene, camera );
  29. /**
  30. * This flag can be used for type testing.
  31. *
  32. * @type {boolean}
  33. * @readonly
  34. * @default true
  35. */
  36. this.isSSAAPassNode = true;
  37. /**
  38. * The sample level specified as n, where the number of samples is 2^n,
  39. * so sampleLevel = 4, is 2^4 samples, 16.
  40. *
  41. * @type {number}
  42. * @default 4
  43. */
  44. this.sampleLevel = 4;
  45. /**
  46. * Whether rounding errors should be mitigated or not.
  47. *
  48. * @type {boolean}
  49. * @default true
  50. */
  51. this.unbiased = true;
  52. /**
  53. * A uniform node representing the sample weight.
  54. *
  55. * @type {UniformNode<float>}
  56. * @default 1
  57. */
  58. this.sampleWeight = uniform( 1 );
  59. /**
  60. * Reference to the internal render target that holds the current sample.
  61. *
  62. * @private
  63. * @type {?RenderTarget}
  64. * @default null
  65. */
  66. this._sampleRenderTarget = null;
  67. /**
  68. * Reference to the internal quad mesh.
  69. *
  70. * @private
  71. * @type {QuadMesh}
  72. */
  73. this._quadMesh = new QuadMesh();
  74. }
  75. /**
  76. * This method is used to render the SSAA effect once per frame.
  77. *
  78. * @param {NodeFrame} frame - The current node frame.
  79. */
  80. updateBefore( frame ) {
  81. const { renderer } = frame;
  82. const { scene, camera } = this;
  83. //
  84. this._pixelRatio = renderer.getPixelRatio();
  85. const size = renderer.getSize( _size );
  86. this.setSize( size.width, size.height );
  87. this._sampleRenderTarget.setSize( this.renderTarget.width, this.renderTarget.height );
  88. //
  89. this._cameraNear.value = camera.near;
  90. this._cameraFar.value = camera.far;
  91. const currentRenderTarget = renderer.getRenderTarget();
  92. const currentMRT = renderer.getMRT();
  93. const currentAutoClear = renderer.autoClear;
  94. const currentClearColor = renderer.getClearColor( _clearColor );
  95. const currentClearAlpha = renderer.getClearAlpha();
  96. renderer.setMRT( this.getMRT() );
  97. renderer.autoClear = false;
  98. const jitterOffsets = _JitterVectors[ Math.max( 0, Math.min( this.sampleLevel, 5 ) ) ];
  99. const baseSampleWeight = 1.0 / jitterOffsets.length;
  100. const roundingRange = 1 / 32;
  101. const viewOffset = {
  102. fullWidth: this.renderTarget.width,
  103. fullHeight: this.renderTarget.height,
  104. offsetX: 0,
  105. offsetY: 0,
  106. width: this.renderTarget.width,
  107. height: this.renderTarget.height
  108. };
  109. const originalViewOffset = Object.assign( {}, camera.view );
  110. if ( originalViewOffset.enabled ) Object.assign( viewOffset, originalViewOffset );
  111. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  112. for ( let i = 0; i < jitterOffsets.length; i ++ ) {
  113. const jitterOffset = jitterOffsets[ i ];
  114. if ( camera.setViewOffset ) {
  115. camera.setViewOffset(
  116. viewOffset.fullWidth, viewOffset.fullHeight,
  117. viewOffset.offsetX + jitterOffset[ 0 ] * 0.0625, viewOffset.offsetY + jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  118. viewOffset.width, viewOffset.height
  119. );
  120. }
  121. this.sampleWeight.value = baseSampleWeight;
  122. if ( this.unbiased ) {
  123. // the theory is that equal weights for each sample lead to an accumulation of rounding errors.
  124. // The following equation varies the sampleWeight per sample so that it is uniformly distributed
  125. // across a range of values whose rounding errors cancel each other out.
  126. const uniformCenteredDistribution = ( - 0.5 + ( i + 0.5 ) / jitterOffsets.length );
  127. this.sampleWeight.value += roundingRange * uniformCenteredDistribution;
  128. }
  129. renderer.setRenderTarget( this._sampleRenderTarget );
  130. renderer.clear();
  131. renderer.render( scene, camera );
  132. // accumulation
  133. renderer.setRenderTarget( this.renderTarget );
  134. if ( i === 0 ) {
  135. renderer.setClearColor( 0x000000, 0.0 );
  136. renderer.clear();
  137. renderer.setClearColor( currentClearColor, currentClearAlpha );
  138. }
  139. this._quadMesh.render( renderer );
  140. }
  141. renderer.copyTextureToTexture( this._sampleRenderTarget.depthTexture, this.renderTarget.depthTexture );
  142. // restore
  143. if ( camera.setViewOffset && originalViewOffset.enabled ) {
  144. camera.setViewOffset(
  145. originalViewOffset.fullWidth, originalViewOffset.fullHeight,
  146. originalViewOffset.offsetX, originalViewOffset.offsetY,
  147. originalViewOffset.width, originalViewOffset.height
  148. );
  149. } else if ( camera.clearViewOffset ) {
  150. camera.clearViewOffset();
  151. }
  152. renderer.setRenderTarget( currentRenderTarget );
  153. renderer.setMRT( currentMRT );
  154. renderer.autoClear = currentAutoClear;
  155. }
  156. /**
  157. * This method is used to setup the effect's MRT configuration and quad mesh.
  158. *
  159. * @param {NodeBuilder} builder - The current node builder.
  160. * @return {PassTextureNode}
  161. */
  162. setup( builder ) {
  163. if ( this._sampleRenderTarget === null ) {
  164. this._sampleRenderTarget = this.renderTarget.clone();
  165. }
  166. let sampleTexture;
  167. const passMRT = this.getMRT();
  168. if ( passMRT !== null ) {
  169. const outputs = {};
  170. for ( const name in passMRT.outputNodes ) {
  171. const index = getTextureIndex( this._sampleRenderTarget.textures, name );
  172. if ( index >= 0 ) {
  173. outputs[ name ] = texture( this._sampleRenderTarget.textures[ index ] ).mul( this.sampleWeight );
  174. }
  175. }
  176. sampleTexture = mrt( outputs );
  177. } else {
  178. sampleTexture = texture( this._sampleRenderTarget.texture ).mul( this.sampleWeight );
  179. }
  180. this._quadMesh.material = new NodeMaterial();
  181. this._quadMesh.material.fragmentNode = unpremultiplyAlpha( sampleTexture );
  182. this._quadMesh.material.transparent = true;
  183. this._quadMesh.material.depthTest = false;
  184. this._quadMesh.material.depthWrite = false;
  185. this._quadMesh.material.premultipliedAlpha = true;
  186. this._quadMesh.material.blending = AdditiveBlending;
  187. this._quadMesh.material.name = 'SSAA';
  188. return super.setup( builder );
  189. }
  190. /**
  191. * Frees internal resources. This method should be called
  192. * when the pass is no longer required.
  193. */
  194. dispose() {
  195. super.dispose();
  196. if ( this._sampleRenderTarget !== null ) {
  197. this._sampleRenderTarget.dispose();
  198. }
  199. }
  200. }
  201. export default SSAAPassNode;
  202. // These jitter vectors are specified in integers because it is easier.
  203. // I am assuming a [-8,8) integer grid, but it needs to be mapped onto [-0.5,0.5)
  204. // before being used, thus these integers need to be scaled by 1/16.
  205. //
  206. // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
  207. const _JitterVectors = [
  208. [
  209. [ 0, 0 ]
  210. ],
  211. [
  212. [ 4, 4 ], [ - 4, - 4 ]
  213. ],
  214. [
  215. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  216. ],
  217. [
  218. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  219. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  220. ],
  221. [
  222. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  223. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  224. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  225. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  226. ],
  227. [
  228. [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ],
  229. [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ],
  230. [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ],
  231. [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ],
  232. [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ],
  233. [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ],
  234. [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ],
  235. [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ]
  236. ]
  237. ];
  238. /**
  239. * TSL function for creating a SSAA pass node for Supersampling Anti-Aliasing.
  240. *
  241. * @tsl
  242. * @function
  243. * @param {Scene} scene - The scene to render.
  244. * @param {Camera} camera - The camera to render the scene with.
  245. * @returns {SSAAPassNode}
  246. */
  247. export const ssaaPass = ( scene, camera ) => new SSAAPassNode( scene, camera );
粤ICP备19079148号