1 -- Textures for raymarcher
5 local function make_phong_texture(lights, pigment, amb, diff, spec, shiny)
6 local normalize = V.normalize
9 local function light_visibility(location, light_dir, pos, dest, res, sdf)
10 local p = sdf(location)
11 if pos + p.dist > dest then
13 elseif p.dist < eps then
16 return light_visibility(location + light_dir*p.dist, light_dir, pos+p.dist,
17 dest, math.min(res, 30*p.dist/(pos+p.dist)), sdf)
21 return function (location, ray_dir, normal, count, sdf)
22 local colour = pigment(location[1],location[2])
23 local thiscol = {colour[1]*amb, colour[2]*amb, colour[3]*amb}
25 local reflected_ray = ray_dir - normal*(ray_dir*normal*2)
26 for _,light in ipairs(lights) do
27 local light_dist = V.norm(light-location)
28 local light_dir = (light-location)/light_dist
30 local vis = light_visibility(location + light_dir*eps, light_dir, eps, light_dist, 1, sdf)
32 local Idiff = math.max(normal*light_dir, 0)
33 local Ispec = math.pow(math.max(light_dir*reflected_ray,0),shiny)
35 thiscol[1] = thiscol[1] + vis*(colour[1]*Idiff*diff + Ispec*spec)
36 thiscol[2] = thiscol[2] + vis*(colour[2]*Idiff*diff + Ispec*spec)
37 thiscol[3] = thiscol[3] + vis*(colour[3]*Idiff*diff + Ispec*spec)
44 local function make_count_texture(scale)
45 return function (location, ray_dir, normal, count, sdf)
46 return {count*scale, count*scale, count*scale}
50 local function make_flat_texture(pigment)
51 return function (location, ray_dir, normal, count, sdf)
52 return pigment(location[1], location[2])
56 local function make_solid_pigment(colour)
62 local function make_checkered_pigment(colour1, colour2)
64 if (x%1 < 0.5 and y%1 < 0.5) or (x%1 > 0.5 and y%1 > 0.5) then
72 local function make_mandelbrot_pigment()
75 local set_colour = {0,0,0}
77 local function get_col(x,y,cx,cy,iter)
80 elseif x^2 + y^2 > 4 then
81 local I = iter/max_iter
84 return get_col(x^2 - y^2 + cx,
89 local xp = x^2 - y^2 + cx
94 get_col(0,0,x,y,maxiter)
99 make_phong_texture = make_phong_texture,
100 make_flat_texture = make_flat_texture,
101 make_count_texture = make_count_texture,
102 make_solid_pigment = make_solid_pigment,
103 make_checkered_pigment = make_checkered_pigment