R 透明 planes3d 不应隐藏 plot3d points/spheres

R transparent planes3d should not hide plot3d points/spheres

我想将 points/spheres 和一架飞机放在一个 3d 图中。我希望飞机的 alpha 透明度为 ~0.5(它既不应该完全透明也不应该完全不透明)。这样我就可以看到 points/spheres 和穿过平面的轴。

我试过了:

library(rgl)

#Generating points:
m=20
a1=runif(m,-1,1)
a2=runif(m,-1,1)
b=a1+2*a2+rnorm(m,mean=0,sd=0.3)

# Plotting the points:
plot3d(a1,a2,b, type='s', xlim = c(-1, 1), ylim = c(-1, 1), zlim = c(-3.1, 3.1),xlab = 'a_i,1', ylab = 'a_i,2', zlab = 'b_i',alpha=0.9)
# Plotting the transparent plane:
planes3d(1, 2, -1, 0, col = 'red', alpha = 0.1, add=T)
# plot the points again (because I thought, maybe the the execution order could be relevant)
plot3d(a1,a2,b, add=T, type='s', xlim = c(-1, 1), ylim = c(-1, 1), zlim = c(-3.1, 3.1),xlab = 'a_i,1', ylab = 'a_i,2', zlab = 'b_i',alpha=0.9)

我得到的结果是我可以看到穿过平面的轴,但是我看不到隐藏在平面后面的 points/spheres :(

部分球体无法通过平面看到:

我也希望通过平面看到 points/spheres(就像它对轴非常有效,可以通过平面看到)。 我想看到所有 20 个点/球体,还有那些被平面覆盖/隐藏/隐藏/掩盖的点。

您正在使用 "data oriented routines",它可以快速渲染,但会扭曲轴,因为数据点通常在几何上彼此不相关。可能它是为了速度而剪裁和渲染并忽略 alpha 缓冲区,以便能够快速绘制很多点。

如果你使用不同的渲染技术你可以得到这个,但它当然要慢很多。并且它尊重坐标之间的纵横比。

library(rgl)

sphere3d <- function(cen, r=1,n = 65, ...){
  f <- function(s,t){ 
    cbind(   r * cos(t)*cos(s) + cen[1],
             r *        sin(s) + cen[2],
             r * sin(t)*cos(s) + cen[3])
  }
  persp3d(f, slim = c(-pi/2,pi/2), tlim = c(0, 2*pi), n = n, add = T,axes=T,...)
}

m=20
xx=runif(m,-1,1)
yy=runif(m,-1,1)
zz=xx+2*yy+rnorm(m,mean=0,sd=0.3)

# Plotting the points:
for (i in 1:m){
  cen <- c(xx[i],yy[i],zz[i])
  sphere3d(cen,col="black",r=0.15,n=17)
}

# add corner points to make the bounding box span the space
sphere3d(c(-1,-1,-3),col="black",r=0.15,n=17)
sphere3d(c( 1, 1, 3),col="black",r=0.15,n=17)

# Plotting the transparent plane:
planes3d(1, 2, -1, 0, col = 'red', alpha = 0.5)

# no axes by default
axes3d( edges="bbox",box=T )

产生这个 - 但请注意它也不是完美的 - 当你在球体上设置透明度时也会以类似的方式搞砸。一般来说,你只能通过光线追踪相关的东西完全正确地做多层透明,这非常慢:

这是另一个视图 head-on,alpha 设置为 0.8。

我认为这是一个错误,但有一个解决方法。当你绘制平面时,明确地说你不想要深度掩蔽,即使用

planes3d(1, 2, -1, 0, col = 'red', alpha = 0.1, depth_mask = FALSE)

编辑添加:

确实有两个错误。上面的行修复了一个。另一个错误是平面在球体之前绘制,因为它不像球体那样被 xlimylimzlim 值裁剪。 (如果它们都是透明的,这是不可避免的,但如果球体是实心的,则应首先绘制它们。)您可以使用另一种解决方法强制其在第二个绘制。在 plot3d() 调用之后,运行 以下内容:

subs <- subsceneInfo()
useSubscene3d(subs$children)
planes3d(1, 2, -1, 0, col = 'red', alpha = 0.1, depth_mask = FALSE)
useSubscene3d(subs$id)

或者,如果限制不重要,则将其排除在外,无需任何其他解决方法,事情就会正常进行。