-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
drawing_primitives.jl
813 lines (730 loc) · 32.5 KB
/
drawing_primitives.jl
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
using Makie: transform_func_obs, apply_transform
using Makie: attribute_per_char, FastPixel, el32convert, Pixel
using Makie: convert_arguments
function handle_lights(attr::Dict, screen::Screen, lights::Vector{Makie.AbstractLight})
@inline function push_inplace!(trg, idx, src)
for i in eachindex(src)
trg[idx + i] = src[i]
end
return idx + length(src)
end
MAX_LIGHTS = screen.config.max_lights
MAX_PARAMS = screen.config.max_light_parameters
# Every light has a type and a color. Therefore we have these as independent
# uniforms with a max length of MAX_LIGHTS.
# Other parameters like position, direction, etc differe between light types.
# To avoid wasting a bunch of memory we squash all of them into one vector of
# size MAX_PARAMS.
attr[:N_lights] = Observable(0)
attr[:light_types] = Observable(sizehint!(Int32[], MAX_LIGHTS))
attr[:light_colors] = Observable(sizehint!(RGBf[], MAX_LIGHTS))
attr[:light_parameters] = Observable(sizehint!(Float32[], MAX_PARAMS))
on(screen.render_tick, priority = typemin(Int)) do _
# derive number of lights from available lights. Both MAX_LIGHTS and
# MAX_PARAMS are considered for this.
n_lights = 0
n_params = 0
for light in lights
delta = 0
if light isa PointLight
delta = 5 # 3 position + 2 attenuation
elseif light isa DirectionalLight
delta = 3 # 3 direction
elseif light isa SpotLight
delta = 8 # 3 position + 3 direction + 2 angles
elseif light isa RectLight
delta = 12 # 3 position + 2x 3 rect basis vectors + 3 direction
end
if n_params + delta > MAX_PARAMS || n_lights == MAX_LIGHTS
if n_params > MAX_PARAMS
@warn "Exceeded the maximum number of light parameters ($n_params > $MAX_PARAMS). Skipping lights beyond number $n_lights."
else
@warn "Exceeded the maximum number of lights ($n_lights > $MAX_LIGHTS). Skipping lights beyond number $n_lights."
end
break
end
n_params += delta
n_lights += 1
end
# Update number of lights
attr[:N_lights][] = n_lights
# Update light types
trg = attr[:light_types][]
resize!(trg, n_lights)
map!(i -> Makie.light_type(lights[i]), trg, 1:n_lights)
notify(attr[:light_types])
# Update light colors
trg = attr[:light_colors][]
resize!(trg, n_lights)
map!(i -> Makie.light_color(lights[i]), trg, 1:n_lights)
notify(attr[:light_colors])
# Update other light parameters
# This precalculates world space pos/dir -> view/cam space pos/dir
parameters = attr[:light_parameters][]
resize!(parameters, n_params)
idx = 0
for i in 1:n_lights
light = lights[i]
if light isa PointLight
idx = push_inplace!(parameters, idx, light.position[])
idx = push_inplace!(parameters, idx, light.attenuation[])
elseif light isa DirectionalLight
if light.camera_relative
T = inv(attr[:view][][Vec(1,2,3), Vec(1,2,3)])
dir = normalize(T * light.direction[])
else
dir = normalize(light.direction[])
end
idx = push_inplace!(parameters, idx, dir)
elseif light isa SpotLight
idx = push_inplace!(parameters, idx, light.position[])
idx = push_inplace!(parameters, idx, normalize(light.direction[]))
idx = push_inplace!(parameters, idx, cos.(light.angles[]))
elseif light isa RectLight
idx = push_inplace!(parameters, idx, light.position[])
idx = push_inplace!(parameters, idx, light.u1[])
idx = push_inplace!(parameters, idx, light.u2[])
idx = push_inplace!(parameters, idx, normalize(light.direction[]))
end
end
notify(attr[:light_parameters])
return Consume(false)
end
return attr
end
Makie.el32convert(x::GLAbstraction.Texture) = x
gpuvec(x) = GPUVector(GLBuffer(x))
to_range(x, y) = to_range.((x, y))
to_range(x::ClosedInterval) = (minimum(x), maximum(x))
to_range(x::VecTypes{2}) = x
to_range(x::AbstractRange) = (minimum(x), maximum(x))
to_range(x::AbstractVector) = (minimum(x), maximum(x))
function to_range(x::AbstractArray)
if length(x) in size(x) # assert that just one dim != 1
to_range(vec(x))
else
error("Can't convert to a range. Please supply a range/vector/interval or a tuple (min, max)")
end
end
function to_glvisualize_key(k)
k === :rotations && return :rotation
k === :markersize && return :scale
k === :glowwidth && return :glow_width
k === :glowcolor && return :glow_color
k === :strokewidth && return :stroke_width
k === :strokecolor && return :stroke_color
k === :positions && return :position
k === :linewidth && return :thickness
k === :marker_offset && return :quad_offset
k === :colormap && return :color_map
k === :colorrange && return :color_norm
k === :transform_marker && return :scale_primitive
return k
end
function connect_camera!(plot, gl_attributes, cam, space = gl_attributes[:space])
for key in (:pixel_space, :eyeposition)
# Overwrite these, user defined attributes shouldn't use those!
gl_attributes[key] = lift(identity, plot, getfield(cam, key))
end
get!(gl_attributes, :view) do
# get!(cam.calculated_values, Symbol("view_$(space[])")) do
return lift(plot, cam.view, space) do view, space
return is_data_space(space) ? view : Mat4f(I)
end
# end
end
# for lighting
get!(gl_attributes, :world_normalmatrix) do
return lift(plot, gl_attributes[:model]) do m
i = Vec(1, 2, 3)
return transpose(inv(m[i, i]))
end
end
# for SSAO
get!(gl_attributes, :view_normalmatrix) do
return lift(plot, gl_attributes[:view], gl_attributes[:model]) do v, m
i = Vec(1, 2, 3)
return transpose(inv(v[i, i] * m[i, i]))
end
end
get!(gl_attributes, :projection) do
# return get!(cam.calculated_values, Symbol("projection_$(space[])")) do
return lift(plot, cam.projection, cam.pixel_space, space) do _, _, space
return Makie.space_to_clip(cam, space, false)
end
# end
end
get!(gl_attributes, :projectionview) do
# get!(cam.calculated_values, Symbol("projectionview_$(space[])")) do
return lift(plot, cam.projectionview, cam.pixel_space, space) do _, _, space
Makie.space_to_clip(cam, space, true)
end
# end
end
# resolution in real hardware pixels, not scaled pixels/units
get!(gl_attributes, :resolution) do
get!(cam.calculated_values, :resolution) do
return lift(*, plot, gl_attributes[:px_per_unit], cam.resolution)
end
end
delete!(gl_attributes, :space)
delete!(gl_attributes, :markerspace)
return nothing
end
function handle_intensities!(screen, attributes, plot)
color = plot.calculated_colors
if color[] isa Makie.ColorMapping
onany(plot, color[].color_scaled, color[].colorrange_scaled, color[].colormap, color[].nan_color) do args...
screen.requires_update = true
end
attributes[:intensity] = color[].color_scaled
interp = color[].color_mapping_type[] === Makie.continuous ? :linear : :nearest
attributes[:color_map] = Texture(color[].colormap; minfilter=interp)
attributes[:color_norm] = color[].colorrange_scaled
attributes[:nan_color] = color[].nan_color
attributes[:highclip] = Makie.highclip(color[])
attributes[:lowclip] = Makie.lowclip(color[])
attributes[:color] = nothing
else
attributes[:color] = color
delete!(attributes, :intensity)
delete!(attributes, :color_map)
delete!(attributes, :color_norm)
delete!(attributes, :colorscale)
end
return
end
function get_space(x)
is_fast_pixel = to_value(get(x, :marker, nothing)) isa FastPixel
is_fast_pixel && return x.space
return haskey(x, :markerspace) ? x.markerspace : x.space
end
const EXCLUDE_KEYS = Set([:transformation, :tickranges, :ticklabels, :raw, :SSAO,
:lightposition, :material, :axis_cycler,
:inspector_label, :inspector_hover, :inspector_clear, :inspectable,
:colorrange, :colormap, :colorscale, :highclip, :lowclip, :nan_color,
:calculated_colors, :space, :markerspace, :model])
function cached_robj!(robj_func, screen, scene, plot::AbstractPlot)
# poll inside functions to make wait on compile less prominent
pollevents(screen)
robj = get!(screen.cache, objectid(plot)) do
filtered = filter(plot.attributes) do (k, v)
return !in(k, EXCLUDE_KEYS)
end
track_updates = screen.config.render_on_demand
if track_updates
for arg in plot.args
on(plot, arg) do x
screen.requires_update = true
end
end
on(plot, plot.model) do x
screen.requires_update = true
end
on(plot, scene.camera.projectionview) do x
screen.requires_update = true
end
end
gl_attributes = Dict{Symbol, Any}(map(filtered) do key_value
key, value = key_value
gl_key = to_glvisualize_key(key)
gl_value = lift_convert(key, value, plot, screen)
gl_key => gl_value
end)
gl_attributes[:model] = plot.model
if haskey(plot, :markerspace)
gl_attributes[:markerspace] = plot.markerspace
end
gl_attributes[:space] = plot.space
gl_attributes[:px_per_unit] = screen.px_per_unit
handle_intensities!(screen, gl_attributes, plot)
connect_camera!(plot, gl_attributes, scene.camera, get_space(plot))
# TODO: remove depwarn & conversion after some time
if haskey(gl_attributes, :shading) && to_value(gl_attributes[:shading]) isa Bool
@warn "`shading::Bool` is deprecated. Use `shading = NoShading` instead of false and `shading = FastShading` or `shading = MultiLightShading` instead of true."
gl_attributes[:shading] = ifelse(gl_attributes[:shading][], FastShading, NoShading)
elseif haskey(gl_attributes, :shading) && gl_attributes[:shading] isa Observable
gl_attributes[:shading] = gl_attributes[:shading][]
end
shading = to_value(get(gl_attributes, :shading, NoShading))
if shading == FastShading
dirlight = Makie.get_directional_light(scene)
if !isnothing(dirlight)
gl_attributes[:light_direction] = if dirlight.camera_relative
map(gl_attributes[:view], dirlight.direction) do view, dir
return normalize(inv(view[Vec(1,2,3), Vec(1,2,3)]) * dir)
end
else
map(normalize, dirlight.direction)
end
gl_attributes[:light_color] = dirlight.color
else
gl_attributes[:light_direction] = Observable(Vec3f(0))
gl_attributes[:light_color] = Observable(RGBf(0,0,0))
end
ambientlight = Makie.get_ambient_light(scene)
if !isnothing(ambientlight)
gl_attributes[:ambient] = ambientlight.color
else
gl_attributes[:ambient] = Observable(RGBf(0,0,0))
end
elseif shading == MultiLightShading
handle_lights(gl_attributes, screen, scene.lights)
end
robj = robj_func(gl_attributes) # <-- here
get!(gl_attributes, :ssao, Observable(false))
screen.cache2plot[robj.id] = plot
return robj
end
push!(screen, scene, robj)
return robj
end
Base.insert!(::GLMakie.Screen, ::Scene, ::Makie.PlotList) = nothing
function Base.insert!(screen::Screen, scene::Scene, @nospecialize(x::Plot))
ShaderAbstractions.switch_context!(screen.glscreen)
# poll inside functions to make wait on compile less prominent
pollevents(screen)
if isempty(x.plots) # if no plots inserted, this truly is an atomic
draw_atomic(screen, scene, x)
else
foreach(x.plots) do x
# poll inside functions to make wait on compile less prominent
pollevents(screen)
insert!(screen, scene, x)
end
end
end
index1D(x::SubArray) = parentindices(x)[1]
handle_view(array::AbstractVector, attributes) = array
handle_view(array::Observable, attributes) = array
function handle_view(array::SubArray, attributes)
A = parent(array)
indices = index1D(array)
attributes[:indices] = indices
return A
end
function handle_view(array::Observable{T}, attributes) where T <: SubArray
A = lift(parent, array)
indices = lift(index1D, array)
attributes[:indices] = indices
return A
end
function lift_convert(key, value, plot, screen)
return lift_convert_inner(value, Key{key}(), Key{Makie.plotkey(plot)}(), plot, screen)
end
function lift_convert_inner(value, key, plot_key, plot, screen)
return lift(plot, value) do value
screen.requires_update = true
return convert_attribute(value, key, plot_key)
end
end
to_vec4(val::RGB) = RGBAf(val, 1.0)
to_vec4(val::RGBA) = RGBAf(val)
pixel2world(scene, msize::Number) = pixel2world(scene, Point2f(msize))[1]
function pixel2world(scene, msize::StaticVector{2})
# TODO figure out why Vec(x, y) doesn't work correctly
p0 = Makie.to_world(scene, Point2f(0.0))
p1 = Makie.to_world(scene, Point2f(msize))
diff = p1 - p0
return diff
end
pixel2world(scene, msize::AbstractVector) = pixel2world.(scene, msize)
function draw_atomic(screen::Screen, scene::Scene, @nospecialize(plot::Union{Scatter, MeshScatter}))
return cached_robj!(screen, scene, plot) do gl_attributes
# signals not supported for shading yet
marker = pop!(gl_attributes, :marker)
space = plot.space
positions = handle_view(plot[1], gl_attributes)
positions = lift(apply_transform, plot, transform_func_obs(plot), positions, space)
if plot isa Scatter
mspace = plot.markerspace
cam = scene.camera
gl_attributes[:preprojection] = lift(plot, space, mspace, cam.projectionview,
cam.resolution) do space, mspace, _, _
return Makie.clip_to_space(cam, mspace) * Makie.space_to_clip(cam, space)
end
# fast pixel does its own setup
if !(marker[] isa FastPixel)
gl_attributes[:billboard] = lift(rot -> isa(rot, Billboard), plot, plot.rotations)
atlas = gl_texture_atlas()
isnothing(gl_attributes[:distancefield][]) && delete!(gl_attributes, :distancefield)
shape = lift(m -> Cint(Makie.marker_to_sdf_shape(m)), plot, marker)
gl_attributes[:shape] = shape
get!(gl_attributes, :distancefield) do
if shape[] === Cint(DISTANCEFIELD)
return get_texture!(atlas)
else
return nothing
end
end
font = get(gl_attributes, :font, Observable(Makie.defaultfont()))
gl_attributes[:uv_offset_width][] == Vec4f(0) && delete!(gl_attributes, :uv_offset_width)
get!(gl_attributes, :uv_offset_width) do
return Makie.primitive_uv_offset_width(atlas, marker, font)
end
scale, quad_offset = Makie.marker_attributes(atlas, marker, gl_attributes[:scale], font,
gl_attributes[:quad_offset], plot)
gl_attributes[:scale] = scale
gl_attributes[:quad_offset] = quad_offset
end
end
if marker[] isa FastPixel
if haskey(gl_attributes, :intensity)
gl_attributes[:color] = pop!(gl_attributes, :intensity)
end
filter!(gl_attributes) do (k, v,)
k in (:color_map, :color, :color_norm, :scale, :model, :projectionview, :visible)
end
return draw_pixel_scatter(screen, positions, gl_attributes)
else
if plot isa MeshScatter
if haskey(gl_attributes, :color) && to_value(gl_attributes[:color]) isa AbstractMatrix{<: Colorant}
gl_attributes[:image] = gl_attributes[:color]
end
return draw_mesh_particle(screen, (marker, positions), gl_attributes)
else
return draw_scatter(screen, (marker, positions), gl_attributes)
end
end
end
end
_mean(xs) = sum(xs) / length(xs) # skip Statistics import
function draw_atomic(screen::Screen, scene::Scene, @nospecialize(plot::Lines))
return cached_robj!(screen, scene, plot) do gl_attributes
linestyle = pop!(gl_attributes, :linestyle)
data = Dict{Symbol, Any}(gl_attributes)
positions = handle_view(plot[1], data)
transform_func = transform_func_obs(plot)
ls = to_value(linestyle)
space = plot.space
if isnothing(ls)
data[:pattern] = ls
data[:fast] = true
positions = lift(apply_transform, plot, transform_func, positions, space)
else
linewidth = gl_attributes[:thickness]
px_per_unit = data[:px_per_unit]
data[:pattern] = map(linestyle, linewidth, px_per_unit) do ls, lw, ppu
ppu * _mean(lw) .* ls
end
data[:fast] = false
pvm = lift(*, plot, data[:projectionview], data[:model])
positions = lift(plot, transform_func, positions, space, pvm,
data[:resolution]) do f, ps, space, pvm, res
transformed = apply_transform(f, ps, space)
output = Vector{Point3f}(undef, length(transformed))
scale = Vec3f(res[1], res[2], 1f0)
for i in eachindex(transformed)
clip = pvm * to_ndim(Point4f, to_ndim(Point3f, transformed[i], 0f0), 1f0)
output[i] = scale .* Point3f(clip) ./ clip[4]
end
output
end
end
return draw_lines(screen, positions, data)
end
end
function draw_atomic(screen::Screen, scene::Scene, @nospecialize(plot::LineSegments))
return cached_robj!(screen, scene, plot) do gl_attributes
linestyle = pop!(gl_attributes, :linestyle)
data = Dict{Symbol, Any}(gl_attributes)
px_per_unit = data[:px_per_unit]
ls = to_value(linestyle)
if isnothing(ls)
data[:pattern] = nothing
data[:fast] = true
else
linewidth = gl_attributes[:thickness]
data[:pattern] = lift(plot, linestyle, linewidth, px_per_unit) do ls, lw, ppu
ppu * _mean(lw) .* ls
end
data[:fast] = false
end
positions = handle_view(plot[1], data)
positions = lift(apply_transform, plot, transform_func_obs(plot), positions, plot.space)
if haskey(data, :intensity)
data[:color] = pop!(data, :intensity)
end
return draw_linesegments(screen, positions, data)
end
end
function draw_atomic(screen::Screen, scene::Scene,
plot::Text{<:Tuple{<:Union{<:Makie.GlyphCollection, <:AbstractVector{<:Makie.GlyphCollection}}}})
return cached_robj!(screen, scene, plot) do gl_attributes
glyphcollection = plot[1]
transfunc = Makie.transform_func_obs(plot)
pos = gl_attributes[:position]
space = plot.space
markerspace = plot.markerspace
offset = pop!(gl_attributes, :offset, Vec2f(0))
atlas = gl_texture_atlas()
# calculate quad metrics
glyph_data = lift(plot, pos, glyphcollection, offset, transfunc, space) do pos, gc, offset, transfunc, space
return Makie.text_quads(atlas, pos, to_value(gc), offset, transfunc, space)
end
# unpack values from the one signal:
positions, char_offset, quad_offset, uv_offset_width, scale = map((1, 2, 3, 4, 5)) do i
lift(getindex, plot, glyph_data, i)
end
filter!(gl_attributes) do (k, v)
# These are liftkeys without model
!(k in (
:position, :space, :markerspace, :font,
:fontsize, :rotation, :justification
)) # space,
end
gl_attributes[:color] = lift(plot, glyphcollection) do gc
if gc isa AbstractArray
reduce(vcat, (Makie.collect_vector(g.colors, length(g.glyphs)) for g in gc),
init = RGBAf[])
else
Makie.collect_vector(gc.colors, length(gc.glyphs))
end
end
gl_attributes[:stroke_color] = lift(plot, glyphcollection) do gc
if gc isa AbstractArray
reduce(vcat, (Makie.collect_vector(g.strokecolors, length(g.glyphs)) for g in gc),
init = RGBAf[])
else
Makie.collect_vector(gc.strokecolors, length(gc.glyphs))
end
end
gl_attributes[:rotation] = lift(plot, glyphcollection) do gc
if gc isa AbstractArray
reduce(vcat, (Makie.collect_vector(g.rotations, length(g.glyphs)) for g in gc),
init = Quaternionf[])
else
Makie.collect_vector(gc.rotations, length(gc.glyphs))
end
end
gl_attributes[:shape] = Cint(DISTANCEFIELD)
gl_attributes[:scale] = scale
gl_attributes[:quad_offset] = quad_offset
gl_attributes[:marker_offset] = char_offset
gl_attributes[:uv_offset_width] = uv_offset_width
gl_attributes[:distancefield] = get_texture!(atlas)
gl_attributes[:visible] = plot.visible
gl_attributes[:depthsorting] = get(plot, :depthsorting, false)
cam = scene.camera
# gl_attributes[:preprojection] = Observable(Mat4f(I))
gl_attributes[:preprojection] = lift(plot, space, markerspace, cam.projectionview, cam.resolution) do s, ms, pv, res
Makie.clip_to_space(cam, ms) * Makie.space_to_clip(cam, s)
end
return draw_scatter(screen, (DISTANCEFIELD, positions), gl_attributes)
end
end
# el32convert doesn't copy for array of Float32
# But we assume that xy_convert copies when we use it
xy_convert(x::AbstractArray{Float32}, n) = copy(x)
xy_convert(x::AbstractArray, n) = el32convert(x)
xy_convert(x, n) = Float32[LinRange(extrema(x)..., n + 1);]
function draw_atomic(screen::Screen, scene::Scene, plot::Heatmap)
return cached_robj!(screen, scene, plot) do gl_attributes
t = Makie.transform_func_obs(plot)
mat = plot[3]
space = plot.space # needs to happen before connect_camera! call
xypos = lift(plot, t, plot[1], plot[2], space) do t, x, y, space
x1d = xy_convert(x, size(mat[], 1))
y1d = xy_convert(y, size(mat[], 2))
# Only if transform doesn't do anything, we can stay linear in 1/2D
if Makie.is_identity_transform(t)
return (x1d, y1d)
else
# If we do any transformation, we have to assume things aren't on the grid anymore
# so x + y need to become matrices.
map!(x1d, x1d) do x
return apply_transform(t, Point(x, 0), space)[1]
end
map!(y1d, y1d) do y
return apply_transform(t, Point(0, y), space)[2]
end
return (x1d, y1d)
end
end
xpos = lift(first, plot, xypos)
ypos = lift(last, plot, xypos)
gl_attributes[:position_x] = Texture(xpos, minfilter = :nearest)
gl_attributes[:position_y] = Texture(ypos, minfilter = :nearest)
# number of planes used to render the heatmap
gl_attributes[:instances] = lift(plot, xpos, ypos) do x, y
(length(x)-1) * (length(y)-1)
end
interp = to_value(pop!(gl_attributes, :interpolate))
interp = interp ? :linear : :nearest
intensity = haskey(gl_attributes, :intensity) ? pop!(gl_attributes, :intensity) : pop!(gl_attributes, :color)
if intensity isa ShaderAbstractions.Sampler
gl_attributes[:intensity] = to_value(intensity)
else
gl_attributes[:intensity] = Texture(el32convert(intensity); minfilter=interp)
end
return draw_heatmap(screen, gl_attributes)
end
end
function draw_atomic(screen::Screen, scene::Scene, plot::Image)
return cached_robj!(screen, scene, plot) do gl_attributes
position = lift(plot, plot[1], plot[2]) do x, y
xmin, xmax = extrema(x)
ymin, ymax = extrema(y)
rect = Rect2f(xmin, ymin, xmax - xmin, ymax - ymin)
return decompose(Point2f, rect)
end
gl_attributes[:vertices] = lift(apply_transform, plot, transform_func_obs(plot), position, plot.space)
rect = Rect2f(0, 0, 1, 1)
gl_attributes[:faces] = decompose(GLTriangleFace, rect)
gl_attributes[:texturecoordinates] = map(decompose_uv(rect)) do uv
return 1.0f0 .- Vec2f(uv[2], uv[1])
end
get!(gl_attributes, :shading, NoShading)
_interp = to_value(pop!(gl_attributes, :interpolate, true))
interp = _interp ? :linear : :nearest
if haskey(gl_attributes, :intensity)
gl_attributes[:image] = Texture(pop!(gl_attributes, :intensity); minfilter=interp)
else
gl_attributes[:image] = Texture(pop!(gl_attributes, :color); minfilter=interp)
end
return draw_mesh(screen, gl_attributes)
end
end
function mesh_inner(screen::Screen, mesh, transfunc, gl_attributes, plot, space=:data)
# signals not supported for shading yet
shading = to_value(gl_attributes[:shading])::Makie.MakieCore.ShadingAlgorithm
matcap_active = !isnothing(to_value(get(gl_attributes, :matcap, nothing)))
color = pop!(gl_attributes, :color)
interp = to_value(pop!(gl_attributes, :interpolate, true))
interp = interp ? :linear : :nearest
if to_value(color) isa Colorant
gl_attributes[:vertex_color] = color
delete!(gl_attributes, :color_map)
delete!(gl_attributes, :color_norm)
elseif to_value(color) isa Makie.AbstractPattern
img = lift(x -> el32convert(Makie.to_image(x)), plot, color)
gl_attributes[:image] = ShaderAbstractions.Sampler(img, x_repeat=:repeat, minfilter=:nearest)
get!(gl_attributes, :fetch_pixel, true)
elseif to_value(color) isa AbstractMatrix{<:Colorant}
gl_attributes[:image] = Texture(lift(el32convert, plot, color), minfilter = interp)
delete!(gl_attributes, :color_map)
delete!(gl_attributes, :color_norm)
elseif to_value(color) isa AbstractMatrix{<: Number}
gl_attributes[:image] = Texture(lift(el32convert, plot, color), minfilter = interp)
gl_attributes[:color] = nothing
elseif to_value(color) isa AbstractVector{<: Union{Number, Colorant}}
gl_attributes[:vertex_color] = lift(el32convert, plot, color)
else
# error("Unsupported color type: $(typeof(to_value(color)))")
end
if haskey(gl_attributes, :intensity)
intensity = pop!(gl_attributes, :intensity)
if intensity[] isa Matrix
gl_attributes[:image] = Texture(intensity, minfilter = interp)
else
gl_attributes[:vertex_color] = intensity
end
gl_attributes[:color] = nothing
end
gl_attributes[:vertices] = lift(transfunc, mesh, space) do t, mesh, space
apply_transform(t, metafree(coordinates(mesh)), space)
end
gl_attributes[:faces] = lift(x-> decompose(GLTriangleFace, x), mesh)
if hasproperty(to_value(mesh), :uv)
gl_attributes[:texturecoordinates] = lift(decompose_uv, mesh)
end
if hasproperty(to_value(mesh), :normals) && (shading !== NoShading || matcap_active)
gl_attributes[:normals] = lift(decompose_normals, mesh)
end
return draw_mesh(screen, gl_attributes)
end
function draw_atomic(screen::Screen, scene::Scene, meshplot::Mesh)
x = cached_robj!(screen, scene, meshplot) do gl_attributes
t = transform_func_obs(meshplot)
space = meshplot.space # needs to happen before connect_camera! call
x = mesh_inner(screen, meshplot[1], t, gl_attributes, meshplot, space)
return x
end
return x
end
function draw_atomic(screen::Screen, scene::Scene, plot::Surface)
robj = cached_robj!(screen, scene, plot) do gl_attributes
color = pop!(gl_attributes, :color)
img = nothing
# We automatically insert x[3] into the color channel, so if it's equal we don't need to do anything
if haskey(gl_attributes, :intensity)
img = pop!(gl_attributes, :intensity)
elseif to_value(color) isa Makie.AbstractPattern
pattern_img = lift(x -> el32convert(Makie.to_image(x)), plot, color)
img = ShaderAbstractions.Sampler(pattern_img, x_repeat=:repeat, minfilter=:nearest)
haskey(gl_attributes, :fetch_pixel) || (gl_attributes[:fetch_pixel] = true)
gl_attributes[:color_map] = nothing
gl_attributes[:color] = nothing
gl_attributes[:color_norm] = nothing
elseif isa(to_value(color), AbstractMatrix{<: Colorant})
img = color
gl_attributes[:color_map] = nothing
gl_attributes[:color] = nothing
gl_attributes[:color_norm] = nothing
end
space = plot.space
gl_attributes[:image] = img
@assert to_value(plot[3]) isa AbstractMatrix
types = map(v -> typeof(to_value(v)), plot[1:2])
if all(T -> T <: Union{AbstractMatrix, AbstractVector}, types)
t = Makie.transform_func_obs(plot)
mat = plot[3]
xypos = lift(plot, t, plot[1], plot[2], space) do t, x, y, space
# Only if transform doesn't do anything, we can stay linear in 1/2D
if Makie.is_identity_transform(t)
return (x, y)
else
matrix = if x isa AbstractMatrix && y isa AbstractMatrix
apply_transform.((t,), Point.(x, y), space)
else
# If we do any transformation, we have to assume things aren't on the grid anymore
# so x + y need to become matrices.
[apply_transform(t, Point(x, y), space) for x in x, y in y]
end
return (first.(matrix), last.(matrix))
end
end
xpos = lift(first, plot, xypos)
ypos = lift(last, plot, xypos)
args = map((xpos, ypos, mat)) do arg
Texture(lift(x-> convert(Array, el32convert(x)), plot, arg); minfilter=:linear)
end
if isnothing(img)
gl_attributes[:image] = args[3]
end
return draw_surface(screen, args, gl_attributes)
else
gl_attributes[:ranges] = to_range.(to_value.(plot[1:2]))
z_data = Texture(lift(el32convert, plot, plot[3]); minfilter=:linear)
if isnothing(img)
gl_attributes[:image] = z_data
end
return draw_surface(screen, z_data, gl_attributes)
end
end
return robj
end
function draw_atomic(screen::Screen, scene::Scene, plot::Volume)
return cached_robj!(screen, scene, plot) do gl_attributes
model = plot.model
x, y, z = plot[1], plot[2], plot[3]
gl_attributes[:model] = lift(plot, model, x, y, z) do m, xyz...
mi = minimum.(xyz)
maxi = maximum.(xyz)
w = maxi .- mi
m2 = Mat4f(
w[1], 0, 0, 0,
0, w[2], 0, 0,
0, 0, w[3], 0,
mi[1], mi[2], mi[3], 1
)
return convert(Mat4f, m) * m2
end
if haskey(gl_attributes, :intensity)
intensity = pop!(gl_attributes, :intensity)
return draw_volume(screen, intensity, gl_attributes)
else
return draw_volume(screen, plot[4], gl_attributes)
end
end
end