Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created September 11, 2021 07:24
Show Gist options
  • Save unitycoder/30b3857c5477defa9908b76689e04f84 to your computer and use it in GitHub Desktop.
Save unitycoder/30b3857c5477defa9908b76689e04f84 to your computer and use it in GitHub Desktop.
Unity Texture Bombing (rain drops)
// https://forum.unity.com/threads/texture-bombing-lets-tackle-this-one.185724/#post-1278100
Shader "Annihlator/Raindrop TextureBomber" {
    Properties {
        _MainTex("Texture (RGB)", 2D) = "black" {}
        _BumpMap ("Normalmap", 2D) = "bump" {}
        _BombTex("Bomb Texture (RGB)", 2D) = "black" {}
        _RandomTex("Random Texture (RGB)", 2D) = "black" {}
        _Slider("Slider", Range(0,1)) = 1
        _Params("Params, Z = Scale",Vector) = (0.037, 0.119, 5, 0)
        _ReflectionTex ("Reflection", 2D) = "white" { TexGen ObjectLinear }
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        Cull back
        LOD 200
       
        CGPROGRAM
        #pragma surface surf Lambert
        #pragma target 3.0
        #include "UnityCG.cginc"
 
        sampler2D _MainTex;
                sampler2D _BombTex;
                sampler2D _RandomTex;
                sampler2D _BumpMap;
                float _Slider;
                float4 _Params;
                sampler2D _ReflectionTex;
 
        struct Input {
            float2 uv_MainTex;
            float2 uv_RandomTex;
            float4 screenPos;
        };
       
        float4 MakeLoop(float2 offset, float2 cell, float3 rnd, float2 movement){
        float2 curCell = cell + movement;
        float2 curoffset = offset - movement;
        float TimeMulti = floor((_Time.z+1));
        float2 randomUV = curCell * float2 ( _Params.x*TimeMulti, _Params.y*TimeMulti );
        #if !defined(SHADER_API_OPENGL)
            rnd = tex2Dlod ( _RandomTex, float4(randomUV, 0.0, 0.0) ).xyz;
            float4 decCol = tex2Dlod( _BombTex, float4(curoffset.xy - rnd.xy, 0.0, 0.0));
            #else
            rnd = tex2D ( _RandomTex, randomUV ).xyz;
            float4 decCol = tex2D ( _BombTex, curoffset.xy - rnd.xy );
            #endif
        float Visibility = _Time.z+rnd.b*2;
        Visibility = (Visibility-floor(Visibility));
        float4 StackedDecal = decCol*smoothstep(0.5,0.75,Visibility-0.15);
        return StackedDecal;
        }
 
        void surf (Input IN, inout SurfaceOutput o) {
       
            fixed2 screenUV = (IN.screenPos.xy) / (IN.screenPos.w);
            fixed4 refl = tex2D(_ReflectionTex, screenUV);
       
            fixed3 norm = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
            float3 decalColor = tex2D ( _MainTex, IN.uv_MainTex ).xyz;
            fixed4 StackedDecal = 0;
           
            float2 ScaledUV = IN.uv_MainTex * _Params.z;
            float2 cell = floor ( ScaledUV );
            float2 offset = ScaledUV - cell;
            float3 rnd;
           
            StackedDecal = MakeLoop(offset,cell,rnd,float2(-1,-1))+MakeLoop(offset,cell,rnd,float2(-1,0))+MakeLoop(offset,cell,rnd,float2(0,-1))+MakeLoop(offset,cell,rnd,float2(0,0));
            o.Albedo = decalColor;
            fixed4 RainNormal = normalize(float4(0,0,1,1)+StackedDecal);
            o.Normal = normalize(lerp(norm,UnpackNormal(RainNormal),_Slider));
            o.Emission = refl*0.5 ;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
@unitycoder
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment