説明なし

terrain.frag 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. layout (set = 0, binding = 1) uniform sampler2D samplerHeight;
  5. layout (set = 0, binding = 2) uniform sampler2DArray samplerLayers;
  6. layout (location = 0) in vec3 inNormal;
  7. layout (location = 1) in vec2 inUV;
  8. layout (location = 2) in vec3 inViewVec;
  9. layout (location = 3) in vec3 inLightVec;
  10. layout (location = 4) in vec3 inEyePos;
  11. layout (location = 5) in vec3 inWorldPos;
  12. layout (location = 0) out vec4 outFragColor;
  13. vec3 sampleTerrainLayer()
  14. {
  15. // Define some layer ranges for sampling depending on terrain height
  16. vec2 layers[6];
  17. layers[0] = vec2(-10.0, 10.0);
  18. layers[1] = vec2(5.0, 45.0);
  19. layers[2] = vec2(45.0, 80.0);
  20. layers[3] = vec2(75.0, 100.0);
  21. layers[4] = vec2(95.0, 140.0);
  22. layers[5] = vec2(140.0, 190.0);
  23. vec3 color = vec3(0.0);
  24. // Get height from displacement map
  25. float height = textureLod(samplerHeight, inUV, 0.0).r * 255.0;
  26. for (int i = 0; i < 6; i++)
  27. {
  28. float range = layers[i].y - layers[i].x;
  29. float weight = (range - abs(height - layers[i].y)) / range;
  30. weight = max(0.0, weight);
  31. color += weight * texture(samplerLayers, vec3(inUV * 16.0, i)).rgb;
  32. }
  33. return color;
  34. }
  35. float fog(float density)
  36. {
  37. const float LOG2 = -1.442695;
  38. float dist = gl_FragCoord.z / gl_FragCoord.w * 0.1;
  39. float d = density * dist;
  40. return 1.0 - clamp(exp2(d * d * LOG2), 0.0, 1.0);
  41. }
  42. void main()
  43. {
  44. vec3 N = normalize(inNormal);
  45. vec3 L = normalize(inLightVec);
  46. vec3 ambient = vec3(0.5);
  47. vec3 diffuse = max(dot(N, L), 0.0) * vec3(1.0);
  48. vec4 color = vec4((ambient + diffuse) * sampleTerrainLayer(), 1.0);
  49. const vec4 fogColor = vec4(0.47, 0.5, 0.67, 0.0);
  50. outFragColor = mix(color, fogColor, fog(0.25));
  51. }