<p>You can refer to the available literature on the topic. The papers I am aware of are:</p>
<p><a href="https://www.researchgate.net/publication/220853139_Ray_Tracing_in_Non-Constant_Media" rel="nofollow noreferrer">Ray Tracing in Non-Constant Media</a></p>
<p><a href="https://onlinelibrary.wiley.com/doi/abs/10.1111/j.1467-8659.2010.01733.x" rel="nofollow noreferrer">Interactive Rendering of Non-Constant, Refractive Media Using the Ray Equations of Gradient-Index Optics</a></p>
<p>and</p>
<p><a href="https://dl.acm.org/doi/abs/10.1145/2557605" rel="nofollow noreferrer">Refractive radiative transfer equation</a></p>
<p>The general algorithm for handling a varying index of refraction is ray-marching, where the size of the steps is related to the accuracy of your solution.</p>

CC BY-SA 4.0
2022-06-20T08:41:40.383Z
[This](https://euvze1.csb.app/) is what I came up with BTW, it's not 100 % physically correct, but I really like the look!
fweth
CC BY-SA 4.0
2022-06-08T17:21:01.077Z
2024-09-06T07:19:12.107Z
2024-09-06T07:19:12.107Z
lightxbulb
<p>I want to write a simple ray tracing(?) algorithm in WebGL with continuous refraction.  So my idea was, what if you have a material whose optical density varies continuously, if this is physically possible?</p>
<p>So say you have some closed zero-set <span class="math-container">$A\subseteq\mathbf{R}^3$</span> and the optical density of some point <span class="math-container">$x$</span> in your world is <span class="math-container">$n(x):=\exp(-d(x,A)^2)$</span> where <span class="math-container">$d(x,A)$</span> is the distance between <span class="math-container">$x$</span> and (one of) the closest point to <span class="math-container">$x$</span> in <span class="math-container">$A$</span>.  I couldn't find a formula for this, but I assume it would be some limit of <a href="https://en.wikipedia.org/wiki/Snell%27s_law" rel="nofollow noreferrer">Snell's law</a> where you you round <span class="math-container">$n(x)$</span> to some multiple of <span class="math-container">$\epsilon$</span> and let <span class="math-container">$\epsilon$</span> approach <span class="math-container">$0$</span>.</p>
<p>What would be a good algorithm to approximate this?  Basically my idea is to have a position and a direction, which describe the current stat of your ray, then you loop over some fixed number of iterations and in each step do</p>
<ul>
<li>compute the vector <span class="math-container">$v$</span> from the current position to the closest point in <span class="math-container">$A$</span> (I hope the set of <span class="math-container">$x$</span> with more than one closest point in <span class="math-container">$A$</span> is a zero-set and I will never encounter it),</li>
<li>compute length of <span class="math-container">$v$</span> and its angle relative to the current travelling direction,</li>
<li>then compute a new direction and some distance to travel, update the position and continue the loop, or break if you encounter some texture inbetween.</li>
</ul>
<p>The idea is that the travel distances are shorter when you are closer to <span class="math-container">$A$</span>, as the refraction index changes more rapidly here.  How could the function for the last step look like?</p>

CC BY-SA 4.0
2022-06-08T15:31:46.373Z
See this paper: "Interactive Rendering of Non-Constant, Refractive Media Using the Ray Equations of Gradient-Index Optics", and potentially "Ray Tracing in Non-Constant Media" by Stam.
Essentially you need to do some kind of ray-marching. You need to find a potentially curved path up to some precision such that it minimises time traveled (i.e. geodesic curves).
lightxbulb
<raytracing><webgl><raymarching>
Ray Tracing With Continuous Refraction
fweth
# Ray Tracing With Continuous Refraction

(Asked by: username_0 on 2022-06-08)

I want to write a simple ray tracing(?) algorithm in WebGL with continuous refraction. So my idea was, what if you have a material whose optical density varies continuously, if this is physically possible?

So say you have some closed zero-set $A\subseteq\mathbf{R}^3$ and the optical density of some point $x$ in your world is $n(x):=\exp(-d(x,A)^2)$ where $d(x,A)$ is the distance between $x$ and (one of) the closest point to $x$ in $A$. I couldn't find a formula for this, but I assume it would be some limit of [Snell's law](https://en.wikipedia.org/wiki/Snell%27s_law) where you you round $n(x)$ to some multiple of $\epsilon$ and let $\epsilon$ approach $0$.

What would be a good algorithm to approximate this? Basically my idea is to have a position and a direction, which describe the current stat of your ray, then you loop over some fixed number of iterations and in each step do

* compute the vector $v$ from the current position to the closest point in $A$ (I hope the set of $x$ with more than one closest point in $A$ is a zero-set and I will never encounter it),
* compute length of $v$ and its angle relative to the current travelling direction,
* then compute a new direction and some distance to travel, update the position and continue the loop, or break if you encounter some texture inbetween.

The idea is that the travel distances are shorter when you are closer to $A$, as the refraction index changes more rapidly here. How could the function for the last step look like?

**Tags:** raytracing, webgl, raymarching

> **Comments:**
> **username_1**: See this paper: "Interactive Rendering of Non-Constant, Refractive Media Using the Ray Equations of Gradient-Index Optics", and potentially "Ray Tracing in Non-Constant Media" by Stam. Essentially you need to do some kind of ray-marching. You need to find a potentially curved path up to some precision such that it minimises time traveled (i.e. geodesic curves).  

---

## 1 answers

### Answer by username_1 on 2022-06-08. Score: 3 | Accepted answer

You can refer to the available literature on the topic. The papers I am aware of are:

[Ray Tracing in Non-Constant Media](https://www.researchgate.net/publication/220853139_Ray_Tracing_in_Non-Constant_Media)

[Interactive Rendering of Non-Constant, Refractive Media Using the Ray Equations of Gradient-Index Optics](https://onlinelibrary.wiley.com/doi/abs/10.1111/j.1467-8659.2010.01733.x)

and

[Refractive radiative transfer equation](https://dl.acm.org/doi/abs/10.1145/2557605)

The general algorithm for handling a varying index of refraction is ray-marching, where the size of the steps is related to the accuracy of your solution.

> **Comments:**
> **username_0**: [This](https://euvze1.csb.app/) is what I came up with BTW, it's not 100 % physically correct, but I really like the look!  

---

