Advertisement
Outside_of_Awareness

Godot Procedural Terrain Shading Code

Feb 17th, 2024 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shader_type spatial;
  2.  
  3. // Fragment shader
  4. uniform sampler2D grass_texture;
  5. uniform sampler2D stone_texture;
  6. uniform sampler2D earth_texture;
  7.  
  8. // Define scaling factors for each texture (adjust as needed)
  9. uniform float grass_scale = 300;
  10. uniform float stone_scale = 75;
  11. uniform float earth_scale = 75;
  12.  
  13. // Stone vs Grass
  14. uniform float min_stone_slope:hint_range(0.0, 1.0) = 0.80;
  15. uniform float max_grass_slope:hint_range(0.0, 1.0) = 0.81;
  16. // Earth vs StoneGrass
  17. uniform float min_stonegrass_height = -0.5;
  18. uniform float max_earth_height = 0;
  19.  
  20. varying float normal_y;
  21. varying float vertex_y;
  22.  
  23. void vertex(){
  24.         // Called for every vertex the material is visible on.
  25.     normal_y = NORMAL.y;
  26.     vertex_y = VERTEX.y;
  27. }
  28.  
  29. void fragment() {
  30.     // Called for every pixel the material is visible on.
  31. vec3 grass_albedo = texture(grass_texture, UV * grass_scale).xyz;
  32. vec3 stone_albedo = texture(stone_texture, UV * stone_scale).xyz;
  33. vec3 earth_albedo = texture(earth_texture, UV * earth_scale).xyz;
  34. // Weights
  35. float stone_grass_weight = normal_y;
  36. float earth_stonegrass_weight = vertex_y;
  37. // Calculating Stone/Grass Weight
  38. stone_grass_weight = max(min_stone_slope, stone_grass_weight);
  39. stone_grass_weight = min(max_grass_slope, stone_grass_weight);
  40. stone_grass_weight -= min_stone_slope;
  41. stone_grass_weight /= max_grass_slope - min_stone_slope;
  42. // Calculating Earth/StoneGrass Weight
  43. earth_stonegrass_weight = max(min_stonegrass_height, earth_stonegrass_weight);
  44. earth_stonegrass_weight = min(max_earth_height, earth_stonegrass_weight);
  45. earth_stonegrass_weight -= min_stonegrass_height;
  46. earth_stonegrass_weight /= max_earth_height - min_stonegrass_height;
  47.  
  48. vec3 stonegrass_albedo = mix(stone_albedo, grass_albedo, stone_grass_weight);
  49.  
  50. ALBEDO = mix(earth_albedo, stonegrass_albedo, earth_stonegrass_weight);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement