Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 460 core
- out vec4 FragColor;
- in Vertex {
- vec3 FragPos;
- vec2 TexCoords;
- vec3 TangentLightPos;
- vec3 TangentViewPos;
- vec3 TangentFragPos;
- } vertex;
- uniform sampler2D albedoTexture;
- uniform sampler2D normalTexture;
- uniform sampler2D heightTexture;
- uniform sampler2D ambientTexture;
- uniform sampler2D roughnessTexture;
- uniform sampler2D shininessTexture;
- uniform mat4 ModelWorldMatrix;
- uniform vec3 lightPosition;
- uniform vec3 cameraPosition;
- bool shadow = false;
- vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir, vec3 lightdir) {
- float scale = 0.25;
- float bias = -0.0147;
- vec3 stepsize = viewDir * -0.001;
- vec3 rayPosition = vec3(texCoords, 0);
- float height = texture(heightTexture, vec2(rayPosition)).r * -scale - bias;
- int i = 0;
- while(i < 512) {
- i++;
- rayPosition += stepsize;
- height = texture(heightTexture, vec2(rayPosition)).r * -scale - bias;
- if(rayPosition.z < height)
- break;
- }
- stepsize = lightdir * 0.001;
- vec3 lightrayPosition = rayPosition;
- lightrayPosition += stepsize;
- height = texture(heightTexture, vec2(lightrayPosition)).r * -scale - bias;
- i = 0;
- while(i < 512) {
- i++;
- lightrayPosition += stepsize;
- height = texture(heightTexture, vec2(lightrayPosition)).r * -scale - bias;
- if(lightrayPosition.z < height) {
- shadow = true;
- break;
- }
- }
- return vec2(rayPosition);
- }
- vec3 lightCalculation(float radius, vec3 lightPos, vec3 fragPos, vec3 camPos, vec2 texcoord) {
- vec3 albedo = vec3(texture(albedoTexture, texcoord));
- vec3 viewdirection = normalize(camPos - fragPos);
- vec3 lightdirection = normalize(lightPos - fragPos);
- vec3 halfway = normalize(viewdirection + lightdirection);
- float lightdistance = length(lightPos - fragPos);
- vec3 normal = vec3(texture(normalTexture, texcoord)) * 2 - 1;
- vec3 reflectDir = reflect(-lightdirection, normal);
- vec3 shininess = vec3(texture(shininessTexture, texcoord));
- float spec = pow(max(dot(normal, halfway), 0.0), shininess.x * 50); //blinn phong
- float diffuse = max(dot(lightdirection, normal), 0);
- float attenuation = (1.0 / (1.0f + ((2 / radius) * lightdistance) + ((1 / (radius * radius)) * lightdistance * lightdistance)));
- vec3 ambient = vec3(texture(ambientTexture, texcoord));
- vec3 diff = albedo * diffuse * attenuation;
- vec3 specular = spec * albedo * attenuation * sqrt(shininess);
- if(!shadow) {
- if(dot(lightdirection, normal) <= 0)
- return (albedo * ambient * attenuation * 0.025);
- else
- return (albedo * ambient * attenuation * 0.025) + (diff) + (specular);
- } else
- return (albedo * ambient * attenuation * 0.025);
- }
- void main() {
- vec3 viewdirection = normalize(vertex.TangentViewPos - vertex.TangentFragPos);
- vec3 lightdir = normalize(vertex.TangentLightPos - vertex.TangentFragPos);
- vec2 parralaxCoords = ParallaxMapping(vertex.TexCoords, viewdirection, lightdir);
- vec3 light = lightCalculation(2, vertex.TangentLightPos, vertex.TangentFragPos, vertex.TangentViewPos, parralaxCoords);
- if(parralaxCoords.x > 1.0 || parralaxCoords.y > 1.0 || parralaxCoords.x < 0.0 || parralaxCoords.y < 0.0)
- discard;
- FragColor = vec4(light, 1.0f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement