-
Notifications
You must be signed in to change notification settings - Fork 1
/
texture.nelua
41 lines (36 loc) · 930 Bytes
/
texture.nelua
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
global TEXTURES = @enum {
COLOR = 0,
CHECKER = 1,
NOISE = 2,
}
global Texture = @record {
kind: TEXTURES,
diffuse_color: vec3,
scale: number
}
function Texture:color_color(uv: vec2, p: vec3): vec3
return self.diffuse_color
end
function Texture:checker_color(uv: vec2, p: vec3): vec3
local sines = math.fastsin(10*p.x)*math.fastsin(10*p.y)*math.fastsin(10*p.z)
if sines < 0 then
return vec3{1,1,1}
else
return self.diffuse_color
end
end
function Texture:noise_color(uv: vec2, p: vec3): vec3
local noise = Perlin.turb(p * self.scale, 2)*0.5+0.5
return self.diffuse_color * noise
end
function Texture:color(uv: vec2, p: vec3): vec3
switch self.kind
case TEXTURES.COLOR then
return Texture.color_color(self, uv, p)
case TEXTURES.CHECKER then
return Texture.checker_color(self, uv, p)
case TEXTURES.NOISE then
return Texture.noise_color(self, uv, p)
end
return vec3{}
end