1 uniform vec3 m_region1; 2 uniform vec3 m_region2; 3 uniform vec3 m_region3; 4 uniform vec3 m_region4; 5 6 uniform sampler2D m_region1ColorMap; 7 uniform sampler2D m_region2ColorMap; 8 uniform sampler2D m_region3ColorMap; 9 uniform sampler2D m_region4ColorMap; 10 uniform sampler2D m_slopeColorMap; 11 12 uniform float m_slopeTileFactor; 13 uniform float m_terrainSize; 14 15 varying vec3 normal; 16 varying vec4 position; 17 18 vec4 GenerateTerrainColor() { 19 float height = position.y; 20 vec4 p = position / m_terrainSize; 21 22 vec3 blend = abs( normal ); 23 blend = (blend -0.2) * 0.7; 24 blend = normalize(max(blend, 0.00001)); // Force weights to sum to 1.0 (very important!) 25 float b = (blend.x + blend.y + blend.z); 26 blend /= vec3(b, b, b); 27 28 vec4 terrainColor = vec4(0.0, 0.0, 0.0, 1.0); 29 30 float m_regionMin = 0.0; 31 float m_regionMax = 0.0; 32 float m_regionRange = 0.0; 33 float m_regionWeight = 0.0; 34 35 vec4 slopeCol1 = texture2D(m_slopeColorMap, p.yz * m_slopeTileFactor); 36 vec4 slopeCol2 = texture2D(m_slopeColorMap, p.xy * m_slopeTileFactor); 37 38 // Terrain m_region 1. 39 m_regionMin = m_region1.x; 40 m_regionMax = m_region1.y; 41 m_regionRange = m_regionMax - m_regionMin; 42 m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange; 43 m_regionWeight = max(0.0, m_regionWeight); 44 terrainColor += m_regionWeight * texture2D(m_region1ColorMap, p.xz * m_region1.z); 45 46 // Terrain m_region 2. 47 m_regionMin = m_region2.x; 48 m_regionMax = m_region2.y; 49 m_regionRange = m_regionMax - m_regionMin; 50 m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange; 51 m_regionWeight = max(0.0, m_regionWeight); 52 terrainColor += m_regionWeight * (texture2D(m_region2ColorMap, p.xz * m_region2.z)); 53 54 // Terrain m_region 3. 55 m_regionMin = m_region3.x; 56 m_regionMax = m_region3.y; 57 m_regionRange = m_regionMax - m_regionMin; 58 m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange; 59 m_regionWeight = max(0.0, m_regionWeight); 60 terrainColor += m_regionWeight * texture2D(m_region3ColorMap, p.xz * m_region3.z); 61 62 // Terrain m_region 4. 63 m_regionMin = m_region4.x; 64 m_regionMax = m_region4.y; 65 m_regionRange = m_regionMax - m_regionMin; 66 m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange; 67 m_regionWeight = max(0.0, m_regionWeight); 68 terrainColor += m_regionWeight * texture2D(m_region4ColorMap, p.xz * m_region4.z); 69 70 return (blend.y * terrainColor + blend.x * slopeCol1 + blend.z * slopeCol2); 71 } 72 73 void main() { 74 vec4 color = GenerateTerrainColor(); 75 gl_FragColor = color; 76 } 77