SHOW:
|
|
- or go back to the newest paste.
1 | - | Shader "Stylized Water 3/Underwater Masking test" |
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 | - | Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline" } |
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 | - | //Declare these properties |
30 | + | #include "Assets/Stylized Water 3/Shaders/Underwater/UnderwaterShading.hlsl" |
31 | - | bool _FullySubmerged, _UnderwaterRenderingEnabled; |
31 | + | |
32 | - | TEXTURE2D_X(_UnderwaterMask); |
32 | + | |
33 | - | //Note: you can use any sampler, as long as it doesn't use point-filtering |
33 | + | |
34 | - | SAMPLER(sampler_UnderwaterMask); |
34 | + | |
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 | - | //Boolean toggled if the current rendering camera is valid and touching the water line |
62 | + | //Factor to fade the alpha with, material will dissappear into the fog |
63 | - | if(_UnderwaterRenderingEnabled) |
63 | + | float fogDensity = GetUnderwaterFogDensity(input.positionWS); |
64 | - | { |
64 | + | |
65 | - | //This value will be true if the top of the screen is completely below the water level |
65 | + | //Used to restrict visibility to underwater |
66 | - | //If so the pixels in this function should be completely discarded because the underwater fog consumes the visible area |
66 | + | half underwaterMask = SampleUnderwaterMask(uv) * fogDensity; |
67 | - | if(_FullySubmerged) |
67 | + | |
68 | - | { |
68 | + | //Fade out material as it touches the water level |
69 | - | alpha = 0; |
69 | + | float waterLevel = SampleWaterLevel(input.positionWS); |
70 | - | } |
70 | + | |
71 | - | else |
71 | + | //Will make the parts that are BELOW the water completely transparent |
72 | - | { |
72 | + | alpha *= 1-underwaterMask; |
73 | - | //Screen-space texture. White for parts that are underwater, anything else is black. |
73 | + | alpha *= saturate(waterLevel - input.positionWS.y); |
74 | - | half underwaterMask = SAMPLE_TEXTURE2D_X(_UnderwaterMask, sampler_UnderwaterMask, uv).r; |
74 | + | |
75 | ||
76 | - | //Will make the parts that are BELOW the water completely transparent |
76 | + | |
77 | - | alpha *= 1-underwaterMask; |
77 | + | |
78 | - | } |
78 | + | |
79 | - | } |
79 | + | |
80 | } | |
81 | } | |
82 | } |