-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterials.sc
69 lines (57 loc) · 2.08 KB
/
materials.sc
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
using import enum
using import struct
using import glm
using import .utils
using import .extensions
struct LambertianM
albedo : vec3
fn scatter (self iray record rng)
scatter-dir := record.normal + (random-unit-vector rng)
scattered := (Ray record.p scatter-dir)
attenuation := self.albedo
_ true scattered (deref attenuation)
struct MetallicM
albedo : vec3
roughness : f32
fn scatter (self iray record rng)
reflected := (reflect (normalize iray.direction) record.normal)
scattered := (Ray record.p (reflected + ((random-in-unit-sphere rng) * self.roughness)))
attenuation := self.albedo
_
(dot scattered.direction record.normal) > 0
scattered
deref attenuation
struct DielectricM
refraction-index : f32
fn scatter (self iray record rng)
attenuation := (vec3 1) # never absorb
let coef =
# air -> dielectric or dielectric -> air
? record.front? (1 / self.refraction-index) (deref self.refraction-index)
ndir := (normalize iray.direction)
let cos-theta =
min
dot -ndir record.normal
1.0
let sin-theta = (sqrt (1.0 - cos-theta * cos-theta))
if ((coef * sin-theta) > 1.0)
reflected := (reflect ndir record.normal)
_ true (Ray record.p reflected) attenuation
# reflect on a probability based on Schlick's approximation
elseif (('normalized rng) < (schlick cos-theta coef))
reflected := (reflect ndir record.normal)
_ true (Ray record.p reflected) attenuation
else
refracted := (refract ndir record.normal coef)
_ true (Ray record.p refracted) attenuation
enum Material
Lambertian : LambertianM
Metallic : MetallicM
Dielectric : DielectricM
let __typecall = enum-class-constructor
inline... scatter (self iray record rng)
'apply self
(T self) -> ('scatter self (va-tail *...))
do
let LambertianM MetallicM DielectricM Material
locals;