HalftoneShader.js 8.5 KB

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