Search Unity

Question about "binormal" calculation.

Discussion in 'General Graphics' started by GreatWall, Sep 22, 2018.

  1. GreatWall

    GreatWall

    Joined:
    Aug 7, 2014
    Posts:
    57
    I use the following code to draw tangent, normal,binormal.but the w of tangent always be -1.
    Vector3[] vertices = mesh.vertices;
    Vector3[] normals = mesh.normals;
    Vector4[] tangents = mesh.tangents;
    for (int i = 0; i < vertices.Length; i++) {
    ShowTangentSpace(
    transform.TransformPoint(vertices),
    transform.TransformDirection(normals),
    transform.TransformDirection(tangents),
    tangents.w
    );
    }
    void ShowTangentSpace (
    Vector3 vertex, Vector3 normal, Vector3 tangent, float binormalSign
    ) {
    Gizmos.color = Color.green;
    Gizmos.DrawLine(vertex, vertex + normal * scale);
    Gizmos.color = Color.red;
    Gizmos.DrawLine(vertex, vertex + tangent * scale);
    Vector3 binormal = Vector3.Cross(normal, tangent) * binormalSign;
    Gizmos.color = Color.blue;
    Gizmos.DrawLine(vertex, vertex + binormal * scale);
    }
    the result is right ,However the tangents.w is -1,which seems like make the local space form left-handed to right-handed.Then why not just tangents.w=1 and modify the binormal calculation?using Vector3.Cross(tangent,normal).I think it is more simple.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    The w component is used to handle mirrored UVs, so it might be -1 or 1 depending on the UVs. Performance wise, since the value needs to exist, it doesn't matter if it defaults to -1 or 1. Correctness wise, the bitangent / binormal is defined as the cross product of the normal and tangent, so it makes sense to keep the calculations correctly in the space & handedness it’s actually in, and use -1 to be explicit about the orientation flip of the spaces.

    Ultimately all that matters is that there are an even number of sign errors. ;)