-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector3.fs
70 lines (47 loc) · 1.79 KB
/
Vector3.fs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module DummyPathtracer.Vector3
open System.Numerics
open DummyPathtracer
let lengthSquared (v: Vector3) = v.LengthSquared()
let cross (u: Vector3) (v: Vector3) =
Vector3(u.Y * v.Z - u.Z * v.Y, u.Z * v.X - u.X * v.Z, u.X * v.Y - u.Y * v.X)
let unitVector (v: Vector3) = v / v.Length()
let dot (u: Vector3) (v: Vector3) = u.X * v.X + u.Y * v.Y + u.Z * v.Z
let random random =
Vector3(randomFloat32 random, randomFloat32 random, randomFloat32 random)
let randomMinMax random min max =
Vector3(randomFloat32MinMax random min max, randomFloat32MinMax random min max, randomFloat32MinMax random min max)
let randomInUnitSphere random =
let rec recRandom () =
let p = randomMinMax random -1f 1f
if p.LengthSquared() >= 1.f then
recRandom ()
else
p
recRandom ()
let randomUnitVector random = randomInUnitSphere random |> unitVector
let randomInHemisphere random normal =
let inUnitSphere = randomInUnitSphere random
if dot inUnitSphere normal > 0.f then
inUnitSphere
else
-inUnitSphere
let nearZero (v: Vector3) =
let eps = 1e-8f
abs v.X < eps && abs v.Y < eps && abs v.Z < eps
let reflect v n = v - 2.f * (dot v n) * n
let refract uv n (etaIOverEtaT: float32) =
let cosTheta = min (dot -uv n) 1.f
let rOutPErp = etaIOverEtaT * (uv + cosTheta * n)
let rOutParallel =
- sqrt(abs (1.f - lengthSquared rOutPErp)) * n
rOutPErp + rOutParallel
let randomInUnitDisk random =
let rec recRandomInUnitDisk () =
let p =
Vector3(randomFloat32MinMax random -1.f 1.f, randomFloat32MinMax random -1.f 1.f, 0.f)
if p.LengthSquared() >= 1.f then
recRandomInUnitDisk ()
else
p
recRandomInUnitDisk ()
let length (v: Vector3) = v.Length()