X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=Textures.lua;h=2b2e70151f24eaec9150152a01ebc27792ccab5c;hb=f564e0e7248f977ea2397ea49e3d2e1804a65f7c;hp=cee3dd9f715d371071ad053acfab3ea386e480cf;hpb=46c1311e546ae5cefc4f596ed13476c649f12f66;p=raymarcher.git diff --git a/Textures.lua b/Textures.lua index cee3dd9..2b2e701 100644 --- a/Textures.lua +++ b/Textures.lua @@ -1,22 +1,41 @@ -- Textures for raymarcher + require "Vector" local V = Vector -local function make_phong_texture(lights, pigment, amb, diff, spec, shiny) +local function make_phong_texture(lights, mapped_pigment, amb, diff, spec, shiny) local normalize = V.normalize - return function (location, ray_dir, normal, count) - local colour = pigment(location[1],location[3]) + + local eps = 0.0001 + local function light_visibility(location, light_dir, pos, dest, res, sdf) + local p = sdf(location) + if pos + p.dist > dest then + return res + elseif p.dist < eps then + return 0.0 + else + return light_visibility(location + light_dir*p.dist, light_dir, pos+p.dist, + dest, math.min(res, 30*p.dist/(pos+p.dist)), sdf) + end + end + + return function (location, ray_dir, normal, count, sdf) + local colour = mapped_pigment(location) local thiscol = {colour[1]*amb, colour[2]*amb, colour[3]*amb} local reflected_ray = ray_dir - normal*(ray_dir*normal*2) for _,light in ipairs(lights) do - local light_dir = V.normalize(light-location) + local light_dist = V.norm(light-location) + local light_dir = (light-location)/light_dist + + local vis = light_visibility(location + light_dir*eps, light_dir, eps, light_dist, 1, sdf) + local Idiff = math.max(normal*light_dir, 0) local Ispec = math.pow(math.max(light_dir*reflected_ray,0),shiny) - thiscol[1] = thiscol[1] + colour[1]*Idiff*diff + Ispec*spec - thiscol[2] = thiscol[2] + colour[2]*Idiff*diff + Ispec*spec - thiscol[3] = thiscol[3] + colour[3]*Idiff*diff + Ispec*spec + thiscol[1] = thiscol[1] + vis*(colour[1]*Idiff*diff + Ispec*spec) + thiscol[2] = thiscol[2] + vis*(colour[2]*Idiff*diff + Ispec*spec) + thiscol[3] = thiscol[3] + vis*(colour[3]*Idiff*diff + Ispec*spec) end return thiscol @@ -24,25 +43,28 @@ local function make_phong_texture(lights, pigment, amb, diff, spec, shiny) end local function make_count_texture(scale) - return function (location, ray_dir, normal, count) + return function (location, ray_dir, normal, count, sdf) return {count*scale, count*scale, count*scale} end end -local function make_flat_texture(pigment) - return function (location, ray_dir, normal, count) - return pigment(location[1], location[2]) +local function make_flat_texture(mapped_pigment) + return function (location, ray_dir, normal, count, sdf) + return mapped_pigment(location) end end + +-- Pigments + local function make_solid_pigment(colour) - return function (x,y) + return function(x,y) return colour end end local function make_checkered_pigment(colour1, colour2) - return function (x,y) + return function(x,y) if (x%1 < 0.5 and y%1 < 0.5) or (x%1 > 0.5 and y%1 > 0.5) then return colour1 else @@ -50,13 +72,58 @@ local function make_checkered_pigment(colour1, colour2) end end end + +local function make_mandelbrot_pigment(set_pigment, nonset_pigment, max_iter) + + local function get_col(x,y,cx,cy,iter) + if iter == 0 then + return set_pigment(cx, cy) + elseif x^2 + y^2 > 4 then + return nonset_pigment(cx, cy) + else + return get_col(x^2 - y^2 + cx, + 2*x*y + cy, + cx, cy, iter-1) + end + end + + return function (x,y) + return get_col(0,0,x,y,max_iter) + end +end + +-- Mapping functions + +-- These functions define mappings from 3D world coordinates to 2D +-- texture/pigment coordinates + +local function map_rectangular(pigment, xvec, yvec) + return function(location) + return pigment(location*xvec, location*yvec) + end +end + +local function map_spherical(pigment, centre, scale_theta, scale_phi) + return function(location) + local r = location-centre + local rnorm = V.norm(r) + + local phi = math.acos((V.z*r)/rnorm) + local theta = math.acos((V.x*r)/V.norm(V.cross(V.z,r))) + + return pigment(theta*scale_theta/math.pi, phi*scale_phi/math.pi) + end +end Textures = { make_phong_texture = make_phong_texture, make_flat_texture = make_flat_texture, make_count_texture = make_count_texture, make_solid_pigment = make_solid_pigment, - make_checkered_pigment = make_checkered_pigment + make_checkered_pigment = make_checkered_pigment, + make_mandelbrot_pigment = make_mandelbrot_pigment, + map_rectangular = map_rectangular, + map_spherical = map_spherical } return Textures