Advertisement
Guest User

VSM

a guest
Apr 5th, 2012
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. //fragment
  2. #ifdef GL_ES
  3. #define LOWP lowp
  4. precision mediump float;
  5. #else
  6. #define LOWP  
  7. #endif
  8.  
  9. uniform sampler2D u_texture;
  10. uniform sampler2D u_texture1;
  11.  
  12. uniform float invFar; //   1/far
  13. uniform vec3 camPos;
  14. uniform float spot;
  15. uniform vec3 lightDir;
  16.  
  17.  
  18. varying vec2 v_texCoords;
  19. varying vec3 v_normal;
  20. varying vec4 shadowCoord;
  21. varying vec3 v_lightDir;
  22. varying vec3 v_eye;
  23.  
  24. varying vec3 v_lightDis;
  25.  
  26. const float shininessFactor = 15.0;
  27. const vec3 specularColor = vec3(0.575, 0.675, 0.445);
  28. const vec3 diffuseColor = vec3(0.975, 0.95, 0.925);
  29. const vec3 ambient = vec3(0.3, 0.3, 0.275);
  30.  
  31. const vec4 bitShifts = vec4(1.0 / 256.0, 1.0, 1.0 / 256.0, 1.0);
  32. vec2 unPack(vec4 zz)
  33. {
  34.     zz *= bitShifts;
  35.     return vec2((zz.x + zz.y),(zz.z + zz.w));
  36. }
  37.  
  38. const float MIN_VARIANCE = 0.0035;
  39. const float LIGHT_BLEEDING_DROPPER = 0.75;
  40.  
  41. float VSM( vec4 coord, float distance)
  42.     {
  43.        
  44.         vec2 shadowCoordPostW = (coord.xy / coord.w) * 0.5 + 0.5;
  45.         //retrive the two moments previously stored (depth and depth*depth)
  46.         vec4 momentsPack = texture2D(u_texture1, shadowCoordPostW);    
  47.        
  48.         float depth = distance * invFar;
  49.        
  50.         vec2 moments = unPack(momentsPack);    
  51.        
  52.         // The fragment is either in shadow or penumbra. We now use chebyshev's upperBound to check
  53.         // How likely this pixel is to be lit (p_max)
  54.        
  55.         float variance = moments.y - (moments.x * moments.x);
  56.         variance = max(variance, MIN_VARIANCE);
  57.    
  58.         float d = depth - moments.x;
  59.         float p_max = variance / (variance + d*d);
  60.        
  61.         float shade = clamp( ((p_max - LIGHT_BLEEDING_DROPPER) / (1.0 - LIGHT_BLEEDING_DROPPER)), 0.0, 1.0);
  62.         shade*=shade;
  63.        
  64.         // if Surface is fully lit. as the current fragment is before the light occluder return 1 other
  65.         return (depth <= moments.x) ? 1.0: shade;
  66. }
  67.  
  68. void main()
  69. {  
  70.    
  71.     vec3 tex = texture2D(u_texture, v_texCoords).rgb;
  72.     vec3 surfaceNormal = ( v_normal );
  73.    
  74.     //intensity
  75.     float dist = length(v_lightDis);
  76.     vec3 lightDirection = v_lightDis / dist;
  77.     float diffuse = dot(surfaceNormal, lightDirection);
  78.    
  79.     float intensity = clamp( 1.0 - (dist * invFar),0.0, 1.0 );
  80.     intensity = (diffuse >= 0.0) ? intensity : 0.0;
  81.  
  82.     float lightConeAngle = dot(lightDir, -lightDirection );
  83.     lightConeAngle = clamp( ((lightConeAngle - spot) / (1.0 - spot)), 0.0, 1.0);
  84.     intensity *=lightConeAngle;
  85.    
  86.     //specular 
  87.     vec3 fromEye = normalize(v_eye);    
  88.     vec3 halfAngle = normalize(lightDirection + fromEye);
  89.     float specular = pow(clamp(dot(halfAngle, surfaceNormal),0.0,1.0), shininessFactor);
  90.    
  91.    
  92.     //shadow   
  93.     float shadow = VSM(shadowCoord, dist); 
  94.    
  95.     intensity = intensity * shadow;    
  96.    
  97.     //combine lights+shadow
  98.     vec3 light = intensity * (specularColor * specular *  dot( tex, tex) + diffuse * diffuseColor * tex );
  99.    
  100.     gl_FragColor = vec4( light + (ambient * tex) , 1.0);
  101.  
  102. }
  103.  
  104. //Vertex
  105. attribute vec4 a_position;
  106. attribute vec2 a_texCoord0;
  107. attribute vec3 a_normal;
  108.  
  109. uniform vec3 lightPos;
  110. uniform vec3 camPos;
  111. uniform mat4 u_projectionViewMatrix;
  112.  
  113. uniform mat4 shadowProjMatrix;
  114.  
  115. varying vec2 v_texCoords;
  116. varying vec3 v_normal;
  117. varying vec3 v_lightDis;
  118. varying vec3 v_eye;
  119.  
  120. varying vec4 shadowCoord;
  121.                        
  122. void main()
  123. {
  124.     shadowCoord = shadowProjMatrix * a_position;
  125.    
  126.     v_texCoords = a_texCoord0;  
  127.     v_normal    = a_normal;
  128.     vec3 pos  = a_position.xyz;
  129.     v_lightDis  = lightPos - pos;
  130.     v_eye       = camPos   - pos;
  131.    
  132.     gl_Position = u_projectionViewMatrix * a_position;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement