Improve normal mapping

This commit is contained in:
Lubos Lenco 2016-02-14 15:33:23 +01:00
parent 3c112ed571
commit b95f047c57
3 changed files with 30 additions and 37 deletions

View file

@ -1527,47 +1527,39 @@ class ArmoryExporter(bpy.types.Operator, ExportHelper):
# Export tangents
# TODO: check for texture coords
export_tangents = self.get_export_tangents(exportMesh)
if (export_tangents and len(exportMesh.uv_textures) > 0):
# exportMesh.calc_tangents() # TODO: use to export tangents
if (self.get_export_tangents(exportMesh) == True and len(exportMesh.uv_textures) > 0):
ia = om.index_arrays[0].values
posa = pa.values
uva = ta.values
tangents = []
#bitangents = []
for i in range(0, int(len(ia) / 3)):
i0 = ia[i * 3 + 0]
i1 = ia[i * 3 + 1]
i2 = ia[i * 3 + 2]
# TODO: Speed up
v0 = Vector((posa[i0 * 3 + 0], posa[i0 * 3 + 1], posa[i0 * 3 + 2]))
v1 = Vector((posa[i1 * 3 + 0], posa[i1 * 3 + 1], posa[i1 * 3 + 2]))
v2 = Vector((posa[i2 * 3 + 0], posa[i2 * 3 + 1], posa[i2 * 3 + 2]))
uv0 = Vector((uva[i0 * 2 + 0], uva[i0 * 2 + 1]))
uv1 = Vector((uva[i1 * 2 + 0], uva[i1 * 2 + 1]))
uv2 = Vector((uva[i2 * 2 + 0], uva[i2 * 2 + 1]))
deltaPos1 = v1 - v0
deltaPos2 = v2 - v0
deltaUV1 = uv1 - uv0
deltaUV2 = uv2 - uv0
r = 1.0 / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
#bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x) * r;
tangents.append(tangent.x)
tangents.append(tangent.y)
tangents.append(tangent.z)
#bitangents.append(bitangent.x)
#bitangents.append(bitangent.y)
#bitangents.append(bitangent.z)
tana = Object()
tana.attrib = "tangent"
tana.size = 3
tana.values = tangents
om.vertex_arrays.append(tana)
# btana = Object()
# btana.attrib = "bitangent"
# btana.size = 3
# btana.values = bitangents
# om.vertex_arrays.append(btana)
# If the mesh is skinned, export the skinning data here.
if (armature):

View file

@ -42,11 +42,15 @@ in vec3 position;
#ifdef _Texturing
in vec2 texCoord;
#endif
in vec3 normal;
in vec4 lPos;
in vec4 matColor;
in vec3 lightDir;
in vec3 eyeDir;
#ifdef _NormalMapping
in mat3 TBN;
#else
in vec3 normal;
#endif
float shadowTest(vec4 lPos, float dotNL) {
@ -186,15 +190,16 @@ float getMipLevelFromRoughness(float roughness) {
}
void main() {
vec3 n = normalize(normal);
vec3 l = normalize(lightDir);
#ifdef _NormalMapping
vec3 tn = normalize(texture(snormal, texCoord).rgb * 2.0 - 1.0);
float dotNL = clamp(dot(tn, l), 0.0, 1.0);
vec3 n = (texture(snormal, texCoord).rgb * 2.0 - 1.0);
n = normalize(TBN * normalize(n));
#else
float dotNL = max(dot(n, l), 0.0);
vec3 n = normalize(normal);
#endif
vec3 l = normalize(lightDir);
float dotNL = max(dot(n, l), 0.0);
float visibility = 1.0;
if (receiveShadow) {
@ -247,7 +252,7 @@ void main() {
indirectDiffuse = pow(indirectDiffuse, vec3(2.2)) * albedo;
vec3 reflectionWorld = reflect(-v, n);
float lod = getMipLevelFromRoughness(roughness);
float lod = getMipLevelFromRoughness(roughness) + 1.0;
vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld), lod).rgb;
prefilteredColor = pow(prefilteredColor, vec3(2.2));
@ -260,6 +265,7 @@ void main() {
#ifdef _OMTex
vec3 occlusion = texture(som, texCoord).rgb;
outColor.rgb *= 1.8;
outColor.rgb *= occlusion;
#endif
}

View file

@ -43,18 +43,14 @@ out vec3 position;
#ifdef _Texturing
out vec2 texCoord;
#endif
out vec3 normal;
out vec4 lPos;
out vec4 matColor;
out vec3 lightDir;
out vec3 eyeDir;
#ifdef _NormalMapping
mat3 transpose(mat3 m) {
return mat3(m[0][0], m[1][0], m[2][0],
m[0][1], m[1][1], m[2][1],
m[0][2], m[1][2], m[2][2]);
}
out mat3 TBN;
#else
out vec3 normal;
#endif
#ifdef _Skinning
@ -122,9 +118,9 @@ void main() {
#endif
#ifdef _Skinning
normal = normalize(mat3(NM) * (nor * skinningMatVec));
vec3 _normal = normalize(mat3(NM) * (nor * skinningMatVec));
#else
normal = normalize(mat3(NM) * nor);
vec3 _normal = normalize(mat3(NM) * nor);
#endif
matColor = albedo_color;
@ -134,15 +130,14 @@ void main() {
#endif
vec3 mPos = vec4(M * sPos).xyz;
lightDir = (light - mPos);
eyeDir = (eye - mPos);
lightDir = light - mPos;
eyeDir = eye - mPos;
#ifdef _NormalMapping
vec3 vtan = (tan);
vec3 vbitan = cross(normal, vtan) * 1.0;//tangent.w;
mat3 TBN = transpose(mat3(vtan, vbitan, normal));
lightDir = normalize(TBN * lightDir);
eyeDir = normalize(TBN * eyeDir);
vec3 tangent = (mat3(NM) * (tan));
vec3 bitangent = normalize(cross(_normal, tangent));
TBN = mat3(tangent, bitangent, _normal);
#else
normal = _normal;
#endif
}