Typically denoted by $u$ and $v$, Texture [[../Math/Coordinate System|Coordinates]] are a normalised [[../Math/Coordinate System|Coordinate System]] for [[Pixel|Pixels]] based on the [[../Math/Unit Square|Unit Square]]. Typically, $u$ is the horizantal [[Axis]] and $v$ is the vertical [[Axis]]. >[!tip] Visualization ![[../../00 Asset Bank/Pasted image 20241002140711.png]] > [[Texture Coordinates|UV Coordinates]] visualization where the red channel correlates to $u$ and the green channel correlates to $v$. ### Mapping from [[Bitmap Image|Bitmap Images]] To map the typical [[../Math/Coordinate System|Coordinate System]] in a [[Bitmap Image]] to [[Texture Coordinates]] you would [[../Math/Resize Transformation|Scale]] by the [[../Math/Inverse Matrices|Inverse]] off the [[Bitmap Image|Bitmap Image's]] width $w$ and $h$ and then a [[../Math/Affine Transformations|Translation]] to bring the [[Origin]] to $\mat{ \frac{1}{2} \\ \frac{1}{2} }$. $\huge \mathcal{C}_{B\to T} = S_{\ang{\frac{1}{w}, \frac{1}{h}}} \cdot T_\ang{\frac{1}{2}, \frac{1}{2}} = \mat{\frac{1}{w} & 0 & \frac{1}{2w} \\0& \frac{1}{h}& \frac{1}{2h} \\0&0&1} $ >[!example] A given [[Bitmap Image]] is $10 \times 8$. > >Find the [[Bitmap Image|Bitmap]] to [[../../99 Old/03 Games/GAM200/Engine/gfx/Texture]] [[Transformation]]: >>[!check]- Solution >>$\huge \begin{align} >>B &= S_{\ang{ \frac{1}{10}, \frac{1}{8} }} \cdot >>T_\ang{\frac{1}{2}, \frac{1}{2}} \\ >>&= \mat{ \frac{1}{10} & 0 & \frac{1}{20} \\ 0 & \frac{1}{8} & \frac{1}{16}\\0&0&1} >>\end{align}$ > > Find the [[Texture Coordinates]] corresponding to the bitmap coordinates $\pa{4,7}$. >>[!check]- Solution >>$\huge \begin{align} >>\mat{ \frac{1}{10} & 0 & \frac{1}{20} \\ 0 & \frac{1}{8} & \frac{1}{16}\\0&0&1}\mat{4\\7\\1} =\mat{ >>\frac{9}{20}\\ >>\frac{15}{20}\\ >>1 >>} \\ >>(u,v) \implies \pa{ \frac{9}{20}, \frac{15}{20} } >>\end{align} >>$ ### Mapping *to* [[Bitmap Image]] To map from [[Texture Coordinates]] to [[Bitmap Image]], simply take the [[../Math/Inverse Matrices|Inverse]] of $\mathcal{C}_{{B\to T}}$. $\huge \begin{align} \mathcal C_{T\to B} &= \pa{\mathcal C_{B\to T}}^{-1} \\ &= \pa{T_\ang{\frac{1}{2}, \frac{1}{2}}}^{-1} \cdot \pa{S_{\ang{\frac{1}{w}, \frac{1}{h}}} }^{-1} \\ &= T_\ang{-\frac{1}{2}, -\frac{1}{2}}\cdot S_{\ang{w, h}} \\ &= \mat{ w & 0 & -\frac{1}{2} \\ 0 & h & -\frac{1}{2} \\ 0&0&1 } \end{align}$ > [!note] > When applying this transformation - it won't land on an [[Integer]] [[Pixel]] [[../Math/Coordinate System|Coordinate]], so you'll have to round it to the nearest [[Integer]].