Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Godot 3.X Grass Shader
- // Copyright 2018 Anonymous
- //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:
- //The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- //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.
- shader_type spatial;
- render_mode blend_mix,depth_draw_alpha_prepass,cull_disabled,diffuse_burley,specular_schlick_ggx;
- uniform vec4 albedo : hint_color;
- uniform sampler2D texture_albedo : hint_albedo;
- uniform float specular;
- uniform float metallic;
- uniform float alpha_scissor_threshold;
- uniform float roughness : hint_range(0,1);
- uniform float point_size : hint_range(0,128);
- uniform sampler2D texture_metallic : hint_white;
- uniform vec4 metallic_texture_channel;
- uniform sampler2D texture_roughness : hint_white;
- uniform vec4 roughness_texture_channel;
- uniform vec4 transmission : hint_color;
- uniform sampler2D texture_transmission : hint_black;
- uniform vec3 uv1_scale;
- uniform vec3 uv1_offset;
- uniform vec3 uv2_scale;
- uniform vec3 uv2_offset;
- // direction of the wind, in radians
- uniform float wind_dir_in : hint_range(0,6.29);
- // speed of the whole shader
- uniform float speed = 2.0;
- // amount of "wave motion"
- uniform float wind_pow = 1.0;
- // how much the wind is offset
- uniform float wind_offset = 1.0;
- // used to pass height data from the vertex shader to the light shader
- varying float height;
- vec3 get_offset(float time, vec3 w_wind_dir)
- {
- // sin() multiplied by the wind direction and magnitude, then offset in the wind direction multiplied by the wind offset
- // the /100.0's are just to make editing the shader parameters saner
- return sin(time) * wind_pow * w_wind_dir / 100.0 + wind_offset*w_wind_dir/100.0 ;
- }
- void vertex() {
- UV=UV*uv1_scale.xy+uv1_offset.xy;
- // calculate world coordinates of the vertex (Godot 3.x uses local coords)
- vec4 world_coords = (WORLD_MATRIX * vec4(VERTEX, 1.0));
- // calculate wind direction vector
- vec4 wind_dir = vec4(sin(wind_dir_in),0.0,cos(wind_dir_in), 0.0);
- vec3 wind_dir_3 = (WORLD_MATRIX * wind_dir).xyz;
- // mulitply time by speed and offset by distance along the wind direction.
- float time = TIME * speed + length( world_coords.xyz*wind_dir.xyz ) / 6.0 + length(world_coords)/12.0;
- // tried to add multiple layers of offset, but it doesn't look as good as just one
- vec3 offset = get_offset(time, wind_dir_3);// + get_offset(time*2.5, wind_dir_3/4.0);
- // fug I don't remember what this does but it's needed
- offset.y = 1.0-length(offset) / 1.4;
- // prevent the offset from modifying vertices at the base of the grass (remember VERTEX is in local (model) coordinates)
- if (VERTEX.y <= 0.1) {
- offset = vec3(0.0);
- }
- // "height" is used for shading the grass in the light shader below
- // -0.2+2.0*VERTEX.y: shades the grass so the bottom of the grass is dark
- // pow(length(offset), 2.0): shades the grass based on how much the vertex is offset
- 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));
- // offset the position along the normal.
- vec3 v = VERTEX.xyz + offset;
- // transform the attributes.
- VERTEX = (PROJECTION_MATRIX * vec4( v, 1.0 )).xyz;
- }
- void fragment() {
- // this is just the standard Godot fragment shader, taken directly from a converted SpatialMaterial
- vec2 base_uv = UV;
- vec4 albedo_tex = texture(texture_albedo,base_uv);
- ALBEDO = albedo.rgb * albedo_tex.rgb;
- float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);
- METALLIC = metallic_tex * metallic;
- float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);
- ROUGHNESS = roughness_tex * roughness;
- SPECULAR = specular;
- ALPHA = albedo.a * albedo_tex.a;
- vec3 transmission_tex = texture(texture_transmission,base_uv).rgb;
- TRANSMISSION = (transmission.rgb+transmission_tex);
- ALPHA_SCISSOR=alpha_scissor_threshold;
- }
- void light()
- {
- // ATTENUATION: is in shadow or not
- // LIGHT_COLOR: color of the light * power of the light
- // height: calculated in the vertex shader, allows "waves" of brightness
- DIFFUSE_LIGHT = LIGHT_COLOR * ATTENUATION * ALBEDO.rgb / 5.0 * clamp(height, -0.02, 999.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement