HalftoneShader.js 9.0 KB

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