BlurNode.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { Vector2 } from '../../../../build/three.module.js';
  2. import { TempNode } from '../core/TempNode.js';
  3. import { FunctionNode } from '../core/FunctionNode.js';
  4. import { FloatNode } from '../inputs/FloatNode.js';
  5. import { Vector2Node } from '../inputs/Vector2Node.js';
  6. import { UVNode } from '../accessors/UVNode.js';
  7. class BlurNode extends TempNode {
  8. constructor( value, uv, radius, size ) {
  9. super( 'v4' );
  10. this.value = value;
  11. this.uv = uv || new UVNode();
  12. this.radius = radius || new Vector2Node( 1, 1 );
  13. this.size = size;
  14. this.blurX = true;
  15. this.blurY = true;
  16. this.horizontal = new FloatNode( 1 / 64 );
  17. this.vertical = new FloatNode( 1 / 64 );
  18. }
  19. updateFrame( /* frame */ ) {
  20. if ( this.size ) {
  21. this.horizontal.value = this.radius.x / this.size.x;
  22. this.vertical.value = this.radius.y / this.size.y;
  23. } else if ( this.value.value && this.value.value.image ) {
  24. const image = this.value.value.image;
  25. this.horizontal.value = this.radius.x / image.width;
  26. this.vertical.value = this.radius.y / image.height;
  27. }
  28. }
  29. generate( builder, output ) {
  30. if ( builder.isShader( 'fragment' ) ) {
  31. const blurCode = [];
  32. let code;
  33. const blurX = builder.include( BlurNode.Nodes.blurX ),
  34. blurY = builder.include( BlurNode.Nodes.blurY );
  35. if ( this.blurX ) {
  36. blurCode.push( blurX + '( ' + this.value.build( builder, 'sampler2D' ) + ', ' + this.uv.build( builder, 'v2' ) + ', ' + this.horizontal.build( builder, 'f' ) + ' )' );
  37. }
  38. if ( this.blurY ) {
  39. blurCode.push( blurY + '( ' + this.value.build( builder, 'sampler2D' ) + ', ' + this.uv.build( builder, 'v2' ) + ', ' + this.vertical.build( builder, 'f' ) + ' )' );
  40. }
  41. if ( blurCode.length == 2 ) code = '( ' + blurCode.join( ' + ' ) + ' / 2.0 )';
  42. else if ( blurCode.length ) code = '( ' + blurCode[ 0 ] + ' )';
  43. else code = 'vec4( 0.0 )';
  44. return builder.format( code, this.getType( builder ), output );
  45. } else {
  46. console.warn( 'THREE.BlurNode is not compatible with ' + builder.shader + ' shader.' );
  47. return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );
  48. }
  49. }
  50. copy( source ) {
  51. super.copy( source );
  52. this.value = source.value;
  53. this.uv = source.uv;
  54. this.radius = source.radius;
  55. if ( source.size !== undefined ) this.size = new Vector2( source.size.x, source.size.y );
  56. this.blurX = source.blurX;
  57. this.blurY = source.blurY;
  58. return this;
  59. }
  60. toJSON( meta ) {
  61. let data = this.getJSONNode( meta );
  62. if ( ! data ) {
  63. data = this.createJSONNode( meta );
  64. data.value = this.value.toJSON( meta ).uuid;
  65. data.uv = this.uv.toJSON( meta ).uuid;
  66. data.radius = this.radius.toJSON( meta ).uuid;
  67. if ( this.size ) data.size = { x: this.size.x, y: this.size.y };
  68. data.blurX = this.blurX;
  69. data.blurY = this.blurY;
  70. }
  71. return data;
  72. }
  73. }
  74. BlurNode.Nodes = ( function () {
  75. const blurX = new FunctionNode( /* glsl */`
  76. vec4 blurX( sampler2D tex, vec2 uv, float s ) {
  77. vec4 sum = vec4( 0.0 );
  78. sum += texture2D( tex, vec2( uv.x - 4.0 * s, uv.y ) ) * 0.051;
  79. sum += texture2D( tex, vec2( uv.x - 3.0 * s, uv.y ) ) * 0.0918;
  80. sum += texture2D( tex, vec2( uv.x - 2.0 * s, uv.y ) ) * 0.12245;
  81. sum += texture2D( tex, vec2( uv.x - 1.0 * s, uv.y ) ) * 0.1531;
  82. sum += texture2D( tex, vec2( uv.x, uv.y ) ) * 0.1633;
  83. sum += texture2D( tex, vec2( uv.x + 1.0 * s, uv.y ) ) * 0.1531;
  84. sum += texture2D( tex, vec2( uv.x + 2.0 * s, uv.y ) ) * 0.12245;
  85. sum += texture2D( tex, vec2( uv.x + 3.0 * s, uv.y ) ) * 0.0918;
  86. sum += texture2D( tex, vec2( uv.x + 4.0 * s, uv.y ) ) * 0.051;
  87. return sum * .667;
  88. }`
  89. );
  90. const blurY = new FunctionNode( /* glsl */`
  91. vec4 blurY( sampler2D tex, vec2 uv, float s ) {
  92. vec4 sum = vec4( 0.0 );
  93. sum += texture2D( tex, vec2( uv.x, uv.y - 4.0 * s ) ) * 0.051;
  94. sum += texture2D( tex, vec2( uv.x, uv.y - 3.0 * s ) ) * 0.0918;
  95. sum += texture2D( tex, vec2( uv.x, uv.y - 2.0 * s ) ) * 0.12245;
  96. sum += texture2D( tex, vec2( uv.x, uv.y - 1.0 * s ) ) * 0.1531;
  97. sum += texture2D( tex, vec2( uv.x, uv.y ) ) * 0.1633;
  98. sum += texture2D( tex, vec2( uv.x, uv.y + 1.0 * s ) ) * 0.1531;
  99. sum += texture2D( tex, vec2( uv.x, uv.y + 2.0 * s ) ) * 0.12245;
  100. sum += texture2D( tex, vec2( uv.x, uv.y + 3.0 * s ) ) * 0.0918;
  101. sum += texture2D( tex, vec2( uv.x, uv.y + 4.0 * s ) ) * 0.051;
  102. return sum * .667;
  103. }`
  104. );
  105. return {
  106. blurX: blurX,
  107. blurY: blurY
  108. };
  109. } )();
  110. BlurNode.prototype.nodeType = 'Blur';
  111. BlurNode.prototype.hashProperties = [ 'blurX', 'blurY' ];
  112. export { BlurNode };