(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports"], factory); } else if (typeof exports !== "undefined") { factory(exports); } else { var mod = { exports: {} }; factory(mod.exports); global.HalftoneShader = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.HalftoneShader = void 0; /** * RGB Halftone shader for three.js. * NOTE: * Shape (1 = Dot, 2 = Ellipse, 3 = Line, 4 = Square) * Blending Mode (1 = Linear, 2 = Multiply, 3 = Add, 4 = Lighter, 5 = Darker) */ var HalftoneShader = { uniforms: { 'tDiffuse': { value: null }, 'shape': { value: 1 }, 'radius': { value: 4 }, 'rotateR': { value: Math.PI / 12 * 1 }, 'rotateG': { value: Math.PI / 12 * 2 }, 'rotateB': { value: Math.PI / 12 * 3 }, 'scatter': { value: 0 }, 'width': { value: 1 }, 'height': { value: 1 }, 'blending': { value: 1 }, 'blendingMode': { value: 1 }, 'greyscale': { value: false }, 'disable': { value: false } }, vertexShader: /* glsl */ "\n\n\t\tvarying vec2 vUV;\n\n\t\tvoid main() {\n\n\t\t\tvUV = uv;\n\t\t\tgl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n\n\t\t}", fragmentShader: /* glsl */ "\n\n\t\t#define SQRT2_MINUS_ONE 0.41421356\n\t\t#define SQRT2_HALF_MINUS_ONE 0.20710678\n\t\t#define PI2 6.28318531\n\t\t#define SHAPE_DOT 1\n\t\t#define SHAPE_ELLIPSE 2\n\t\t#define SHAPE_LINE 3\n\t\t#define SHAPE_SQUARE 4\n\t\t#define BLENDING_LINEAR 1\n\t\t#define BLENDING_MULTIPLY 2\n\t\t#define BLENDING_ADD 3\n\t\t#define BLENDING_LIGHTER 4\n\t\t#define BLENDING_DARKER 5\n\t\tuniform sampler2D tDiffuse;\n\t\tuniform float radius;\n\t\tuniform float rotateR;\n\t\tuniform float rotateG;\n\t\tuniform float rotateB;\n\t\tuniform float scatter;\n\t\tuniform float width;\n\t\tuniform float height;\n\t\tuniform int shape;\n\t\tuniform bool disable;\n\t\tuniform float blending;\n\t\tuniform int blendingMode;\n\t\tvarying vec2 vUV;\n\t\tuniform bool greyscale;\n\t\tconst int samples = 8;\n\n\t\tfloat blend( float a, float b, float t ) {\n\n\t\t// linear blend\n\t\t\treturn a * ( 1.0 - t ) + b * t;\n\n\t\t}\n\n\t\tfloat hypot( float x, float y ) {\n\n\t\t// vector magnitude\n\t\t\treturn sqrt( x * x + y * y );\n\n\t\t}\n\n\t\tfloat rand( vec2 seed ){\n\n\t\t// get pseudo-random number\n\t\t\treturn fract( sin( dot( seed.xy, vec2( 12.9898, 78.233 ) ) ) * 43758.5453 );\n\n\t\t}\n\n\t\tfloat distanceToDotRadius( float channel, vec2 coord, vec2 normal, vec2 p, float angle, float rad_max ) {\n\n\t\t// apply shape-specific transforms\n\t\t\tfloat dist = hypot( coord.x - p.x, coord.y - p.y );\n\t\t\tfloat rad = channel;\n\n\t\t\tif ( shape == SHAPE_DOT ) {\n\n\t\t\t\trad = pow( abs( rad ), 1.125 ) * rad_max;\n\n\t\t\t} else if ( shape == SHAPE_ELLIPSE ) {\n\n\t\t\t\trad = pow( abs( rad ), 1.125 ) * rad_max;\n\n\t\t\t\tif ( dist != 0.0 ) {\n\t\t\t\t\tfloat dot_p = abs( ( p.x - coord.x ) / dist * normal.x + ( p.y - coord.y ) / dist * normal.y );\n\t\t\t\t\tdist = ( dist * ( 1.0 - SQRT2_HALF_MINUS_ONE ) ) + dot_p * dist * SQRT2_MINUS_ONE;\n\t\t\t\t}\n\n\t\t\t} else if ( shape == SHAPE_LINE ) {\n\n\t\t\t\trad = pow( abs( rad ), 1.5) * rad_max;\n\t\t\t\tfloat dot_p = ( p.x - coord.x ) * normal.x + ( p.y - coord.y ) * normal.y;\n\t\t\t\tdist = hypot( normal.x * dot_p, normal.y * dot_p );\n\n\t\t\t} else if ( shape == SHAPE_SQUARE ) {\n\n\t\t\t\tfloat theta = atan( p.y - coord.y, p.x - coord.x ) - angle;\n\t\t\t\tfloat sin_t = abs( sin( theta ) );\n\t\t\t\tfloat cos_t = abs( cos( theta ) );\n\t\t\t\trad = pow( abs( rad ), 1.4 );\n\t\t\t\trad = rad_max * ( rad + ( ( sin_t > cos_t ) ? rad - sin_t * rad : rad - cos_t * rad ) );\n\n\t\t\t}\n\n\t\t\treturn rad - dist;\n\n\t\t}\n\n\t\tstruct Cell {\n\n\t\t// grid sample positions\n\t\t\tvec2 normal;\n\t\t\tvec2 p1;\n\t\t\tvec2 p2;\n\t\t\tvec2 p3;\n\t\t\tvec2 p4;\n\t\t\tfloat samp2;\n\t\t\tfloat samp1;\n\t\t\tfloat samp3;\n\t\t\tfloat samp4;\n\n\t\t};\n\n\t\tvec4 getSample( vec2 point ) {\n\n\t\t// multi-sampled point\n\t\t\tvec4 tex = texture2D( tDiffuse, vec2( point.x / width, point.y / height ) );\n\t\t\tfloat base = rand( vec2( floor( point.x ), floor( point.y ) ) ) * PI2;\n\t\t\tfloat step = PI2 / float( samples );\n\t\t\tfloat dist = radius * 0.66;\n\n\t\t\tfor ( int i = 0; i < samples; ++i ) {\n\n\t\t\t\tfloat r = base + step * float( i );\n\t\t\t\tvec2 coord = point + vec2( cos( r ) * dist, sin( r ) * dist );\n\t\t\t\ttex += texture2D( tDiffuse, vec2( coord.x / width, coord.y / height ) );\n\n\t\t\t}\n\n\t\t\ttex /= float( samples ) + 1.0;\n\t\t\treturn tex;\n\n\t\t}\n\n\t\tfloat getDotColour( Cell c, vec2 p, int channel, float angle, float aa ) {\n\n\t\t// get colour for given point\n\t\t\tfloat dist_c_1, dist_c_2, dist_c_3, dist_c_4, res;\n\n\t\t\tif ( channel == 0 ) {\n\n\t\t\t\tc.samp1 = getSample( c.p1 ).r;\n\t\t\t\tc.samp2 = getSample( c.p2 ).r;\n\t\t\t\tc.samp3 = getSample( c.p3 ).r;\n\t\t\t\tc.samp4 = getSample( c.p4 ).r;\n\n\t\t\t} else if (channel == 1) {\n\n\t\t\t\tc.samp1 = getSample( c.p1 ).g;\n\t\t\t\tc.samp2 = getSample( c.p2 ).g;\n\t\t\t\tc.samp3 = getSample( c.p3 ).g;\n\t\t\t\tc.samp4 = getSample( c.p4 ).g;\n\n\t\t\t} else {\n\n\t\t\t\tc.samp1 = getSample( c.p1 ).b;\n\t\t\t\tc.samp3 = getSample( c.p3 ).b;\n\t\t\t\tc.samp2 = getSample( c.p2 ).b;\n\t\t\t\tc.samp4 = getSample( c.p4 ).b;\n\n\t\t\t}\n\n\t\t\tdist_c_1 = distanceToDotRadius( c.samp1, c.p1, c.normal, p, angle, radius );\n\t\t\tdist_c_2 = distanceToDotRadius( c.samp2, c.p2, c.normal, p, angle, radius );\n\t\t\tdist_c_3 = distanceToDotRadius( c.samp3, c.p3, c.normal, p, angle, radius );\n\t\t\tdist_c_4 = distanceToDotRadius( c.samp4, c.p4, c.normal, p, angle, radius );\n\t\t\tres = ( dist_c_1 > 0.0 ) ? clamp( dist_c_1 / aa, 0.0, 1.0 ) : 0.0;\n\t\t\tres += ( dist_c_2 > 0.0 ) ? clamp( dist_c_2 / aa, 0.0, 1.0 ) : 0.0;\n\t\t\tres += ( dist_c_3 > 0.0 ) ? clamp( dist_c_3 / aa, 0.0, 1.0 ) : 0.0;\n\t\t\tres += ( dist_c_4 > 0.0 ) ? clamp( dist_c_4 / aa, 0.0, 1.0 ) : 0.0;\n\t\t\tres = clamp( res, 0.0, 1.0 );\n\n\t\t\treturn res;\n\n\t\t}\n\n\t\tCell getReferenceCell( vec2 p, vec2 origin, float grid_angle, float step ) {\n\n\t\t// get containing cell\n\t\t\tCell c;\n\n\t\t// calc grid\n\t\t\tvec2 n = vec2( cos( grid_angle ), sin( grid_angle ) );\n\t\t\tfloat threshold = step * 0.5;\n\t\t\tfloat dot_normal = n.x * ( p.x - origin.x ) + n.y * ( p.y - origin.y );\n\t\t\tfloat dot_line = -n.y * ( p.x - origin.x ) + n.x * ( p.y - origin.y );\n\t\t\tvec2 offset = vec2( n.x * dot_normal, n.y * dot_normal );\n\t\t\tfloat offset_normal = mod( hypot( offset.x, offset.y ), step );\n\t\t\tfloat normal_dir = ( dot_normal < 0.0 ) ? 1.0 : -1.0;\n\t\t\tfloat normal_scale = ( ( offset_normal < threshold ) ? -offset_normal : step - offset_normal ) * normal_dir;\n\t\t\tfloat offset_line = mod( hypot( ( p.x - offset.x ) - origin.x, ( p.y - offset.y ) - origin.y ), step );\n\t\t\tfloat line_dir = ( dot_line < 0.0 ) ? 1.0 : -1.0;\n\t\t\tfloat line_scale = ( ( offset_line < threshold ) ? -offset_line : step - offset_line ) * line_dir;\n\n\t\t// get closest corner\n\t\t\tc.normal = n;\n\t\t\tc.p1.x = p.x - n.x * normal_scale + n.y * line_scale;\n\t\t\tc.p1.y = p.y - n.y * normal_scale - n.x * line_scale;\n\n\t\t// scatter\n\t\t\tif ( scatter != 0.0 ) {\n\n\t\t\t\tfloat off_mag = scatter * threshold * 0.5;\n\t\t\t\tfloat off_angle = rand( vec2( floor( c.p1.x ), floor( c.p1.y ) ) ) * PI2;\n\t\t\t\tc.p1.x += cos( off_angle ) * off_mag;\n\t\t\t\tc.p1.y += sin( off_angle ) * off_mag;\n\n\t\t\t}\n\n\t\t// find corners\n\t\t\tfloat normal_step = normal_dir * ( ( offset_normal < threshold ) ? step : -step );\n\t\t\tfloat line_step = line_dir * ( ( offset_line < threshold ) ? step : -step );\n\t\t\tc.p2.x = c.p1.x - n.x * normal_step;\n\t\t\tc.p2.y = c.p1.y - n.y * normal_step;\n\t\t\tc.p3.x = c.p1.x + n.y * line_step;\n\t\t\tc.p3.y = c.p1.y - n.x * line_step;\n\t\t\tc.p4.x = c.p1.x - n.x * normal_step + n.y * line_step;\n\t\t\tc.p4.y = c.p1.y - n.y * normal_step - n.x * line_step;\n\n\t\t\treturn c;\n\n\t\t}\n\n\t\tfloat blendColour( float a, float b, float t ) {\n\n\t\t// blend colours\n\t\t\tif ( blendingMode == BLENDING_LINEAR ) {\n\t\t\t\treturn blend( a, b, 1.0 - t );\n\t\t\t} else if ( blendingMode == BLENDING_ADD ) {\n\t\t\t\treturn blend( a, min( 1.0, a + b ), t );\n\t\t\t} else if ( blendingMode == BLENDING_MULTIPLY ) {\n\t\t\t\treturn blend( a, max( 0.0, a * b ), t );\n\t\t\t} else if ( blendingMode == BLENDING_LIGHTER ) {\n\t\t\t\treturn blend( a, max( a, b ), t );\n\t\t\t} else if ( blendingMode == BLENDING_DARKER ) {\n\t\t\t\treturn blend( a, min( a, b ), t );\n\t\t\t} else {\n\t\t\t\treturn blend( a, b, 1.0 - t );\n\t\t\t}\n\n\t\t}\n\n\t\tvoid main() {\n\n\t\t\tif ( ! disable ) {\n\n\t\t// setup\n\t\t\t\tvec2 p = vec2( vUV.x * width, vUV.y * height );\n\t\t\t\tvec2 origin = vec2( 0, 0 );\n\t\t\t\tfloat aa = ( radius < 2.5 ) ? radius * 0.5 : 1.25;\n\n\t\t// get channel samples\n\t\t\t\tCell cell_r = getReferenceCell( p, origin, rotateR, radius );\n\t\t\t\tCell cell_g = getReferenceCell( p, origin, rotateG, radius );\n\t\t\t\tCell cell_b = getReferenceCell( p, origin, rotateB, radius );\n\t\t\t\tfloat r = getDotColour( cell_r, p, 0, rotateR, aa );\n\t\t\t\tfloat g = getDotColour( cell_g, p, 1, rotateG, aa );\n\t\t\t\tfloat b = getDotColour( cell_b, p, 2, rotateB, aa );\n\n\t\t// blend with original\n\t\t\t\tvec4 colour = texture2D( tDiffuse, vUV );\n\t\t\t\tr = blendColour( r, colour.r, blending );\n\t\t\t\tg = blendColour( g, colour.g, blending );\n\t\t\t\tb = blendColour( b, colour.b, blending );\n\n\t\t\t\tif ( greyscale ) {\n\t\t\t\t\tr = g = b = (r + b + g) / 3.0;\n\t\t\t\t}\n\n\t\t\t\tgl_FragColor = vec4( r, g, b, 1.0 );\n\n\t\t\t} else {\n\n\t\t\t\tgl_FragColor = texture2D( tDiffuse, vUV );\n\n\t\t\t}\n\n\t\t}" }; _exports.HalftoneShader = HalftoneShader; });