Search Unity

Looking for advice for 'flowing water' effect...

Discussion in 'General Graphics' started by webster, Feb 18, 2021.

  1. webster

    webster

    Joined:
    Jun 8, 2013
    Posts:
    21
    Hi, I'm trying to achieve the look of a fluid (tinted red) flowing in one direction or another. It should look like this: upload_2021-2-17_16-29-40.png
    I'm no shader expert, but what I see here is 1) bump or normal or displacement (that should scroll left or right), 2) specularity 3) refraction 4) a transparency ramp on the left side. So it would look like red fluid is materializing from thin air as it move to the right.
    I tried a simple transparent shader and animated UV's with a script, and that got me the motion, but looks awful. I tried Standard Assets Water which looks somewhat closer but has lots of issues.
    My target is webGL if that helps.
    Are there any assets available anywhere that can do this? Or shader graph that's not too hard? I'm using Unity 2019.4.20
    Edit: I realize what I want is a GLASS shader, but one where I can apply transparency (which would affect refraction as well) via a texture.
     
    Last edited: Feb 18, 2021
  2. webster

    webster

    Joined:
    Jun 8, 2013
    Posts:
    21
    Code (CSharp):
    1. Shader "Custom/Glass-Seethrough Alpha" {
    2.     Properties{
    3.         _Color("Main Color", Color) = (1,1,1,1)
    4.         _SpecColor("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    5.         _Shininess("Shininess", Range(0.03, 1)) = 0.3
    6.         _MainTex("Base (RGB)", 2D) = "white" {}
    7.         _BumpMap("Normalmap", 2D) = "bump" {}
    8.         _AlphaMap("Alphamap", 2D) = "black" {}
    9.         _DistAmt("Distortion", range(0,256)) = 100
    10.     }
    11.         SubShader{
    12.             GrabPass { }
    13.  
    14.             Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
    15.             LOD 200
    16.  
    17.             CGPROGRAM
    18.             #pragma target 3.0
    19.             #pragma exclude_renderers gles
    20.             #pragma vertex vert
    21.             #pragma surface surf BlinnPhong alpha
    22.             #include "UnityCG.cginc"
    23.  
    24.             float4 _Color;
    25.             float _Shininess;
    26.             sampler2D _MainTex;
    27.             sampler2D _BumpMap;
    28.             sampler2D _AlphaMap;
    29.             sampler2D _GrabTexture;
    30.             float _DistAmt;
    31.             float4 _GrabTexture_TexelSize;
    32.  
    33.             struct Input {
    34.                 float2 uv_MainTex;
    35.                 float2 uv_BumpMap;
    36.                 float2 uv_AlphaMap;
    37.                 float4 proj : TEXCOORD;
    38.             };
    39.  
    40.             void vert(inout appdata_full v, out Input o) {
    41.                 UNITY_INITIALIZE_OUTPUT(Input, o);
    42.                 float4 oPos = UnityObjectToClipPos(v.vertex);
    43.                 #if UNITY_UV_STARTS_AT_TOP
    44.                     float scale = -1.0;
    45.                 #else
    46.                     float scale = 1.0;
    47.                 #endif
    48.                 o.proj.xy = (float2(oPos.x, oPos.y * scale) + oPos.w) * 0.5;
    49.                 o.proj.zw = oPos.zw;
    50.             }
    51.  
    52.             void surf(Input IN, inout SurfaceOutput o) {
    53.                 half3 tint = tex2D(_MainTex, IN.uv_MainTex);
    54.                 half3 nor = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    55.                 half3 alpha = tex2D(_AlphaMap, IN.uv_AlphaMap);
    56.  
    57.                 float2 offset = nor * _DistAmt * _GrabTexture_TexelSize.xy;
    58.                 IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
    59.                 half4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(IN.proj));
    60.                 half3 tinted = col * tint * _Color;
    61.  
    62.                 o.Albedo = lerp(col, tinted, alpha);//tex.rgb * _Color.rgb * col.rgb;
    63.                 o.Normal = nor.rgb;
    64.                 o.Specular = _Shininess;
    65.                 o.Gloss = col.a;
    66.                 o.Alpha = alpha.r;
    67.             }
    68.             ENDCG
    69.         }
    70.         FallBack "Diffuse"
    71. }