SnightCoder

Liquid For Android.shader

Nov 8th, 2019
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. Shader "Unlit/SpecialFX/Liquid"
  2. {
  3. Properties
  4. {
  5. _Tint ("Tint", Color) = (1,1,1,1)
  6. _MainTex ("Texture", 2D) = "white" {}
  7. _FillAmount ("Fill Amount", Range(-1,1)) = 0.0
  8. [HideInInspector] _WobbleX ("WobbleX", Range(-1,1)) = 0.0
  9. [HideInInspector] _WobbleZ ("WobbleZ", Range(-1,1)) = 0.0
  10. _TopColor ("Top Color", Color) = (1,1,1,1)
  11. _FoamColor ("Foam Line Color", Color) = (1,1,1,1)
  12. _Rim ("Foam Line Width", Range(0,0.1)) = 0.0
  13. _RimColor ("Rim Color", Color) = (1,1,1,1)
  14. _RimPower ("Rim Power", Range(0,100)) = 0.0
  15. }
  16.  
  17. SubShader
  18. {
  19. Tags {"Queue"="Transparent" "DisableBatching" = "True" }
  20.  
  21. Pass
  22. {
  23. Zwrite off
  24. Cull Off // we want the front and back faces
  25. AlphaToMask off // transparency
  26. Blend SrcAlpha OneMinusSrcAlpha
  27.  
  28. CGPROGRAM
  29.  
  30.  
  31. #pragma vertex vert
  32. #pragma fragment frag
  33. // make fog work
  34. #pragma multi_compile_fog
  35.  
  36. #include "UnityCG.cginc"
  37.  
  38. struct appdata
  39. {
  40. float4 vertex : POSITION;
  41. float2 uv : TEXCOORD0;
  42. float3 normal : NORMAL;
  43. };
  44.  
  45. struct v2f
  46. {
  47. float2 uv : TEXCOORD0;
  48. UNITY_FOG_COORDS(1)
  49. float4 vertex : SV_POSITION;
  50. float3 viewDir : COLOR;
  51. float3 normal : COLOR2;
  52. float fillEdge : TEXCOORD2;
  53. };
  54.  
  55. sampler2D _MainTex;
  56. float4 _MainTex_ST;
  57. float _FillAmount, _WobbleX, _WobbleZ;
  58. float4 _TopColor, _RimColor, _FoamColor, _Tint;
  59. float _Rim, _RimPower;
  60.  
  61. float4 RotateAroundYInDegrees (float4 vertex, float degrees)
  62. {
  63. float alpha = degrees * UNITY_PI / 180;
  64. float sina, cosa;
  65. sincos(alpha, sina, cosa);
  66. float2x2 m = float2x2(cosa, sina, -sina, cosa);
  67. return float4(vertex.yz , mul(m, vertex.xz)).xzyw ;
  68. }
  69.  
  70.  
  71. v2f vert (appdata v)
  72. {
  73. v2f o;
  74.  
  75. o.vertex = UnityObjectToClipPos(v.vertex);
  76. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  77. UNITY_TRANSFER_FOG(o,o.vertex);
  78. // get world position of the vertex
  79. float3 worldPos = mul (unity_ObjectToWorld, v.vertex.xyz);
  80. // rotate it around XY
  81. float3 worldPosX= RotateAroundYInDegrees(float4(worldPos,0),360);
  82. // rotate around XZ
  83. float3 worldPosZ = float3 (worldPosX.y, worldPosX.z, worldPosX.x);
  84. // combine rotations with worldPos, based on sine wave from script
  85. float3 worldPosAdjusted = worldPos + (worldPosX * _WobbleX)+ (worldPosZ* _WobbleZ);
  86. // how high up the liquid is
  87. o.fillEdge = worldPosAdjusted.y + _FillAmount;
  88.  
  89. o.viewDir = normalize(ObjSpaceViewDir(v.vertex));
  90. o.normal = v.normal;
  91. return o;
  92. }
  93.  
  94. fixed4 frag (v2f i, fixed facing : VFACE) : SV_Target
  95. {
  96. // sample the texture
  97. fixed4 col = tex2D(_MainTex, i.uv) * _Tint;
  98. // apply fog
  99. UNITY_APPLY_FOG(i.fogCoord, col);
  100.  
  101. // rim light
  102. float dotProduct = 1 - pow(dot(i.normal, i.viewDir), _RimPower);
  103. float4 RimResult = smoothstep(0.5, 1.0, dotProduct);
  104. RimResult *= _RimColor;
  105.  
  106. // foam edge
  107. float4 foam = ( step(i.fillEdge, 0.5) - step(i.fillEdge, (0.5 - _Rim))) ;
  108. float4 foamColored = foam * (_FoamColor * 0.9);
  109. // rest of the liquid
  110. float4 result = step(i.fillEdge, 0.5) - foam;
  111. float4 resultColored = result * col;
  112. // both together, with the texture
  113. float4 finalResult = resultColored + foamColored;
  114. finalResult.rgb += RimResult;
  115.  
  116. // color of backfaces/ top
  117. float4 topColor = _TopColor * (foam + result);
  118. //VFACE returns positive for front facing, negative for backfacing
  119. return facing > 0 ? finalResult: topColor;
  120.  
  121. }
  122. ENDCG
  123. }
  124.  
  125. }
  126. }
Add Comment
Please, Sign In to add comment