WAIT WTF
Workspace.RaycastBase.Script:11: Should be Vector3 but is a Vector3!
WAIT WTF
Workspace.RaycastBase.Script:11: Should be Vector3 but is a Vector3!
-1, -0, -0
is a tuple (because of how lua works it will probably only look at the first value when checking type, so it gets number).
You need to put the tuple in the Vector3 constructor to make it a Vector3:
Vector3.new(-1, -0, -0)
Try it again, I copied the line of code but didnāt replace the variable name in one spot.
(unitNormalDirection isnāt a vector)
now i get this
Workspace.RaycastBase.Script:21: attempt to call a number value
return r*unitLightDirection + (r*c - math.sqrt(1-math.pow(r,2)(1-math.pow(c,2)))) * unitNormalDirection
Edit: idk whats going on since r and c are both numbers
sorry for my stupidity in math
Edit: OH OHH WAIII HHAHAHH OOOOHHHH HOAHHHAHHA ehehehe okay so that line of code should have a * between the math.pows BUT IT WORKS
So one thing though, Iām trying to make it refract like robloxās glass, but it seems to do this.
There are a few reasons this might be.
First, I think this equation could have been typed wrong:
In our code, itās
math.sqrt( 1 - math.pow(r,2) * (1 - math.pow(c,2)) )
It was originally
math.sqrt( 1 - math.pow(r,2) (1 - math.pow(c,2)) )
I think it possibly should be:
math.sqrt( (1 - math.pow(r,2)) * (1 - math.pow(c,2)) )
(Just something to check)
Second, when you do refraction through substances:
Here is a picture of what I mean:
(Notice how there are three lines.)
(Raycast the first one until it hits the glass, get the refraction and raycast until it leaves the glass, then finally get the refraction of that and raycast until it hits something else.)
To do a backwards raycast, do a bunch of little raycasts going the opposite direction. The length of a cast is basically proportional to the comp. cost, so its fine to do this. You need to do it backwards so you can raycast from inside the mesh. Once you get a hit remember to flip the normal value.
woah, those 2 parentheses changed the whole thing. I inverted the unitNormalDirection
, and it seemed to work, but the new code seems to work better. However, Iām not too sure why I need to do a backwards raycast though.
Hereās it rendered:
Edit: Hereās why it should/ what I want it to look like.
The surface youāre trying to find only has one side and itās going the opposite direction. (If the mesh was double sided you wouldnāt need to backwards raycast.)
See how the surface the green arrows are trying to find is facing the opposite direction? If you did a regular raycast the raycast would go right through the surface since itās single sided.
The first two yellow points (between red+green and between green+blue) are where the refraction happens. (At the first point n1 = air and n2 = glass, at the second point n1 = glass and n2 = air.)
The green raycast should be backwards because itās trying to find the outer surface of an object it is inside. Basically your raycast should flip the raycasting direction when it is inside something.
I might be wrong though, maybe regular raycasting can find single sided surfaces going the opposite direction. I didnāt think so though: for example a raycast from inside a characterās head doesnāt return the surface of the head.
Does your code have this feature? Are there 3 raycasts per pixel with refraction or is it only 2?
So yes, there are only 2 raycasts with refraction. So would I refract the second raycast? or the third? the third raycast looks to be parallel to the first, so Im confused.
Edit: is the third (blue) ray always parallel to the first (red)?
You refract every time the sample changes materials (for example, in the picture above it goes from air > glass then glass > air, so it refracts twice).
The the second object (the grey box in the side) is meant to represent an opaque object. You only stop raycasting when you hit an opaque thing.
In that case it is parallel (it depends on the shape of the object), but itās also translated, which is still important. In the case of the sphere all the casts are bent inwards.
No, it depends on the entrance and exit normals.
Edit: Here is a picture for a sphere:
Edit:
It looks like the original code was more correct, though still missing the second refraction point:
(properly working refraction)
(notice how this one works the same. If things are further away than the focus they are flipped. Maybe Roblox uses different refraction index values to make the effect more subtle?)
Edit:
Robloxās refraction appears to be not like real refraction.
I think itās just an optimization because calculating real refraction is hard with single sided meshes and stuff (not all meshes close and things).
I think they use a super simple formula like this:
local factor = 0.1
local function refraction(unitLightDirection, unitNormalDirection)
-- Force the vector inputs to be unit vectors
unitLightDirection = unitLightDirection.Unit
unitNormalDirection = unitNormalDirection.Unit
return (unitLightDirection + -1 * factor * unitNormalDirection).Unit
end
actually probably like this:
local factor = 0.75
local function refractionOffset(unitLightDirection, unitNormalDirection, transparency)
-- Force the vector inputs to be unit vectors
unitLightDirection = unitLightDirection.Unit
unitNormalDirection = unitNormalDirection.Unit
local offset = unitNormalDirection - unitNormalDirection:Dot(unitLightDirection) * unitNormalDirection
-- Offset is the amount to move the raycast origin by. Once the ray has
-- gone through a transparent obejct ignore all other transparent objects
-- until something opaque is hit.
-- (dont change the raycast direction)
return offset * (1-transparency) * factor
end
This isnāt real refraction but it seems to be what Roblox uses (my math is probably wrong). I imagine this is for numerous optimization and compatibility reasons.
Hey! Sorry this is such an odd time, but I randomly decided to revive this project, and I just have a quick question - so I still havenāt done the second/exit raycast for the refraction, but is the second raycasts indicie(?) (the n1 or n2 thing) the same as the material (probably glass) or should it be air? To me it looks like the material indicie (idek)
n1 is the current material refractive index, and n2 is the material being enteredās refractive index.
To answer your question, when the light exits the glass, n1 is glass and n2 is air.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.