Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. #version 300 es
  2. #ifdef GL_ES
  3. precision mediump float;
  4. #endif
  5.  
  6. #define PI 3.14159265359
  7. #define TWO_PI 6.28318530718
  8. #define BLINN true
  9. #define GAMMA_CORRECTION true
  10.  
  11. in vec3 camera;
  12. in vec2 uvcoords;
  13. in vec3 fragPos;
  14. in vec3 nrms;
  15.  
  16. // Tangent In
  17. in vec3 tangentLightPos;
  18. in vec3 tangentCameraPos;
  19. in vec3 tangentFragPos;
  20.  
  21.  
  22. uniform sampler2D texture0;
  23. uniform sampler2D texture1;
  24. uniform sampler2D texture2;
  25.  
  26. uniform float time;
  27. uniform float uvtile;
  28.  
  29. out vec4 color;
  30.  
  31. struct Light {
  32.     vec3 position;
  33.  
  34.     vec3 ambient;
  35.     vec3 diffuse;
  36.     vec3 specular;
  37.  
  38.     float constant;
  39.     float linear;
  40.     float quadratic;
  41. };
  42.  
  43. struct Material {
  44.     vec3 diffuse;
  45.     vec3 specular;
  46.     float shininess;
  47. };
  48.  
  49. Light lights[2];
  50.  
  51.  
  52.  
  53. vec3 calcPointLight(Light light, vec3 normal, vec3 camDir, vec3 diffuseMap, vec3 specularMap, Material material)  {
  54.  
  55.     // vec3 lightDirRAW    = light.position - fragPos;
  56.     vec3 lightDirRAW    = light.position - tangentFragPos;
  57.     vec3 lightDir       = normalize(lightDirRAW);
  58.     float distance      = length(lightDirRAW);
  59.  
  60.     // Attenuation
  61.     float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance *  distance));
  62.  
  63.     // Ambient
  64.     vec3 ambient = light.ambient * diffuseMap.rgb;
  65.  
  66.     // Diffuse
  67.     float diffuseBrightness = max(dot(normal, lightDir), 0.0);
  68.     vec3 diffuse = light.diffuse * diffuseBrightness * diffuseMap.rgb;
  69.  
  70.     // Specular
  71.  
  72.     vec3 specular = vec3(0.0);
  73.     if (BLINN) {
  74.         vec3 halfwayDir          = normalize(lightDir + camDir);
  75.         float specularBrightness = pow(max(dot(normal, halfwayDir), 0.0), material.shininess);
  76.         specular                 = light.specular * specularBrightness * specularMap.rgb;
  77.  
  78.     }
  79.     else {
  80.         vec3 reflectDir          = reflect(-lightDir, normal);
  81.         float specularBrightness = pow(max(dot(camDir, reflectDir), 0.0), material.shininess);
  82.         specular                 = light.specular * specularBrightness * specularMap.rgb;
  83.     }
  84.  
  85.     ambient *= attenuation;
  86.     diffuse *= attenuation;
  87.     specular *= attenuation;
  88.  
  89.     // Composition
  90.     vec3 composition = ambient + diffuse + specular;
  91.  
  92.     return composition;
  93. }
  94.  
  95. void main () {
  96.  
  97.     vec3 composition = vec3(0.0);
  98.  
  99.     float moveX = sin(time * 1.2) * 2.0;
  100.     float moveZ = cos(time * 1.2) * 2.0;
  101.  
  102.     // Light 1
  103.     Light light;
  104.     light.ambient    = vec3 (0.2, 0.2, 0.2);
  105.     light.diffuse    = vec3 (1.0, 1.0, 1.0);
  106.     light.specular   = vec3 (1.0, 1.0, 1.0);
  107.     light.constant   = 1.0;
  108.     light.linear     = 0.09;
  109.     light.quadratic  = 0.032;
  110.  
  111.     lights[0] = light;
  112.  
  113.     Material material;
  114.     material.shininess =  120.6;
  115.  
  116.     vec2 uvs =  fract(uvcoords).xy * uvtile;
  117.  
  118.     // Colors
  119.     vec4 diffuseMap     = texture(texture0, uvs);
  120.     vec4 specularMap    = texture(texture1, uvs);
  121.     vec4 normalMap      = texture(texture2, uvs);
  122.  
  123.  
  124.     // vec3 camDir         = normalize(camera - fragPos);
  125.     vec3 camDir             = normalize(tangentCameraPos - tangentFragPos);
  126.     vec3 tangentNormals     = normalize(normalMap.rgb * 2.0 - 1.0);
  127.  
  128.     for (int i = 0; i < 1; i += 1) {
  129.         composition += calcPointLight(lights[i], tangentNormals, camDir, diffuseMap.rgb, specularMap.rgb, material);
  130.     }
  131.  
  132.     if (GAMMA_CORRECTION) {
  133.         composition = pow(composition, vec3(1.0 / 2.0));
  134.     }
  135.    
  136.     color = vec4(composition, diffuseMap.a);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement