-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorUtil.cs
55 lines (51 loc) · 1.58 KB
/
VectorUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Numerics;
namespace RayTracer
{
static class VectorUtil
{
public static Vector3 Reflect(in Vector3 v, in Vector3 normal)
{
return v - 2 * Vector3.Dot(v, normal) * normal;
}
public static Vector3 Refract(in Vector3 v, in Vector3 normal, float refIndex)
{
Vector3 uv = Vector3.Normalize(v);
Vector3 outwardNormal;
float dt = Vector3.Dot(uv, normal);
float niOverNt;
if (dt > 0)
{
outwardNormal = -normal;
niOverNt = refIndex;
}
else
{
outwardNormal = normal;
niOverNt = 1.0f / refIndex;
}
float discriminant = 1.0f - niOverNt*niOverNt*(1-dt*dt);
if (discriminant > 0)
{
return niOverNt * (uv - outwardNormal * dt) - outwardNormal * (float)Math.Sqrt(discriminant);
}
return Vector3.Zero;
}
public static float Schlick(in Vector3 v, in Vector3 normal, float refIndex)
{
float dt = Vector3.Dot(v, normal);
float cosine;
if (dt > 0)
{
cosine = refIndex*dt/v.Length();
}
else
{
cosine = -dt/v.Length();
}
float r0 = (1-refIndex) / (1+refIndex);
r0 = r0 * r0;
return r0 + (1-r0) * (float)Math.Pow((1-cosine), 5);
}
}
}