<p>I think that I got a solution but I would gladly know if there are some optimizations possible.</p>

<p>My UVs and local coordinates values are corresponding. I mean that they are in the same range value. That said, I can use my XY vertices values for sampling the occlusion texture. The main problem is that the local coordinates are dependent to the rotation of my object which doesn't solve anything...</p>

<p>To solve that I am converting my local coordinates into world space using a objectToWorld matrix (inverse of current world matrix).<br>
Then I am converting it back into object space using the inverse TRS matrix but without taking into account the rotation of the object.</p>

<p>It's a bit hacky and I think that I could avoid some steps but this is working.<br>
Any advice is welcome :)</p>

CC BY-SA 3.0
2016-11-02T15:10:39.593Z
2016-11-02T15:10:39.593Z
MaT
<p>A static occlusion map won't generally work accurately with a dynamic mesh, as you can see. In your case you can separate the occlusion map and instead put it on a static object that wraps around the eye. You can render that object with multiplicative blending and get the effect you want.</p>

CC BY-SA 3.0
2016-10-31T15:32:06.603Z
2016-10-31T15:32:06.603Z
Ryan
<p>GLSL has built-in <a href="https://www.opengl.org/wiki/Built-in_Variable_(GLSL)#Fragment_shader_inputs" rel="nofollow">fragment shader inputs</a>, and one of them is gl_FragCoord. You can get the $x$ and $y$ value from this and use that as the lookup coordinate of the texture map in a sequence of two passes, but that could be a good or bad thing depending on the application (such as caching the result). This would both be rotation invariant while allowing you to scale and move the mesh.</p>

<h1>First Pass</h1>

<p>Transform the mesh without rotation. Bind a texture to the fragment shader and write the interpolated UV coordinates to it.</p>

<h1>Second Pass</h1>

<p>Transform the mesh correctly this time. Bind the texture from the last pass and read from it this time. You preserved the old UV coordinates, and now you can use these to look up whatever texture you have before. Obviously, this works best with a mesh that's a sphere (such as an eye).</p>

<pre><code>vec2 texCoords = gl_FragCoord.xy; // do some sort of scaling
vec2 oldTexCoords = texture(samplerOfUVTexture, texCoords); // sample UV
</code></pre>

<p>Now you have both the old UV coordinates and the new UV coordinates. You can use the new UV coordinates for you diffuse map and the old UV coordinates for the occlusion map.</p>

CC BY-SA 3.0
2016-10-31T09:07:49.493Z
How would that work? gl_FragCoord are the screen UVs. If the mesh occupies part of the screen it's not going to be textured correctly from these.
Syntac_
CC BY-SA 3.0
2016-10-31T09:48:04.310Z
@aces Yes I agree, I don't really get how this could work.
MaT
CC BY-SA 3.0
2016-11-01T02:58:26.640Z
@Syntac_ The picture makes the intent clearer, but it certainly can be textured correctly with this if you save the rendering of the pre-rotated mesh onto a texture and used screenspace coordinates to look it up. See my edit for a more detailed explanation.
aces
CC BY-SA 3.0
2016-10-30T20:24:28.240Z
2016-11-01T03:08:38.110Z
2016-11-01T03:08:38.110Z
aces
<p>I have some classic texture coordinates and as a normal behaviour they follow the mesh's transformations.</p>

<p>I am trying to use the same texture coordinates behaviour but without being affected by the mesh rotation transformation. The results would be a sort of texture coordinates projection.</p>

<p>I don't know if my explanations are well explained but how could I achieve such effect.</p>

<p>Thanks a lot.</p>

<p><a href="https://i.sstatic.net/bTlg5.jpg" rel="noreferrer"><img src="https://i.sstatic.net/bTlg5.jpg" alt="enter image description here"></a></p>

CC BY-SA 3.0
2016-10-31T10:16:53.860Z
Could you explain further the end result. Are you after decal projection? A picture of the expected result would help.
Syntac_
CC BY-SA 3.0
2016-10-31T10:47:44.343Z
This is still for eye rendering :) post updated.
MaT
CC BY-SA 3.0
2016-10-31T12:35:33.907Z
@MaT based on the images i don't think a normal map is enough , you also need a height map. you need to compute the direction vector on the uv space and then offset it based on surface "depth"
Raxvan
CC BY-SA 3.0
2016-10-31T12:40:35.797Z
My question seems to be really badly formulated :P
The Normal screenshot shows the eyes without any rotation therefore the occlusion map is well applied but as soon as you rotate the eye, the occlusion classical behaviour breaks the effect.
That's why I am trying apply the occlusion map not only in object space to avoid the effect of the eye rotation, but I don't see how.
MaT
<shader><glsl><real-time><hlsl><uv-mapping>
Texture coordinates projection
MaT
# Texture coordinates projection

(Asked by: username_0 on 2016-10-30)

I have some classic texture coordinates and as a normal behaviour they follow the mesh's transformations.

I am trying to use the same texture coordinates behaviour but without being affected by the mesh rotation transformation. The results would be a sort of texture coordinates projection.

I don't know if my explanations are well explained but how could I achieve such effect.

Thanks a lot.

[![enter image description here](https://i.sstatic.net/bTlg5.jpg)](https://i.sstatic.net/bTlg5.jpg)

**Tags:** shader, glsl, real-time, hlsl, uv-mapping

> **Comments:**
> **username_1**: Could you explain further the end result. Are you after decal projection? A picture of the expected result would help.  
> **username_0**: This is still for eye rendering :) post updated.  
> **username_2**: @username_0 based on the images i don't think a normal map is enough , you also need a height map. you need to compute the direction vector on the uv space and then offset it based on surface "depth"  
> **username_0**: My question seems to be really badly formulated :P The Normal screenshot shows the eyes without any rotation therefore the occlusion map is well applied but as soon as you rotate the eye, the occlusion classical behaviour breaks the effect. That's why I am trying apply the occlusion map not only in object space to avoid the effect of the eye rotation, but I don't see how.  

---

## 3 answers

### Answer by username_0 on 2016-11-02. Score: 0 | Accepted answer

I think that I got a solution but I would gladly know if there are some optimizations possible.

My UVs and local coordinates values are corresponding. I mean that they are in the same range value. That said, I can use my XY vertices values for sampling the occlusion texture. The main problem is that the local coordinates are dependent to the rotation of my object which doesn't solve anything...

To solve that I am converting my local coordinates into world space using a objectToWorld matrix (inverse of current world matrix).  
Then I am converting it back into object space using the inverse TRS matrix but without taking into account the rotation of the object.

It's a bit hacky and I think that I could avoid some steps but this is working.  
Any advice is welcome :)


---

### Answer by username_3 on 2016-10-31. Score: 1

A static occlusion map won't generally work accurately with a dynamic mesh, as you can see. In your case you can separate the occlusion map and instead put it on a static object that wraps around the eye. You can render that object with multiplicative blending and get the effect you want.


---

### Answer by username_4 on 2016-10-30. Score: 1

GLSL has built-in [fragment shader inputs](https://www.opengl.org/wiki/Built-in_Variable_(GLSL)#Fragment_shader_inputs), and one of them is gl\_FragCoord. You can get the $x$ and $y$ value from this and use that as the lookup coordinate of the texture map in a sequence of two passes, but that could be a good or bad thing depending on the application (such as caching the result). This would both be rotation invariant while allowing you to scale and move the mesh.

# First Pass

Transform the mesh without rotation. Bind a texture to the fragment shader and write the interpolated UV coordinates to it.

# Second Pass

Transform the mesh correctly this time. Bind the texture from the last pass and read from it this time. You preserved the old UV coordinates, and now you can use these to look up whatever texture you have before. Obviously, this works best with a mesh that's a sphere (such as an eye).

```
vec2 texCoords = gl_FragCoord.xy; // do some sort of scaling
vec2 oldTexCoords = texture(samplerOfUVTexture, texCoords); // sample UV

```

Now you have both the old UV coordinates and the new UV coordinates. You can use the new UV coordinates for you diffuse map and the old UV coordinates for the occlusion map.

> **Comments:**
> **username_1**: How would that work? gl_FragCoord are the screen UVs. If the mesh occupies part of the screen it's not going to be textured correctly from these.  
> **username_0**: @username_4 Yes I agree, I don't really get how this could work.  
> **username_4**: @username_1 The picture makes the intent clearer, but it certainly can be textured correctly with this if you save the rendering of the pre-rotated mesh onto a texture and used screenspace coordinates to look it up. See my edit for a more detailed explanation.  

---

