Advertisement
Guest User

Height based custom material - fragment shader.

a guest
Jul 11th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. uniform vec3 m_region1;
  2. uniform vec3 m_region2;
  3. uniform vec3 m_region3;
  4. uniform vec3 m_region4;
  5. uniform vec4 g_LightDirection;
  6. uniform vec4 g_LightPosition;
  7.  
  8. uniform sampler2D m_region1ColorMap;
  9. uniform sampler2D m_region2ColorMap;
  10. uniform sampler2D m_region3ColorMap;
  11. uniform sampler2D m_region4ColorMap;
  12. uniform sampler2D m_slopeColorMap;
  13.  
  14. uniform float m_minSlopeAngle;
  15. uniform float m_maxSlopeAngle;
  16.  
  17. uniform float m_slopeTileFactor;
  18. uniform float m_terrainSize;
  19.  
  20. varying vec3 normal ;
  21. varying vec4 position;
  22.  
  23. vec4 GenerateTerrainColor() {
  24.  
  25.     float height = position.y;
  26.     vec4 p = position / m_terrainSize;
  27.     // This angles can be changed to match user
  28.  
  29.     // The minimum and maximum angles at which slope texture appears
  30.     float minAngle = 30;
  31.     float maxAngle = 60;
  32.  
  33.     float angleRange = m_maxSlopeAngle - m_minSlopeAngle;
  34.    
  35.     vec3 toUp = vec3(0.0, 1.0, 0.0);
  36.     float angle = degrees (acos (  dot(toUp, normal) ))  ;
  37.  
  38.     // The amount the current angle is above the minimum angle the slope tex appears
  39.     float deltaAngle = angle - minAngle;
  40.  
  41.     float angleImpact = clamp (deltaAngle / angleRange,0.00001 , 1 );
  42.  
  43.     vec4 slopeColor = texture2D(m_slopeColorMap, p.xz * m_slopeTileFactor);
  44.  
  45.     vec4 terrainColor = vec4(0.0, 0.0, 0.0, 1.0);
  46.  
  47.     float m_regionMin = 0.0;
  48.     float m_regionMax = 0.0;
  49.     float m_regionRange = 0.0;
  50.     float m_regionWeight = 0.0;
  51.  
  52.  
  53.     // Terrain m_region 1.
  54.     m_regionMin = m_region1.x;
  55.     m_regionMax = m_region1.y;
  56.     m_regionRange = m_regionMax - m_regionMin;
  57.     m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange;
  58.     m_regionWeight = max(0.0, m_regionWeight);
  59.     terrainColor += m_regionWeight * texture2D(m_region1ColorMap, p.xz * m_region1.z);
  60.  
  61.     // Terrain m_region 2.
  62.     m_regionMin = m_region2.x;
  63.     m_regionMax = m_region2.y;
  64.     m_regionRange = m_regionMax - m_regionMin;
  65.     m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange;
  66.     m_regionWeight = max(0.0, m_regionWeight);
  67.     terrainColor += m_regionWeight * (texture2D(m_region2ColorMap, p.xz * m_region2.z));
  68.  
  69.     // Terrain m_region 3.
  70.     m_regionMin = m_region3.x;
  71.     m_regionMax = m_region3.y;
  72.     m_regionRange = m_regionMax - m_regionMin;
  73.     m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange;
  74.     m_regionWeight = max(0.0, m_regionWeight);
  75.     terrainColor += m_regionWeight * texture2D(m_region3ColorMap, p.xz * m_region3.z);
  76.  
  77.     // Terrain m_region 4.
  78.     m_regionMin = m_region4.x;
  79.     m_regionMax = m_region4.y;
  80.     m_regionRange = m_regionMax - m_regionMin;
  81.     m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange;
  82.     m_regionWeight = max(0.0, m_regionWeight);
  83.     terrainColor += m_regionWeight * texture2D(m_region4ColorMap, p.xz * m_region4.z);
  84.    
  85.    
  86.  
  87.     return (mix(terrainColor, slopeColor, angleImpact ));
  88. }
  89.  
  90. void main() {
  91.     vec4 color = GenerateTerrainColor();
  92.  
  93.         vec3 lightDir = vec3(1.0, 3.0, 2.0);
  94.  
  95.         float lambert = dot(-g_LightPosition.xyz,normal);
  96.         vec4 darkColor = vec4(0.0, 0.0, 0.0, 1.0);
  97.    
  98.         float rgb = 1;
  99.         color = mix(darkColor,color , lambert);
  100.         color = clamp(color, vec4(0,0,0,0), vec4(rgb,rgb,rgb,1));
  101.         color = color * 0.8 ;
  102.         gl_FragColor = color;
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement