Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FRAGMENT SHADER
- #version 330
- // Matrices
- uniform mat4 matrixView;
- // Materials
- uniform vec3 materialAmbient;
- uniform vec3 materialDiffuse;
- uniform vec3 materialSpecular;
- uniform float shininess;
- uniform vec3 fogColor;
- // Textures
- uniform sampler2D texture0;
- uniform sampler2D textureNormal;
- uniform sampler2D textureShore;
- uniform sampler2D textureUnderwater;
- uniform float NormalMap;
- // Water
- uniform vec3 waterColor;
- // Local Variables
- vec3 finalNormal;
- in vec4 color;
- in vec4 position;
- in vec3 normal;
- in float fogStrength;
- in vec2 texCoord0;
- in mat3 matrixTangent;
- in float waterDepth;
- out vec4 outColor;
- // point light declaration
- struct POINT
- {
- int on;
- vec3 position;
- vec3 diffuse;
- vec3 specular;
- };
- uniform POINT lightPoint;
- // spot light declaration
- struct SPOT
- {
- int on;
- vec3 position;
- vec3 diffuse;
- vec3 specular;
- vec3 direction;
- float cutoff;
- float attenuation;
- float strength;
- mat4 matrix;
- };
- uniform SPOT lightSpot;
- // point light function
- vec4 PointLight(POINT light)
- {
- // diffuse light
- vec4 color = vec4(0, 0, 0, 1);
- vec3 L = normalize((matrixView * vec4(light.position, 1)) - position).xyz;
- float NdotL = dot(L, finalNormal);
- if (NdotL > 0)
- {
- color += vec4 (light.diffuse * materialDiffuse, 1) * NdotL;
- }
- // specular specular
- vec3 V = normalize(-position.xyz);
- vec3 R = reflect(-L, finalNormal);
- float RdotV = dot(R, V);
- if (NdotL > 0 && RdotV > 0)
- {
- color += vec4(light.specular * materialSpecular * pow(RdotV, shininess), 1);
- }
- return color;
- }
- // spot light function
- vec4 SpotLight(SPOT light)
- {
- vec4 color = vec4(0, 0, 0, 1);
- vec3 L = normalize(light.matrix * vec4(light.position, 1) - position).xyz;
- // diffuse light
- float NdotL = dot(L, finalNormal);
- if (NdotL > 0)
- {
- color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
- }
- // specular light
- vec3 V = normalize(-position.xyz);
- vec3 R = reflect(-L, finalNormal);
- float RdotV = dot(R, V);
- if (NdotL > 0 && RdotV > 0)
- {
- color += vec4(materialSpecular * light.specular * pow(RdotV, shininess), 1);
- }
- // spot light
- vec3 D = normalize(mat3(light.matrix) * light.direction);
- float S = dot(-L, D);
- float A = acos(S);
- float cutoff = radians(clamp(light.cutoff, 0, 360));
- if (A < cutoff) S = pow(S, light.attenuation);
- else S = 0;
- // attenuation
- float dist = length(light.matrix * vec4(light.position, 1) - position);
- float att = 1 / (dist * dist) / light.strength;
- return color * S * att;
- }
- void main(void)
- {
- outColor = color;
- if (NormalMap == 1)
- {
- finalNormal = 2.0 * texture(textureNormal, texCoord0).xyz - vec3(1.0, 1.0, 1.0);
- finalNormal = normalize(matrixTangent * finalNormal);
- }
- else finalNormal = normal;
- if (lightPoint.on == 1) outColor += PointLight(lightPoint);
- if (lightSpot.on == 1) outColor += SpotLight(lightSpot);
- // shoreline multitexturing
- float isAboveWater = clamp(-waterDepth, 0, 1);
- outColor *= texture(texture0, texCoord0);
- outColor *= mix(texture(textureUnderwater, texCoord0), texture(textureShore, texCoord0), isAboveWater);
- outColor = mix(vec4(fogColor, 1.0), outColor, fogStrength);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement