SHOW:
|
|
- or go back to the newest paste.
| 1 | - | Shader "Stylized Water 2/Underwater Masking test" |
| 1 | + | Shader "Stylized Water 3/Underwater Masking test" |
| 2 | {
| |
| 3 | Properties | |
| 4 | {
| |
| 5 | _Color("Color", Color) = (1,1,1, 1)
| |
| 6 | } | |
| 7 | ||
| 8 | SubShader | |
| 9 | {
| |
| 10 | Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
| |
| 11 | Blend SrcAlpha OneMinusSrcAlpha | |
| 12 | ZWrite Off | |
| 13 | ||
| 14 | Pass | |
| 15 | {
| |
| 16 | Tags { "LightMode" = "UniversalForward" }
| |
| 17 | ||
| 18 | HLSLPROGRAM | |
| 19 | #pragma target 3.0 | |
| 20 | #pragma vertex vert | |
| 21 | #pragma fragment frag | |
| 22 | ||
| 23 | //Using a shader variant is optional, as the code is still conditionally executed | |
| 24 | #pragma multi_compile_fragment _ UNDERWATER_ENABLED | |
| 25 | ||
| 26 | #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" | |
| 27 | ||
| 28 | float4 _Color; | |
| 29 | ||
| 30 | //Declare these properties | |
| 31 | bool _FullySubmerged, _UnderwaterRenderingEnabled; | |
| 32 | TEXTURE2D_X(_UnderwaterMask); | |
| 33 | //Note: you can use any sampler, as long as it doesn't use point-filtering | |
| 34 | SAMPLER(sampler_UnderwaterMask); | |
| 35 | ||
| 36 | struct appdata | |
| 37 | {
| |
| 38 | float4 vertex : POSITION; | |
| 39 | }; | |
| 40 | ||
| 41 | struct v2f | |
| 42 | {
| |
| 43 | float4 positionCS : SV_POSITION; | |
| 44 | }; | |
| 45 | ||
| 46 | v2f vert(appdata v) | |
| 47 | {
| |
| 48 | v2f o; | |
| 49 | ||
| 50 | o.positionCS = TransformObjectToHClip(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 | //Boolean toggled if the current rendering camera is valid and touching the water line | |
| 63 | if(_UnderwaterRenderingEnabled) | |
| 64 | {
| |
| 65 | //This value will be true if the top of the screen is completely below the water level | |
| 66 | //If so the pixels in this function should be completely discarded because the underwater fog consumes the visible area | |
| 67 | if(_FullySubmerged) | |
| 68 | {
| |
| 69 | alpha = 0; | |
| 70 | } | |
| 71 | else | |
| 72 | {
| |
| 73 | //Screen-space texture. White for parts that are underwater, anything else is black. | |
| 74 | half underwaterMask = SAMPLE_TEXTURE2D_X(_UnderwaterMask, sampler_UnderwaterMask, uv).r; | |
| 75 | ||
| 76 | //Will make the parts that are BELOW the water completely transparent | |
| 77 | alpha *= 1-underwaterMask; | |
| 78 | } | |
| 79 | } | |
| 80 | #endif | |
| 81 | ||
| 82 | return float4(color.rgb, alpha); | |
| 83 | ||
| 84 | } | |
| 85 | ENDHLSL | |
| 86 | } | |
| 87 | } | |
| 88 | } |