# 3D scenes in GLMakie Glitch out and Slow down after few seconds

**URL:** <https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110>\
**Category:** Modelling & Simulations\
**Tags:** plotting\
**Created:** [May 10, 2024, 2:25pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110 "2024-05-10T14:25:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maddy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maddy/32/210658_2.png) [@Maddy](https://discourse.julialang.org/u/Maddy)\
**Post date:** [May 10, 2024, 2:25pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110/1 "2024-05-10T14:25:33Z")

</div>

I was recently playing around with GLMakie Scenes using the cam3d! camera which works pretty much like plotly() backend on Plots.  
I was impressed by the zoom in-out controls. However, after a few seconds of scrolling and panning, the controls slowed down to a crawl until it all came to a hault. Further, scrolling seemed to have caused some weird objects to appear.

```julia

using GLMakie

molecule = cumsum(randn(Point3f, 500))

s = Scene(; camera = cam3d!)

meshscatter!(s, molecule, markersize = rand(0.6:0.1:1.5, 500), color = rand(500))

center!(s)

```

here’s the code snippet

---

<div class="post-metadata">

**Author:** ![Maddy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maddy/32/210658_2.png) [@Maddy](https://discourse.julialang.org/u/Maddy)\
**Post date:** [May 10, 2024, 2:31pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110/2 "2024-05-10T14:31:13Z")

</div>

A few more questions that I have about GLMakie… How on earth do I create:

1. A Unit Circle? The most computationally light way to do it. Is it scatter(, marker=Circle)? Is there any other, better way to define the circle parameters which then I can add to the scene/plot as a normal object?
2. A Unit Sphere? Basically, the same question. I belive meshscatter! by default creates a sphere but I don’t know about the dimensions. Playing around with it, it seems like it’s not a unit sphere. How do I create a Custom mesh and plot/add it to scene as an object?

Also, how do coordinates in the camera=cam2d and cam3d work? campixel makes sense, but I seem to not understand what is the scaling in cam2d.

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [May 10, 2024, 3:25pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110/3 "2024-05-10T15:25:28Z")

</div>

Circle and Sphere

```Julia
using GLMakie

fig= Figure()
ax = LScene(fig[1,1])
lines!(Circle(Point2f(0,0),1); linewidth=5, transparency=true) # origin, radius
mesh!(Sphere(Point3f(0,0,0.5), 0.75); color = :red) # origin, radius
fig

```

 ![Screenshot 2024-05-10 at 17.25.11](https://global.discourse-cdn.com/julialang/original/3X/5/7/57a84e5bc35f165627a8c586f3487e25b87e5d43.png)

---

<div class="post-metadata">

**Author:** ![Maddy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maddy/32/210658_2.png) [@Maddy](https://discourse.julialang.org/u/Maddy)\
**Post date:** [May 10, 2024, 4:14pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110/4 "2024-05-10T16:14:13Z")

</div>

Used it in a scene with cam3D!.. Seems to have a lower resolution sphere as compared to meshscatter!

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/8/e8527e553509d63a7711dd72186db7ef9ae1a0b2.png)

Although not what I wanted to do, this explains why its a low res and not a perfect sphere… that’s what is used as the mesh skeleton. Any way to change this?

And is cam3d even reliable for prolonged simulations?

Or any way to tweak the radius parameter of meshscatter? (markersize is not in completely perfect correspondence to radius)

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [May 10, 2024, 4:28pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110/5 "2024-05-10T16:28:04Z")

</div>

> And is cam3d even reliable for prolonged simulations?

Yeah it should be, this must be some bug.  
I can’t reproduce it, do you have any more pointer on how to reproduce the problem?

> (markersize is not in completely perfect correspondence to radius)

It doesn’t? In what way?

> Seems to have a lower resolution sphere as compared to meshscatter!

You can use `Tesselation` for higher res spheres:

```julia
using GLMakie, GeometryBasics

molecule = cumsum(randn(Point3f, 500))

s = Scene(; camera = cam3d!)
# Use Tesselation for for a smoother sphere:
sphere = normal_mesh(Tesselation(Sphere(Point3f(0), 1), 100))
meshscatter!(s, molecule, marker=sphere, markersize=rand(0.6:0.1:1.5, 500), color=rand(500))

center!(s)

```

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [May 10, 2024, 4:40pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110/6 "2024-05-10T16:40:44Z")

</div>

Ah do you mean scrolling into one direction continuously?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/c/7c03f5b665c1f9931d6e9c27ae63dff6f2bedf63.jpeg)  
This is not a bug, just not perfect behavior.  
The camera has a lookat position and eye position, and when zooming you move the camera closer to lookat. The closer you get the slower it will move, since it moves by a fraction of the distance between those.  
This will make the distance between lookat and eyeposition super small:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/5/c590d40340972e4e3d16bfd9a806a3448d2ff2f4.png)  
Which I think will at some point mess up the projection, creating those artifacts.

---

<div class="post-metadata">

**Author:** ![Maddy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maddy/32/210658_2.png) [@Maddy](https://discourse.julialang.org/u/Maddy)\
**Post date:** [May 10, 2024, 5:00pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110/7 "2024-05-10T17:00:24Z")

</div>

> [@sdanisch](#):
>
> I can’t reproduce it

```julia

using GLMakie, GeometryBasics

scene = Scene(camera=cam3d!)
points = Vector{Point2f}()
for i in 0:100
    for j in 0:100
        push!(points, 10*Point2f(-i, j))
    end
end
dots = meshscatter!(scene, points, markersize = 10, color=:red)
display(scene)

```

Zoom out of the sphere to get a sense of what the entire grid of spheres is like.  
Try zooming in to the centre of the grid. Tried it countless times, every time it just comes to a crawl! And then the entire thing explodes into some weird behaviour.

> [@sdanisch](#):
>
> It doesn’t? In what way?

In terms of the number of vertices.

> [@sdanisch](#):
>
> You can use `Tesselation` for higher res spheres:

Thank you! I didn’t know that… I found a more native way to do this too btw…

```julia

using GeometryBasics, LinearAlgebra, GLMakie

# Create vertices for a Sphere
r = 0.5f0
n = 30
θ = LinRange(0, pi, n)
φ2 = LinRange(0, 2pi, 2 * n)
x2 = [r * cos(φv) * sin(θv) for θv in θ, φv in φ2]
y2 = [r * sin(φv) * sin(θv) for θv in θ, φv in φ2]
z2 = [r * cos(θv) for θv in θ, φv in 2φ2]
points = vec([Point3f((xv, yv, zv)) for (xv, yv, zv) in zip(x2, y2, z2)])

# The coordinates form a matrix, so to connect neighboring vertices with a face
# we can just use the faces of a rectangle with the same dimension as the matrix:
faces = decompose(QuadFace{GLIndex}, Tesselation(Rect(0, 0, 1, 1), size(z2)))
# Normals of a centered sphere are easy, they're just the vertices normalized.
normals = normalize.(points)
gb_mesh = GeometryBasics.Mesh(meta(points; normals), faces)

scene = Scene(camera=cam3d!)
mesh!(scene, gb_mesh, color=rand(100,100), colormap=:blues)
display(scene)

```

source: [mesh · Makie](https://docs.makie.org/stable/reference/plots/mesh/#using_geometrybasicsmesh_and_buffersampler_type)

---

<div class="post-metadata">

**Author:** ![Maddy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maddy/32/210658_2.png) [@Maddy](https://discourse.julialang.org/u/Maddy)\
**Post date:** [May 10, 2024, 5:06pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110/8 "2024-05-10T17:06:55Z")

</div>

btw… do you know any way to have the radius of the meshscatter!() known? I want to make very precise spheres. Say of a unit radius. But keeping 2 meshscatter! spheres at a unit distance don’t Perfectly coincide.

It used to be the same for marker=circle. marker=Circle was precisely a unit circle.

Unfortunately, meshscatter!(marker=Sphere) isn’t a thing tho xD.

---

<div class="post-metadata">

**Author:** ![Maddy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maddy/32/210658_2.png) [@Maddy](https://discourse.julialang.org/u/Maddy)\
**Post date:** [May 10, 2024, 5:08pm UTC](https://discourse.julialang.org/t/3d-scenes-in-glmakie-glitch-out-and-slow-down-after-few-seconds/114110/9 "2024-05-10T17:08:58Z")

</div>

Ahhhh I see! So just avoid making stuff so small and/or zooming to close to objects. (Tbh felt like this is close to what Desmos does - an infinite zoom hehe… but its all right. This is good enough!)
