Skip to content

Commit afef8f2

Browse files
committed
fix TraceMakie
1 parent d760f40 commit afef8f2

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

docs/code/TraceMakie.jl

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ end
5252

5353
function to_trace_primitive(plot::Makie.Mesh)
5454
# Potentially per instance attributes
55-
triangles = Trace.create_triangle_mesh(
56-
plot.mesh[], Trace.ShapeCore(Trace.translate(Vec3f(0)), false),
57-
)
55+
triangles = Trace.create_triangle_mesh(plot.mesh[])
5856
material = extract_material(plot, plot.color)
59-
return [Trace.GeometricPrimitive(t, material) for t in triangles]
57+
return Trace.GeometricPrimitive(triangles, material)
6058
end
6159

6260
function to_trace_primitive(plot::Makie.Surface)
@@ -85,14 +83,13 @@ function to_trace_primitive(plot::Makie.Surface)
8583
# with this we can beuild a mesh
8684
mesh = normal_mesh(GeometryBasics.Mesh(meta(vec(positions[]), uv=uv), faces))
8785

88-
triangles = Trace.create_triangle_mesh(
89-
mesh, Trace.ShapeCore(Trace.translate(Vec3f(0)), false),
90-
)
86+
triangles = Trace.create_triangle_mesh(mesh)
9187
material = extract_material(plot, plot.z)
92-
return [Trace.GeometricPrimitive(t, material) for t in triangles]
88+
return Trace.GeometricPrimitive(triangles, material)
9389
end
90+
9491
function to_trace_primitive(plot::Makie.Plot)
95-
return []
92+
return nothing
9693
end
9794

9895
function to_trace_light(light::Makie.AmbientLight)
@@ -118,13 +115,10 @@ function to_trace_light(light::Makie.PointLight)
118115
)
119116
end
120117

121-
122-
123118
function to_trace_light(light)
124119
return nothing
125120
end
126121

127-
128122
function to_trace_camera(scene::Makie.Scene, film)
129123
cc = scene.camera_controls
130124
fov = cc.fov[]
@@ -156,7 +150,7 @@ function render_scene(integrator_f, mscene::Makie.Scene)
156150
primitives = []
157151
for plot in mscene.plots
158152
prim = to_trace_primitive(plot)
159-
append!(primitives, prim)
153+
!isnothing(prim) && push!(primitives, prim)
160154
end
161155
camera = to_trace_camera(mscene, film)
162156
lights = []
@@ -167,48 +161,50 @@ function render_scene(integrator_f, mscene::Makie.Scene)
167161
if isempty(lights)
168162
error("Must have at least one light")
169163
end
170-
bvh = Trace.BVHAccel(map(identity, primitives), 1)
164+
bvh = Trace.BVHAccel(primitives, 1)
171165
integrator = integrator_f(camera)
172166
scene = Trace.Scene([lights...], bvh)
173-
integrator(scene)
167+
integrator(scene, film)
174168
return reverse(film.framebuffer, dims=1)
175169
end
176-
170+
glass = Trace.GlassMaterial(
171+
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
172+
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
173+
Trace.ConstantTexture(0.0f0),
174+
Trace.ConstantTexture(0.0f0),
175+
Trace.ConstantTexture(1.25f0),
176+
true,
177+
)
178+
mirror = Trace.MirrorMaterial(Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)))
179+
plastic = Trace.PlasticMaterial(
180+
Trace.ConstantTexture(Trace.RGBSpectrum(0.6399999857f0, 0.6399999857f0, 0.6399999857f0)),
181+
Trace.ConstantTexture(Trace.RGBSpectrum(0.1000000015f0, 0.1000000015f0, 0.1000000015f0)),
182+
Trace.ConstantTexture(0.010408001f0),
183+
true,
184+
)
177185
catmesh = load(Makie.assetpath("cat.obj"))
186+
178187
begin
179-
glass = Trace.GlassMaterial(
180-
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
181-
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
182-
Trace.ConstantTexture(0.0f0),
183-
Trace.ConstantTexture(0.0f0),
184-
Trace.ConstantTexture(1.25f0),
185-
true,
188+
scene = Scene(size=(1024, 1024);
189+
lights=[AmbientLight(RGBf(0.7, 0.6, 0.6)), PointLight(Vec3f(0, 1, 0.5), RGBf(1.3, 1.3, 1.3))]
186190
)
187-
mirror = Trace.MirrorMaterial(Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)))
188-
plastic = Trace.PlasticMaterial(
189-
Trace.ConstantTexture(Trace.RGBSpectrum(0.6399999857f0, 0.6399999857f0, 0.6399999857f0)),
190-
Trace.ConstantTexture(Trace.RGBSpectrum(0.1000000015f0, 0.1000000015f0, 0.1000000015f0)),
191-
Trace.ConstantTexture(0.010408001f0),
192-
true,
193-
)
194-
scene = Scene(size=(1024, 1024); lights=[AmbientLight(RGBf(0.7, 0.6, 0.6)), PointLight(Vec3f(0, 1, 0.5), RGBf(1.3, 1.3, 1.3))])
195191
cam3d!(scene)
196192
mesh!(scene, catmesh, color=load(Makie.assetpath("diffusemap.png")))
197193
center!(scene)
198-
render_scene(scene)
194+
# 1.024328 seconds (16.94 M allocations: 5.108 GiB, 46.19% gc time, 81 lock conflicts)
195+
@time render_scene(scene)
199196
end
200197

201-
202198
begin
203-
scene = Scene(size=(1024, 1024); lights=[
204-
AmbientLight(RGBf(0.4, 0.4, 0.4)), PointLight(Vec3f(4, 4, 10), RGBf(500, 500, 500))])
199+
scene = Scene(size=(1024, 1024);
200+
lights=[AmbientLight(RGBf(0.4, 0.4, 0.4)), PointLight(Vec3f(4, 4, 10), RGBf(500, 500, 500))]
201+
)
205202
cam3d!(scene)
206203
xs = LinRange(0, 10, 100)
207204
ys = LinRange(0, 15, 100)
208205
zs = [cos(x) * sin(y) for x in xs, y in ys]
209206
surface!(scene, xs, ys, zs)
210207
center!(scene)
211-
render_scene(scene) do cam
212-
213-
end
208+
# 1.598740s
209+
@time render_scene(scene)
214210
end

docs/code/basic-scene.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ begin
5252
lights = [
5353
# Trace.PointLight(Vec3f(0, -1, 2), Trace.RGBSpectrum(22.0f0)),
5454
Trace.PointLight(Vec3f(0, 0, 2), Trace.RGBSpectrum(10.0f0)),
55-
Trace.PointLight(Vec3f(0, 3, 3), Trace.RGBSpectrum(15.0f0)),
55+
Trace.PointLight(Vec3f(0, 3, 3), Trace.RGBSpectrum(25.0f0)),
5656
]
5757
scene = Trace.Scene(lights, bvh);
5858
resolution = Point2f(1024)
@@ -74,7 +74,7 @@ begin
7474
@time integrator(scene, film)
7575
img = reverse(film.framebuffer, dims=1)
7676
end
77-
# 6.7s
77+
# 6.296157 seconds (17.64 k allocations: 19.796 MiB, 0.13% gc time, 45 lock conflicts)
7878

7979

8080
camera_sample = Trace.get_camera_sample(integrator.sampler, Point2f(512))

0 commit comments

Comments
 (0)