This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hvTerrain.hlsl
287 lines (239 loc) · 12.9 KB
/
hvTerrain.hlsl
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#ifndef HVTERRAIN
#define HVTERRAIN
half2 _TexturesScale[256];
SAMPLER(SamplerState_Linear_Repeat);
SAMPLER(SamplerState_Point_Clamp);
half4 _AlbedoMaps_TexelSize;
// https://www.gamedev.net/tutorials/programming/graphics/advanced-terrain-texture-splatting-r3287/
// inline half4 BlendTexture(half4 texA, half4 texB, half t, half depth)
// {
// half lumiA = Luminance(texA.rgb) / _TextureBlend0;
// half lumiB = Luminance(texB.rgb) / _TextureBlend0;
// half tA = 1.0 - t;
// half tB = t;
// half ma = max(lumiA + tA, lumiB + tB) - depth;
// half b1 = max(lumiA + tA - ma, 0);
// half b2 = max(lumiB + tB - ma, 0);
// return (texA * b1 + texB * b2) / (b1 + b2);
// }
half Sample_Terrain_HolesMap(half2 uv)
{
half hole = SAMPLE_TEXTURE2D(_HolesMap, sampler_HolesMap, uv).r;
return hole == 0.0f ? -1 : 1;
}
inline half4 Sample_Terrain_Texture2DArray_ID01(Texture2DArray tex, SamplerState ss, half2 idUV, half2 texUV, half4 ddxy)
{
half3 blendData = SAMPLE_TEXTURE2D_LOD(_BlendMap0, SamplerState_Point_Clamp, idUV, 0).rgb;
int id0 = blendData.r * 255;
int id1 = blendData.g * 255;
half weight = blendData.b;
half2 tilling0 = _TexturesScale[id0];
half2 tilling1 = _TexturesScale[id1];
half4 ddxy0 = half4(ddxy / ((tilling0.x + tilling0.y) / 2));
half4 ddxy1 = half4(ddxy / ((tilling1.x + tilling1.y) / 2));
half2 uv0 = texUV / tilling0.xy;
half2 uv1 = texUV / tilling1.xy;
half4 texSample0 = PLATFORM_SAMPLE_TEXTURE2D_ARRAY_GRAD(tex, ss, uv0, id0, ddxy0.xy, ddxy0.zw);
half4 texSample1 = PLATFORM_SAMPLE_TEXTURE2D_ARRAY_GRAD(tex, ss, uv1, id1, ddxy1.xy, ddxy1.zw);
// return BlendTexture(texSample0, texSample1, weight, _TextureBlend1);
return lerp(texSample0, texSample1, weight);
}
inline half4 Sample_Terrain_Texture2DArray_ID0(Texture2DArray tex, SamplerState ss, half2 idUV, half2 texUV, half4 ddxy)
{
half blendData = SAMPLE_TEXTURE2D_LOD(_BlendMap0, SamplerState_Point_Clamp, idUV, 0).r;
int id0 = blendData * 255;
half2 tilling0 = _TexturesScale[id0];
half4 ddxy0 = half4(ddxy / ((tilling0.x + tilling0.y) / 2));
half2 uv0 = texUV / tilling0.xy;
half4 texSample0 = PLATFORM_SAMPLE_TEXTURE2D_ARRAY_GRAD(tex, ss, uv0, id0, ddxy0.xy, ddxy0.zw);
return texSample0;
}
// https://community.khronos.org/t/manual-bilinear-filter/58504/8
// https://community.khronos.org/t/bilinear-interpolation-texture-rendering/52613
inline half4 Sample_Terrain_Texture2DArray_ID01_Bilinear(Texture2DArray tex, SamplerState ss, half2 idUV, half2 texUV, half4 ddxy)
{
half2 f = frac(idUV * _BlendMap0_TexelSize.zw);
// offset to fix seam
idUV += (0.5 - f) * _BlendMap0_TexelSize.xy;
half4 bottomL = Sample_Terrain_Texture2DArray_ID01(tex, ss, idUV, texUV, ddxy);
half4 bottomR = Sample_Terrain_Texture2DArray_ID01(tex, ss, idUV + half2(_BlendMap0_TexelSize.x, 0), texUV, ddxy);
half4 topL = Sample_Terrain_Texture2DArray_ID01(tex, ss, idUV + half2(0, _BlendMap0_TexelSize.y), texUV, ddxy);
half4 topR = Sample_Terrain_Texture2DArray_ID01(tex, ss, idUV + half2(_BlendMap0_TexelSize.x, _BlendMap0_TexelSize.y), texUV, ddxy);
half4 colA = lerp(bottomL, bottomR, f.x);
half4 colB = lerp(topL, topR, f.x);
return lerp(colA, colB, f.y);
}
inline half4 Sample_Terrain_Texture2DArray_ID0_Bilinear(Texture2DArray tex, SamplerState ss, half2 idUV, half2 texUV, half4 ddxy)
{
half2 f = frac(idUV * _BlendMap0_TexelSize.zw);
// offset to fix seam
idUV += (0.5 - f) * _BlendMap0_TexelSize.xy;
half4 bottomL = Sample_Terrain_Texture2DArray_ID0(tex, ss, idUV, texUV, ddxy);
half4 bottomR = Sample_Terrain_Texture2DArray_ID0(tex, ss, idUV + half2(_BlendMap0_TexelSize.x, 0), texUV, ddxy);
half4 topL = Sample_Terrain_Texture2DArray_ID0(tex, ss, idUV + half2(0, _BlendMap0_TexelSize.y), texUV, ddxy);
half4 topR = Sample_Terrain_Texture2DArray_ID0(tex, ss, idUV + half2(_BlendMap0_TexelSize.x, _BlendMap0_TexelSize.y), texUV, ddxy);
half4 colA = lerp(bottomL, bottomR, f.x);
half4 colB = lerp(topL, topR, f.x);
return lerp(colA, colB, f.y);
}
inline half3 UnpackNormal_TS(half2 normalTSSample)
{
// more vecs
half3 normalTS;
normalTS.xy = normalTSSample.rg * 2.0 - 1.0;
normalTS.z = max(1.0e-16, sqrt(1.0 - saturate(dot(normalTS.xy, normalTS.xy))));
// normalTS.xy *= 2;
return normalTS;
}
// using triplanar code from: https://bgolus.medium.com/normal-mapping-for-a-triplanar-shader-10bf39dca05a
void Fragment_half(in half2 inUV, in half3 inPositionWS, out half3 outColor, out half3 outNormal, out half outSmoothness, out half outAlpha)
{
// derivs
half4 ddxy = half4(ddx(inUV * _BlendMap0_TexelSize.z), ddy(inUV * _BlendMap0_TexelSize.w)) / _MipMapFactor;
// offset to fix seam + proper fit (magic number i dunno)
half2 blendMapUV = inUV - inUV * _BlendMap0_TexelSize.xy;
// samples
half4 blendSample1 = SAMPLE_TEXTURE2D(_BlendMap1, SamplerState_Linear_Repeat, blendMapUV);
half3 normalWS = blendSample1.xyz * 2.0 - 1.0;
#if TRIPLANAR
half axisBlend = blendSample1.a;
#endif
// alpha0 is coded as: 00000[000] -> [bilinear x z] (x z are uv axis)
// hopefully GPUs support bitwise ops ?
int alpha0 = SAMPLE_TEXTURE2D_LOD(_BlendMap0, SamplerState_Point_Clamp, blendMapUV, 0).a * 255;
#if TRIPLANAR
bool isX = alpha0 & 0x2;
bool isZ = alpha0 & 0x1;
#endif
#if BILINEAR
bool useBilinear = alpha0 & 0x4;
#endif
#if NORMALMAP
// Get the sign (-1 or 1) of the surface normal
half3 axisSign = sign(normalWS);
axisSign = axisSign == 0 ? 1 : axisSign;
// Construct tangent to world matrices for each axis
half3 tangentY = normalize(cross(normalWS, half3(0, 0, axisSign.y)));
half3 bitangentY = normalize(cross(tangentY, normalWS)) * axisSign.y;
half3x3 tbnY = half3x3(tangentY, bitangentY, normalWS);
#endif
// get some vecs
half2 yUV = inPositionWS.xz; // y facing plane
#if TRIPLANAR
half2 xUV = inPositionWS.zy; // x facing plane
half2 zUV = inPositionWS.xy; // z facing plane
half2 texUV = lerp(lerp(yUV, xUV, isX), zUV, isZ);
#if NORMALMAP
// define tbns for all axis
half3 tangentX = normalize(cross(normalWS, half3(0, axisSign.x, 0)));
half3 bitangentX = normalize(cross(tangentX, normalWS)) * axisSign.x;
half3x3 tbnX = half3x3(tangentX, bitangentX, normalWS);
half3 tangentZ = normalize(cross(normalWS, half3(0, -axisSign.z, 0)));
half3 bitangentZ = normalize(-cross(tangentZ, normalWS)) * axisSign.z;
half3x3 tbnZ = half3x3(tangentZ, bitangentZ, normalWS);
half3x3 tbn = lerp(lerp(tbnY, tbnX, isX), tbnZ, isZ);
#endif
#else
half2 texUV = yUV;
#if NORMALMAP
half3x3 tbn = tbnY;
#endif
#endif
half4 albedoSmooth;
half3 normalWSblended = normalWS;
#if NORMALMAP
half3 normalTS;
#endif
half holes;
#if HOLES
holes = Sample_Terrain_HolesMap(inUV);
#else
holes = 1;
#endif
#if BILINEAR
#if USEBRANCHES
UNITY_BRANCH
#endif
if (useBilinear)
{
albedoSmooth = Sample_Terrain_Texture2DArray_ID01_Bilinear(_AlbedoMaps, SamplerState_Linear_Repeat, blendMapUV, texUV, ddxy);
#if NORMALMAP
normalTS = UnpackNormal_TS(Sample_Terrain_Texture2DArray_ID0_Bilinear(_NormalMaps, SamplerState_Linear_Repeat, blendMapUV, texUV, ddxy).rg);
normalWSblended = normalize(mul(normalTS, tbn));
#endif
#if TRIPLANAR
// this branch blurs the 'triplanar' at bad angles
#if USEBRANCHES
UNITY_BRANCH
#endif
if (axisBlend > 0)
{
// blends
half3 blend = pow(normalWS, 4);
blend /= dot(blend, half3(1,1,1));
// albedo
half4 xAlbedoSample = Sample_Terrain_Texture2DArray_ID01_Bilinear(_AlbedoMaps, SamplerState_Linear_Repeat, blendMapUV, xUV, ddxy);
half4 yAlbedoSample = Sample_Terrain_Texture2DArray_ID01_Bilinear(_AlbedoMaps, SamplerState_Linear_Repeat, blendMapUV, yUV, ddxy);
half4 zAlbedoSample = Sample_Terrain_Texture2DArray_ID01_Bilinear(_AlbedoMaps, SamplerState_Linear_Repeat, blendMapUV, zUV, ddxy);
albedoSmooth = lerp(albedoSmooth, xAlbedoSample * blend.x + yAlbedoSample * blend.y + zAlbedoSample * blend.z, axisBlend);
// normals
#if NORMALMAP
half3 xNormalTSSample = UnpackNormal_TS(Sample_Terrain_Texture2DArray_ID0_Bilinear(_NormalMaps, SamplerState_Linear_Repeat, blendMapUV, xUV, ddxy).rg);
half3 yNormalTSSample = UnpackNormal_TS(Sample_Terrain_Texture2DArray_ID0_Bilinear(_NormalMaps, SamplerState_Linear_Repeat, blendMapUV, yUV, ddxy).rg);
half3 zNormalTSSample = UnpackNormal_TS(Sample_Terrain_Texture2DArray_ID0_Bilinear(_NormalMaps, SamplerState_Linear_Repeat, blendMapUV, zUV, ddxy).rg);
// Apply tangent to world matrix and triblend
normalWSblended = lerp(normalWSblended, normalize(
mul(xNormalTSSample, tbnX) * blend.x +
mul(yNormalTSSample, tbnY) * blend.y +
mul(zNormalTSSample, tbnZ) * blend.z),
axisBlend);
#endif
}
#endif
}
else
{
#endif
albedoSmooth = Sample_Terrain_Texture2DArray_ID01(_AlbedoMaps, SamplerState_Linear_Repeat, blendMapUV, texUV, ddxy);
#if NORMALMAP
normalTS = UnpackNormal_TS(Sample_Terrain_Texture2DArray_ID0(_NormalMaps, SamplerState_Linear_Repeat, blendMapUV, texUV, ddxy).rg);
normalWSblended = normalize(mul(normalTS, tbn));
#endif
#if TRIPLANAR
// this branch blurs the 'triplanar' at bad angles
#if USEBRANCHES
UNITY_BRANCH
#endif
if (axisBlend > 0)
{
// blends
half3 blend = pow(normalWS, 4);
blend /= dot(blend, half3(1,1,1));
// albedo
half4 xAlbedoSample = Sample_Terrain_Texture2DArray_ID01(_AlbedoMaps, SamplerState_Linear_Repeat, blendMapUV, xUV, ddxy);
half4 yAlbedoSample = Sample_Terrain_Texture2DArray_ID01(_AlbedoMaps, SamplerState_Linear_Repeat, blendMapUV, yUV, ddxy);
half4 zAlbedoSample = Sample_Terrain_Texture2DArray_ID01(_AlbedoMaps, SamplerState_Linear_Repeat, blendMapUV, zUV, ddxy);
albedoSmooth = lerp(albedoSmooth, xAlbedoSample * blend.x + yAlbedoSample * blend.y + zAlbedoSample * blend.z, axisBlend);
// normals
#if NORMALMAP
half3 xNormalTSSample = UnpackNormal_TS(Sample_Terrain_Texture2DArray_ID0(_NormalMaps, SamplerState_Linear_Repeat, blendMapUV, xUV, ddxy).rg);
half3 yNormalTSSample = UnpackNormal_TS(Sample_Terrain_Texture2DArray_ID0(_NormalMaps, SamplerState_Linear_Repeat, blendMapUV, yUV, ddxy).rg);
half3 zNormalTSSample = UnpackNormal_TS(Sample_Terrain_Texture2DArray_ID0(_NormalMaps, SamplerState_Linear_Repeat, blendMapUV, zUV, ddxy).rg);
// Apply tangent to world matrix and triblend
normalWSblended = lerp(normalWSblended, normalize(
mul(xNormalTSSample, tbnX) * blend.x +
mul(yNormalTSSample, tbnY) * blend.y +
mul(zNormalTSSample, tbnZ) * blend.z),
axisBlend);
#endif
}
#endif
#if BILINEAR
}
#endif
outColor = albedoSmooth;
outNormal = normalWSblended;
outSmoothness = albedoSmooth.a;
outAlpha = holes;
}
#endif