compute bitangeant in fragment shader

This saves 4 varying components.
Fixes #2092
This commit is contained in:
Pixelflinger
2020-05-01 13:29:43 -07:00
committed by Mathias Agopian
parent f19533b149
commit 3bd1afedbd
4 changed files with 8 additions and 14 deletions

View File

@@ -9,8 +9,7 @@ LAYOUT_LOCATION(4) in highp vec3 vertex_worldPosition;
#if defined(HAS_ATTRIBUTE_TANGENTS)
LAYOUT_LOCATION(5) SHADING_INTERPOLATION in mediump vec3 vertex_worldNormal;
#if defined(MATERIAL_NEEDS_TBN)
LAYOUT_LOCATION(6) SHADING_INTERPOLATION in mediump vec3 vertex_worldTangent;
LAYOUT_LOCATION(7) SHADING_INTERPOLATION in mediump vec3 vertex_worldBitangent;
LAYOUT_LOCATION(6) SHADING_INTERPOLATION in mediump vec4 vertex_worldTangent;
#endif
#endif

View File

@@ -60,8 +60,7 @@ LAYOUT_LOCATION(4) out highp vec3 vertex_worldPosition;
#if defined(HAS_ATTRIBUTE_TANGENTS)
LAYOUT_LOCATION(5) SHADING_INTERPOLATION out mediump vec3 vertex_worldNormal;
#if defined(MATERIAL_NEEDS_TBN)
LAYOUT_LOCATION(6) SHADING_INTERPOLATION out mediump vec3 vertex_worldTangent;
LAYOUT_LOCATION(7) SHADING_INTERPOLATION out mediump vec3 vertex_worldBitangent;
LAYOUT_LOCATION(6) SHADING_INTERPOLATION out mediump vec4 vertex_worldTangent;
#endif
#endif

View File

@@ -9,7 +9,7 @@ void main() {
#if defined(MATERIAL_NEEDS_TBN)
// Extract the normal and tangent in world space from the input quaternion
// We encode the orthonormal basis as a quaternion to save space in the attributes
toTangentFrame(mesh_tangents, material.worldNormal, vertex_worldTangent);
toTangentFrame(mesh_tangents, material.worldNormal, vertex_worldTangent.xyz);
#if defined(HAS_SKINNING_OR_MORPHING)
if (objectUniforms.morphingEnabled == 1) {
@@ -27,7 +27,7 @@ void main() {
if (objectUniforms.skinningEnabled == 1) {
skinNormal(material.worldNormal, mesh_bone_indices, mesh_bone_weights);
skinNormal(vertex_worldTangent, mesh_bone_indices, mesh_bone_weights);
skinNormal(vertex_worldTangent.xyz, mesh_bone_indices, mesh_bone_weights);
}
#endif
@@ -35,13 +35,9 @@ void main() {
// because we ensure the worldFromModelNormalMatrix pre-scales the normal such that
// all its components are < 1.0. This precents the bitangent to exceed the range of fp16
// in the fragment shader, where we renormalize after interpolation
vertex_worldTangent = objectUniforms.worldFromModelNormalMatrix * vertex_worldTangent;
vertex_worldTangent.xyz = objectUniforms.worldFromModelNormalMatrix * vertex_worldTangent.xyz;
vertex_worldTangent.w = mesh_tangents.w;
material.worldNormal = objectUniforms.worldFromModelNormalMatrix * material.worldNormal;
// Reconstruct the bitangent from the normal and tangent. We don't bother with
// normalization here since we'll do it after interpolation in the fragment stage
vertex_worldBitangent =
cross(material.worldNormal, vertex_worldTangent) * sign(mesh_tangents.w);
#else // MATERIAL_NEEDS_TBN
// Without anisotropy or normal mapping we only need the normal vector
toTangentFrame(mesh_tangents, material.worldNormal);

View File

@@ -10,8 +10,8 @@ void computeShadingParams() {
#if defined(HAS_ATTRIBUTE_TANGENTS)
highp vec3 n = vertex_worldNormal;
#if defined(MATERIAL_NEEDS_TBN)
highp vec3 t = vertex_worldTangent;
highp vec3 b = vertex_worldBitangent;
highp vec3 t = vertex_worldTangent.xyz;
highp vec3 b = cross(n, t) * sign(vertex_worldTangent.w);
#endif
#if defined(MATERIAL_HAS_DOUBLE_SIDED_CAPABILITY)