Advertisement
Guest User

liquid.shader

a guest
Dec 7th, 2022
5,846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2.  
  3. Shader "Unlit/FX/Liquid"
  4. {
  5.     Properties
  6.     {
  7.         [Header(Main)]
  8.         [HDR]_Tint ("Tint", Color) = (1,1,1,1)
  9.         _MainTex ("Texture", 2D) = "white" {}
  10.         [HDR]_TopColor ("Top Color", Color) = (1,1,1,1)
  11.         [Header(Foam)]
  12.         [HDR]_FoamColor ("Foam Line Color", Color) = (1,1,1,1)
  13.         _Line ("Foam Line Width", Range(0,0.1)) = 0.0    
  14.         _LineSmooth ("Foam Line Smoothness", Range(0,0.1)) = 0.0    
  15.         [Header(Rim)]
  16.         [HDR]_RimColor ("Rim Color", Color) = (1,1,1,1)
  17.         _RimPower ("Rim Power", Range(0,10)) = 0.0
  18.         [Header(Sine)]
  19.         _Freq ("Frequency", Range(0,15)) = 8
  20.         _Amplitude ("Amplitude", Range(0,0.5)) = 0.15
  21.     }
  22.    
  23.     SubShader
  24.     {
  25.         Tags {"Queue"="Geometry"  "DisableBatching" = "True" }
  26.        
  27.         Pass
  28.         {
  29.             Zwrite On
  30.             Cull Off // we want the front and back faces
  31.             AlphaToMask On // transparency
  32.  
  33.             CGPROGRAM
  34.             #pragma vertex vert
  35.             #pragma fragment frag
  36.             // make fog work
  37.             #pragma multi_compile_fog
  38.            
  39.             #include "UnityCG.cginc"
  40.            
  41.             struct appdata
  42.             {
  43.                 float4 vertex : POSITION;
  44.                 float2 uv : TEXCOORD0;
  45.                 float3 normal : NORMAL;
  46.             };
  47.            
  48.             struct v2f
  49.             {
  50.                 float2 uv : TEXCOORD0;
  51.                 UNITY_FOG_COORDS(1)
  52.                 float4 vertex : SV_POSITION;
  53.                 float3 viewDir : COLOR;
  54.                 float3 normal : COLOR2;    
  55.                 float3 fillPosition : TEXCOORD2;
  56.                 float3 worldNormal : TEXCOORD3;
  57.             };
  58.            
  59.             sampler2D _MainTex;
  60.             float4 _MainTex_ST;
  61.             float3 _FillAmount;
  62.             float _WobbleX, _WobbleZ;
  63.             float4 _TopColor, _RimColor, _FoamColor, _Tint;
  64.             float _Line, _RimPower, _LineSmooth;
  65.             float _Freq, _Amplitude;
  66.  
  67.  
  68.             // https://docs.unity3d.com/Packages/[email protected]/manual/Rotate-About-Axis-Node.html
  69.             float3 Unity_RotateAboutAxis_Degrees(float3 In, float3 Axis, float Rotation)
  70.             {
  71.                 Rotation = radians(Rotation);
  72.                 float s = sin(Rotation);
  73.                 float c = cos(Rotation);
  74.                 float one_minus_c = 1.0 - c;
  75.  
  76.                 Axis = normalize(Axis);
  77.                 float3x3 rot_mat =
  78.                 {   one_minus_c * Axis.x * Axis.x + c, one_minus_c * Axis.x * Axis.y - Axis.z * s, one_minus_c * Axis.z * Axis.x + Axis.y * s,
  79.                     one_minus_c * Axis.x * Axis.y + Axis.z * s, one_minus_c * Axis.y * Axis.y + c, one_minus_c * Axis.y * Axis.z - Axis.x * s,
  80.                     one_minus_c * Axis.z * Axis.x - Axis.y * s, one_minus_c * Axis.y * Axis.z + Axis.x * s, one_minus_c * Axis.z * Axis.z + c
  81.                 };
  82.                 float3 Out = mul(rot_mat,  In);
  83.                 return Out;
  84.             }
  85.  
  86.  
  87.             v2f vert (appdata v)
  88.             {
  89.                 v2f o;
  90.  
  91.                 o.vertex = UnityObjectToClipPos(v.vertex);
  92.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  93.                 UNITY_TRANSFER_FOG(o,o.vertex);
  94.                 // get world position of the vertex - transform position
  95.                 float3 worldPos = mul (unity_ObjectToWorld, v.vertex.xyz);  
  96.                 float3 worldPosOffset = float3(worldPos.x, worldPos.y , worldPos.z) - _FillAmount;
  97.                 // rotate it around XY
  98.                 float3 worldPosX= Unity_RotateAboutAxis_Degrees(worldPosOffset, float3(0,0,1),90);
  99.                 // rotate around XZ
  100.                 float3 worldPosZ = Unity_RotateAboutAxis_Degrees(worldPosOffset, float3(1,0,0),90);
  101.                 // combine rotations with worldPos, based on sine wave from script
  102.                 float3 worldPosAdjusted = worldPos + (worldPosX  * _WobbleX)+ (worldPosZ* _WobbleZ);
  103.                 // how high up the liquid is
  104.                 o.fillPosition =  worldPosAdjusted - _FillAmount;
  105.                 o.viewDir = normalize(WorldSpaceViewDir(v.vertex));
  106.                 o.normal = v.normal;
  107.                 o.worldNormal  = mul ((float4x4)unity_ObjectToWorld, v.normal );
  108.                 return o;
  109.             }
  110.            
  111.             fixed4 frag (v2f i, fixed facing : VFACE) : SV_Target
  112.             {          
  113.                 float3 worldNormal = mul( unity_ObjectToWorld, float4( i.normal, 0.0 ) ).xyz;
  114.                 // rim light              
  115.                 float fresnel = pow(1 - saturate(dot(worldNormal, i.viewDir)), _RimPower);          
  116.                 float4 RimResult = fresnel * _RimColor;
  117.                 RimResult *= _RimColor;
  118.                
  119.                 // add movement based deform, using a sine wave
  120.                 float wobbleIntensity =  abs(_WobbleX) + abs(_WobbleZ);            
  121.                 float wobble = sin((i.fillPosition.x * _Freq) + (i.fillPosition.z * _Freq ) + ( _Time.y)) * (_Amplitude *wobbleIntensity);              
  122.                 float movingfillPosition = i.fillPosition.y + wobble;
  123.  
  124.                 // sample the texture based on the fill line
  125.                 fixed4 col = tex2D(_MainTex, movingfillPosition) * _Tint;
  126.                 // apply fog
  127.                 UNITY_APPLY_FOG(i.fogCoord, col);
  128.  
  129.                 // foam edge
  130.                 float cutoffTop = step(movingfillPosition, 0.5);
  131.                 float foam = cutoffTop * smoothstep(0.5 - _Line- _LineSmooth, 0.5 - _Line ,movingfillPosition);
  132.                 float4 foamColored = foam * _FoamColor;
  133.  
  134.                 // rest of the liquid minus the foam
  135.                 float result = cutoffTop - foam;
  136.                 float4 resultColored = result * col;
  137.  
  138.                 // both together, with the texture
  139.                 float4 finalResult = resultColored + foamColored;              
  140.                 finalResult.rgb += RimResult;
  141.  
  142.                 // little edge on the top of the backfaces
  143.                 float backfaceFoam = (cutoffTop * smoothstep(0.5 - (0.2 * _Line)- _LineSmooth,0.5 - (0.2 * _Line),movingfillPosition ));
  144.                 float4 backfaceFoamColor = _FoamColor * backfaceFoam;
  145.                 // color of backfaces/ top
  146.                 float4 topColor = (_TopColor * (1-backfaceFoam) + backfaceFoamColor) * (foam + result);
  147.  
  148.                 // clip above the cutoff
  149.                 clip(result + foam - 0.01);
  150.  
  151.                 //VFACE returns positive for front facing, negative for backfacing
  152.                 return facing > 0 ? finalResult: topColor;
  153.                
  154.                
  155.             }
  156.             ENDCG
  157.         }
  158.        
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement