Skip to content

Instantly share code, notes, and snippets.

@breakin
Created January 27, 2018 22:48
Show Gist options
  • Save breakin/0e5590bc25d7b3e27ee295e4a8d1cec5 to your computer and use it in GitHub Desktop.
Save breakin/0e5590bc25d7b3e27ee295e4a8d1cec5 to your computer and use it in GitHub Desktop.
Voxel stuff
First here is a nice derivation of beers law: http://www.applet-magic.com/beerslaw.htm
Say that the cube is one unit deep.
First lets say you see through the cube on a orthohonal ray where alpha = 0.33 everywhere.
We want 100-33=67% of the image from "behind" the cube to be seen through.
Let say that light enters from behind on our ray.
We will now pretend that "we" are the light so we do the traveling from behind.
We use beers law to solve this. It states
I(x) = e^(-kx)
I: Proportion of light suriving after x units travelled (not always used like this.. transmittance)
k: Some parameter related to how much the medium (the volume) is stopping light.
Lets find a k such that I(1) = R (where R is our alpha value).
Using wolfram alpha we get the real solutions:
k = log(1/R) (R not zero)
That is when you look at a ray where alpha is always R, then beers law with k = log(1/R) should give you what you want.
Now if we only look through half the cube, how much energy do we get?
I(0.5) = e^(-log(1/0.33)*0.5) = 0.574456 (with R=0.33)
We see that after light travels through half the cube we have 57% left.
And if we go through half the cube twice? We attenuate twice, using beers law on the two segments:
I(0.5)*I(0.5)=0.574456*0.574456 = 0.3299996959 ~= 0.33
Starts to sound like alpha blending, right?
Now I am not sure if this is what you want :)
And I am not sure what to do for diagonals..
I mean I know you get a more complicated integral since you need to integrate with actual log(1/alpha)
but I don't know if the effect is what you want or what value you get.
@breakin
Copy link
Author

breakin commented Jan 27, 2018

Fixed errors:

First here is a nice derivation of beers law: http://www.applet-magic.com/beerslaw.htm

Say that the cube is one unit deep.
First lets say you see through the cube on a orthohonal ray where alpha = 0.33 everywhere.
We want 100-33=67% of the image from "behind" the cube to be seen through.

Let
A=alpha
R=1-A (if we have 33% alpha we want to see 67% through)

Let say that light enters from behind on our ray.
We will now pretend that "we" are the light so we do the traveling from behind.

We use beers law to solve this. It states

I(x) = e^(-kx)

I: Proportion of light suriving after x units travelled (Beers law can be expressed in different ways)
k: Some parameter related to how much the medium (the volume) is stopping light.

Lets find a k such that I(1) = R (where R=1-A).

Using wolframalpha we get the real solutions:

k = log(1/R) (R not zero)

That is when you look at a ray where alpha is always 1-R, then beers law with k = log(1/R) should give you what you want.

Now if we only look through half the cube, how much energy do we get?

I(0.5) = e^(-log(1/0.33)*0.5) = 0.574456 (with R=0.33)

We see that after light travels through half the cube we have 57% left.

And if we go through half the cube twice? We attenuate twice, using beers law on the two segments:

I(0.5)I(0.5)=0.5744560.574456 = 0.3299996959 ~= 0.33

Starts to sound like alpha blending, right? Stepping through with stepsize 0.5 then 0.5.
Remember: If you do plane with constant z-value then distance through world for each slice depends on which ray you are on (x,y). Not same step size for all pixels for constant z!

Now I am not sure if this is what you want :) And I am not sure what to do for diagonals.. I mean I know you get a more complicated integral since you need to integrate with actual log(1/alpha) but I don't know if the effect is what you want or what value you get.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment