2
0

HalftoneShader.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. console.warn( "THREE.HalftoneShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author meatbags / xavierburrow.com, github/meatbags
  4. *
  5. * RGB Halftone shader for three.js.
  6. * NOTE:
  7. * Shape (1 = Dot, 2 = Ellipse, 3 = Line, 4 = Square)
  8. * Blending Mode (1 = Linear, 2 = Multiply, 3 = Add, 4 = Lighter, 5 = Darker)
  9. */
  10. THREE.HalftoneShader = {
  11. uniforms: {
  12. "tDiffuse": { value: null },
  13. "shape": { value: 1 },
  14. "radius": { value: 4 },
  15. "rotateR": { value: Math.PI / 12 * 1 },
  16. "rotateG": { value: Math.PI / 12 * 2 },
  17. "rotateB": { value: Math.PI / 12 * 3 },
  18. "scatter": { value: 0 },
  19. "width": { value: 1 },
  20. "height": { value: 1 },
  21. "blending": { value: 1 },
  22. "blendingMode": { value: 1 },
  23. "greyscale": { value: false },
  24. "disable": { value: false }
  25. },
  26. vertexShader: [
  27. "varying vec2 vUV;",
  28. "void main() {",
  29. " vUV = uv;",
  30. " gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);",
  31. "}"
  32. ].join( "\n" ),
  33. fragmentShader: [
  34. "#define SQRT2_MINUS_ONE 0.41421356",
  35. "#define SQRT2_HALF_MINUS_ONE 0.20710678",
  36. "#define PI2 6.28318531",
  37. "#define SHAPE_DOT 1",
  38. "#define SHAPE_ELLIPSE 2",
  39. "#define SHAPE_LINE 3",
  40. "#define SHAPE_SQUARE 4",
  41. "#define BLENDING_LINEAR 1",
  42. "#define BLENDING_MULTIPLY 2",
  43. "#define BLENDING_ADD 3",
  44. "#define BLENDING_LIGHTER 4",
  45. "#define BLENDING_DARKER 5",
  46. "uniform sampler2D tDiffuse;",
  47. "uniform float radius;",
  48. "uniform float rotateR;",
  49. "uniform float rotateG;",
  50. "uniform float rotateB;",
  51. "uniform float scatter;",
  52. "uniform float width;",
  53. "uniform float height;",
  54. "uniform int shape;",
  55. "uniform bool disable;",
  56. "uniform float blending;",
  57. "uniform int blendingMode;",
  58. "varying vec2 vUV;",
  59. "uniform bool greyscale;",
  60. "const int samples = 8;",
  61. "float blend( float a, float b, float t ) {",
  62. // linear blend
  63. " return a * ( 1.0 - t ) + b * t;",
  64. "}",
  65. "float hypot( float x, float y ) {",
  66. // vector magnitude
  67. " return sqrt( x * x + y * y );",
  68. "}",
  69. "float rand( vec2 seed ){",
  70. // get pseudo-random number
  71. "return fract( sin( dot( seed.xy, vec2( 12.9898, 78.233 ) ) ) * 43758.5453 );",
  72. "}",
  73. "float distanceToDotRadius( float channel, vec2 coord, vec2 normal, vec2 p, float angle, float rad_max ) {",
  74. // apply shape-specific transforms
  75. " float dist = hypot( coord.x - p.x, coord.y - p.y );",
  76. " float rad = channel;",
  77. " if ( shape == SHAPE_DOT ) {",
  78. " rad = pow( abs( rad ), 1.125 ) * rad_max;",
  79. " } else if ( shape == SHAPE_ELLIPSE ) {",
  80. " rad = pow( abs( rad ), 1.125 ) * rad_max;",
  81. " if ( dist != 0.0 ) {",
  82. " float dot_p = abs( ( p.x - coord.x ) / dist * normal.x + ( p.y - coord.y ) / dist * normal.y );",
  83. " dist = ( dist * ( 1.0 - SQRT2_HALF_MINUS_ONE ) ) + dot_p * dist * SQRT2_MINUS_ONE;",
  84. " }",
  85. " } else if ( shape == SHAPE_LINE ) {",
  86. " rad = pow( abs( rad ), 1.5) * rad_max;",
  87. " float dot_p = ( p.x - coord.x ) * normal.x + ( p.y - coord.y ) * normal.y;",
  88. " dist = hypot( normal.x * dot_p, normal.y * dot_p );",
  89. " } else if ( shape == SHAPE_SQUARE ) {",
  90. " float theta = atan( p.y - coord.y, p.x - coord.x ) - angle;",
  91. " float sin_t = abs( sin( theta ) );",
  92. " float cos_t = abs( cos( theta ) );",
  93. " rad = pow( abs( rad ), 1.4 );",
  94. " rad = rad_max * ( rad + ( ( sin_t > cos_t ) ? rad - sin_t * rad : rad - cos_t * rad ) );",
  95. " }",
  96. " return rad - dist;",
  97. "}",
  98. "struct Cell {",
  99. // grid sample positions
  100. " vec2 normal;",
  101. " vec2 p1;",
  102. " vec2 p2;",
  103. " vec2 p3;",
  104. " vec2 p4;",
  105. " float samp2;",
  106. " float samp1;",
  107. " float samp3;",
  108. " float samp4;",
  109. "};",
  110. "vec4 getSample( vec2 point ) {",
  111. // multi-sampled point
  112. " vec4 tex = texture2D( tDiffuse, vec2( point.x / width, point.y / height ) );",
  113. " float base = rand( vec2( floor( point.x ), floor( point.y ) ) ) * PI2;",
  114. " float step = PI2 / float( samples );",
  115. " float dist = radius * 0.66;",
  116. " for ( int i = 0; i < samples; ++i ) {",
  117. " float r = base + step * float( i );",
  118. " vec2 coord = point + vec2( cos( r ) * dist, sin( r ) * dist );",
  119. " tex += texture2D( tDiffuse, vec2( coord.x / width, coord.y / height ) );",
  120. " }",
  121. " tex /= float( samples ) + 1.0;",
  122. " return tex;",
  123. "}",
  124. "float getDotColour( Cell c, vec2 p, int channel, float angle, float aa ) {",
  125. // get colour for given point
  126. " float dist_c_1, dist_c_2, dist_c_3, dist_c_4, res;",
  127. " if ( channel == 0 ) {",
  128. " c.samp1 = getSample( c.p1 ).r;",
  129. " c.samp2 = getSample( c.p2 ).r;",
  130. " c.samp3 = getSample( c.p3 ).r;",
  131. " c.samp4 = getSample( c.p4 ).r;",
  132. " } else if (channel == 1) {",
  133. " c.samp1 = getSample( c.p1 ).g;",
  134. " c.samp2 = getSample( c.p2 ).g;",
  135. " c.samp3 = getSample( c.p3 ).g;",
  136. " c.samp4 = getSample( c.p4 ).g;",
  137. " } else {",
  138. " c.samp1 = getSample( c.p1 ).b;",
  139. " c.samp3 = getSample( c.p3 ).b;",
  140. " c.samp2 = getSample( c.p2 ).b;",
  141. " c.samp4 = getSample( c.p4 ).b;",
  142. " }",
  143. " dist_c_1 = distanceToDotRadius( c.samp1, c.p1, c.normal, p, angle, radius );",
  144. " dist_c_2 = distanceToDotRadius( c.samp2, c.p2, c.normal, p, angle, radius );",
  145. " dist_c_3 = distanceToDotRadius( c.samp3, c.p3, c.normal, p, angle, radius );",
  146. " dist_c_4 = distanceToDotRadius( c.samp4, c.p4, c.normal, p, angle, radius );",
  147. " res = ( dist_c_1 > 0.0 ) ? clamp( dist_c_1 / aa, 0.0, 1.0 ) : 0.0;",
  148. " res += ( dist_c_2 > 0.0 ) ? clamp( dist_c_2 / aa, 0.0, 1.0 ) : 0.0;",
  149. " res += ( dist_c_3 > 0.0 ) ? clamp( dist_c_3 / aa, 0.0, 1.0 ) : 0.0;",
  150. " res += ( dist_c_4 > 0.0 ) ? clamp( dist_c_4 / aa, 0.0, 1.0 ) : 0.0;",
  151. " res = clamp( res, 0.0, 1.0 );",
  152. " return res;",
  153. "}",
  154. "Cell getReferenceCell( vec2 p, vec2 origin, float grid_angle, float step ) {",
  155. // get containing cell
  156. " Cell c;",
  157. // calc grid
  158. " vec2 n = vec2( cos( grid_angle ), sin( grid_angle ) );",
  159. " float threshold = step * 0.5;",
  160. " float dot_normal = n.x * ( p.x - origin.x ) + n.y * ( p.y - origin.y );",
  161. " float dot_line = -n.y * ( p.x - origin.x ) + n.x * ( p.y - origin.y );",
  162. " vec2 offset = vec2( n.x * dot_normal, n.y * dot_normal );",
  163. " float offset_normal = mod( hypot( offset.x, offset.y ), step );",
  164. " float normal_dir = ( dot_normal < 0.0 ) ? 1.0 : -1.0;",
  165. " float normal_scale = ( ( offset_normal < threshold ) ? -offset_normal : step - offset_normal ) * normal_dir;",
  166. " float offset_line = mod( hypot( ( p.x - offset.x ) - origin.x, ( p.y - offset.y ) - origin.y ), step );",
  167. " float line_dir = ( dot_line < 0.0 ) ? 1.0 : -1.0;",
  168. " float line_scale = ( ( offset_line < threshold ) ? -offset_line : step - offset_line ) * line_dir;",
  169. // get closest corner
  170. " c.normal = n;",
  171. " c.p1.x = p.x - n.x * normal_scale + n.y * line_scale;",
  172. " c.p1.y = p.y - n.y * normal_scale - n.x * line_scale;",
  173. // scatter
  174. " if ( scatter != 0.0 ) {",
  175. " float off_mag = scatter * threshold * 0.5;",
  176. " float off_angle = rand( vec2( floor( c.p1.x ), floor( c.p1.y ) ) ) * PI2;",
  177. " c.p1.x += cos( off_angle ) * off_mag;",
  178. " c.p1.y += sin( off_angle ) * off_mag;",
  179. " }",
  180. // find corners
  181. " float normal_step = normal_dir * ( ( offset_normal < threshold ) ? step : -step );",
  182. " float line_step = line_dir * ( ( offset_line < threshold ) ? step : -step );",
  183. " c.p2.x = c.p1.x - n.x * normal_step;",
  184. " c.p2.y = c.p1.y - n.y * normal_step;",
  185. " c.p3.x = c.p1.x + n.y * line_step;",
  186. " c.p3.y = c.p1.y - n.x * line_step;",
  187. " c.p4.x = c.p1.x - n.x * normal_step + n.y * line_step;",
  188. " c.p4.y = c.p1.y - n.y * normal_step - n.x * line_step;",
  189. " return c;",
  190. "}",
  191. "float blendColour( float a, float b, float t ) {",
  192. // blend colours
  193. " if ( blendingMode == BLENDING_LINEAR ) {",
  194. " return blend( a, b, 1.0 - t );",
  195. " } else if ( blendingMode == BLENDING_ADD ) {",
  196. " return blend( a, min( 1.0, a + b ), t );",
  197. " } else if ( blendingMode == BLENDING_MULTIPLY ) {",
  198. " return blend( a, max( 0.0, a * b ), t );",
  199. " } else if ( blendingMode == BLENDING_LIGHTER ) {",
  200. " return blend( a, max( a, b ), t );",
  201. " } else if ( blendingMode == BLENDING_DARKER ) {",
  202. " return blend( a, min( a, b ), t );",
  203. " } else {",
  204. " return blend( a, b, 1.0 - t );",
  205. " }",
  206. "}",
  207. "void main() {",
  208. " if ( ! disable ) {",
  209. // setup
  210. " vec2 p = vec2( vUV.x * width, vUV.y * height );",
  211. " vec2 origin = vec2( 0, 0 );",
  212. " float aa = ( radius < 2.5 ) ? radius * 0.5 : 1.25;",
  213. // get channel samples
  214. " Cell cell_r = getReferenceCell( p, origin, rotateR, radius );",
  215. " Cell cell_g = getReferenceCell( p, origin, rotateG, radius );",
  216. " Cell cell_b = getReferenceCell( p, origin, rotateB, radius );",
  217. " float r = getDotColour( cell_r, p, 0, rotateR, aa );",
  218. " float g = getDotColour( cell_g, p, 1, rotateG, aa );",
  219. " float b = getDotColour( cell_b, p, 2, rotateB, aa );",
  220. // blend with original
  221. " vec4 colour = texture2D( tDiffuse, vUV );",
  222. " r = blendColour( r, colour.r, blending );",
  223. " g = blendColour( g, colour.g, blending );",
  224. " b = blendColour( b, colour.b, blending );",
  225. " if ( greyscale ) {",
  226. " r = g = b = (r + b + g) / 3.0;",
  227. " }",
  228. " gl_FragColor = vec4( r, g, b, 1.0 );",
  229. " } else {",
  230. " gl_FragColor = texture2D( tDiffuse, vUV );",
  231. " }",
  232. "}"
  233. ].join( "\n" )
  234. };