For an given surface $P$ defined by a [[Parametric Function]] $P(u,v)$, and a given 'bump' [[Function|function]] $\beta(u,v)$,
we can generate a 'perturbed' surface of $P$.
For every point $\vec p\in P, p=P(u,v)$, we displace that point by its [[Normal Vector|Normal]] $\vec p_{\perp}$ scaled by the bump [[Function]] $\beta(u,v)$. This gives us a new surface we will denote as $P'$.
$\huge P' = P + \beta P_{\perp} $
$\huge
\forall \vec p \in P ;\vec p \mapsto \vec p + \beta p_{\perp}
$
In $\R^{3}$, we can find the [[Normal Vector|normals]] $P_{\perp}$ of the surface $P$ by defining $P_{\perp}$ to be [[Normal Vector|Normal]] of the tangeant [[Planes|Plane]] of $P$ at $(u,v)$ (which can be generated by the [[Partial Derivative|partial derivatives]] of $P$).
$\huge \begin{align}
% \pderiv{P'}{u} = \pderiv{P}{u} +
\partial_{u}{P'} &= \partial_{u}{P}
+ (\partial_{u}{\beta}) P_{\perp}
+ \beta(\partial_{u}P_{\perp})\\
\partial_{v}{P'} &= \partial_{v}{P}
+ (\partial_{v}{\beta}) P_{\perp}
+ \beta(\partial_{v}P_{\perp})
\end{align}$
$\huge \begin{align} P_{\perp}' &= \partial_{u}P' \times \partial_{v}P' \end{align}$
For computation, this can be very expensive with all the [[Partial Derivative|partial derivatives]] and [[Cross Product|cross product]], so a common optimisation is made.
For the surface normal $P_{\perp}$, small changes in $u,v$ leads to very small changes in $P_\perp$ if the surface $P$ is sufficiently smooth. The bump [[Function]] $\beta$ is always less than or equal to $1$, thereforce the following factors can be approximated to zero.
$\huge \begin{align}
\partial_{u}{P'} &= \partial_{u}{P}
+ (\partial_{u}{\beta}) P_{\perp}
+
{\color{red} \underbrace{\beta(\partial_{u}P_{\perp})}_{\approx\,{0}}}\\
\partial_{v}{P'} &= \partial_{v}{P}
+ (\partial_{v}{\beta}) P_{\perp}
+ {\color{red}\underbrace{\beta(\partial_{v}P_{\perp})}_{\approx\,0}}
\end{align}$
Which leads us to the following:
$\huge \begin{align}
\partial_{u}{P'} &= \partial_{u}{P} + (\partial_{u}{\beta}) P_{\perp} \\
\partial_{v}{P'} &= \partial_{v}{P} + (\partial_{v}{\beta}) P_{\perp} \\
\end{align}$
Substituting these new partial derivatives to calculate the [[Normal Vector|Normal]] [[Function]] $P_{\perp}$,
$\begin{align}
P_{\perp}' &= \partial_{u}P' \times \partial_{v}P' \\
&=(\partial_{u}P+ P_{\perp} \partial_{u}\beta ) \times (\partial_{u}P +
P_{\perp} \partial_{v} \beta ) \\
&=
(\partial_{u}P \times \partial_{v}P)
+ (\partial_{u}\beta \times \partial_{v}P)
+ (\partial_{u}P \times P_{\perp} \partial_{u}\beta)
+ (P_{\perp} \partial_{u}\beta \times P_{\perp} \partial_{v}\beta )\\
&=
(\partial_{u}P \times \partial_{v}P)
+ (\partial_{u}\beta \times \partial_{v}P)
+ (\partial_{u}P \times P_{\perp} \partial_{u}\beta)
\\
&=
P_{\perp}+
\partial \beta_{v}(P \times P_{\perp}) - \partial_{u} \beta( \partial P_{v} \times P_{\perp})
\end{align}$
$\huge P'_{\perp} = \partial_{u} P' \times \partial_{v} P' $
$\huge
P'_{\perp} \approx P_{\perp} +
(\partial_{v}\beta \cdot \partial_{u}P - \partial_{u}\beta \cdot \partial_{v}P) \times P_{\perp}
$
Note that after this, $P_{\perp}'$ is not a [[Unit Vector]].
### Applying this to a [[Triangular Mesh]]
Given a triangle with three [[Point|points]] $p_{0},p_{1},p_{2}$, [[Texture Coordinates]] $T_{0},T_{1},T_{2}$
We can express the texture coordinate for any $u,v$ as a [[Linear Combination]] of $T_0$ and $\vec T_{02}=T_{2}-T_{0}$.
$\huge \begin{align}
T(u,v) =aT_{0}+b \vec T_{02}
\end{align} $
$\huge \begin{align}
\vec T_{01} &= \mat{u_{1}-u_{0}\\v_{1}-v_{0}}\\
\vec T_{02} &= \mat{u_{2}-u_{0}\\v_{2}-v_{0}}\\
\end{align}$
$\huge \begin{align}
T(u,v) &= \mat{
u_{0}+a u_{01}+b u_{0{2}}\\
v_{0}+a v_{01}+b v_{0{2}}
} =\mat{u\\ v} \\
\end{align}$
$\huge \begin{align}
u-u_{0} &= a u_{01} + b u_{02} \\
v-v_{0}&= a v_{01} + b v_{02} \\
A\mat{a\\b} &= T(u,v) - T_{0}\\
\mat{
u_{01} & u_{02} \\
v _{01} &v_{02}
}\mat{a\\b} &= \mat{u-u_{0}\\v-v_{0}}\\
M=\det(A) &= u_{01}v_{02} - u_{02}v_{01}
\end{align}$
$\huge \begin{align}
a &= \frac{(u-u_{0})v_{02}-(v-v_{0})u_{02}}{M} \\
b &= \frac{ (v-v_{0})u_{01} - (u-u_{0})v_{01} }{M}
\end{align} $
$\huge \begin{align}
\partial_{u} a &= \frac{ v_{02} }{M} \\
\partial_{v} a &= \frac{ -u_{02} }{M} \\
\partial_{u} b &= \frac{ -v_{01} }{M} \\
\partial_{v} b &= \frac{ u_{01} }{M} \\
\end{align}$
We can relate these [[Barycentric Coordinates]] to the point $P$.
$\huge \begin{align}
P(u,v) &= P_{0} + a\vec P_{01} + b\vec P_{02} \\
\partial_{u}P &= \partial_{u}a \cdot \vec P_{0 1} + \partial_{u}b \cdot \vec P_{02} \\
\partial_{v}P &= \partial_{v}a \cdot \vec P_{01} + \partial_{v}b \cdot \vec P_{02}
\end{align}$
Wen can then compute $P_{\perp}$:
$\huge \begin{align}
P_{\perp} &= \partial_{u}P \times \partial_{v}P \\
&= (\partial_{u}a \cdot \vec P_{0 1} + \partial_{u}b \cdot \vec P_{02}) \times
(\partial_{v}a \cdot \vec P_{01} + \partial_{v}b \cdot \vec P_{02})
\end{align}$
#### Summary / What to do:
1. [[Universal Quantifier|For any]] $P=(x,y,z); p\in \triangle P_{0}P_{1}P_{2}$, find the coressonding $(u,v)$.
2. Compute:
$\huge \begin{align}
\partial_u P &= \frac{v_{02}}{M} \vec P_{01} - \frac{v_{01}}{M}\vec P_{02} \\
\partial_{v} P &= \frac{u_{01}}{M}\vec P_{02} - \frac{u_{02}}{M}\vec P_{01}\\
M &= u_{01}v_{02} - u_{02}v_{01}
\end{align} $
To solve for $\partial_{u}\beta$ and $\partial_{v}\beta$ from a [[Bump Map|Bump Map Texture]] you can approximate these partial derivatives by manually sampling.
$\huge
\begin{align}
\partial_{u}\beta(u_{i}, v_{j}) &= \frac{
\beta(u_{i+1},v_{j}) - \beta(u_{i}, v_{j})
}{ u_{i+1} - u_{i} }\\
\partial_{v}\beta(u_{i}, v_{j}) &= \frac{
\beta(u_{i},v_{j+1}) - \beta(u_{i}, v_{j})
}{ v_{j+1} - v_{i} }\\
\end{align}
$
Which you can then use to calculate the following:
$\huge
P'_{\perp} \approx P_{\perp} +
(\partial_{v}\beta \cdot \partial_{u}P - \partial_{u}\beta \cdot \partial_{v}P) \times P_{\perp}
$
>[!info] Possible optimisations improvements:
>- Form a [[Surface Tangent Space]] (TBN space)
>- Construct a [[Function|Transformation]] [[Matrix]] from [[World Space]] to TBN
>- Transform light source and viewpoint to TBN
>- Compute light and halfway vectors per [[Vertex]]
>- For each [[Pixel]], compute light and halfway vectors by interpolating among vertices
>- Combined with perturbed [[Normal Vector|normal vectors]] and use in light equation