Untitled

mail@pastecode.io avatar
unknown
glsl
3 years ago
1.5 kB
25
Indexable
Never
//--------------------------------------------------------------
//First pass (Loop for each metaball, yeah, it's dirty, but you can certainly do it more clean for your project.)            
//--------------------------------------------------------------

float3 _Metaball_Position;
float _Metaball_Radius;
float4 _Metaball_Color;
float _Metaball_Smooth;

float4 smin(float4 a, float4 b, float k)
{
    float4 res = exp2(-k * a) + exp2(-k * b);
    return -log2(res) / k;
}

float4 frag(v2f i) : SV_Target
{
    float4 col = tex2D(_MainTex, i.uv);

    float2 pos = i.uv * 2.0 - 1.0;
    float dist = distance(pos, _Metaball_Position.xz);
    dist = min(dist, 1.0);
    float factor = dist - _Metaball_Radius;
    col = smin(col, _Metaball_Color * factor, _Metaball_Smooth);


    return col;
}

//--------------------------------------------------------------
//Second pass     
//--------------------------------------------------------------


sampler2D _MainTex;
float _Metaball_Smooth;

float4 frag(v2f i) : SV_Target
{
    float4 col = tex2D(_MainTex, i.uv);
    float gb = min(col.g, col.b);
    float br = min(col.b, col.r);
    float rg = min(col.r, col.g);

    float alpha = step(col.a, -(1.0 / _Metaball_Smooth) - 0.001);

    if (col.r < gb)
        return float4(1, 0, 0, 0) * alpha;// *smoothstep(0.2, 0.0, -col.r);
    if (col.g < br)
        return float4(0, 1, 0, 0) * alpha;
    if (col.b < rg)
        return float4(0, 0, 1, 0) * alpha;

    return saturate(-col);
}