How to get a Semi transparent sphere in Makie?

A few years back I asked a question, that I could not fully resolve. At JuliaCon I think I heard it is now possible, so I’ll give it a new try. If I do something like (e.g. in a Pluto cell)

begin
using WGLMakie, Colors, Manifolds #versions 0.10.5, 0.12.11, 0.9.20
# A sphere
n = 45
u = range(0,stop=2*π,length=n);
v = range(0,stop=π,length=n);
sx = zeros(n,n); sy = zeros(n,n); sz = zeros(n,n)
for i in 1:n
    for j in 1:n
        sx[i,j] = cos.(u[i]) * sin(v[j]);
        sy[i,j] = sin.(u[i]) * sin(v[j]);
        sz[i,j] = cos(v[j]);
    end
end
fig, ax, plt = surface(
  sx,sy,sz,
  color = fill(RGBA(1.,1.,1.,0.3), n, n),
  shading = Makie.automatic
)
ax.show_axis = false
wireframe!(ax, sx, sy, sz, color = RGBA(0.5,0.5,0.7,0.3))
# Scatter some points
    π1(x) = x[1]
    π2(x) = x[2]
    π3(x) = x[3]
	M = Manifolds.Sphere(2)
	pts = [rand(M) for _ in 1:300]
	scatter!(ax, π1.(pts), π2.(pts), π3.(pts); markersize=4, color=:blue)
	fig
end

I get an image like

What I would love to have is actual transparency, From the 300 points some are also on the back of the sphere – and just seeing them shining through would be great. If they were for example a curve, one could easily see which part is upfront, which behind.

Is that doable in Makie? I already do use transparency in the color, that does not seem to do the job here.

You can pass alpha = x and transparency = true when you call surface:

fig, ax, plt = surface(sx,sy,sz,color = fill(:red, n, n),invert_normals=true,alpha=0.5,transparency=true)
meshscatter!(ax, sx[1:10:end], sy[1:10:end], sz[1:10:end], color = :blue)
fig

or just add transparency=true if you pass the alpha value to the colour:

fig, ax, plt = surface(
  sx,sy,sz,
  color = fill((:red,0.5), n, n),invert_normals=true,transparency=true)
meshscatter!(ax, sx[1:10:end], sy[1:10:end], sz[1:10:end], color = :blue)
fig

image

1 Like

Thanks, that did indeed work as soon as I applied it to both the mesh and the surface.