<p>Turns out I needed to be looking at the former normal as a unit vector <span class="math-container">$(\hat{r}, \hat{\theta},\hat{\varphi})$</span>, and then use a rotation matrix as outlined <a href="https://en.wikipedia.org/wiki/Vector_fields_in_cylindrical_and_spherical_coordinates" rel="nofollow noreferrer">here</a> to find <span class="math-container">$(\hat{x},\hat{y},\hat{z})$</span>.</p>

CC BY-SA 4.0
2019-07-22T15:24:23.673Z
2019-07-22T15:24:23.673Z
PixelArtDragon
<p>I'm using a vertex shader to effectively wrap vertices on a sphere. Starting with <code>world_position</code> already after multiplying the vertex by the model transform matrix to get the coordinates in world space, I calculate the new position via an inverse Mercator projection:</p>

<pre><code>float theta = 2.0 * PI * world_position.x / world_radius;
float phi = 2.0 * atan(exp(world_position.y / world_radius)) - PI / 2.0;
float radius = world_position.z

gl_Position = vec4(radius * vec3(sin(theta)*cos(phi), sin(phi), cos(theta)*cos(phi)), 1.0);
</code></pre>

<p>What I now need to do is take these angles, <span class="math-container">$\theta$</span> and <span class="math-container">$\varphi$</span>, and use them to transform the normals as well. I've tried creating a rotation matrix to represent rotation first around the Y axis (for <span class="math-container">$\theta$</span>) and then around the X axis (for <span class="math-container">$\varphi$</span>), and multiplying that by the vertex's normal (after multiplying the normal by the model matrix's inverse transpose), but that didn't give the results I expected.</p>

<p>How do I use the angles to rotate the normals so they match the rotations of the vertices?</p>

<glsl><normal-mapping>
Wrapping normals around a sphere
PixelArtDragon
# Wrapping normals around a sphere

(Asked by: username_0 on 2019-07-22)

I'm using a vertex shader to effectively wrap vertices on a sphere. Starting with `world_position` already after multiplying the vertex by the model transform matrix to get the coordinates in world space, I calculate the new position via an inverse Mercator projection:

```
float theta = 2.0 * PI * world_position.x / world_radius;
float phi = 2.0 * atan(exp(world_position.y / world_radius)) - PI / 2.0;
float radius = world_position.z

gl_Position = vec4(radius * vec3(sin(theta)*cos(phi), sin(phi), cos(theta)*cos(phi)), 1.0);

```

What I now need to do is take these angles, $\theta$ and $\varphi$, and use them to transform the normals as well. I've tried creating a rotation matrix to represent rotation first around the Y axis (for $\theta$) and then around the X axis (for $\varphi$), and multiplying that by the vertex's normal (after multiplying the normal by the model matrix's inverse transpose), but that didn't give the results I expected.

How do I use the angles to rotate the normals so they match the rotations of the vertices?

**Tags:** glsl, normal-mapping


---

## 1 answers

### Answer by username_0 on 2019-07-22. Score: 1 | Accepted answer

Turns out I needed to be looking at the former normal as a unit vector $(\hat{r}, \hat{\theta},\hat{\varphi})$, and then use a rotation matrix as outlined [here](https://en.wikipedia.org/wiki/Vector_fields_in_cylindrical_and_spherical_coordinates) to find $(\hat{x},\hat{y},\hat{z})$.


---

