Cleanup in the shaders, making them look prettier and a bit more optimized

This commit is contained in:
Peder Bergebakken Sundt 2019-03-22 23:50:03 +01:00
parent 84cbd5e5c7
commit 2164551e25
2 changed files with 20 additions and 17 deletions

View File

@ -62,8 +62,7 @@ vec3 reflection(vec3 basecolor, vec3 nnormal) {
: mix(basecolor, reflection, reflexiveness); : mix(basecolor, reflection, reflexiveness);
} }
vec3 phong(vec3 basecolor) { vec3 get_nnormal() {
vec3 nnormal; // normalized normal
if (isNormalMapped) { if (isNormalMapped) {
mat3 TBN; mat3 TBN;
if (isDisplacementMapped) { if (isDisplacementMapped) {
@ -83,19 +82,23 @@ vec3 phong(vec3 basecolor) {
normalize(normal) normalize(normal)
); );
} }
nnormal = TBN * normalize(texture(normalTexture, UV).rgb * 2.0 - 1.0); return TBN * normalize(texture(normalTexture, UV).rgb * 2.0 - 1.0);
} }
else { else {
if (isDisplacementMapped) { if (isDisplacementMapped) {
float o = texture(displacementTexture, UV).r * 2.0 - 1.0; float o = texture(displacementTexture, UV).r * 2.0 - 1.0;
float u = (texture(displacementTexture, UV + vec2(0.00001, 0.0)).r*2.0-1.0 - o) / 0.00004; float u = (texture(displacementTexture, UV + vec2(0.00001, 0.0)).r*2.0-1.0 - o) / 0.00004;
float v = (texture(displacementTexture, UV + vec2(0.0, 0.00001)).r*2.0-1.0 - o) / 0.00004; float v = (texture(displacementTexture, UV + vec2(0.0, 0.00001)).r*2.0-1.0 - o) / 0.00004;
nnormal = normalize(cross(tangent + normal*u, bitangent + normal*v)); return normalize(cross(tangent + normal*u, bitangent + normal*v));
} }
else { else {
nnormal = normalize(normal); return normalize(normal);
} }
} }
}
vec3 phong(vec3 basecolor) {
vec3 nnormal = get_nnormal(); // normalized normal
vec3 diffuse_component = vec3(0.0); vec3 diffuse_component = vec3(0.0);
vec3 specular_component = vec3(0.0); vec3 specular_component = vec3(0.0);
@ -123,16 +126,16 @@ vec3 phong(vec3 basecolor) {
? pow(specular_i, shininess) ? pow(specular_i, shininess)
: 0; : 0;
specular_component += specular_color * light[i].color * specular_i * attenuation; specular_component += light[i].color * specular_i * attenuation;
if (diffuse_i>0) diffuse_component += diffuse_color * light[i].color * diffuse_i * attenuation; if (diffuse_i>0) diffuse_component += light[i].color * diffuse_i * attenuation;
} }
basecolor *= (emissive_color + diffuse_component); basecolor *= (emissive_color + diffuse_color *diffuse_component);
if (isReflectionMapped) if (isReflectionMapped)
basecolor = reflection(basecolor, nnormal); basecolor = reflection(basecolor, nnormal);
return basecolor + specular_component; return basecolor + specular_color * specular_component;
} }
void main() { void main() {

View File

@ -44,19 +44,19 @@ void main() {
vec3 displacement = vec3(0.0); vec3 displacement = vec3(0.0);
if (isDisplacementMapped) { if (isDisplacementMapped) {
float o = texture(displacementTexture, UV + uvOffset).r * 2.0 - 1.0; float o = texture(displacementTexture, UV + uvOffset).r * 2.0 - 1.0;
float u = (texture(displacementTexture, UV + uvOffset + vec2(0.001, 0.0)).r*2.0-1.0 - o) / 0.004; //float u = (texture(displacementTexture, UV + uvOffset + vec2(0.001, 0.0)).r*2.0-1.0 - o) / 0.004;
float v = (texture(displacementTexture, UV + uvOffset + vec2(0.0, 0.001)).r*2.0-1.0 - o) / 0.004; //float v = (texture(displacementTexture, UV + uvOffset + vec2(0.0, 0.001)).r*2.0-1.0 - o) / 0.004;
displacement = normal * displacementCoefficient * o; displacement = normal * displacementCoefficient * o;
} }
normal_out = normalize(vec3(MVnormal * vec4(normal, 1.0f)));
vertex_out = vec3(MV * vec4(position+displacement, 1.0f)); vertex_out = vec3(MV * vec4(position+displacement, 1.0f));
uv_out = UV + uvOffset;
gl_Position = MVP * vec4(position+displacement, 1.0f); gl_Position = MVP * vec4(position+displacement, 1.0f);
uv_out = UV + uvOffset;
color_out = color; color_out = color;
normal_out = normalize(vec3(MVnormal * vec4(normal, 1.0f)));
tangent_out = normalize(vec3(MVnormal * vec4(tangent, 1.0f))); tangent_out = normalize(vec3(MVnormal * vec4(tangent, 1.0f)));
bitangent_out = normalize(vec3(MVnormal * vec4(bitangent, 1.0f))); bitangent_out = normalize(vec3(MVnormal * vec4(bitangent, 1.0f)));
} }