Advertisement
Guest User

Godot 3.X Grass Shader

a guest
Apr 6th, 2018
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Godot 3.X Grass Shader
  2. // Copyright 2018 Anonymous
  3. //Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4. //The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  6.  
  7. shader_type spatial;
  8. render_mode blend_mix,depth_draw_alpha_prepass,cull_disabled,diffuse_burley,specular_schlick_ggx;
  9. uniform vec4 albedo : hint_color;
  10. uniform sampler2D texture_albedo : hint_albedo;
  11. uniform float specular;
  12. uniform float metallic;
  13. uniform float alpha_scissor_threshold;
  14. uniform float roughness : hint_range(0,1);
  15. uniform float point_size : hint_range(0,128);
  16. uniform sampler2D texture_metallic : hint_white;
  17. uniform vec4 metallic_texture_channel;
  18. uniform sampler2D texture_roughness : hint_white;
  19. uniform vec4 roughness_texture_channel;
  20. uniform vec4 transmission : hint_color;
  21. uniform sampler2D texture_transmission : hint_black;
  22. uniform vec3 uv1_scale;
  23. uniform vec3 uv1_offset;
  24. uniform vec3 uv2_scale;
  25. uniform vec3 uv2_offset;
  26.  
  27. // direction of the wind, in radians
  28. uniform float wind_dir_in : hint_range(0,6.29);
  29. // speed of the whole shader
  30. uniform float speed = 2.0;
  31. // amount of "wave motion"
  32. uniform float wind_pow = 1.0;
  33. // how much the wind is offset
  34. uniform float wind_offset = 1.0;
  35. // used to pass height data from the vertex shader to the light shader
  36. varying float height;
  37.  
  38.  
  39. vec3 get_offset(float time, vec3 w_wind_dir)
  40. {
  41.     // sin() multiplied by the wind direction and magnitude, then offset in the wind direction multiplied by the wind offset
  42.     // the /100.0's are just to make editing the shader parameters saner
  43.     return sin(time) * wind_pow * w_wind_dir / 100.0 + wind_offset*w_wind_dir/100.0 ;
  44. }
  45.  
  46. void vertex() {
  47.     UV=UV*uv1_scale.xy+uv1_offset.xy;
  48.    
  49.     // calculate world coordinates of the vertex (Godot 3.x uses local coords)
  50.     vec4 world_coords = (WORLD_MATRIX * vec4(VERTEX, 1.0));
  51.     // calculate wind direction vector
  52.     vec4 wind_dir = vec4(sin(wind_dir_in),0.0,cos(wind_dir_in), 0.0);
  53.     vec3 wind_dir_3 = (WORLD_MATRIX * wind_dir).xyz;
  54.     // mulitply time by speed and offset by distance along the wind direction.
  55.     float time = TIME * speed + length( world_coords.xyz*wind_dir.xyz ) / 6.0 + length(world_coords)/12.0;
  56.     // tried to add multiple layers of offset, but it doesn't look as good as just one
  57.     vec3 offset = get_offset(time, wind_dir_3);// + get_offset(time*2.5, wind_dir_3/4.0);
  58.    
  59.     // fug I don't remember what this does but it's needed
  60.     offset.y = 1.0-length(offset) / 1.4;
  61.     // prevent the offset from modifying vertices at the base of the grass (remember VERTEX is in local (model) coordinates)
  62.     if (VERTEX.y <= 0.1) {
  63.         offset = vec3(0.0);
  64.     }
  65.    
  66.     // "height" is used for shading the grass in the light shader below
  67.     // -0.2+2.0*VERTEX.y: shades the grass so the bottom of the grass is dark
  68.     // pow(length(offset), 2.0): shades the grass based on how much the vertex is offset
  69.     height = -0.25 + 2.0*VERTEX.y * pow(length(offset), 2.0);// (pow(14.0, VERTEX.y) - 1.2) / 5.0 - pow(2.0, length(offset));
  70.    
  71.     // offset the position along the normal.
  72.     vec3 v = VERTEX.xyz + offset;
  73.    
  74.     // transform the attributes.
  75.     VERTEX = (PROJECTION_MATRIX * vec4( v, 1.0 )).xyz;
  76. }
  77.  
  78.  
  79. void fragment() {
  80.     // this is just the standard Godot fragment shader, taken directly from a converted SpatialMaterial
  81.     vec2 base_uv = UV;
  82.     vec4 albedo_tex = texture(texture_albedo,base_uv);
  83.     ALBEDO = albedo.rgb * albedo_tex.rgb;
  84.     float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);
  85.     METALLIC = metallic_tex * metallic;
  86.     float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);
  87.     ROUGHNESS = roughness_tex * roughness;
  88.     SPECULAR = specular;
  89.     ALPHA = albedo.a * albedo_tex.a;
  90.     vec3 transmission_tex = texture(texture_transmission,base_uv).rgb;
  91.     TRANSMISSION = (transmission.rgb+transmission_tex);
  92.     ALPHA_SCISSOR=alpha_scissor_threshold;
  93. }
  94.  
  95. void light()
  96. {
  97.     // ATTENUATION: is in shadow or not
  98.     // LIGHT_COLOR: color of the light * power of the light
  99.     // height: calculated in the vertex shader, allows "waves" of brightness
  100.     DIFFUSE_LIGHT = LIGHT_COLOR * ATTENUATION * ALBEDO.rgb / 5.0 * clamp(height, -0.02, 999.0);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement