Compute sphere tangent for normal mapping
You can do this a little bit more efficiently and accurately by calculating derivatives of $P$ with respect to $u, v$—or the derivatives with respect to $\phi, \theta$, equivalently (up to a scalar multiple).

Start by expressing $P$ in terms of the spherical coordinates:
$$\begin{aligned}
P_x &amp;= r \cos \theta \cos \phi \\
P_y &amp;= r \sin \theta \\
P_z &amp;= r \cos \theta \sin \phi
\end{aligned}$$

Now you can get the tangent as $T = \partial P / \partial \phi$, which works out as:
$$\begin{aligned}
T_x &amp;= -r \cos \theta \sin \phi \\
T_y &amp;= 0 \\
T_z &amp;= r \cos \theta \cos \phi
\end{aligned}$$

Once you normalize it, the $r \cos \theta$ factors drop out and you're left with just $T = (-\sin \phi, 0, \cos \phi)$. Pretty simple!

You can similarly get the bitangent as $B = \partial P / \partial \theta$, or you can just calculate it as the cross product $N \times T$, which is probably faster. (Note it should be $N \times T$, not $T \times N$, assuming you want $T, B, N$ to form a right-handed basis in that order.)



The above approach will work for pretty much any kind of surface that can be parameterized, not just spheres. However, in the case of spheres with the usual $\phi, \theta$ coordinates, there's an even quicker shortcut that you can do with just vector math, no trig at all. Let $C$ be the center of the sphere, and $A$ be a vector representing the axis of the spherical coordinates—i.e. pointing from the center toward the north pole. Then you can just calculate $T = \text{normalize}\bigl(A \times (P - C) \bigr)$, then $B = N \times T$ as before.