| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
#define BLADE_FRACTION 0.1 |
| 5 |
#define MAX_LAYERS 20 |
| 6 |
#define MAX_DISTANCE 1000.0 |
| 7 |
|
| 8 |
uniform float visibility; |
| 9 |
uniform float scattering; |
| 10 |
uniform float overlay_bias; |
| 11 |
uniform float season; |
| 12 |
uniform float max_height; |
| 13 |
uniform float grass_density; |
| 14 |
uniform float grass_modulate_height_min; |
| 15 |
|
| 16 |
uniform float wind_x; |
| 17 |
uniform float wind_y; |
| 18 |
|
| 19 |
uniform float wash_x; |
| 20 |
uniform float wash_y; |
| 21 |
uniform float wash_strength; |
| 22 |
|
| 23 |
uniform int grass_modulate_by_overlay; |
| 24 |
uniform int grass_groups; |
| 25 |
uniform int wind_effects; |
| 26 |
|
| 27 |
uniform sampler2D colorTex; |
| 28 |
uniform sampler2D densityTex; |
| 29 |
|
| 30 |
uniform float osg_SimulationTime; |
| 31 |
|
| 32 |
varying vec2 g_rawpos; // Horizontal position in model space |
| 33 |
varying float g_distance_to_eye; // Distance to the camera. Layers were disregarded |
| 34 |
varying float g_layer; // The layer where the fragment lives (0-1 range) |
| 35 |
|
| 36 |
float rand2D(in vec2 co); |
| 37 |
float Noise2D(in vec2 co, in float wavelength); |
| 38 |
vec3 filter_combined (in vec3 color) ; |
| 39 |
|
| 40 |
float getShadowing(); |
| 41 |
|
| 42 |
|
| 43 |
float map(float s, float a1, float a2, float b1, float b2) |
| 44 |
{ |
| 45 |
return b1+(s-a1)*(b2-b1)/(a2-a1); |
| 46 |
} |
| 47 |
|
| 48 |
float decodeBinary(float n, float layer) |
| 49 |
{ |
| 50 |
return float(mod(floor(n*pow(0.5, layer)), 2.0)); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
float bladeNoise2D(in float x, in float y, in float dDensity, in float layer, in float d_factor, in float h_factor) |
| 55 |
{ |
| 56 |
float integer_x = x - fract(x); |
| 57 |
float fractional_x = x - integer_x; |
| 58 |
|
| 59 |
float integer_y = y - fract(y); |
| 60 |
float fractional_y = y - integer_y; |
| 61 |
|
| 62 |
if (rand2D(vec2(integer_x+1.0, integer_y +1.0)) > dDensity) |
| 63 |
{return 0.0;} |
| 64 |
|
| 65 |
float hfact = 0.7 + 0.3 * (rand2D(vec2(integer_x, integer_y + 2.0))); |
| 66 |
hfact *= h_factor; |
| 67 |
|
| 68 |
if (layer > hfact) {return 0.0;} |
| 69 |
|
| 70 |
float xoffset = (rand2D(vec2(integer_x, integer_y)) -0.5); |
| 71 |
float yoffset = (rand2D(vec2(integer_x+1.0, integer_y)) - 0.5); |
| 72 |
|
| 73 |
float xbend = (rand2D(vec2(integer_x+1.0, integer_y + 1.0)) - 0.5); |
| 74 |
float ybend = (rand2D(vec2(integer_x, integer_y + 1.0)) - 0.5); |
| 75 |
float fraction = BLADE_FRACTION * (0.5 + 0.5 * (1.0 - smoothstep(0.5, 1.0, layer))); |
| 76 |
|
| 77 |
float bend = 0.5 * layer * layer; |
| 78 |
|
| 79 |
vec2 truePos = vec2 (0.5 + xoffset * (1.0 - 2.0 * BLADE_FRACTION) + xbend * bend, 0.5 + yoffset * (1.0 -2.0 * BLADE_FRACTION) + ybend * bend); |
| 80 |
|
| 81 |
float distance = length(truePos - vec2(fractional_x, fractional_y)); |
| 82 |
return 1.0 - step (fraction * d_factor, distance); |
| 83 |
} |
| 84 |
|
| 85 |
float BladeNoise2D(in vec2 coord, in float wavelength, in float dDensity, in float layer, in float d_factor, in float h_factor) |
| 86 |
{ |
| 87 |
return bladeNoise2D(coord.x/wavelength, coord.y/wavelength, dDensity, layer, d_factor, h_factor); |
| 88 |
} |
| 89 |
|
| 90 |
void main() |
| 91 |
{ |
| 92 |
|
| 93 |
if (season > 1.6) {discard;} |
| 94 |
if (g_distance_to_eye > MAX_DISTANCE) {discard;} |
| 95 |
|
| 96 |
vec2 texCoord = gl_TexCoord[0].st; |
| 97 |
|
| 98 |
if (wind_effects > 1) |
| 99 |
{ |
| 100 |
|
| 101 |
vec2 eyePos = (gl_ModelViewMatrixInverse * vec4 (0.0, 0.0, 0.0, 1.0)).xy; |
| 102 |
|
| 103 |
vec2 washDir = vec2 (wash_x, wash_y) - (g_rawpos - eyePos); |
| 104 |
float washStrength = 20.0 * min(14.0 * wash_strength/(length(washDir) + 1.0), 1.0); |
| 105 |
washStrength *= (1.0 - 0.8 * sin(20.0 * osg_SimulationTime + length(washDir) + dot(normalize(washDir), vec2(1.0, 0.0)))); |
| 106 |
|
| 107 |
vec2 windDir = normalize(vec2 (max(wind_x, 0.1), wind_y) + washStrength * vec2 (-washDir.y, washDir.x)); |
| 108 |
|
| 109 |
float windStrength = 0.5 * length(vec2 (wind_x, wind_y)) + washStrength; |
| 110 |
float windAmplitude = 1.0 + 0.3 * windStrength; |
| 111 |
float sineTerm = sin(0.7 * windStrength * osg_SimulationTime + 0.05 * (g_rawpos.x + g_rawpos.y)); |
| 112 |
sineTerm = sineTerm + sin(0.6 * windStrength * osg_SimulationTime + 0.04 * (g_rawpos.x + g_rawpos.y)); |
| 113 |
sineTerm = sineTerm + sin(0.44 * windStrength * osg_SimulationTime + 0.05 * (g_rawpos.x + g_rawpos.y)); |
| 114 |
sineTerm = sineTerm/3.0; |
| 115 |
sineTerm = 5.0 * sineTerm * sineTerm; |
| 116 |
|
| 117 |
float windDisplacement = pow(g_layer/32.0, 2.0) * clamp((windStrength + windAmplitude * sineTerm), -35.0, 35.0); |
| 118 |
|
| 119 |
texCoord += (windDisplacement * windDir); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
float noise_1m = Noise2D(g_rawpos.xy, 1.0); |
| 126 |
float noise_2m = Noise2D(g_rawpos.xy, 2.0); ; |
| 127 |
float noise_10m = Noise2D(g_rawpos.xy, 10.0); |
| 128 |
|
| 129 |
float h_factor; |
| 130 |
float overlay_mix = smoothstep(0.45, 0.65, overlay_bias + (0.5 * noise_1m + 0.1 * noise_2m + 0.4 * noise_10m)); |
| 131 |
|
| 132 |
if (grass_modulate_by_overlay == 1) |
| 133 |
{h_factor = grass_modulate_height_min + (1.0 - grass_modulate_height_min) * (1.0 - overlay_mix) ;} |
| 134 |
else |
| 135 |
{h_factor = 1.0;} |
| 136 |
|
| 137 |
float value = 0.0; |
| 138 |
|
| 139 |
float d_fade =smoothstep(100.0, MAX_DISTANCE, g_distance_to_eye); |
| 140 |
float d_factor = 1.0 + 1.0 * d_fade; |
| 141 |
d_factor *= clamp(max_height/0.3,0.5, 1.0); |
| 142 |
|
| 143 |
float bladeFlag = BladeNoise2D(texCoord, 0.015, grass_density, g_layer, d_factor, h_factor); |
| 144 |
if (grass_groups >1) {bladeFlag += BladeNoise2D(texCoord, 0.01, grass_density, g_layer, d_factor, h_factor);} |
| 145 |
if (grass_groups >2) {bladeFlag += BladeNoise2D(texCoord, 0.007, grass_density, g_layer, d_factor, h_factor);} |
| 146 |
|
| 147 |
|
| 148 |
if (bladeFlag > 0.0) {value = 1.0;} |
| 149 |
else {discard;} |
| 150 |
|
| 151 |
vec3 texel = texture2D(colorTex, texCoord).rgb; |
| 152 |
|
| 153 |
// autumn coloring |
| 154 |
|
| 155 |
texel.r = min(1.0, (1.0 + 2.5 * 0.1 * season) * texel.r); |
| 156 |
texel.g = texel.g; |
| 157 |
texel.b = max(0.0, (1.0 - 4.0 * 0.1 * season) * texel.b); |
| 158 |
float intensity = length(texel.rgb) * (1.0 - 0.5 * smoothstep(1.1,2.0,season)) * mix(0.3, 1.0, getShadowing()); |
| 159 |
texel.rgb = intensity * normalize(mix(texel.rgb, vec3(0.23,0.17,0.08), smoothstep(1.1,2.0, season))); |
| 160 |
|
| 161 |
float base_alpha = clamp(0.4 * max_height/0.3, 0.4, 1.0); |
| 162 |
value*= base_alpha * (1.0 - d_fade); |
| 163 |
|
| 164 |
value *= 1.0 - smoothstep(visibility* 0.5, visibility, g_distance_to_eye); |
| 165 |
value= clamp(value, 0.0, 1.0); |
| 166 |
|
| 167 |
texel *= length(gl_LightSource[0].diffuse.rgb)/1.73 * scattering; |
| 168 |
texel = clamp(texel, 0.0, 1.0); |
| 169 |
|
| 170 |
|
| 171 |
vec4 fragColor = vec4 (texel, value); |
| 172 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 173 |
|
| 174 |
gl_FragColor = fragColor; |
| 175 |
} |