|
1 | 1 | using GeometryBasics
|
2 | 2 | using LinearAlgebra
|
3 | 3 | using Trace, FileIO, MeshIO
|
| 4 | +begin |
| 5 | + glass = Trace.GlassMaterial( |
| 6 | + Trace.ConstantTexture(Trace.RGBSpectrum(1f0)), |
| 7 | + Trace.ConstantTexture(Trace.RGBSpectrum(1f0)), |
| 8 | + Trace.ConstantTexture(0f0), |
| 9 | + Trace.ConstantTexture(0f0), |
| 10 | + Trace.ConstantTexture(1.25f0), |
| 11 | + true, |
| 12 | + ) |
| 13 | + plastic = Trace.PlasticMaterial( |
| 14 | + Trace.ConstantTexture(Trace.RGBSpectrum(0.6399999857f0, 0.6399999857f0, 0.6399999857f0)), |
| 15 | + Trace.ConstantTexture(Trace.RGBSpectrum(0.1000000015f0, 0.1000000015f0, 0.1000000015f0)), |
| 16 | + Trace.ConstantTexture(0.010408001f0), |
| 17 | + true, |
| 18 | + ) |
4 | 19 |
|
5 |
| -glass = Trace.GlassMaterial( |
6 |
| - Trace.ConstantTexture(Trace.RGBSpectrum(1f0)), |
7 |
| - Trace.ConstantTexture(Trace.RGBSpectrum(1f0)), |
8 |
| - Trace.ConstantTexture(0f0), |
9 |
| - Trace.ConstantTexture(0f0), |
10 |
| - Trace.ConstantTexture(1.25f0), |
11 |
| - true, |
12 |
| -) |
13 |
| -plastic = Trace.PlasticMaterial( |
14 |
| - Trace.ConstantTexture(Trace.RGBSpectrum(0.6399999857f0, 0.6399999857f0, 0.6399999857f0)), |
15 |
| - Trace.ConstantTexture(Trace.RGBSpectrum(0.1000000015f0, 0.1000000015f0, 0.1000000015f0)), |
16 |
| - Trace.ConstantTexture(0.010408001f0), |
17 |
| - true, |
18 |
| -) |
| 20 | + model = load(joinpath(@__DIR__, "..", "src", "assets", "models", "caustic-glass.ply")) |
19 | 21 |
|
20 |
| -model = load(joinpath(@__DIR__, "..", "src", "assets", "models", "caustic-glass.ply")) |
21 | 22 |
|
| 23 | + triangles = Trace.create_triangle_mesh( |
| 24 | + model, Trace.ShapeCore(Trace.translate(Vec3f(5, -1.49, -100)), false), |
| 25 | + ) |
22 | 26 |
|
23 |
| -triangles = Trace.create_triangle_mesh( |
24 |
| - model, Trace.ShapeCore(Trace.translate(Vec3f(5, -1.49, -100)), false), |
25 |
| -) |
| 27 | + floor_triangles = Trace.create_triangle_mesh( |
| 28 | + Trace.ShapeCore(Trace.translate(Vec3f(-10, 0, -87)), false), |
| 29 | + UInt32[1, 2, 3, 1, 4, 3], |
| 30 | + [ |
| 31 | + Point3f(0, 0, 0), Point3f(0, 0, -30), |
| 32 | + Point3f(30, 0, -30), Point3f(30, 0, 0), |
| 33 | + ], |
| 34 | + [ |
| 35 | + Trace.Normal3f(0, 1, 0), Trace.Normal3f(0, 1, 0), |
| 36 | + Trace.Normal3f(0, 1, 0), Trace.Normal3f(0, 1, 0), |
| 37 | + ], |
| 38 | + ) |
26 | 39 |
|
27 |
| -floor_triangles = Trace.create_triangle_mesh( |
28 |
| - Trace.ShapeCore(Trace.translate(Vec3f(-10, 0, -87)), false), |
29 |
| - UInt32[1, 2, 3, 1, 4, 3], |
30 |
| - [ |
31 |
| - Point3f(0, 0, 0), Point3f(0, 0, -30), |
32 |
| - Point3f(30, 0, -30), Point3f(30, 0, 0), |
33 |
| - ], |
34 |
| - [ |
35 |
| - Trace.Normal3f(0, 1, 0), Trace.Normal3f(0, 1, 0), |
36 |
| - Trace.Normal3f(0, 1, 0), Trace.Normal3f(0, 1, 0), |
37 |
| - ], |
38 |
| -) |
| 40 | + primitives = Trace.GeometricPrimitive[] |
| 41 | + for t in triangles |
| 42 | + push!(primitives, Trace.GeometricPrimitive(t, glass)) |
| 43 | + end |
| 44 | + for t in floor_triangles |
| 45 | + push!(primitives, Trace.GeometricPrimitive(t, plastic)) |
| 46 | + end |
39 | 47 |
|
40 |
| -primitives = Trace.GeometricPrimitive[] |
41 |
| -for t in triangles |
42 |
| - push!(primitives, Trace.GeometricPrimitive(t, glass)) |
43 |
| -end |
44 |
| -for t in floor_triangles |
45 |
| - push!(primitives, Trace.GeometricPrimitive(t, plastic)) |
46 |
| -end |
47 |
| - |
48 |
| -bvh = Trace.BVHAccel(map(identity, primitives), 1); |
| 48 | + bvh = Trace.BVHAccel(map(identity, primitives), 1); |
49 | 49 |
|
50 |
| -from, to = Point3f(0, 2, 0), Point3f(-5, 0, 5) |
51 |
| -cone_angle, cone_δ_angle = 30f0, 10f0 |
52 |
| -dir = normalize(Vec3f(to - from)) |
53 |
| -dir, du, dv = Trace.coordinate_system(dir) |
| 50 | + from, to = Point3f(0, 2, 0), Point3f(-5, 0, 5) |
| 51 | + cone_angle, cone_δ_angle = 30f0, 10f0 |
| 52 | + dir = normalize(Vec3f(to - from)) |
| 53 | + dir, du, dv = Trace.coordinate_system(dir) |
54 | 54 |
|
55 |
| -dir_to_z = Trace.Transformation(transpose(Mat4f( |
56 |
| - du[1], du[2], du[3], 0f0, |
57 |
| - dv[1], dv[2], dv[3], 0f0, |
58 |
| - dir[1], dir[2], dir[3], 0f0, |
59 |
| - 0f0, 0f0, 0f0, 1f0, |
60 |
| -))) |
61 |
| -light_to_world = ( |
62 |
| - Trace.translate(Vec3f(4.5, 0, -101)) |
63 |
| - * Trace.translate(Vec3f(from)) |
64 |
| - * inv(dir_to_z) |
65 |
| -) |
| 55 | + dir_to_z = Trace.Transformation(transpose(Mat4f( |
| 56 | + du[1], du[2], du[3], 0f0, |
| 57 | + dv[1], dv[2], dv[3], 0f0, |
| 58 | + dir[1], dir[2], dir[3], 0f0, |
| 59 | + 0f0, 0f0, 0f0, 1f0, |
| 60 | + ))) |
| 61 | + light_to_world = ( |
| 62 | + Trace.translate(Vec3f(4.5, 0, -101)) |
| 63 | + * Trace.translate(Vec3f(from)) |
| 64 | + * inv(dir_to_z) |
| 65 | + ) |
66 | 66 |
|
67 |
| -lights = [ |
68 |
| - Trace.SpotLight( |
69 |
| - light_to_world, Trace.RGBSpectrum(60f0), |
70 |
| - cone_angle, cone_angle - cone_δ_angle, |
71 |
| - ), |
72 |
| -] |
| 67 | + lights = [ |
| 68 | + Trace.SpotLight( |
| 69 | + light_to_world, Trace.RGBSpectrum(100f0), |
| 70 | + cone_angle, cone_angle - cone_δ_angle, |
| 71 | + ), |
| 72 | + Trace.AmbientLight(Trace.RGBSpectrum(0.5f0)), |
| 73 | + ] |
73 | 74 |
|
74 |
| -scene = Trace.Scene(lights, bvh) |
| 75 | + scene = Trace.Scene(lights, bvh) |
75 | 76 |
|
76 |
| -n_samples = 8 |
77 |
| -ray_depth = 5 |
| 77 | + n_samples = 8 |
| 78 | + ray_depth = 5 |
78 | 79 |
|
79 |
| -look_point = Point3f(-3, 0, -91) |
80 |
| -screen = Trace.Bounds2(Point2f(-1f0), Point2f(1f0)) |
81 |
| -filter = Trace.LanczosSincFilter(Point2f(1f0), 3f0) |
82 |
| - |
83 |
| -resolution = Point2f(1024) |
84 |
| -ir = Int64.(resolution) |
85 |
| -film = Trace.Film( |
86 |
| - resolution, Trace.Bounds2(Point2f(0), Point2f(1)), |
87 |
| - filter, 1f0, 1f0, |
88 |
| - "./scenes/caustics-sppm-$(ir[1])x$(ir[2]).png", |
89 |
| -) |
90 |
| -camera = Trace.PerspectiveCamera( |
91 |
| - Trace.look_at(Point3f(0, 150, 150), look_point, Vec3f(0, 1, 0)), |
92 |
| - screen, 0f0, 1f0, 0f0, 1f6, 90f0, film, |
93 |
| -) |
94 |
| - |
95 |
| -integrator = Trace.SPPMIntegrator(camera, 0.075f0, ray_depth, 1) |
96 |
| -@profview_allocs integrator(scene) |
97 |
| -@time integrator(scene) |
| 80 | + look_point = Point3f(-3, 0, 0) |
| 81 | + screen = Trace.Bounds2(Point2f(-1f0), Point2f(1f0)) |
| 82 | + filter = Trace.LanczosSincFilter(Point2f(1f0), 3f0) |
| 83 | + resolution = Point2f(1024) |
| 84 | + ir = Int64.(resolution) |
| 85 | + film = Trace.Film( |
| 86 | + resolution, Trace.Bounds2(Point2f(0), Point2f(1)), |
| 87 | + filter, 1f0, 1f0, |
| 88 | + "./scenes/caustics-sppm-$(ir[1])x$(ir[2]).png", |
| 89 | + ) |
| 90 | + camera = Trace.PerspectiveCamera( |
| 91 | + Trace.look_at(Point3f(0, 4, 4), look_point, Vec3f(0, 1, 0)), |
| 92 | + screen, 0f0, 1f0, 0f0, 1f6, 90f0, film, |
| 93 | + ) |
| 94 | + integrator = Trace.SPPMIntegrator(camera, 0.075f0, ray_depth, 1) |
| 95 | + @time integrator(scene) |
| 96 | + reverse(film.framebuffer, dims=1) |
| 97 | +end |
| 98 | +mean(decompose(Point, Rect3f(model))) |
| 99 | +Rect3f(model) |
0 commit comments