DotScreenNode.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { TempNode } from 'three/webgpu';
  2. import { nodeObject, Fn, uv, uniform, vec2, vec3, sin, cos, add, vec4, screenSize } from 'three/tsl';
  3. /**
  4. * Post processing node for creating dot-screen effect.
  5. *
  6. * @augments TempNode
  7. * @three_import import { dotScreen } from 'three/addons/tsl/display/DotScreenNode.js';
  8. */
  9. class DotScreenNode extends TempNode {
  10. static get type() {
  11. return 'DotScreenNode';
  12. }
  13. /**
  14. * Constructs a new dot screen node.
  15. *
  16. * @param {Node} inputNode - The node that represents the input of the effect.
  17. * @param {number} [angle=1.57] - The rotation of the effect in radians.
  18. * @param {number} [scale=1] - The scale of the effect. A higher value means smaller dots.
  19. */
  20. constructor( inputNode, angle = 1.57, scale = 1 ) {
  21. super( 'vec4' );
  22. /**
  23. * The node that represents the input of the effect.
  24. *
  25. * @type {Node}
  26. */
  27. this.inputNode = inputNode;
  28. /**
  29. * A uniform node that represents the rotation of the effect in radians.
  30. *
  31. * @type {UniformNode<float>}
  32. */
  33. this.angle = uniform( angle );
  34. /**
  35. * A uniform node that represents the scale of the effect. A higher value means smaller dots.
  36. *
  37. * @type {UniformNode<float>}
  38. */
  39. this.scale = uniform( scale );
  40. }
  41. /**
  42. * This method is used to setup the effect's TSL code.
  43. *
  44. * @param {NodeBuilder} builder - The current node builder.
  45. * @return {ShaderCallNodeInternal}
  46. */
  47. setup() {
  48. const inputNode = this.inputNode;
  49. const pattern = Fn( () => {
  50. const s = sin( this.angle );
  51. const c = cos( this.angle );
  52. const tex = uv().mul( screenSize );
  53. const point = vec2( c.mul( tex.x ).sub( s.mul( tex.y ) ), s.mul( tex.x ).add( c.mul( tex.y ) ) ).mul( this.scale );
  54. return sin( point.x ).mul( sin( point.y ) ).mul( 4 );
  55. } );
  56. const dotScreen = Fn( () => {
  57. const color = inputNode;
  58. const average = add( color.r, color.g, color.b ).div( 3 );
  59. return vec4( vec3( average.mul( 10 ).sub( 5 ).add( pattern() ) ), color.a );
  60. } );
  61. const outputNode = dotScreen();
  62. return outputNode;
  63. }
  64. }
  65. export default DotScreenNode;
  66. /**
  67. * TSL function for creating a dot-screen node for post processing.
  68. *
  69. * @tsl
  70. * @function
  71. * @param {Node<vec4>} node - The node that represents the input of the effect.
  72. * @param {number} [angle=1.57] - The rotation of the effect in radians.
  73. * @param {number} [scale=1] - The scale of the effect. A higher value means smaller dots.
  74. * @returns {DotScreenNode}
  75. */
  76. export const dotScreen = ( node, angle, scale ) => nodeObject( new DotScreenNode( nodeObject( node ), angle, scale ) );