-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShoreline.shader
155 lines (117 loc) · 5.11 KB
/
Shoreline.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Shader "Custom/Shoreline"
{
Properties
{
_DeepColor ("Deep Color", Color) = (1, 1, 1, 1)
_ShallowColor ("Shallow Color", Color) = (1, 1, 1, 1)
// Fade length
_DepthFactor ("Depth Factor", float) = 1
// Fade Smoothness
_DepthPow ("Depth Power", float) = 0.5
// Color of the shoreline foam
_FoamColor ("Foam Color", Color) = (1, 1, 1, 1)
// Shoreline Strenght
_ShorelinePow ("Shoreline Strength", float) = 2
// Shoreline Size
_ShorelineThresh ("Shoreline Size", float) = 0.1
[NoScaleOffset] _NormalMap ("Normal Map", 2D) = "bump" {}
}
SubShader
{
Tags {"Queue" = "Transparent" "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _DeepColor;
float4 _ShallowColor;
float _DepthFactor;
float _DepthPow;
float4 _FoamColor;
float _ShorelinePow;
float _ShorelineThresh;
sampler2D _NormalMap;
UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
//sampler2D FoamMap;
struct MeshData
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
float4 tangent : TANGENT;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float3 normal : TEXTCOORD1;
float4 tangent : TEXTCOORD2;
float4 bitangent : TEXTCOORD3;
float4 screenPos : TEXTCOORD4;
float4 viewVector : TEXTCOORD5;
float3 worldPos : TEXTCOORD6;
float3 worldNormal : TEXTCOORD7;
};
v2f vert (MeshData v)
{
v2f o;
/*
o.normal = UnityObjectToWorldNormal(v.normal);
o.tangent = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w);
o.bitangent = float4(cross(o.normal.xyz, o.tangent) * (v.tangent.w * unity_WorldTransformParams.w), v.tangent.w);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.screenPos = ComputeScreenPos(v.vertex);
o.worldNormal = normalize(mul(unity_ObjectToWorld, float4(v.normal, 0)).xyz);
*/
o.vertex = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeScreenPos(o.vertex);
COMPUTE_EYEDEPTH(o.screenPos.z);
//o.uv = v.uv.xy;
//o.screenPos = ComputeScreenPos(v.vertex);
// Lastly, pass the vertex, after wave modulation
//o.vertex = UnityObjectToClipPos(v.vertex);
//float3 viewVector = mul(unity_CameraInvProjection, float4((o.screenPos.xy / o.screenPos.w) * 2 - 1, 0, -1));
//o.viewVector = mul(unity_CameraToWorld, float4(viewVector, 0));
return o;
}
float4 frag (v2f i) : SV_Target
{
// Find the water depth (zBuffer - distance)
float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos)));
float depth = sceneZ - i.screenPos.z;
// Fade the water
float depthFade = saturate((abs(pow(depth, _DepthPow))) / _DepthFactor);
float4 color = lerp(_ShallowColor, _DeepColor, depthFade);
// Find the shoreline intersection
float intersection = saturate((abs(depth)) / _ShorelineThresh);
// Add the foam color on the intersection
float shoreline = 1 - intersection;
color += _FoamColor * pow(shoreline, 4) * _ShorelinePow * 2;
color.a = intersection;
return color;
/*
// Applies Normal Map to Normals
float3 tangentSpaceNormal = UnpackNormal(tex2D(_NormalMap, i.uv));
float3x3 mtxTangentToWorld = {
i.tangent.x, i.bitangent.x, i.normal.x,
i.tangent.y, i.bitangent.y, i.normal.y,
i.tangent.z, i.bitangent.z, i.normal.z
};
float3 normal = mul(mtxTangentToWorld, tangentSpaceNormal);
float nonLinearDepth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, i.screenPos);
float depth = LinearEyeDepth(nonLinearDepth);
float dstToWater = i.screenPos.w;
float waterViewDepth = depth - dstToWater;
float waterAlpha = 1;
float colDepthFactor = 4;
float3 waterColor = lerp(_ShallowColor, _DeepColor, 1 - exp(-waterViewDepth * colDepthFactor));
return float4(waterColor, waterAlpha);
*/
}
ENDCG
}
}
}