Skip to content

Instantly share code, notes, and snippets.

@drawcode
Created January 6, 2019 07:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drawcode/06f1e9d823f96958a63e412719756709 to your computer and use it in GitHub Desktop.
Save drawcode/06f1e9d823f96958a63e412719756709 to your computer and use it in GitHub Desktop.
unity-vector3-refract.cs
/**
* returns:
* normalized Vector3 refracted by passing from one medium to another in a realistic manner according to Snell's Law
*
* parameters:
* RI1 - the refractive index of the first medium
* RI2 - the refractive index of the second medium
* surfNorm - the normal of the interface between the two mediums (for example the normal returned by a raycast)
* incident - the incoming Vector3 to be refracted
*
* usage example (laser pointed from a medium with RI roughly equal to air through a medium with RI roughly equal to water):
* Vector3 laserRefracted = Refract(1.0f, 1.33f, waterPointNorm, laserForward);
*/
public static Vector3 Refract(float RI1, float RI2, Vector3 surfNorm, Vector3 incident)
{
surfNorm.Normalize(); //should already be normalized, but normalize just to be sure
incident.Normalize();
return (RI1/RI2 * Vector3.Cross(surfNorm, Vector3.Cross(-surfNorm, incident)) - surfNorm * Mathf.Sqrt(1 - Vector3.Dot(Vector3.Cross(surfNorm, incident)*(RI1/RI2*RI1/RI2), Vector3.Cross(surfNorm, incident)))).normalized;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment