1 line
11 KiB
Plaintext
1 line
11 KiB
Plaintext
|
|
{"version":3,"file":"HalftoneShader.cjs","sources":["../../src/shaders/HalftoneShader.ts"],"sourcesContent":["/**\n * RGB Halftone shader for three.js.\n *\tNOTE:\n * \t\tShape (1 = Dot, 2 = Ellipse, 3 = Line, 4 = Square)\n *\t\tBlending Mode (1 = Linear, 2 = Multiply, 3 = Add, 4 = Lighter, 5 = Darker)\n */\n\nexport const HalftoneShader = {\n uniforms: {\n tDiffuse: { value: null },\n shape: { value: 1 },\n radius: { value: 4 },\n rotateR: { value: (Math.PI / 12) * 1 },\n rotateG: { value: (Math.PI / 12) * 2 },\n rotateB: { value: (Math.PI / 12) * 3 },\n scatter: { value: 0 },\n width: { value: 1 },\n height: { value: 1 },\n blending: { value: 1 },\n blendingMode: { value: 1 },\n greyscale: { value: false },\n disable: { value: false },\n },\n\n vertexShader: /* glsl */ `\n varying vec2 vUV;\n\n void main() {\n\n \tvUV = uv;\n \tgl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n\n }\n `,\n\n fragmentShader: /* glsl */ `\n #define SQRT2_MINUS_ONE 0.41421356\n #define SQRT2_HALF_MINUS_ONE 0.20710678\n #define PI2 6.28318531\n #define SHAPE_DOT 1\n #define SHAPE_ELLIPSE 2\n #define SHAPE_LINE 3\n #define SHAPE_SQUARE 4\n #define BLENDING_LINEAR 1\n #define BLENDING_MULTIPLY 2\n #define BLENDING_ADD 3\n #define BLENDING_LIGHTER 4\n #define BLENDING_DARKER 5\n uniform sampler2D tDiffuse;\n uniform float radius;\n uniform float rotateR;\n uniform float rotateG;\n uniform float rotateB;\n uniform float scatter;\n uniform float width;\n uniform float height;\n uniform int shape;\n uniform bool disable;\n uniform float blending;\n uniform int blendingMode;\n varying vec2 vUV;\n uniform bool greyscale;\n const int samples = 8;\n\n float blend( float a, float b, float t ) {\n\n // linear blend\n \treturn a * ( 1.0 - t ) + b * t;\n\n }\n\n float hypot( float x, float y ) {\n\n // vector magnitude\n \treturn sqrt( x * x + y * y );\n\n }\n\n float rand( vec2 seed ){\n\n // get pseudo-random number\n return fract( sin( dot( seed.xy, vec2( 12.9898, 78.233 ) ) ) * 43758.5453 );\n\n }\n\n float distanceToDotRadius( float channel, vec2 coord, vec2 normal, vec2 p, float angle, float rad_max ) {\n\n // apply shape-specific transforms\n \tfloat dist = hypot( coord.x - p.x, coord.y - p.y );\n \tfloat rad = channel;\n\n \tif ( shape == SHAPE_DOT ) {\n\n \t\trad = pow( abs( rad ), 1.125 ) * rad_max;\n\n \t} else if ( shape == SHAPE_ELLIPSE ) {\n\n \t\trad = pow( abs( rad ), 1.125 ) * rad_max;\n\n \t\tif ( dist != 0.0 ) {\n \t\t\tfloat dot_p = abs( ( p.x - coord.x ) / dist * normal.x + ( p.y - coord.y ) / dist * normal.y );\n \t\t\tdist = ( dist * ( 1.0 - SQRT2_HALF_MINUS_ONE ) ) + dot_p * dist * SQRT2_MINUS_ONE;\n \t\t}\n\n \t} else if ( shape == SHAPE_LINE ) {\n\n \t\trad = pow( abs( rad ), 1.5) * rad_max;\n \t\tfloat dot_p = ( p.x - coord.x ) * normal.x + ( p.y - coord.y ) * normal.y;\n \t\tdist = hypot( normal.x * dot_p, normal.y * dot_p );\n\n \t} else if ( shape == SHAPE_SQUARE ) {\n\n \t\tfloat theta = atan( p.y - coord.y, p.x - coord.x ) - angle;\n \t\tfloat sin_t = abs( sin( theta ) );\n \t\tfloat cos_t = abs( cos( theta ) );\n \t\trad = pow( abs( rad ), 1.4 );\n \t\trad = rad_max * ( rad + ( ( sin_t > cos_t ) ? rad - sin_t * rad : rad - cos_t * rad ) );\n\n \t}\n\n \treturn rad - dist;\n\n }\n\n struct Cell {\n\n // grid sample positions\n \tvec2 normal;\n \tvec2 p1;\n \tvec2 p2;\n \tvec2 p3;\n \tvec2 p4;\n \tfloat samp2;\n \tfloat samp1;\n \tfloat samp3;\n \tfloat samp4;\n\n };\n\n vec4 getSample( vec2 point ) {\n\n // multi-sampled point\n \tvec4 tex = texture2D( tDiffuse, vec2( point.x / width, point.y / height ) );\n \tfloat base = rand( vec2( floor( point.x ), floor( point.y ) ) ) * PI2;\n \tfloat step = PI2 / float( samples );\n \tfloat dist = radius * 0.66;\n\n \tfor ( int i = 0; i < samples; ++i ) {\n\
|