Skip to content

Commit ed6b745

Browse files
authored
Add files via upload
1 parent 8677d5e commit ed6b745

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#ifndef __PBR_TERRAIN_UTILS_MODULE__
2+
#define __PBR_TERRAIN_UTILS_MODULE__
3+
4+
#import "Common/MatDefs/ShaderLib/PBRTerrainTextureLayer.glsl"
5+
6+
#import "MatDefs/ShaderLib/TangentUtils.glsllib"
7+
#import "MatDefs/ShaderLib/TriPlanarUtils.glsllib"
8+
9+
#ifdef ENABLE_PBRTerrainUtils_readPBRTerrainLayers
10+
11+
//texture arrays:
12+
uniform sampler2DArray m_AlbedoTextureArray;
13+
uniform sampler2DArray m_NormalParallaxTextureArray;
14+
uniform sampler2DArray m_MetallicRoughnessAoEiTextureArray;
15+
16+
//texture-slot params for 12 unique texture slots (0-11) where the integer value points to the desired texture's index in the corresponding texture array:
17+
#for i=0..12 (#ifdef ALBEDOMAP_$i $0 #endif)
18+
uniform int m_AfflictionMode_$i;
19+
uniform float m_Roughness_$i;
20+
uniform float m_Metallic_$i;
21+
uniform float m_AlbedoMap_$i_scale;
22+
uniform vec4 m_EmissiveColor_$i;
23+
24+
uniform int m_AlbedoMap_$i;
25+
26+
#endfor
27+
#for n=0..12 (#ifdef METALLICROUGHNESSMAP_$n $0 #endif)
28+
uniform int m_MetallicRoughnessMap_$n;
29+
#endfor
30+
#for x=0..12 (#ifdef NORMALMAP_$x $0 #endif)
31+
uniform int m_NormalMap_$x;
32+
#endfor
33+
34+
//3 alpha maps :
35+
#ifdef ALPHAMAP
36+
uniform sampler2D m_AlphaMap;
37+
#endif
38+
#ifdef ALPHAMAP_1
39+
uniform sampler2D m_AlphaMap_1;
40+
#endif
41+
#ifdef ALPHAMAP_2
42+
uniform sampler2D m_AlphaMap_2;
43+
#endif
44+
vec4 alphaBlend_0, alphaBlend_1, alphaBlend_2;
45+
46+
47+
void PBRTerrainUtils_readAlphaMaps(){
48+
49+
#ifdef ALPHAMAP
50+
alphaBlend_0 = texture2D( m_AlphaMap, texCoord.xy );
51+
#endif
52+
#ifdef ALPHAMAP_1
53+
alphaBlend_1 = texture2D( m_AlphaMap_1, texCoord.xy );
54+
#endif
55+
#ifdef ALPHAMAP_2
56+
alphaBlend_2 = texture2D( m_AlphaMap_2, texCoord.xy );
57+
#endif
58+
59+
60+
}
61+
62+
float PBRTerrainUtils_getAlphaBlendFromChannel(int layer){
63+
float finalAlphaBlendForLayer = 0.0;
64+
vec4 alphaBlend;
65+
if(layer <= 3.0){
66+
alphaBlend = alphaBlend_0;
67+
}else if(layer <= 7.0){
68+
alphaBlend = alphaBlend_1;
69+
}else if(layer <= 11.0){
70+
alphaBlend = alphaBlend_2;
71+
}
72+
int texChannelForAlphaBlending = int(mod(float(layer), 4.0)); //pick the correct channel (r g b or a) based on the layer's index
73+
switch(texChannelForAlphaBlending) {
74+
case 0:
75+
finalAlphaBlendForLayer = alphaBlend.r;
76+
break;
77+
case 1:
78+
finalAlphaBlendForLayer = alphaBlend.g;
79+
break;
80+
case 2:
81+
finalAlphaBlendForLayer = alphaBlend.b;
82+
break;
83+
case 3:
84+
finalAlphaBlendForLayer = alphaBlend.a;
85+
break;
86+
}
87+
88+
return finalAlphaBlendForLayer;
89+
}
90+
91+
92+
PBRTerrainTextureLayer PBRTerrainUtils_createAdvancedPBRTerrainLayer(int layerNum){
93+
94+
PBRTerrainTextureLayer terrainTextureLayer;
95+
terrainTextureLayer.blendValue = PBRTerrainUtils_getAlphaBlendFromChannel(layerNum);
96+
97+
return terrainTextureLayer;
98+
}
99+
100+
//________
101+
102+
void updateLayerFromPackedAlbedoMap(inout vec4 packedAlbedoVec, inout PBRTerrainTextureLayer layer){
103+
layer.albedo = packedAlbedoVec;
104+
layer.alpha = packedAlbedoVec.a;
105+
}
106+
void updateLayerFromPackedNormalParallaxVec(inout vec4 packedNormalParallaxVec, inout PBRTerrainTextureLayer layer){
107+
layer.normal = calculateTangentsAndApplyToNormals(packedNormalParallaxVec.rgb, PBRLightingUtils_getWorldNormal());
108+
layer.height = packedNormalParallaxVec.a;
109+
}
110+
void updateLayerFromPackedMRAoEiVec(inout vec4 packedMRAoEiVec, inout PBRTerrainTextureLayer layer){
111+
layer.ao = packedMRAoEiVec.r;
112+
layer.roughness = packedMRAoEiVec.g;
113+
layer.metallic = packedMRAoEiVec.b;
114+
layer.emission *= packedMRAoEiVec.a * layer.emission.a;
115+
}
116+
//________
117+
118+
// read Triplanar Albedo from TextureArray:
119+
void PBRTerrainUtils_readTriPlanarAlbedoTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){
120+
vec4 packedAlbedoVec = getTriPlanarBlendFromTexArray(lPosition, indexInTexArray, scale, texArray);
121+
updateLayerFromPackedAlbedoMap(packedAlbedoVec, layer);
122+
}
123+
// read Triplanar normal from TextureArray:
124+
void PBRTerrainUtils_readTriPlanarNormalTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){
125+
vec4 packedNormalParallaxVec = getTriPlanarBlendFromTexArray(lPosition, indexInTexArray, scale, texArray);
126+
updateLayerFromPackedNormalParallaxVec(packedNormalParallaxVec, layer);
127+
}
128+
// read TriPlanar metallicRoughnessAoEi from TextureArray:
129+
void PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){
130+
vec4 packedMRAoEi = getTriPlanarBlendFromTexArray(lPosition, indexInTexArray, scale, texArray);
131+
updateLayerFromPackedMRAoEiVec(packedMRAoEi, layer);
132+
}
133+
//________
134+
135+
// read Albedo from TextureArray:
136+
void PBRTerrainUtils_readAlbedoTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){
137+
vec4 packedAlbedoVec = texture2DArray(texArray, vec3(texCoord * scale, indexInTexArray));
138+
updateLayerFromPackedAlbedoMap(packedAlbedoVec, layer);
139+
140+
}
141+
// read Normal from TextureArray:
142+
void PBRTerrainUtils_readNormalTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){
143+
vec4 packedNormalParallaxVec = texture2DArray(texArray, vec3(texCoord * scale, indexInTexArray));
144+
updateLayerFromPackedNormalParallaxVec(packedNormalParallaxVec, layer);
145+
}
146+
// read metallicRoughnessAoEi from TextureArray:
147+
void PBRTerrainUtils_readMetallicRoughnessAoEiTexArray(in int indexInTexArray, float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){
148+
vec4 packedMRAoEi = texture2DArray(texArray, vec3(texCoord * scale, indexInTexArray));
149+
updateLayerFromPackedMRAoEiVec(packedMRAoEi, layer);
150+
151+
}
152+
//________
153+
154+
155+
156+
void PBRTerrainUtils_blendPBRTerrainLayer(inout PBRSurface surface, inout PBRTerrainTextureLayer layer){
157+
158+
159+
//mix values from this index layer to final output values based on finalAlphaBlendForLayer
160+
surface.albedo = mix(surface.albedo, layer.albedo.rgb, layer.blendValue);
161+
surface.normal = mix(surface.normal.rgb, layer.normal, layer.blendValue);
162+
surface.metallic = mix(surface.metallic, layer.metallic, layer.blendValue);
163+
surface.roughness = mix(surface.roughness, layer.roughness, layer.blendValue);
164+
surface.ao = mix(surface.ao, vec3(layer.ao), layer.blendValue);
165+
surface.emission = mix(surface.emission, layer.emission.rgb, layer.blendValue);
166+
}
167+
168+
#endif
169+
#endif
170+

0 commit comments

Comments
 (0)