Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 300 es
- #ifdef GL_ES
- precision mediump float;
- #endif
- #define PI 3.14159265359
- #define TWO_PI 6.28318530718
- #define BLINN true
- #define GAMMA_CORRECTION true
- in vec3 camera;
- in vec2 uvcoords;
- in vec3 fragPos;
- in vec3 nrms;
- // Tangent In
- in vec3 tangentLightPos;
- in vec3 tangentCameraPos;
- in vec3 tangentFragPos;
- uniform sampler2D texture0;
- uniform sampler2D texture1;
- uniform sampler2D texture2;
- uniform float time;
- uniform float uvtile;
- out vec4 color;
- struct Light {
- vec3 position;
- vec3 ambient;
- vec3 diffuse;
- vec3 specular;
- float constant;
- float linear;
- float quadratic;
- };
- struct Material {
- vec3 diffuse;
- vec3 specular;
- float shininess;
- };
- Light lights[2];
- vec3 calcPointLight(Light light, vec3 normal, vec3 camDir, vec3 diffuseMap, vec3 specularMap, Material material) {
- // vec3 lightDirRAW = light.position - fragPos;
- vec3 lightDirRAW = light.position - tangentFragPos;
- vec3 lightDir = normalize(lightDirRAW);
- float distance = length(lightDirRAW);
- // Attenuation
- float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
- // Ambient
- vec3 ambient = light.ambient * diffuseMap.rgb;
- // Diffuse
- float diffuseBrightness = max(dot(normal, lightDir), 0.0);
- vec3 diffuse = light.diffuse * diffuseBrightness * diffuseMap.rgb;
- // Specular
- vec3 specular = vec3(0.0);
- if (BLINN) {
- vec3 halfwayDir = normalize(lightDir + camDir);
- float specularBrightness = pow(max(dot(normal, halfwayDir), 0.0), material.shininess);
- specular = light.specular * specularBrightness * specularMap.rgb;
- }
- else {
- vec3 reflectDir = reflect(-lightDir, normal);
- float specularBrightness = pow(max(dot(camDir, reflectDir), 0.0), material.shininess);
- specular = light.specular * specularBrightness * specularMap.rgb;
- }
- ambient *= attenuation;
- diffuse *= attenuation;
- specular *= attenuation;
- // Composition
- vec3 composition = ambient + diffuse + specular;
- return composition;
- }
- void main () {
- vec3 composition = vec3(0.0);
- float moveX = sin(time * 1.2) * 2.0;
- float moveZ = cos(time * 1.2) * 2.0;
- // Light 1
- Light light;
- light.ambient = vec3 (0.2, 0.2, 0.2);
- light.diffuse = vec3 (1.0, 1.0, 1.0);
- light.specular = vec3 (1.0, 1.0, 1.0);
- light.constant = 1.0;
- light.linear = 0.09;
- light.quadratic = 0.032;
- lights[0] = light;
- Material material;
- material.shininess = 120.6;
- vec2 uvs = fract(uvcoords).xy * uvtile;
- // Colors
- vec4 diffuseMap = texture(texture0, uvs);
- vec4 specularMap = texture(texture1, uvs);
- vec4 normalMap = texture(texture2, uvs);
- // vec3 camDir = normalize(camera - fragPos);
- vec3 camDir = normalize(tangentCameraPos - tangentFragPos);
- vec3 tangentNormals = normalize(normalMap.rgb * 2.0 - 1.0);
- for (int i = 0; i < 1; i += 1) {
- composition += calcPointLight(lights[i], tangentNormals, camDir, diffuseMap.rgb, specularMap.rgb, material);
- }
- if (GAMMA_CORRECTION) {
- composition = pow(composition, vec3(1.0 / 2.0));
- }
- color = vec4(composition, diffuseMap.a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement