Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Stylized Water 2/Underwater Masking test"
- {
- Properties
- {
- _Color("Color", Color) = (1,1,1, 1)
- }
- SubShader
- {
- Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
- Blend SrcAlpha OneMinusSrcAlpha
- ZWrite Off
- Pass
- {
- Tags { "LightMode" = "UniversalForward" }
- HLSLPROGRAM
- #pragma target 3.0
- #pragma vertex vert
- #pragma fragment frag
- //Using a shader variant is optional, as the code is still conditionally executed
- #pragma multi_compile_fragment _ UNDERWATER_ENABLED
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
- float4 _Color;
- //Declare these properties
- bool _FullySubmerged, _UnderwaterRenderingEnabled;
- TEXTURE2D_X(_UnderwaterMask);
- //Note: you can use any sampler, as long as it doesn't use point-filtering
- SAMPLER(sampler_UnderwaterMask);
- struct appdata
- {
- float4 vertex : POSITION;
- };
- struct v2f
- {
- float4 positionCS : SV_POSITION;
- };
- v2f vert(appdata v)
- {
- v2f o;
- o.positionCS = TransformObjectToHClip(v.vertex.xyz);
- return o;
- }
- half4 frag(v2f input) : SV_Target
- {
- float3 color = _Color.rgb;
- float alpha = _Color.a;
- float2 uv = GetNormalizedScreenSpaceUV(input.positionCS);
- #if UNDERWATER_ENABLED
- //Boolean toggled if the current rendering camera is valid and touching the water line
- if(_UnderwaterRenderingEnabled)
- {
- //This value will be true if the top of the screen is completely below the water level
- //If so the pixels in this function should be completely discarded because the underwater fog consumes the visible area
- if(_FullySubmerged)
- {
- alpha = 0;
- }
- else
- {
- //Screen-space texture. White for parts that are underwater, anything else is black.
- half underwaterMask = SAMPLE_TEXTURE2D_X(_UnderwaterMask, sampler_UnderwaterMask, uv).r;
- //Will make the parts that are BELOW the water completely transparent
- alpha *= 1-underwaterMask;
- }
- }
- #endif
- return float4(color.rgb, alpha);
- }
- ENDHLSL
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement