BlurNode.js 4.8 KB

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