Advertisement
Staggart

Stylized Water 3 - Underwater Transparent

May 19th, 2025
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Stylized Water 3/Underwater Transparent"
  2. {
  3.     Properties
  4.     {
  5.         _Color("Color", Color) = (1,1,1, 1)
  6.     }
  7.    
  8.     SubShader
  9.     {
  10.         //Offset the render queue so that the material actually renders after the underwater fog!
  11.         Tags { "RenderType" = "Transparent" "Queue" = "Transparent+2" "RenderPipeline" = "UniversalPipeline" }
  12.         Blend SrcAlpha OneMinusSrcAlpha
  13.         ZWrite Off
  14.  
  15.         Pass
  16.         {
  17.             Tags { "LightMode" = "UniversalForward" }
  18.  
  19.             HLSLPROGRAM
  20.             #pragma target 3.0
  21.             #pragma vertex vert
  22.             #pragma fragment frag
  23.  
  24.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  25.            
  26.             //Using a shader variant is optional, as the code is still conditionally executed
  27.             #pragma multi_compile_fragment _ UNDERWATER_ENABLED
  28.            
  29.             //Include this library
  30.             #include "Assets/Stylized Water 3/Shaders/Underwater/UnderwaterShading.hlsl"
  31.  
  32.             float4 _Color;
  33.                        
  34.             struct appdata
  35.             {
  36.                 float4 vertex : POSITION;
  37.             };
  38.  
  39.             struct v2f
  40.             {
  41.                 float4 positionCS     : SV_POSITION;
  42.                 float3 positionWS     : TEXCOORD0;
  43.             };
  44.  
  45.             v2f vert(appdata v)
  46.             {
  47.                 v2f o;
  48.                
  49.                 o.positionCS = TransformObjectToHClip(v.vertex.xyz);
  50.                 o.positionWS = TransformObjectToWorld(v.vertex.xyz);
  51.  
  52.                 return o;
  53.             }
  54.  
  55.             half4 frag(v2f input) : SV_Target
  56.             {
  57.                 float3 color = _Color.rgb;
  58.                 float alpha = _Color.a;
  59.                 float2 uv = GetNormalizedScreenSpaceUV(input.positionCS);
  60.  
  61.                 #if UNDERWATER_ENABLED
  62.                 //Factor to fade the alpha with, material will dissappear into the fog
  63.                 float fogDensity = GetUnderwaterFogDensity(input.positionWS);
  64.  
  65.                 //Used to restrict visibility to underwater
  66.                 half underwaterMask = SampleUnderwaterMask(uv) * fogDensity;
  67.  
  68.                 //Fade out material as it touches the water level
  69.                 float waterLevel = SampleWaterLevel(input.positionWS);
  70.  
  71.                 //Will make the parts that are BELOW the water completely transparent      
  72.                 alpha *= 1-underwaterMask;
  73.                 alpha *= saturate(waterLevel - input.positionWS.y);
  74.                 #endif
  75.                
  76.                 return float4(color.rgb, alpha);
  77.  
  78.             }
  79.             ENDHLSL
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement