dirt ball

 avatar
unknown
glsl
4 years ago
4.7 kB
10
Indexable
Shader "JST/InfluenceBall"
{
 
    Properties
    {
        _MainColor("MainColor",COLOR) =  (1,1,1,1)
        _GradientColor("GradientColor",COLOR) = (1,1,1,1)
        _OutLine("OutLine", Range(0 , 1)) = .01
        _GradientRange("Gradient",Range(0,1)) = .65 

        _Test("Test",Range(0,1)) = 0
        _Test1("Test1",Range(0,100)) = 0
    }

    SubShader
    {
        Tags { "RenderPipeline"="UniversalPipeline" "RenderType"="Transparent" "Queue"="Transparent" }
        Cull Back
        AlphaToMask Off

        Pass
        {
            Name "Forward"
            Tags { "LightMode"="UniversalForward" }
            
            Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
            ZWrite Off
            ZTest LEqual
            Offset 0,0
            ColorMask RGBA


            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 positionCS : SV_POSITION;
                float3 positionWS               : TEXCOORD2;
            };

CBUFFER_START(UnityPerMaterial)
            float4 _InfluenceData[7];
            float4 _InfluenceColor[7];
            float _InfluenceDataCount;

            float _OutLine,_GradientRange;
            float4 _MainColor,_GradientColor;
            float _Test,_Test1;
CBUFFER_END

            float _CameraLevel_Auto_02;

            float Remap(float value, float From1, float To1, float From2, float To2)
            {
             return From2 + (value - From1) * (To2 - From2) / (To1 - From1);
            }

            v2f vert (appdata v)
            {
                v2f o;
                o.positionWS = TransformObjectToWorld(v.vertex.xyz);
                o.positionCS = TransformWorldToHClip(o.positionWS);
                o.uv = v.uv;
                return o;
            }

            float getMetaball(float2 pos,float2 center,float radius){

                float value = radius* radius / dot(center - pos,center - pos);
                value *= value * sqrt(value);
                return value;
            }

 

             float getBall(float2 pos,float2 center,float radius){
                float value =  clamp( length( center - pos) / radius,0,1);
 
                return  1 - value;
            }

            float4 frag (v2f i) : SV_Target
            {
                float radius =   length(i.uv - float2(0.5,0.5));
                float2 centerPos = i.positionWS.xz;

                 
 
                float balls = 0;
                float gradient = 0;
 
                for(int i =0 ; i < _InfluenceDataCount;i++)
                {
                    float2 otherWorldPos = _InfluenceData[i].xy;
                    float otherRadius =_InfluenceData[i].z ;     
                    float type = _InfluenceData[i].w;    
                    float metaball = getMetaball(otherWorldPos,centerPos,otherRadius);
                    float metaballGrad = getMetaball(otherWorldPos,centerPos,otherRadius * _GradientRange);
                    gradient += metaballGrad;
                    balls += metaball;
                }
                balls = clamp(balls,0,1);
                gradient = clamp(gradient,0,1);
 
 
                const float cutoff = 1;
                 
                 float outlineStepValue = Remap(_OutLine,0,1,cutoff,0);
                float inside = step(cutoff,balls);
                float outline = step(   outlineStepValue  ,balls);
 
                outline -= inside;
                float gradientValue =  (1 - gradient) * inside ;
             
                gradientValue = pow(gradientValue,8);

                float4 gradientColor = inside * gradientValue;                
                float4 outlineColor = outline * _MainColor;
             
                return  outlineColor + gradientColor;
            }
            ENDHLSL
        }
    }
}
Editor is loading...