title: rgl: drawing a cube with colored faces, vertex points and lines
body: <p>To demonstrate the effect of linear transformations in 3D, <code>x -&gt; A x</code>, I want to draw a cube and show its transformation under <code>A</code>.  For this, I need to color each face separately, and also show the vertex points and the lines that outline each face.</p>
  
  <p>I can't figure out how to use distinct colors for the faces, and how to make this more general so I don't have to repeat all the steps for the result under the transformation.</p>
  
  <p>what I tried:</p>
  
  <pre><code>library(rgl)
  c3d &lt;- cube3d(color=rainbow(6), alpha=0.5)
  open3d()
  shade3d(c3d)
  points3d(t(c3d$vb), size=5)
  for (i in 1:6)
      lines3d(t(c3d$vb)[c3d$ib[,i],])
  </code></pre>
  
  <p>This gives the image below.  But I don't understand how the faces are colored. And, I seem to have to use <code>points3d</code> and <code>lines3d</code> on the components of the <code>c3d</code> shape, and don't have a single object I can transform.</p>
  
  <p><a href="https://i.stack.imgur.com/NFXdV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NFXdV.png" alt="enter image description here"></a></p>
  
  <p>A particular transformation is given by the matrix <code>A</code> below, and here is how I add that to the scene,</p>
  
  <pre><code>A &lt;- matrix(c( 1, 0, 1, 0, 2, 0,  1, 0, 2), 3, 3)
  c3d_trans &lt;- transform3d(c3d, A) 
  shade3d( c3d_trans )
  points3d(t(c3d_trans$vb), size=5)
  </code></pre>
  
  <p>This gives:</p>
  
  <p><a href="https://i.stack.imgur.com/Q6hq3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q6hq3.png" alt="enter image description here"></a></p>
  
  <p>Is there some way to simplify this and make it more generally useful?</p>