Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 330
- in vec2 TexCoord;
- layout(location = 0) out vec4 OutAlbedo;
- layout(location = 1) out vec4 OutWorldPosSpecularity;
- layout(location = 2) out vec4 OutNormalBooleans;
- uniform sampler2D Depth;
- uniform sampler2D Albedo;
- uniform sampler2D Position;
- uniform sampler2D Normal;
- uniform sampler2D Wind;
- uniform mat4 ViewMatrix;
- uniform mat4 ProjectionMatrix;
- uniform vec3 CameraPosition;
- uniform float Time;
- vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); }
- float snoise(vec2 v){
- const vec4 C = vec4(0.211324865405187, 0.366025403784439,
- -0.577350269189626, 0.024390243902439);
- vec2 i = floor(v + dot(v, C.yy) );
- vec2 x0 = v - i + dot(i, C.xx);
- vec2 i1;
- i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
- vec4 x12 = x0.xyxy + C.xxzz;
- x12.xy -= i1;
- i = mod(i, 289.0);
- vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
- + i.x + vec3(0.0, i1.x, 1.0 ));
- vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy),
- dot(x12.zw,x12.zw)), 0.0);
- m = m*m ;
- m = m*m ;
- vec3 x = 2.0 * fract(p * C.www) - 1.0;
- vec3 h = abs(x) - 0.5;
- vec3 ox = floor(x + 0.5);
- vec3 a0 = x - ox;
- m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
- vec3 g;
- g.x = a0.x * x0.x + h.x * x0.y;
- g.yz = a0.yz * x12.xz + h.yz * x12.yw;
- return 130.0 * dot(m, g);
- }
- void GetStates(float Factor, out bool ShadowCatcher, out bool GrassField) {
- ShadowCatcher = (Factor > 0.01 && Factor < 0.51);
- GrassField = (Factor > 0.49);
- }
- void main() {
- bool IsShadowCatcher, IsGrassField;
- vec4 SampleNormal = texture(Normal, TexCoord);
- vec4 SampleWorldPosition = texture(Position, TexCoord);
- vec4 SampleAlbedo = texture(Albedo, TexCoord);
- GetStates(SampleNormal.a, IsShadowCatcher, IsGrassField);
- vec2 TextureSize = vec2(textureSize(Position, 0));
- vec2 TexelSize = 1.0 / TextureSize;
- float CurrentDistance = 10000.0;
- float GrassHeight = 0.;
- vec2 SWING = vec2(0.01, -0.01);
- if(SampleWorldPosition.a > -0.1) {
- vec3 Up = vec3(vec4(SampleWorldPosition.xyz, 1.0) * inverse(transpose(ViewMatrix)));
- vec3 Down = Up - vec3(0.,0.05,0.);
- vec4 NDCUp = ProjectionMatrix * ViewMatrix * vec4(Up,1.);
- vec2 TCUp = (NDCUp.xy / NDCUp.w) * 0.5 + 0.5;
- vec4 NDCDown = ProjectionMatrix * ViewMatrix * vec4(Down,1.);
- vec2 TCDown = (NDCDown.xy / NDCDown.w) * 0.5 + 0.5;
- int StepCount = int(distance(TCDown * TextureSize, TCUp * TextureSize));
- vec2 Direction = normalize(TCDown - TCUp);
- vec3 GrassWorldPosition = vec3(-1.);
- vec3 OneStepInWorldSpace = vec3(0.,0.05, 0.) / vec3(max(StepCount, 1));
- int Steps = 0;
- float GrassHeight;
- vec3 WorldPosGrassBottom;
- vec3 ActualWorldPosGrassBottom;
- bool Hit = false;
- for(int i=0;i<min(max(StepCount,1), 200);i++) {
- vec4 Sample = texture(Position, TexCoord + vec2(Direction * vec2(i)) * TexelSize);
- vec4 SampleNormal = texture(Normal, TexCoord + vec2(Direction * vec2(i)) * TexelSize);
- WorldPosGrassBottom = vec3(vec4(Sample.xyz, 1.0) * inverse(transpose(ViewMatrix)));
- float Distance = distance(CameraPosition, WorldPosGrassBottom);
- vec3 Seed = fract(floor(WorldPosGrassBottom * 1000.0) / 1000.0);
- vec3 WindSample = texture(Wind, WorldPosGrassBottom.xz + vec2(Time * 0.05, -Time * 0.04)).xyz;
- vec2 Wind = ((WindSample.xy * 2. - 1.) * (1.-WindSample.z) * 4.0) * vec2(float(i)/float(max(StepCount,1)));
- if(pow(snoise(Seed.xz * 100.0 + Wind) ,0.5) > 0.85 * pow(1.-min(Distance * .01,1.), 10.0) && SampleNormal.a > 0.49) {
- Hit = true;
- GrassHeight = (Distance > 0. && Distance < CurrentDistance) ? 1. : GrassHeight;
- CurrentDistance = (Distance > 0. && Distance < CurrentDistance) ? Distance : CurrentDistance;
- GrassWorldPosition = (Distance > 0. && Distance < CurrentDistance) ? GrassWorldPosition : (WorldPosGrassBottom - (OneStepInWorldSpace * vec3(-i)));
- GrassHeight = (Distance > 0. && Distance < CurrentDistance) ? GrassHeight : -(OneStepInWorldSpace.y * float(-i));
- ActualWorldPosGrassBottom = (Distance > 0. && Distance < CurrentDistance) ? ActualWorldPosGrassBottom : WorldPosGrassBottom + (vec3(Wind.x,0.,Wind.y) / vec3(100.));
- Steps = i;
- }
- }
- vec3 GrassNormal = vec3(0.,1.,0.);
- //Get The Grass Depth =
- vec4 ClipSpace = ProjectionMatrix * ViewMatrix * vec4(GrassWorldPosition, 1.0);
- vec4 NDCSpace = ClipSpace / ClipSpace.w;
- float GrassDepth = NDCSpace.z * .5 + .5;
- float DefferedDepth = texture(Depth,TexCoord).x;
- vec4 ClipSpaceGrassBottom = ProjectionMatrix * ViewMatrix * vec4(ActualWorldPosGrassBottom, 1.0);
- vec4 NDCSpaceGrassBottom = ClipSpaceGrassBottom / ClipSpaceGrassBottom.w;
- vec4 GrassBottomNormal = texture(Normal, NDCSpaceGrassBottom.xy*.5+.5);
- if(GrassDepth < DefferedDepth && Hit && GrassBottomNormal.a > 0.49) {
- gl_FragDepth = GrassDepth;
- float MixFactor = min(GrassHeight * 5., 1.0);
- OutAlbedo.xyz = texture(Albedo, NDCSpaceGrassBottom.xy*.5+.5).xyz * (GrassHeight*10.);
- //OutAlbedo.xyz = max(OutAlbedo.xyz, vec3(1.));
- OutAlbedo.a = 1.0;
- OutWorldPosSpecularity = vec4(vec3(ViewMatrix * vec4(GrassWorldPosition,1.)), 0.);
- OutNormalBooleans = vec4(GrassNormal, 0.);
- }
- else {
- gl_FragDepth = DefferedDepth;
- OutAlbedo = SampleAlbedo;
- OutWorldPosSpecularity = SampleWorldPosition;
- OutNormalBooleans = SampleNormal;
- }
- }
- else {
- gl_FragDepth = texture(Depth,TexCoord).x;
- OutAlbedo = SampleAlbedo;
- OutWorldPosSpecularity = SampleWorldPosition;
- OutNormalBooleans = SampleNormal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement