DotScreenNode.js 2.4 KB

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