<p>As I am working with Unity, I found an interesting way of calculating the parallax offset inside the engine. It's not physically based but it gives better results than the classic parallax refraction.</p>

<pre><code>float2 ParallaxOffset(half h, half height, half3 viewDir)
{
    h = h * height - height / 2.0;
    float3 v = normalize(viewDir);
    v.z += 0.42;
    return h * (v.xy / v.z);
}
</code></pre>

CC BY-SA 3.0
2016-10-28T07:59:48.507Z
2016-10-28T07:59:48.507Z
MaT
<p>Here is the way I calculate the refraction vector according to Snell's law.</p>

<pre><code>float cosine = dot(viewDir, worldNormal);
float sine = sqrt(1 - cosine * cosine);

float sine2 = (_IOR * sine);
float cosine2 = sqrt(1 - sine2 * sine2);

float3 x = -worldNormal;
float3 y = normalize(cross(cross(viewDir, worldNormal), worldNormal));
float3 refractedW = x * cosine2 + y * sine2;
</code></pre>

<p>This is integrated with the Physically based Refraction and it gives interesting results but I still don't know how to get rid of the high parallax scale issue which appear at grazing angles...</p>

<p>Any idea how to get rig of that ?</p>

CC BY-SA 3.0
2016-10-17T15:50:07.530Z
There might also be some ways to optimize the Snell's law calculations, any advice are also welcome on that.
MaT
CC BY-SA 3.0
2016-10-17T15:49:20.797Z
2016-10-17T15:49:20.797Z
MaT
<p>I am trying to implement the Parallax Refraction effect explained by <strong>Jorge Jimenez</strong> on this presentation:  <a href="http://www.iryoku.com/downloads/Next-Generation-Character-Rendering-v6.pptx" rel="nofollow noreferrer">http://www.iryoku.com/downloads/Next-Generation-Character-Rendering-v6.pptx</a> and I am facing some difficulties. Here is a screenshot of the interesting part.</p>

<p><a href="https://i.sstatic.net/a08kE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/a08kE.png" alt="screen"></a></p>

<p>But this document lacks a bit of explanations especially the second part with the <strong>Physically based Refraction</strong>.</p>

<p>Here is what I've achieved for the moment.<br>
This is the simple <strong>Parallax Refraction</strong> effect and, as you might notice in the screenshot, there is small glitch at grazing angles in the iris when the Parallax Scale value is too high.</p>

<p>This is caused by the parallax calculation but are there some tricks to avoid or minimize such issue ?</p>

<p>I don't want to go deeper with parallax mapping for the moment, so I don't want to use Steep Parallax Mapping or Parallax Occlusion Mapping.</p>

<p><a href="https://i.sstatic.net/y1sCY.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/y1sCY.jpg" alt="eye"></a></p>

<p>Here is the code. Nothing really special.</p>

<pre><code>// height value comes from a texture
float2 offset = height * viewDir;
offset.y = -offset.y;
texcoord -= ParallaxScale * offset;
// Next there is the texture sampling with the texcoord value
</code></pre>

<p>I also have another question about the <strong>Physically based Refraction</strong> part which is not really well explained and I am missing how some values are calculated such as <code>refractedW</code>, <code>frontNormalW</code> or <code>heightW</code>. If anyone could provide some explanations about those values.</p>

<p>Thanks a lot.</p>

<rendering><shader><glsl><hlsl><refraction>
Eye Parallax Refraction
MaT
# Eye Parallax Refraction

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

I am trying to implement the Parallax Refraction effect explained by **Jorge Jimenez** on this presentation: <http://www.iryoku.com/downloads/Next-Generation-Character-Rendering-v6.pptx> and I am facing some difficulties. Here is a screenshot of the interesting part.

[![screen](https://i.sstatic.net/a08kE.png)](https://i.sstatic.net/a08kE.png)

But this document lacks a bit of explanations especially the second part with the **Physically based Refraction**.

Here is what I've achieved for the moment.  
This is the simple **Parallax Refraction** effect and, as you might notice in the screenshot, there is small glitch at grazing angles in the iris when the Parallax Scale value is too high.

This is caused by the parallax calculation but are there some tricks to avoid or minimize such issue ?

I don't want to go deeper with parallax mapping for the moment, so I don't want to use Steep Parallax Mapping or Parallax Occlusion Mapping.

[![eye](https://i.sstatic.net/y1sCY.jpg)](https://i.sstatic.net/y1sCY.jpg)

Here is the code. Nothing really special.

```
// height value comes from a texture
float2 offset = height * viewDir;
offset.y = -offset.y;
texcoord -= ParallaxScale * offset;
// Next there is the texture sampling with the texcoord value

```

I also have another question about the **Physically based Refraction** part which is not really well explained and I am missing how some values are calculated such as `refractedW`, `frontNormalW` or `heightW`. If anyone could provide some explanations about those values.

Thanks a lot.

**Tags:** rendering, shader, glsl, hlsl, refraction


---

## 2 answers

### Answer by username_0 on 2016-10-28. Score: 0 | Accepted answer

As I am working with Unity, I found an interesting way of calculating the parallax offset inside the engine. It's not physically based but it gives better results than the classic parallax refraction.

```
float2 ParallaxOffset(half h, half height, half3 viewDir)
{
    h = h * height - height / 2.0;
    float3 v = normalize(viewDir);
    v.z += 0.42;
    return h * (v.xy / v.z);
}

```


---

### Answer by username_0 on 2016-10-17. Score: 0

Here is the way I calculate the refraction vector according to Snell's law.

```
float cosine = dot(viewDir, worldNormal);
float sine = sqrt(1 - cosine * cosine);

float sine2 = (_IOR * sine);
float cosine2 = sqrt(1 - sine2 * sine2);

float3 x = -worldNormal;
float3 y = normalize(cross(cross(viewDir, worldNormal), worldNormal));
float3 refractedW = x * cosine2 + y * sine2;

```

This is integrated with the Physically based Refraction and it gives interesting results but I still don't know how to get rid of the high parallax scale issue which appear at grazing angles...

Any idea how to get rig of that ?

> **Comments:**
> **username_0**: There might also be some ways to optimize the Snell's law calculations, any advice are also welcome on that.  

---

