# GLMakie: Create 3D animations with GLMakie?

I generate samples from the Vegas algorithm through iterations, from which I draw 3D graphs using GLMakie. The problem is that I want to animate a 3D image but I don’t really understand the tutorial to apply it to my case.
My code:

``````function peakss(x,y, n=49)
xmin=0
xmax=1
ymin=0
ymax=1
x1 = range(xmin, xmax, n)
y1 = range(xmin, xmax, n)
x_min, x_max =0, 1
y_min, y_max = 0, 1

return (x1, y1, count_points_in_grid(shuffle!(x),shuffle!(y) , n,xmin, xmax, ymin, ymax))
end
function count_points_in_grid(x_coords, y_coords,n, x_min=0, x_max=1, y_min=0, y_max=1)
@assert length(x_coords) == length(y_coords)
x_step = (x_max - x_min) / n
y_step = (y_max - y_min) / n
counts = zeros(Int, n, n)
ycop=range(start=y_min, stop=y_max, length=length(y_coords))
xcop=range(start=x_min, stop=x_max, length=length(y_coords))
for k in 1:length(x_coords)
x, y = x_coords[k], y_coords[k]
x1,y2= xcop[k], ycop[k]
i = clamp(floor(Int, (x - x_min) / x_step) + 1, 1, n)
j = clamp(floor(Int, (y - y_min) / y_step) + 1, 1, n)
counts[i, j] += 1
end

return counts
end
function histogram_or_bars_in_3d(x1,y2,n)
x, y, z = peakss(x1,y2,n)
δx = (x[2] - x[1]) / 2
δy = (y[2] - y[1]) / 2
cbarPal = :Spectral_11
ztmp = (z .- minimum(z)) ./ (maximum(z .- minimum(z)))
cmap = get(colorschemes[cbarPal], ztmp)
cmap2 = reshape(cmap, size(z))
ztmp2 = abs.(z) ./ maximum(abs.(z)) .+ 0.15

fig = Figure(size=(3000, 2600), fontsize=30)

ax2 = Axis3(fig[1, 1]; aspect=(1,1,1), perspectiveness=0.5)
rectMesh = Rect3f(Vec3f(-0.5, -0.5, 0), Vec3f(1, 1, 1))

meshscatter!(ax2
, x, y, 0 * z; marker=rectMesh, color=:cyan,
markersize=Vec3f.(2δx, 2δy, z[:]), colormap=(:Spectral_11, 0.25),
#viên
for (idx, i) in enumerate(x), (idy, j) in enumerate(y)
rectMesh=Rect3f(Vec3f(i-δx, j-δy, 0), Vec3f(2δx, 2δy, z[idx,idy]))
recmesh=GeometryBasics.mesh(rectMesh)
lines!(ax2, recmesh; color=(cmap2[idx, idy], ztmp2[idx, idy]))
end
fig
end

f(x)=x[2]^2/((x[1]-0.5)^2+10^-10)+x[1]^2/((x[2]-0.5)^2+10^-10)
plt=[]
for i=1:7
global v = vegas(f, zeros(2), fill(1.0, 2),ncalls = 100000,nbins=1000, maxiter =i)
global n = 100  # number of sample
n=25
global A=histogram_or_bars_in_3d((sample1),(sample2),n)
display(A)
push!(plt,A)
end

for i in 1:length(plt)
save(string(i)*".png",plt[i])
end

``````

Output images:

Can you help me code the animation?

Here’s an example how to do a meshscatter barplot animation

``````using GLMakie

sz = (20, 20)
t = Observable(0.0)
sizes = lift(t) do t
[Vec3f(1, 1, 1 + sin(x/3 + t) * cos(y/3 + t)) for x in 1:sz[1] for y in 1:sz[2]]
end
positions = [(x, y) for x in 1:sz[1] for y in 1:sz[2]]
f, ax, ms = meshscatter(positions, markersize = sizes, marker = Rect3f((-0.5, -0.5, 0), (1, 1, 1)); axis = (; type = Axis3))

record(f, "meshscatter.mp4", 0:1/30:5) do _t
t[] = _t
end
``````

2 Likes

Thanks for the example, for my case I don’t know how to use Observable. Luckily I still get the expected result.