Advertisement
Masterchoc

Untitled

Jun 25th, 2019
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 330 core
  2.  
  3. #include lighting.glsl
  4. #include tonemapping.glsl
  5.  
  6. #define MAX_DIRECTIONAL_LIGHTS 1
  7. #define MAX_POINT_LIGHTS 1
  8. #define MAX_SPOT_LIGHTS 1
  9.  
  10. out vec4 FragColor;
  11.  
  12. struct Material
  13. {
  14.     sampler2D diffuseMap;
  15.     sampler2D specularMap;
  16.     sampler2D normalMap;
  17.     sampler2D shadowMap;
  18.     vec3 diffuseColor;
  19.     vec3 specularColor;
  20.     vec3 ambientColor;
  21.     float shininess;
  22.     float shininess_strength;
  23.     float normal_strength;
  24.     int hasDiffuse;
  25.     int hasSpecular;
  26.     int hasNormal;
  27.     vec2 diffuse_tiling;
  28.     vec2 specular_tiling;
  29.     vec2 normal_tiling;
  30. };
  31.  
  32. struct Directional
  33. {
  34.     vec3 direction;
  35.     vec3 ambient;
  36.     vec3 diffuse;
  37.     vec3 specular;
  38.     float intensity;
  39. };
  40.  
  41. struct Point
  42. {
  43.     vec3 position;
  44.     float constant;
  45.     float linear;
  46.     float quadratic;
  47.     vec3 ambient;
  48.     vec3 diffuse;
  49.     vec3 specular;
  50.     float intensity;
  51. };
  52.  
  53. struct Spot
  54. {
  55.     vec3 position;
  56.     vec3 direction;
  57.     float intensity;
  58.     float inner_cutoff;
  59.     float outer_cutoff;
  60.     float constant;
  61.     float linear;
  62.     float quadratic;
  63.     vec3 ambient;
  64.     vec3 diffuse;
  65.     vec3 specular;
  66. };
  67.  
  68. in vec2 f_uvs;
  69. in vec3 f_position;
  70. in vec3 f_normal;
  71. in vec3 f_tangent;
  72. in vec4 f_posLightSpace;
  73. in mat3 f_tbn;
  74.  
  75. uniform Material material;
  76. uniform vec3 viewPosition;
  77. uniform vec3 lightPos;
  78.  
  79. uniform Directional directionalLights[MAX_DIRECTIONAL_LIGHTS];
  80. uniform Point pointLights[MAX_POINT_LIGHTS];
  81. uniform Spot spotLights[MAX_SPOT_LIGHTS];
  82.  
  83. vec3 CalcDirectionalLight(Directional light, vec3 normal, vec3 viewDir);
  84. vec3 CalcPointLight(Point light, vec3 normal, vec3 fragPos, vec3 viewDir);
  85. vec3 CalcSpotLight(Spot light, vec3 normal, vec3 fragPos, vec3 viewDir);
  86.  
  87. float CalcShadows(vec4 fragPosLightSpace, Material material, vec3 f_normal, vec2 f_uvs, mat3 f_tbn, vec3 f_position, vec3 lightPos)
  88. {
  89.     vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
  90.     projCoords = projCoords * 0.5 + 0.5;
  91.  
  92.     float closestDepth = texture(material.shadowMap, projCoords.xy).r;
  93.     float currentDepth = projCoords.z;
  94.     vec3 normal = normalize(f_normal);
  95.  
  96.     if(material.hasNormal == 1)
  97.     {
  98.         vec3 normal_texel = mix(f_normal, 2.0f * texture(material.normalMap, vec2(f_uvs.x * material.normal_tiling.x, f_uvs.y * material.normal_tiling.y)).rgb - 1.0f, 0.5);
  99.         normal_texel.xy *= material.normal_strength;
  100.         normal_texel = normalize(normal_texel);
  101.         normal = normalize(f_tbn * normal_texel);
  102.     }
  103.  
  104.     vec3 lightDir = normalize(lightPos - f_position);
  105.     float bias = max(0.001 * (1.0 - dot(normal, lightDir)), 0.0035);
  106.  
  107.     float shadow = 0.0;
  108.     vec2 texelSize = 1.0 / textureSize(material.shadowMap, 0);
  109.  
  110.     for(int x = -1; x <= 1; ++x)
  111.     {
  112.         for(int y = -1; y <= 1; ++y)  
  113.         {
  114.             float pcfDepth = texture(material.shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
  115.             shadow += currentDepth - bias > pcfDepth  ? 1.0 : 0.0;
  116.         }    
  117.     }
  118.  
  119.     shadow /= 9.0;
  120.  
  121.     if(projCoords.z > 1.0)
  122.         shadow = 0.0;
  123.        
  124.     return shadow;
  125. }
  126.  
  127. void main()
  128. {
  129.     vec3 norm = normalize(f_normal);
  130.     vec3 viewDir = normalize(viewPosition - f_position);
  131.     vec3 result = vec3(0.0f);
  132.  
  133.     for(int i = 0; i < MAX_DIRECTIONAL_LIGHTS - 1; i++)
  134.         result += CalcDirectionalLight(directionalLights[i], norm, viewDir);
  135.  
  136.     for(int i = 0; i < MAX_POINT_LIGHTS - 1; i++)
  137.         result += CalcPointLight(pointLights[i], norm, f_position, viewDir);
  138.  
  139.     for(int i = 0; i < MAX_SPOT_LIGHTS - 1; i++)
  140.         result += CalcSpotLight(spotLights[i], norm, f_position, viewDir);
  141.        
  142.     FragColor = vec4(result, 1.0f);
  143.     //FragColor.rgb = f_tangent;
  144.     //FragColor.rgb = tonemapUncharted2(FragColor.rgb);
  145. }
  146.  
  147. vec3 CalcDirectionalLight(Directional light, vec3 normal, vec3 viewDir)
  148. {
  149.     vec3 lighting = vec3(0.0);
  150.    
  151.     if(material.hasNormal == 1)
  152.     {
  153.         vec3 normal_texel = mix(normal, 2.0f * texture(material.normalMap, vec2(f_uvs.x * material.normal_tiling.x, f_uvs.y * material.normal_tiling.y)).rgb - 1.0f, 0.5f);
  154.         normal_texel.xy *= material.normal_strength;
  155.         normal_texel = normalize(normal_texel);
  156.         normal = normalize(f_tbn * normal_texel);
  157.     }
  158.  
  159.     vec3 lightDir = normalize(light.direction);
  160.     float diff = max(0.0, dot(normal, lightDir)) * light.intensity;
  161.  
  162.     vec3 diffuse = vec3(0.0f);
  163.     vec3 specular = vec3(0.0f);
  164.     vec3 ambient = light.ambient * material.ambientColor;
  165.  
  166.     vec3 reflectDir = reflect(-lightDir, normal);
  167.     float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess) * material.shininess_strength;
  168.  
  169.     if(material.hasDiffuse == 1)
  170.     {
  171.         vec4 texel = texture(material.diffuseMap, vec2(f_uvs.x * material.diffuse_tiling.x, f_uvs.y * material.diffuse_tiling.y));
  172.  
  173.         if(texel.a <= 0.0f)
  174.             discard;
  175.  
  176.         diffuse = light.diffuse * material.diffuseColor * diff * vec3(texel);
  177.         ambient = light.ambient * material.ambientColor * vec3(texture(material.diffuseMap, vec2(f_uvs.x * material.diffuse_tiling.x, f_uvs.y * material.diffuse_tiling.y)));
  178.     }
  179.     else {
  180.         ambient = light.ambient * material.ambientColor;
  181.         diffuse = light.diffuse * diff * material.diffuseColor;
  182.     }
  183.    
  184.     if(material.hasSpecular == 1)
  185.         specular = light.specular * material.specularColor * spec * vec3(texture(material.specularMap, vec2(f_uvs.x * material.specular_tiling.x, f_uvs.y * material.specular_tiling.y)));
  186.     else
  187.         specular = light.specular * spec * material.specularColor;
  188.  
  189.     float shadow = CalcShadows(f_posLightSpace, material, f_normal, f_uvs, f_tbn, f_position, lightPos);
  190.     lighting = ambient + (1.0 - ambient - shadow) * (diffuse + specular);
  191.  
  192.     return lighting;
  193. }
  194.  
  195. vec3 CalcPointLight(Point light, vec3 normal, vec3 fragPos, vec3 viewDir)
  196. {
  197.     if(material.hasNormal == 1)
  198.     {
  199.         vec3 normal_texel = mix(normal, 2.0f * texture(material.normalMap, vec2(f_uvs.x * material.normal_tiling.x, f_uvs.y * material.normal_tiling.y)).rgb - 1.0f, 0.5);
  200.         normal_texel.xy *= material.normal_strength;
  201.         normal_texel = normalize(normal_texel);
  202.         normal = normalize(f_tbn * normal_texel);
  203.     }
  204.  
  205.     vec3 lightDir = normalize(light.position - fragPos);
  206.     float diff = max(dot(normal, lightDir), 0.0f) * light.intensity;
  207.  
  208.     vec3 reflectDir = reflect(-lightDir, normal);
  209.     float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
  210.  
  211.     float distance = length(light.position - fragPos);
  212.     float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));    
  213.  
  214.     vec3 diffuse = vec3(0.0f);
  215.     vec3 specular = vec3(0.0f);
  216.  
  217.     vec3 ambient = light.ambient * material.ambientColor * vec3(texture(material.diffuseMap, vec2(f_uvs.x * material.diffuse_tiling.x, f_uvs.y * material.diffuse_tiling.y)));
  218.    
  219.     if(material.hasDiffuse == 1)
  220.         diffuse = light.diffuse * material.diffuseColor * diff * vec3(texture(material.diffuseMap, vec2(f_uvs.x * material.diffuse_tiling.x, f_uvs.y * material.diffuse_tiling.y)));
  221.     else
  222.         diffuse = light.diffuse * diff * material.diffuseColor;
  223.  
  224.     if(material.hasSpecular == 1)
  225.         specular = light.specular * material.specularColor * spec * vec3(texture(material.specularMap, vec2(f_uvs.x * material.specular_tiling.x, f_uvs.y * material.specular_tiling.y)));
  226.     else
  227.         specular = light.specular * spec * material.specularColor;
  228.  
  229.     ambient  *= attenuation;
  230.     diffuse  *= attenuation;
  231.     specular *= attenuation;
  232.  
  233.     return (ambient + diffuse + specular);
  234. }
  235.  
  236. vec3 CalcSpotLight(Spot light, vec3 normal, vec3 fragPos, vec3 viewDir)
  237. {
  238.     if(material.hasNormal == 1)
  239.     {
  240.         vec3 normal_texel = mix(normal, 2.0f * texture(material.normalMap,  vec2(f_uvs.x * material.normal_tiling.x, f_uvs.y * material.normal_tiling.y)).rgb - 1.0f, 0.5);
  241.         normal_texel.xy *= material.normal_strength;
  242.         normal_texel = normalize(normal_texel);
  243.         normal = normalize(f_tbn * normal_texel);
  244.     }      
  245.  
  246.     vec3 lightDir = normalize(light.position - fragPos);
  247.     float diff = max(dot(normal, lightDir), 0.0f) * light.intensity;
  248.  
  249.     vec3 reflectDir = reflect(-lightDir, normal);
  250.     float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
  251.  
  252.     float distance = length(light.position - fragPos);
  253.     float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));    
  254.  
  255.     float theta     = dot(lightDir, normalize(-light.direction));
  256.     float epsilon   = light.inner_cutoff - light.outer_cutoff;
  257.     float intensity = clamp((theta - light.outer_cutoff) / epsilon, 0.0f, 1.0f);
  258.  
  259.     vec3 ambient = light.ambient * material.ambientColor * vec3(texture(material.diffuseMap, vec2(f_uvs.x * material.diffuse_tiling.x, f_uvs.y * material.diffuse_tiling.y)));
  260.    
  261.     vec3 diffuse = vec3(0.0f);
  262.     vec3 specular = vec3(0.0f);
  263.  
  264.     if(material.hasDiffuse == 1)
  265.         diffuse = light.diffuse * material.diffuseColor * diff * vec3(texture(material.diffuseMap, vec2(f_uvs.x * material.diffuse_tiling.x, f_uvs.y * material.diffuse_tiling.y)));
  266.     else
  267.         diffuse = light.diffuse * diff * material.diffuseColor;
  268.  
  269.     if(material.hasSpecular == 1)
  270.         specular = light.specular * material.specularColor * spec * vec3(texture(material.specularMap, vec2(f_uvs.x * material.specular_tiling.x, f_uvs.y * material.specular_tiling.y)));
  271.     else
  272.         specular = light.specular * spec * material.specularColor;
  273.  
  274.     ambient  *= attenuation * intensity;
  275.     diffuse  *= attenuation * intensity;
  276.     specular *= attenuation * intensity;
  277.  
  278.     return (ambient + diffuse + specular);
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement