Guest User

Liquid.shader

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