Guest User

LiquidWobble.shader

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