Fix bump mapping

This commit is contained in:
Nikita Lisitsa 2020-12-13 23:45:14 +03:00
parent f10f301443
commit ffd3c6a6e8

View file

@ -69,34 +69,27 @@ out vec4 color;
out vec2 texcoord;
out vec3 normal;
out mat3 normal_transform;
void main()
{
vec4 pos = in_position;
vec3 n = in_normal;
mat3 nt = mat3(1.0);
if ((u_flag_mask & O_PRE_TRANSFORM) != 0u)
{
pos = vec4(u_pre_transform * pos, 1.0);
n = u_pre_transform * vec4(n, 0.0);
nt = mat3(u_pre_transform);
}
if ((u_flag_mask & O_INSTANCED) != 0u)
{
pos = vec4(transpose(in_instance_transform) * pos, 1.0);
n = transpose(in_instance_transform) * vec4(n, 0.0);
nt = mat3(transpose(in_instance_transform)) * nt;
}
if ((u_flag_mask & O_POST_TRANSFORM) != 0u)
{
pos = vec4(u_post_transform * pos, 1.0);
n = u_post_transform * vec4(n, 0.0);
nt = mat3(u_post_transform) * nt;
}
position = pos.xyz;
@ -106,7 +99,6 @@ void main()
color = in_color;
texcoord = in_texcoord;
normal = n;
normal_transform = nt;
}
)";
@ -123,7 +115,6 @@ in vec3 position;
in vec4 color;
in vec2 texcoord;
in vec3 normal;
in mat3 normal_transform;
layout (location = 0) out vec3 out0;
layout (location = 1) out vec4 out1;
@ -201,7 +192,23 @@ void main()
vec3 n = normal;
if ((u_flag_mask & O_BUMP_MAP) != 0u)
{
n = normal_transform * (2.0 * texture(u_bump_texture, texcoord).xyz - vec3(1.0));
mat2 dtdf;
dtdf[0] = dFdx(texcoord);
dtdf[1] = dFdy(texcoord);
mat2x3 dpdf;
dpdf[0] = dFdx(position);
dpdf[1] = dFdy(position);
mat2x3 dpdt = dpdf * inverse(dtdf);
vec3 z = n;
vec3 x = normalize(dpdt[0]);
vec3 y = normalize(dpdt[1]);
vec3 v = texture(u_bump_texture, texcoord).xyz * 2.0 - vec3(1.0);
n = normalize(v.x * x + v.y * y + v.z * z);
}
out0 = position;