Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 330
  2. in vec2 TexCoord;
  3.  
  4. layout(location = 0) out vec4 OutAlbedo;
  5. layout(location = 1) out vec4 OutWorldPosSpecularity;
  6. layout(location = 2) out vec4 OutNormalBooleans;
  7.  
  8. uniform sampler2D Depth;
  9. uniform sampler2D Albedo;
  10. uniform sampler2D Position;
  11. uniform sampler2D Normal;
  12. uniform sampler2D Wind;
  13.  
  14. uniform mat4 ViewMatrix;
  15. uniform mat4 ProjectionMatrix;
  16. uniform vec3 CameraPosition;
  17. uniform float Time;
  18.  
  19. vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); }
  20.  
  21. float snoise(vec2 v){
  22.   const vec4 C = vec4(0.211324865405187, 0.366025403784439,
  23.            -0.577350269189626, 0.024390243902439);
  24.   vec2 i  = floor(v + dot(v, C.yy) );
  25.   vec2 x0 = v -   i + dot(i, C.xx);
  26.   vec2 i1;
  27.   i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
  28.   vec4 x12 = x0.xyxy + C.xxzz;
  29.   x12.xy -= i1;
  30.   i = mod(i, 289.0);
  31.   vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
  32.   + i.x + vec3(0.0, i1.x, 1.0 ));
  33.   vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy),
  34.     dot(x12.zw,x12.zw)), 0.0);
  35.   m = m*m ;
  36.   m = m*m ;
  37.   vec3 x = 2.0 * fract(p * C.www) - 1.0;
  38.   vec3 h = abs(x) - 0.5;
  39.   vec3 ox = floor(x + 0.5);
  40.   vec3 a0 = x - ox;
  41.   m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
  42.   vec3 g;
  43.   g.x  = a0.x  * x0.x  + h.x  * x0.y;
  44.   g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  45.   return 130.0 * dot(m, g);
  46. }
  47.  
  48. void GetStates(float Factor, out bool ShadowCatcher, out bool GrassField) {
  49.     ShadowCatcher = (Factor > 0.01 && Factor < 0.51);
  50.     GrassField = (Factor > 0.49);
  51. }
  52.  
  53. void main() {
  54.  
  55.     bool IsShadowCatcher, IsGrassField;
  56.     vec4 SampleNormal = texture(Normal, TexCoord);
  57.     vec4 SampleWorldPosition = texture(Position, TexCoord);
  58.     vec4 SampleAlbedo = texture(Albedo, TexCoord);
  59.     GetStates(SampleNormal.a, IsShadowCatcher, IsGrassField);
  60.  
  61.     vec2 TextureSize = vec2(textureSize(Position, 0));
  62.     vec2 TexelSize = 1.0 / TextureSize;
  63.    
  64.     float CurrentDistance = 10000.0;
  65.     float GrassHeight = 0.;
  66.  
  67.    
  68.  
  69.     vec2 SWING = vec2(0.01, -0.01);
  70.  
  71.     if(SampleWorldPosition.a > -0.1) {
  72.  
  73.     vec3 Up = vec3(vec4(SampleWorldPosition.xyz, 1.0) * inverse(transpose(ViewMatrix)));
  74.     vec3 Down = Up - vec3(0.,0.05,0.);
  75.  
  76.     vec4 NDCUp = ProjectionMatrix * ViewMatrix * vec4(Up,1.);
  77.     vec2 TCUp = (NDCUp.xy / NDCUp.w) * 0.5 + 0.5;
  78.  
  79.     vec4 NDCDown = ProjectionMatrix * ViewMatrix * vec4(Down,1.);
  80.     vec2 TCDown = (NDCDown.xy / NDCDown.w) * 0.5 + 0.5;
  81.  
  82.  
  83.     int StepCount = int(distance(TCDown * TextureSize, TCUp * TextureSize));
  84.  
  85.     vec2 Direction = normalize(TCDown - TCUp);
  86.  
  87.     vec3 GrassWorldPosition = vec3(-1.);
  88.  
  89.     vec3 OneStepInWorldSpace = vec3(0.,0.05, 0.) / vec3(max(StepCount, 1));
  90.  
  91.     int Steps = 0;
  92.    
  93.     float GrassHeight;
  94.  
  95.     vec3 WorldPosGrassBottom;
  96.     vec3 ActualWorldPosGrassBottom;
  97.     bool Hit = false;
  98.  
  99.     for(int i=0;i<min(max(StepCount,1), 200);i++) {
  100.        
  101.         vec4 Sample = texture(Position, TexCoord + vec2(Direction * vec2(i)) * TexelSize);
  102.  
  103.         vec4 SampleNormal = texture(Normal, TexCoord + vec2(Direction * vec2(i)) * TexelSize);
  104.  
  105.         WorldPosGrassBottom = vec3(vec4(Sample.xyz, 1.0) * inverse(transpose(ViewMatrix)));
  106.  
  107.        
  108.  
  109.         float Distance = distance(CameraPosition, WorldPosGrassBottom);
  110.        
  111.         vec3 Seed = fract(floor(WorldPosGrassBottom * 1000.0) / 1000.0);
  112.  
  113.         vec3 WindSample = texture(Wind, WorldPosGrassBottom.xz + vec2(Time * 0.05, -Time * 0.04)).xyz;  
  114.  
  115.         vec2 Wind = ((WindSample.xy * 2. - 1.) * (1.-WindSample.z) * 4.0) * vec2(float(i)/float(max(StepCount,1)));
  116.  
  117.  
  118.  
  119.         if(pow(snoise(Seed.xz * 100.0 + Wind) ,0.5) > 0.85 * pow(1.-min(Distance * .01,1.), 10.0) && SampleNormal.a > 0.49) {
  120.        
  121.         Hit = true;
  122.         GrassHeight = (Distance > 0. && Distance < CurrentDistance) ? 1. : GrassHeight;
  123.         CurrentDistance = (Distance > 0. && Distance < CurrentDistance) ? Distance : CurrentDistance;
  124.         GrassWorldPosition = (Distance > 0. && Distance < CurrentDistance) ? GrassWorldPosition : (WorldPosGrassBottom - (OneStepInWorldSpace * vec3(-i)));
  125.         GrassHeight = (Distance > 0. && Distance < CurrentDistance) ? GrassHeight : -(OneStepInWorldSpace.y * float(-i));
  126.         ActualWorldPosGrassBottom = (Distance > 0. && Distance < CurrentDistance) ? ActualWorldPosGrassBottom : WorldPosGrassBottom + (vec3(Wind.x,0.,Wind.y) / vec3(100.));
  127.         Steps = i;
  128.  
  129.         }
  130.     }
  131.  
  132.     vec3 GrassNormal = vec3(0.,1.,0.);
  133.  
  134.     //Get The Grass Depth =
  135.  
  136.     vec4 ClipSpace = ProjectionMatrix * ViewMatrix * vec4(GrassWorldPosition, 1.0);
  137.     vec4 NDCSpace = ClipSpace / ClipSpace.w;
  138.  
  139.     float GrassDepth = NDCSpace.z * .5 + .5;
  140.     float DefferedDepth = texture(Depth,TexCoord).x;
  141.  
  142.  
  143.     vec4 ClipSpaceGrassBottom = ProjectionMatrix * ViewMatrix * vec4(ActualWorldPosGrassBottom, 1.0);
  144.     vec4 NDCSpaceGrassBottom = ClipSpaceGrassBottom / ClipSpaceGrassBottom.w;
  145.  
  146.     vec4 GrassBottomNormal = texture(Normal, NDCSpaceGrassBottom.xy*.5+.5);
  147.  
  148.  
  149.     if(GrassDepth < DefferedDepth && Hit && GrassBottomNormal.a > 0.49) {
  150.  
  151.     gl_FragDepth = GrassDepth;
  152.  
  153.     float MixFactor = min(GrassHeight * 5., 1.0);
  154.  
  155.     OutAlbedo.xyz = texture(Albedo, NDCSpaceGrassBottom.xy*.5+.5).xyz * (GrassHeight*10.);
  156.     //OutAlbedo.xyz = max(OutAlbedo.xyz, vec3(1.));
  157.     OutAlbedo.a = 1.0;
  158.     OutWorldPosSpecularity = vec4(vec3(ViewMatrix * vec4(GrassWorldPosition,1.)), 0.);
  159.     OutNormalBooleans = vec4(GrassNormal, 0.);
  160.  
  161.     }
  162.     else {
  163.     gl_FragDepth = DefferedDepth;
  164.     OutAlbedo = SampleAlbedo;
  165.     OutWorldPosSpecularity = SampleWorldPosition;
  166.     OutNormalBooleans = SampleNormal;
  167.     }
  168.  
  169.  
  170.     }
  171.     else {
  172.     gl_FragDepth = texture(Depth,TexCoord).x;
  173.     OutAlbedo = SampleAlbedo;
  174.     OutWorldPosSpecularity = SampleWorldPosition;
  175.     OutNormalBooleans = SampleNormal;
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement