Advertisement
CWHAHN

Liquid Shader

Sep 7th, 2023 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | Gaming | 0 0
  1. Shader "Custom/Liquid_Shader"
  2. {
  3. Properties
  4. {
  5. [HDR]_TintColor("Color", Color) = (1,1,1,1)
  6. [HDR]_TopColor("TopColor", Color) = (1,1,1,1)
  7. _LiquidAmount("_Liquid Amount", Float) = 1
  8.  
  9. _WorldLowestYPos("Liquid Bottom Y Pos", Float) = 0 //C#으로 부터 받은 가장 밑부분 vertex pos
  10.  
  11. _BottleHeight("Bottle Height", Float) = 0
  12. _BottleWidth("Bottle Width", Float) = 0
  13.  
  14. _WobbleX("WobbleX",Float) = 0
  15. _WobbleZ("WobbleZ",Float) = 0
  16.  
  17. [Header(Rim)]
  18. _RimPower("Rim Power", Range(0.01, 2)) = 0.1
  19. [HDR]_RimColor("Rim Color", Color) = (1,1,1,1)
  20. _RimIntensity("Rim Intensity", Range(0.1, 100)) = 1
  21. [Header(Foam)]
  22. [HDR] _FoamColor("Foam Line Color", Color) = (1,1,1,1)
  23. _Line ("Foam Line Width", Range(0,3)) = 0.0
  24. _LineSmooth("Foam Line Smoothness", Range(0,3)) = 0.0
  25. _LineBk("Back Foam Width", Range(-2,0)) = 0
  26. }
  27.  
  28. SubShader
  29. {
  30. Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" "DisableBatching" = "True"}
  31.  
  32. Pass{
  33.  
  34. Zwrite on
  35. Cull off
  36. AlphaToMask On //transparency ?
  37.  
  38. HLSLPROGRAM
  39.  
  40. #pragma vertex vert
  41. #pragma fragment frag
  42.  
  43. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  44. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  45. #pragma target 3.0
  46.  
  47.  
  48. struct Attributes
  49. {
  50. float4 positionOS : POSITION;
  51. float3 normal : NORMAL;
  52. };
  53.  
  54. struct Varyings
  55. {
  56. float4 positionHCS : SV_POSITION;
  57. float3 normal : NORMAL;
  58. float3 positionWS : TEXCOORD1;
  59. float3 liquidPosition : TEXCOORD2;
  60. float3 normalizedDirY : TEXCOORD3;
  61. float3 WSViewDir : TEXCOORD4;
  62. };
  63.  
  64. half4 _TintColor;
  65. half4 _TopColor;
  66. float _LiquidAmount;
  67.  
  68. float _WorldLowestYPos; //C#
  69. float _LocalObjectNormal; //C#
  70.  
  71. float _BottleWidth =1;
  72. float _BottleHeight = 2;
  73.  
  74. float3 _LocalYDir; //C#
  75.  
  76. float _WobbleX, _WobbleZ;
  77.  
  78.  
  79. float _RimPower,_RimIntensity;
  80. half3 _RimColor;
  81. half4 _FoamColor;
  82. float _Line, _LineSmooth;
  83. float _WSPosY;
  84.  
  85. float _LineBk;
  86.  
  87.  
  88. float3 Unity_RotateAboutAxis_Degrees(float3 In, float3 Axis, float Rotation)
  89. {
  90. Rotation = radians(Rotation);
  91. float s = sin(Rotation);
  92. float c = cos(Rotation);
  93. float one_minus_c = 1.0 - c;
  94.  
  95. Axis = normalize(Axis);
  96. float3x3 rot_mat =
  97. { 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,
  98. 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,
  99. 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
  100. };
  101. float3 Out = mul(rot_mat, In);
  102. return Out;
  103. }
  104.  
  105.  
  106. Varyings vert (Attributes IN )
  107. {
  108. Varyings OUT;
  109. OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
  110. OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
  111. OUT.normalizedDirY = normalize(float3(0, IN.positionOS.y ,0));
  112. float3 positionWS = mul(unity_ObjectToWorld, IN.positionOS.xyz);
  113.  
  114. OUT.normal = TransformObjectToWorldNormal(IN.normal.xyz);
  115. OUT.WSViewDir = normalize(_WorldSpaceCameraPos.xyz - OUT.positionWS.xyz);
  116.  
  117. // pos = worldPos(mesh center.xyz의미) - transform.position - new Vector3(0, fillAmount, 0); //C#
  118. //rend.sharedMaterial.SetVector("_FillAmount", pos); //C#
  119.  
  120.  
  121. // float3 worldPos = mul (unity_ObjectToWorld, v.vertex.xyz);
  122. //float3 worldPosOffset = float3(worldPos.x, worldPos.y , worldPos.z) - _FillAmount;
  123.  
  124. //float3 posWSX = Unity_RotateAboutAxis_Degrees(positionWS-IN.positionOS, float3(0,0,1), 90);
  125. //액체 회전 시키기 //변경하고자 하는 버텍스 포지션, 회전 기준 축 , 최대 각도 //90도로 해두어야 sin값을 곱했을 때 좌우 대칭으로 움직인다.
  126. //아니면 float3 (0,0,1)축을 음수버전을 만들어 주어야한다.
  127. float3 posWSX = Unity_RotateAboutAxis_Degrees(IN.positionOS, float3(0,0,1), 90); //IN.positionOS
  128. float3 posWSZ = Unity_RotateAboutAxis_Degrees(IN.positionOS, float3(1,0,0), 90);
  129. float3 worldPosAdjusted = OUT.positionWS + (posWSX * _WobbleX) + (posWSZ * _WobbleZ);
  130.  
  131. float dotNormal = dot(OUT.WSViewDir, OUT.normal);
  132. OUT.liquidPosition = worldPosAdjusted; //-_LiquidAmount;
  133. if(dotNormal>=0)
  134. {
  135. OUT.normal = OUT.normal;
  136. }
  137. else
  138. {
  139. OUT.normal = float3(0,1,0);
  140. }
  141.  
  142.  
  143. return OUT;
  144. }
  145.  
  146. half4 frag(Varyings IN, half facing:VFACE) : SV_Target
  147. {
  148. float3 worldYDir = float3(0,1,0);
  149. //물병과 world Y의 벌어진 각도 정도 float dotWorldContainer
  150. float dotWorldContainer =abs(dot(worldYDir.xyz, _LocalYDir.xyz));
  151. //bottle의 가로 높이 비율에 따라 눕혔을 때의 액체의 높이 조절
  152. float liquidHeight = lerp(_LiquidAmount * _BottleWidth/_BottleHeight, _LiquidAmount, dotWorldContainer); //min, max, 1-0 dot abs
  153. //액체 y 값
  154. float liquidPosition = IN.liquidPosition.y - liquidHeight; //IN.positionWS.y - _LiquidAmount;
  155.  
  156.  
  157. float cutoffTop = step(liquidPosition, _WorldLowestYPos);
  158.  
  159. //--Rim Light --------------------------------------
  160. half face = saturate(dot(IN.WSViewDir, IN.normal));
  161. half3 rim = 1.0 - (pow(face, _RimPower));
  162. ///-------------------------------------------------
  163.  
  164. //--foam--------------------------------------------
  165. float LineUpdate = lerp(_Line * _BottleWidth/_BottleHeight, _Line, dotWorldContainer);
  166. float LineSmooth = lerp(_LineSmooth * _BottleWidth/_BottleHeight, _Line, dotWorldContainer);
  167. float foam = smoothstep(_WSPosY - _Line - _LineSmooth, _WSPosY - _Line, liquidPosition); //liquidPosition
  168. float backfoam = smoothstep(_WSPosY-(_Line + _LineBk)-_LineSmooth, _WSPosY-(_Line+_LineBk),liquidPosition);
  169. ///-------------------------------------------------
  170.  
  171. //물자르기 + 물 색
  172. half4 finalResult = _TintColor + foam *_FoamColor;//cutoffTop * _TintColor;
  173. finalResult.rgb += rim * _RimColor * _RimIntensity;
  174. //면 뒷(윗)면 색
  175. half4 topColor = _TopColor + backfoam;
  176. //topColor.rgb += rim * _RimColor * _RimIntensity;
  177. //자르기 alpha clip
  178. clip(cutoffTop-0.01);
  179.  
  180. return facing > 0 ? finalResult : topColor;
  181. }
  182. ENDHLSL
  183. }
  184. }
  185. }
  186.  
  187.  
Tags: Shader Liquid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement