Guest User

Height based custom material - fragment shader.

a guest
May 19th, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     float height = position.y;
  25.     vec4 p = position / m_terrainSize;
  26.     // This angles can be changed to match user
  27.     float minAngle = 30;
  28.     float maxAngle = 60;
  29.  
  30.     float angleRange = m_maxSlopeAngle - m_minSlopeAngle;
  31.    
  32.     vec3 upDown = vec3(0.0, 1.0, 0.0);
  33.     float angle = degrees (acos (  dot(upDown, normal) ))  ;
  34.  
  35.     float deltaAngle = angle - minAngle;
  36.     float angleImpact = clamp (deltaAngle / angleRange,0 , 1 );
  37.    
  38.  
  39.     vec4 slopeColor = texture2D(m_slopeColorMap, p.xz * m_slopeTileFactor);
  40.  
  41.     vec4 terrainColor = vec4(0.0, 0.0, 0.0, 1.0);
  42.  
  43.     float m_regionMin = 0.0;
  44.     float m_regionMax = 0.0;
  45.     float m_regionRange = 0.0;
  46.     float m_regionWeight = 0.0;
  47.  
  48.  
  49.     // Terrain m_region 1.
  50.     m_regionMin = m_region1.x;
  51.     m_regionMax = m_region1.y;
  52.     m_regionRange = m_regionMax - m_regionMin;
  53.     m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange;
  54.     m_regionWeight = max(0.0, m_regionWeight);
  55.     terrainColor += m_regionWeight * texture2D(m_region1ColorMap, p.xz * m_region1.z);
  56.  
  57.     // Terrain m_region 2.
  58.     m_regionMin = m_region2.x;
  59.     m_regionMax = m_region2.y;
  60.     m_regionRange = m_regionMax - m_regionMin;
  61.     m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange;
  62.     m_regionWeight = max(0.0, m_regionWeight);
  63.     terrainColor += m_regionWeight * (texture2D(m_region2ColorMap, p.xz * m_region2.z));
  64.  
  65.     // Terrain m_region 3.
  66.     m_regionMin = m_region3.x;
  67.     m_regionMax = m_region3.y;
  68.     m_regionRange = m_regionMax - m_regionMin;
  69.     m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange;
  70.     m_regionWeight = max(0.0, m_regionWeight);
  71.     terrainColor += m_regionWeight * texture2D(m_region3ColorMap, p.xz * m_region3.z);
  72.  
  73.     // Terrain m_region 4.
  74.     m_regionMin = m_region4.x;
  75.     m_regionMax = m_region4.y;
  76.     m_regionRange = m_regionMax - m_regionMin;
  77.     m_regionWeight = (m_regionRange - abs(height - m_regionMax)) / m_regionRange;
  78.     m_regionWeight = max(0.0, m_regionWeight);
  79.     terrainColor += m_regionWeight * texture2D(m_region4ColorMap, p.xz * m_region4.z);
  80.    
  81.     return (mix(terrainColor, slopeColor, angleImpact ));
  82. }
  83.  
  84. void main() {
  85.     vec4 color = GenerateTerrainColor();
  86.         vec3 lightDir = vec3(1.0, 3.0, 2.0);
  87.  
  88.         float lambert = dot(-g_LightPosition.xyz,normal);
  89.         vec4 darkColor = vec4(0.0, 0.0, 0.0, 1.0);
  90.        
  91.        
  92.         color = mix(darkColor,color , lambert);
  93.         color = clamp(color, vec4(0,0,0,0), vec4(1,1,1,1));
  94.         gl_FragColor = color;
  95.  
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment