To demonstrate the effect of linear transformations in 3D,
x -> A x
c3d <- cube3d(color=rainbow(6), alpha=0.5)
for (i in 1:6)
A <- matrix(c( 1, 0, 1, 0, 2, 0, 1, 0, 2), 3, 3)
c3d_trans <- transform3d(c3d, A)
shade3d( c3d_trans )
rgl, when drawing primitive shapes, you apply colours to vertices, not faces. The faces are coloured by interpolating the colors at the vertices.
cube3d() is not a primitive shape, it's a "mesh". It is drawn as 6 separate quadrilaterals. Each vertex is used 3 times.
It's not really documented, but the order the colours are used is that the first 4 are used for one face, then the next 4 for the next face, etc. If you want your colours to be
rainbow(6), you need to replicate each colour 4 times:
library(rgl) c3d <- cube3d(color=rep(rainbow(6), each = 4), alpha = 0.5) open3d() shade3d(c3d) points3d(t(c3d$vb), size = 5) for (i in 1:6) lines3d(t(c3d$vb)[c3d$ib[,i],])
I'd recommend a higher
alpha value; I find the transparency a little confusing at
alpha = 0.5.
By the way, for the same purpose, I generally use a shape that looks more spherical as the baseline; I think it gives better intuition about the transformation. Here's code I have used:
sphere <- subdivision3d(cube3d(color=rep(rainbow(6),rep(4*4^4,6)), alpha=0.9), depth=4) sphere$vb[4,] <- apply(sphere$vb[1:3,], 2, function(x) sqrt(sum(x^2))) open3d() shade3d(sphere)
and this gives this shape:
which transforms to this:
A <- matrix(c( 1, 0, 1, 0, 2, 0, 1, 0, 2), 3, 3) trans <- transform3d(sphere, A) open3d() shade3d(trans)
Of course, it all looks better if you can rotate it.