Advertisement
Guest User

Untitled

a guest
Apr 8th, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 460 core
  2. out vec4 FragColor;
  3.  
  4. in Vertex {
  5.     vec3 FragPos;
  6.     vec2 TexCoords;
  7.     vec3 TangentLightPos;
  8.     vec3 TangentViewPos;
  9.     vec3 TangentFragPos;
  10. } vertex;
  11.  
  12. uniform sampler2D albedoTexture;
  13. uniform sampler2D normalTexture;
  14. uniform sampler2D heightTexture;
  15. uniform sampler2D ambientTexture;
  16. uniform sampler2D roughnessTexture;
  17. uniform sampler2D shininessTexture;
  18.  
  19. uniform mat4 ModelWorldMatrix;
  20. uniform vec3 lightPosition;
  21. uniform vec3 cameraPosition;
  22.  
  23. bool shadow = false;
  24. vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir, vec3 lightdir) {
  25.     float scale = 0.25;
  26.     float bias = -0.0147;
  27.     vec3 stepsize = viewDir * -0.001;
  28.     vec3 rayPosition = vec3(texCoords, 0);
  29.     float height = texture(heightTexture, vec2(rayPosition)).r * -scale - bias;
  30.     int i = 0;
  31.     while(i < 512) {
  32.         i++;
  33.         rayPosition += stepsize;
  34.         height = texture(heightTexture, vec2(rayPosition)).r * -scale - bias;
  35.         if(rayPosition.z < height)
  36.             break;
  37.     }
  38.  
  39.     stepsize = lightdir * 0.001;
  40.     vec3 lightrayPosition = rayPosition;
  41.     lightrayPosition += stepsize;
  42.     height = texture(heightTexture, vec2(lightrayPosition)).r * -scale - bias;
  43.     i = 0;
  44.     while(i < 512) {
  45.         i++;
  46.         lightrayPosition += stepsize;
  47.         height = texture(heightTexture, vec2(lightrayPosition)).r * -scale - bias;
  48.         if(lightrayPosition.z < height) {
  49.             shadow = true;
  50.             break;
  51.         }
  52.     }
  53.  
  54.     return vec2(rayPosition);
  55. }
  56.  
  57. vec3 lightCalculation(float radius, vec3 lightPos, vec3 fragPos, vec3 camPos, vec2 texcoord) {
  58.  
  59.     vec3 albedo = vec3(texture(albedoTexture, texcoord));
  60.     vec3 viewdirection = normalize(camPos - fragPos);
  61.     vec3 lightdirection = normalize(lightPos - fragPos);
  62.     vec3 halfway = normalize(viewdirection + lightdirection);
  63.  
  64.     float lightdistance = length(lightPos - fragPos);
  65.     vec3 normal = vec3(texture(normalTexture, texcoord)) * 2 - 1;
  66.     vec3 reflectDir = reflect(-lightdirection, normal);
  67.     vec3 shininess = vec3(texture(shininessTexture, texcoord));
  68.     float spec = pow(max(dot(normal, halfway), 0.0), shininess.x * 50); //blinn phong
  69.     float diffuse = max(dot(lightdirection, normal), 0);
  70.     float attenuation = (1.0 / (1.0f + ((2 / radius) * lightdistance) + ((1 / (radius * radius)) * lightdistance * lightdistance)));
  71.     vec3 ambient = vec3(texture(ambientTexture, texcoord));
  72.     vec3 diff = albedo * diffuse * attenuation;
  73.     vec3 specular = spec * albedo * attenuation * sqrt(shininess);
  74.     if(!shadow) {
  75.         if(dot(lightdirection, normal) <= 0)
  76.             return (albedo * ambient * attenuation * 0.025);
  77.         else
  78.             return (albedo * ambient * attenuation * 0.025) + (diff) + (specular);
  79.     } else
  80.         return (albedo * ambient * attenuation * 0.025);
  81.  
  82. }
  83.  
  84. void main() {
  85.     vec3 viewdirection = normalize(vertex.TangentViewPos - vertex.TangentFragPos);
  86.     vec3 lightdir = normalize(vertex.TangentLightPos - vertex.TangentFragPos);
  87.     vec2 parralaxCoords = ParallaxMapping(vertex.TexCoords, viewdirection, lightdir);
  88.     vec3 light = lightCalculation(2, vertex.TangentLightPos, vertex.TangentFragPos, vertex.TangentViewPos, parralaxCoords);
  89.     if(parralaxCoords.x > 1.0 || parralaxCoords.y > 1.0 || parralaxCoords.x < 0.0 || parralaxCoords.y < 0.0)
  90.         discard;
  91.     FragColor = vec4(light, 1.0f);
  92.  
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement