GTAONode.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. import { DataTexture, RenderTarget, RepeatWrapping, Vector2, Vector3, TempNode, QuadMesh, NodeMaterial, RendererUtils } from 'three/webgpu';
  2. import { reference, logarithmicDepthToViewZ, viewZToPerspectiveDepth, getNormalFromDepth, getScreenPosition, getViewPosition, nodeObject, Fn, float, NodeUpdateType, uv, uniform, Loop, vec2, vec3, vec4, int, dot, max, pow, abs, If, textureSize, sin, cos, PI, texture, passTexture, mat3, add, normalize, mul, cross, div, mix, sqrt, sub, acos, clamp } from 'three/tsl';
  3. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  4. const _size = /*@__PURE__*/ new Vector2();
  5. let _rendererState;
  6. /**
  7. * Post processing node for applying Ground Truth Ambient Occlusion (GTAO) to a scene.
  8. * ```js
  9. * const postProcessing = new THREE.PostProcessing( renderer );
  10. *
  11. * const scenePass = pass( scene, camera );
  12. * scenePass.setMRT( mrt( {
  13. * output: output,
  14. * normal: normalView
  15. * } ) );
  16. *
  17. * const scenePassColor = scenePass.getTextureNode( 'output' );
  18. * const scenePassNormal = scenePass.getTextureNode( 'normal' );
  19. * const scenePassDepth = scenePass.getTextureNode( 'depth' );
  20. *
  21. * const aoPass = ao( scenePassDepth, scenePassNormal, camera );
  22. *
  23. * postProcessing.outputNod = aoPass.getTextureNode().mul( scenePassColor );
  24. * ```
  25. *
  26. * Reference: {@link https://www.activision.com/cdn/research/Practical_Real_Time_Strategies_for_Accurate_Indirect_Occlusion_NEW%20VERSION_COLOR.pdf}.
  27. *
  28. * @augments TempNode
  29. * @three_import import { ao } from 'three/addons/tsl/display/GTAONode.js';
  30. */
  31. class GTAONode extends TempNode {
  32. static get type() {
  33. return 'GTAONode';
  34. }
  35. /**
  36. * Constructs a new GTAO node.
  37. *
  38. * @param {Node<float>} depthNode - A node that represents the scene's depth.
  39. * @param {?Node<vec3>} normalNode - A node that represents the scene's normals.
  40. * @param {Camera} camera - The camera the scene is rendered with.
  41. */
  42. constructor( depthNode, normalNode, camera ) {
  43. super( 'vec4' );
  44. /**
  45. * A node that represents the scene's depth.
  46. *
  47. * @type {Node<float>}
  48. */
  49. this.depthNode = depthNode;
  50. /**
  51. * A node that represents the scene's normals. If no normals are passed to the
  52. * constructor (because MRT is not available), normals can be automatically
  53. * reconstructed from depth values in the shader.
  54. *
  55. * @type {?Node<vec3>}
  56. */
  57. this.normalNode = normalNode;
  58. /**
  59. * The resolution scale. By default the effect is rendered in full resolution
  60. * for best quality but a value of `0.5` should be sufficient for most scenes.
  61. *
  62. * @type {number}
  63. * @default 1
  64. */
  65. this.resolutionScale = 1;
  66. /**
  67. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  68. * its effect once per frame in `updateBefore()`.
  69. *
  70. * @type {string}
  71. * @default 'frame'
  72. */
  73. this.updateBeforeType = NodeUpdateType.FRAME;
  74. /**
  75. * The render target the ambient occlusion is rendered into.
  76. *
  77. * @private
  78. * @type {RenderTarget}
  79. */
  80. this._aoRenderTarget = new RenderTarget( 1, 1, { depthBuffer: false } );
  81. this._aoRenderTarget.texture.name = 'GTAONode.AO';
  82. // uniforms
  83. /**
  84. * The radius of the ambient occlusion.
  85. *
  86. * @type {UniformNode<float>}
  87. */
  88. this.radius = uniform( 0.25 );
  89. /**
  90. * The resolution of the effect. Can be scaled via
  91. * `resolutionScale`.
  92. *
  93. * @type {UniformNode<vec2>}
  94. */
  95. this.resolution = uniform( new Vector2() );
  96. /**
  97. * The thickness of the ambient occlusion.
  98. *
  99. * @type {UniformNode<float>}
  100. */
  101. this.thickness = uniform( 1 );
  102. /**
  103. * Another option to tweak the occlusion. The recommended range is
  104. * `[1,2]` for attenuating the AO.
  105. *
  106. * @type {UniformNode<float>}
  107. */
  108. this.distanceExponent = uniform( 1 );
  109. /**
  110. * The distance fall off value of the ambient occlusion.
  111. * A lower value leads to a larger AO effect. The value
  112. * should lie in the range `[0,1]`.
  113. *
  114. * @type {UniformNode<float>}
  115. */
  116. this.distanceFallOff = uniform( 1 );
  117. /**
  118. * The scale of the ambient occlusion.
  119. *
  120. * @type {UniformNode<float>}
  121. */
  122. this.scale = uniform( 1 );
  123. /**
  124. * How many samples are used to compute the AO.
  125. * A higher value results in better quality but also
  126. * in a more expensive runtime behavior.
  127. *
  128. * @type {UniformNode<float>}
  129. */
  130. this.samples = uniform( 16 );
  131. /**
  132. * The node represents the internal noise texture used by the AO.
  133. *
  134. * @private
  135. * @type {TextureNode}
  136. */
  137. this._noiseNode = texture( generateMagicSquareNoise() );
  138. /**
  139. * Represents the projection matrix of the scene's camera.
  140. *
  141. * @private
  142. * @type {UniformNode<mat4>}
  143. */
  144. this._cameraProjectionMatrix = uniform( camera.projectionMatrix );
  145. /**
  146. * Represents the inverse projection matrix of the scene's camera.
  147. *
  148. * @private
  149. * @type {UniformNode<mat4>}
  150. */
  151. this._cameraProjectionMatrixInverse = uniform( camera.projectionMatrixInverse );
  152. /**
  153. * Represents the near value of the scene's camera.
  154. *
  155. * @private
  156. * @type {ReferenceNode<float>}
  157. */
  158. this._cameraNear = reference( 'near', 'float', camera );
  159. /**
  160. * Represents the far value of the scene's camera.
  161. *
  162. * @private
  163. * @type {ReferenceNode<float>}
  164. */
  165. this._cameraFar = reference( 'far', 'float', camera );
  166. /**
  167. * The material that is used to render the effect.
  168. *
  169. * @private
  170. * @type {NodeMaterial}
  171. */
  172. this._material = new NodeMaterial();
  173. this._material.name = 'GTAO';
  174. /**
  175. * The result of the effect is represented as a separate texture node.
  176. *
  177. * @private
  178. * @type {PassTextureNode}
  179. */
  180. this._textureNode = passTexture( this, this._aoRenderTarget.texture );
  181. }
  182. /**
  183. * Returns the result of the effect as a texture node.
  184. *
  185. * @return {PassTextureNode} A texture node that represents the result of the effect.
  186. */
  187. getTextureNode() {
  188. return this._textureNode;
  189. }
  190. /**
  191. * Sets the size of the effect.
  192. *
  193. * @param {number} width - The width of the effect.
  194. * @param {number} height - The height of the effect.
  195. */
  196. setSize( width, height ) {
  197. width = Math.round( this.resolutionScale * width );
  198. height = Math.round( this.resolutionScale * height );
  199. this.resolution.value.set( width, height );
  200. this._aoRenderTarget.setSize( width, height );
  201. }
  202. /**
  203. * This method is used to render the effect once per frame.
  204. *
  205. * @param {NodeFrame} frame - The current node frame.
  206. */
  207. updateBefore( frame ) {
  208. const { renderer } = frame;
  209. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  210. //
  211. const size = renderer.getDrawingBufferSize( _size );
  212. this.setSize( size.width, size.height );
  213. _quadMesh.material = this._material;
  214. // clear
  215. renderer.setClearColor( 0xffffff, 1 );
  216. // ao
  217. renderer.setRenderTarget( this._aoRenderTarget );
  218. _quadMesh.render( renderer );
  219. // restore
  220. RendererUtils.restoreRendererState( renderer, _rendererState );
  221. }
  222. /**
  223. * This method is used to setup the effect's TSL code.
  224. *
  225. * @param {NodeBuilder} builder - The current node builder.
  226. * @return {PassTextureNode}
  227. */
  228. setup( builder ) {
  229. const uvNode = uv();
  230. const sampleDepth = ( uv ) => {
  231. const depth = this.depthNode.sample( uv ).r;
  232. if ( builder.renderer.logarithmicDepthBuffer === true ) {
  233. const viewZ = logarithmicDepthToViewZ( depth, this._cameraNear, this._cameraFar );
  234. return viewZToPerspectiveDepth( viewZ, this._cameraNear, this._cameraFar );
  235. }
  236. return depth;
  237. };
  238. const sampleNoise = ( uv ) => this._noiseNode.sample( uv );
  239. const sampleNormal = ( uv ) => ( this.normalNode !== null ) ? this.normalNode.sample( uv ).rgb.normalize() : getNormalFromDepth( uv, this.depthNode.value, this._cameraProjectionMatrixInverse );
  240. const ao = Fn( () => {
  241. const depth = sampleDepth( uvNode ).toVar();
  242. depth.greaterThanEqual( 1.0 ).discard();
  243. const viewPosition = getViewPosition( uvNode, depth, this._cameraProjectionMatrixInverse ).toVar();
  244. const viewNormal = sampleNormal( uvNode ).toVar();
  245. const radiusToUse = this.radius;
  246. const noiseResolution = textureSize( this._noiseNode, 0 );
  247. let noiseUv = vec2( uvNode.x, uvNode.y.oneMinus() );
  248. noiseUv = noiseUv.mul( this.resolution.div( noiseResolution ) );
  249. const noiseTexel = sampleNoise( noiseUv );
  250. const randomVec = noiseTexel.xyz.mul( 2.0 ).sub( 1.0 );
  251. const tangent = vec3( randomVec.xy, 0.0 ).normalize();
  252. const bitangent = vec3( tangent.y.mul( - 1.0 ), tangent.x, 0.0 );
  253. const kernelMatrix = mat3( tangent, bitangent, vec3( 0.0, 0.0, 1.0 ) );
  254. const DIRECTIONS = this.samples.lessThan( 30 ).select( 3, 5 ).toVar();
  255. const STEPS = add( this.samples, DIRECTIONS.sub( 1 ) ).div( DIRECTIONS ).toVar();
  256. const ao = float( 0 ).toVar();
  257. Loop( { start: int( 0 ), end: DIRECTIONS, type: 'int', condition: '<' }, ( { i } ) => {
  258. const angle = float( i ).div( float( DIRECTIONS ) ).mul( PI ).toVar();
  259. const sampleDir = vec4( cos( angle ), sin( angle ), 0., add( 0.5, mul( 0.5, noiseTexel.w ) ) );
  260. sampleDir.xyz = normalize( kernelMatrix.mul( sampleDir.xyz ) );
  261. const viewDir = normalize( viewPosition.xyz.negate() ).toVar();
  262. const sliceBitangent = normalize( cross( sampleDir.xyz, viewDir ) ).toVar();
  263. const sliceTangent = cross( sliceBitangent, viewDir );
  264. const normalInSlice = normalize( viewNormal.sub( sliceBitangent.mul( dot( viewNormal, sliceBitangent ) ) ) );
  265. const tangentToNormalInSlice = cross( normalInSlice, sliceBitangent ).toVar();
  266. const cosHorizons = vec2( dot( viewDir, tangentToNormalInSlice ), dot( viewDir, tangentToNormalInSlice.negate() ) ).toVar();
  267. Loop( { end: STEPS, type: 'int', name: 'j', condition: '<' }, ( { j } ) => {
  268. const sampleViewOffset = sampleDir.xyz.mul( radiusToUse ).mul( sampleDir.w ).mul( pow( div( float( j ).add( 1.0 ), float( STEPS ) ), this.distanceExponent ) );
  269. // x
  270. const sampleScreenPositionX = getScreenPosition( viewPosition.add( sampleViewOffset ), this._cameraProjectionMatrix ).toVar();
  271. const sampleDepthX = sampleDepth( sampleScreenPositionX ).toVar();
  272. const sampleSceneViewPositionX = getViewPosition( sampleScreenPositionX, sampleDepthX, this._cameraProjectionMatrixInverse ).toVar();
  273. const viewDeltaX = sampleSceneViewPositionX.sub( viewPosition ).toVar();
  274. If( abs( viewDeltaX.z ).lessThan( this.thickness ), () => {
  275. const sampleCosHorizon = dot( viewDir, normalize( viewDeltaX ) );
  276. cosHorizons.x.addAssign( max( 0, mul( sampleCosHorizon.sub( cosHorizons.x ), mix( 1.0, float( 2.0 ).div( float( j ).add( 2 ) ), this.distanceFallOff ) ) ) );
  277. } );
  278. // y
  279. const sampleScreenPositionY = getScreenPosition( viewPosition.sub( sampleViewOffset ), this._cameraProjectionMatrix ).toVar();
  280. const sampleDepthY = sampleDepth( sampleScreenPositionY ).toVar();
  281. const sampleSceneViewPositionY = getViewPosition( sampleScreenPositionY, sampleDepthY, this._cameraProjectionMatrixInverse ).toVar();
  282. const viewDeltaY = sampleSceneViewPositionY.sub( viewPosition ).toVar();
  283. If( abs( viewDeltaY.z ).lessThan( this.thickness ), () => {
  284. const sampleCosHorizon = dot( viewDir, normalize( viewDeltaY ) );
  285. cosHorizons.y.addAssign( max( 0, mul( sampleCosHorizon.sub( cosHorizons.y ), mix( 1.0, float( 2.0 ).div( float( j ).add( 2 ) ), this.distanceFallOff ) ) ) );
  286. } );
  287. } );
  288. const sinHorizons = sqrt( sub( 1.0, cosHorizons.mul( cosHorizons ) ) ).toVar();
  289. const nx = dot( normalInSlice, sliceTangent );
  290. const ny = dot( normalInSlice, viewDir );
  291. const nxb = mul( 0.5, acos( cosHorizons.y ).sub( acos( cosHorizons.x ) ).add( sinHorizons.x.mul( cosHorizons.x ).sub( sinHorizons.y.mul( cosHorizons.y ) ) ) );
  292. const nyb = mul( 0.5, sub( 2.0, cosHorizons.x.mul( cosHorizons.x ) ).sub( cosHorizons.y.mul( cosHorizons.y ) ) );
  293. const occlusion = nx.mul( nxb ).add( ny.mul( nyb ) );
  294. ao.addAssign( occlusion );
  295. } );
  296. ao.assign( clamp( ao.div( DIRECTIONS ), 0, 1 ) );
  297. ao.assign( pow( ao, this.scale ) );
  298. return vec4( vec3( ao ), 1.0 );
  299. } );
  300. this._material.fragmentNode = ao().context( builder.getSharedContext() );
  301. this._material.needsUpdate = true;
  302. //
  303. return this._textureNode;
  304. }
  305. /**
  306. * Frees internal resources. This method should be called
  307. * when the effect is no longer required.
  308. */
  309. dispose() {
  310. this._aoRenderTarget.dispose();
  311. this._material.dispose();
  312. }
  313. }
  314. export default GTAONode;
  315. /**
  316. * Generates the AO's noise texture for the given size.
  317. *
  318. * @param {number} [size=5] - The noise size.
  319. * @return {DataTexture} The generated noise texture.
  320. */
  321. function generateMagicSquareNoise( size = 5 ) {
  322. const noiseSize = Math.floor( size ) % 2 === 0 ? Math.floor( size ) + 1 : Math.floor( size );
  323. const magicSquare = generateMagicSquare( noiseSize );
  324. const noiseSquareSize = magicSquare.length;
  325. const data = new Uint8Array( noiseSquareSize * 4 );
  326. for ( let inx = 0; inx < noiseSquareSize; ++ inx ) {
  327. const iAng = magicSquare[ inx ];
  328. const angle = ( 2 * Math.PI * iAng ) / noiseSquareSize;
  329. const randomVec = new Vector3(
  330. Math.cos( angle ),
  331. Math.sin( angle ),
  332. 0
  333. ).normalize();
  334. data[ inx * 4 ] = ( randomVec.x * 0.5 + 0.5 ) * 255;
  335. data[ inx * 4 + 1 ] = ( randomVec.y * 0.5 + 0.5 ) * 255;
  336. data[ inx * 4 + 2 ] = 127;
  337. data[ inx * 4 + 3 ] = 255;
  338. }
  339. const noiseTexture = new DataTexture( data, noiseSize, noiseSize );
  340. noiseTexture.wrapS = RepeatWrapping;
  341. noiseTexture.wrapT = RepeatWrapping;
  342. noiseTexture.needsUpdate = true;
  343. return noiseTexture;
  344. }
  345. /**
  346. * Computes an array of magic square values required to generate the noise texture.
  347. *
  348. * @param {number} size - The noise size.
  349. * @return {Array<number>} The magic square values.
  350. */
  351. function generateMagicSquare( size ) {
  352. const noiseSize = Math.floor( size ) % 2 === 0 ? Math.floor( size ) + 1 : Math.floor( size );
  353. const noiseSquareSize = noiseSize * noiseSize;
  354. const magicSquare = Array( noiseSquareSize ).fill( 0 );
  355. let i = Math.floor( noiseSize / 2 );
  356. let j = noiseSize - 1;
  357. for ( let num = 1; num <= noiseSquareSize; ) {
  358. if ( i === - 1 && j === noiseSize ) {
  359. j = noiseSize - 2;
  360. i = 0;
  361. } else {
  362. if ( j === noiseSize ) {
  363. j = 0;
  364. }
  365. if ( i < 0 ) {
  366. i = noiseSize - 1;
  367. }
  368. }
  369. if ( magicSquare[ i * noiseSize + j ] !== 0 ) {
  370. j -= 2;
  371. i ++;
  372. continue;
  373. } else {
  374. magicSquare[ i * noiseSize + j ] = num ++;
  375. }
  376. j ++;
  377. i --;
  378. }
  379. return magicSquare;
  380. }
  381. /**
  382. * TSL function for creating a Ground Truth Ambient Occlusion (GTAO) effect.
  383. *
  384. * @tsl
  385. * @function
  386. * @param {Node<float>} depthNode - A node that represents the scene's depth.
  387. * @param {?Node<vec3>} normalNode - A node that represents the scene's normals.
  388. * @param {Camera} camera - The camera the scene is rendered with.
  389. * @returns {GTAONode}
  390. */
  391. export const ao = ( depthNode, normalNode, camera ) => nodeObject( new GTAONode( nodeObject( depthNode ), nodeObject( normalNode ), camera ) );