Advertisement
Zenn_

Untitled

Aug 10th, 2019
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. uniform sampler2D texture;
  2. uniform int width;
  3. uniform sampler2D lightmap;
  4. uniform vec3 lightColor;
  5.  
  6. //final step in the lighting algorithm.
  7. //generates the final shadow image from the
  8. //generated occlusion map :-DDDD wow haha
  9. const float cutoff = 0.1f;
  10. const float bias = .5f;
  11. const int intensitylevel = 3;
  12.  
  13. vec2 roundP(vec2 poop){
  14.     //pixel to pixel divided by 2
  15.     float p2p = 1.0f / width;
  16.     return poop.xy - mod(poop, vec2(p2p)) + vec2(p2p*0.5f);
  17. }
  18.  
  19. float GetShadowDistanceH(vec2 TexCoord)
  20. {
  21.     float u = TexCoord.x;
  22.     float v = TexCoord.y;
  23.  
  24.     u = abs(u-0.5f) * 2.0f;
  25.     v = v * 2.0f - 1.0f;
  26.     float v0 = v/u;
  27.     v0 = (v0 + 1.0f) / 2.0f;
  28.  
  29.     vec2 newCoords = vec2(TexCoord.x,v0);
  30.     //horizontal info was stored in the Red component
  31.     //gl_FragColor = texture2D(lightmap, newCoords);
  32.     //return newCoords.x < 0.5f ? 0 : 1;
  33.     return texture2D(lightmap, roundP(newCoords)).r; // * 0.70588235; //revert mult done in distancing
  34. }
  35.  
  36. float GetShadowDistanceV(vec2 TexCoord)
  37. {
  38.     float u = TexCoord.y;
  39.     float v = TexCoord.x;
  40.  
  41.     u = abs(u-0.5f) * 2.0f;
  42.     v = v * 2.0f - 1.0f;
  43.     float v0 = v/u;
  44.     v0 = (v0 + 1.0f) / 2.0f;
  45.  
  46.     vec2 newCoords = vec2(TexCoord.y, v0);
  47.     //vertical info was stored in the Green component
  48.     //gl_FragColor = texture2D(lightmap, newCoords);
  49.     //return newCoords.x < 0.5f ? 0 : 1;
  50.     return texture2D(lightmap, roundP(newCoords)).g; // * 0.70588235; //revert mult done in distancing
  51. }
  52.  
  53. void main()
  54. {
  55.     // distance of this pixel from the center
  56.     float dist = length(gl_TexCoord[0].xy - 0.5f);
  57.    
  58.     //apply pixel bias
  59.     dist -= 1.0f / width * bias;
  60.    
  61.     //distance stored in the shadow map
  62.     float shadowMapDistance;
  63.     //coords in [-1,1]
  64.     float nY = 2.0f*( gl_TexCoord[0].y - 0.5f);
  65.     float nX = 2.0f*( gl_TexCoord[0].x - 0.5f);
  66.  
  67.     //for debug
  68.     float red = 1.0f;
  69.     float green = 1.0f;
  70.  
  71.     //we use these to determine which quadrant we are in
  72.     if      (abs(nY) < abs(nX))
  73.     {
  74.         shadowMapDistance = GetShadowDistanceH(gl_TexCoord[0].xy);
  75.         green = 1.5f; //for debug
  76.     }
  77.     else if (abs(nY) > abs(nX))
  78.     {
  79.         red = 1.5f; //for debug
  80.         shadowMapDistance = GetShadowDistanceV(gl_TexCoord[0].xy);
  81.     }
  82.     else shadowMapDistance = 0.0f;
  83.  
  84.  
  85.     //if distance to this pixel is lower than distance from shadowMap,
  86.     //then we are not in shadow
  87.     //float light = dist;
  88.     float light = dist < shadowMapDistance ? 1:0;
  89.    
  90.     float intensity = 1.0f - dist;
  91.     for(int i = 1; i < intensitylevel; i++){
  92.         intensity = intensity * (1-dist);
  93.     }
  94.  
  95.     gl_FragColor = vec4(light > 0 ? (lightColor.r * intensity - cutoff) * red       : 0.0f,
  96.                         light > 0 ? (lightColor.g * intensity - cutoff) * green     : 0.0f,
  97.                         light > 0 ? lightColor.b * intensity - cutoff           : 0.0f,
  98.                         1.0f);
  99.                        
  100.     //gl_FragColor = vec4(light, light, light, 1.0f - (dist / 1.4f));
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement