[Tex/LaTex] Drawing 3D lattice using TikZ

tikz-pgf

I need to draw 3D grid with nodes on it using Tikz. Moreover, would it be possible to construct it simply by 3 nested loops? So that I could mark some node by hollow circle and some with solid circle… The viewpoint is also important for visual clarity.

The linked duplicate have a lot of new command and setting a lot of macros. It should not be that complicated. I want to do it with basic instruction set so that code would remain easy to understand; and easy to recall…

The other difference is that only selected nodes will be marked with solid circle, not all the nodes. The above linked answer does not help in this regard either…

enter image description here

Best Answer

As suggested by @cfr in the comments section, tikz-3dplot is the way to go when it comes to 3D. A suggested solution by tikz-3dplot is

enter image description here

The code is

\documentclass[border={10}]{standalone}
\usepackage{tikz}  
\usepackage{tikz-3dplot} 


\tdplotsetmaincoords{70}{120} % set viewpoint 
\tdplotsetrotatedcoords{0}{0}{0} %<- rotate around (z,y,z)
\begin{document}

\begin{tikzpicture}[scale=3,tdplot_rotated_coords,
                    rotated axis/.style={->,purple,ultra thick},
                    blackBall/.style={ball color = black!80},
                    borderBall/.style={ball color = white,opacity=.25}, 
                    very thick]

\foreach \x in {0,1,2}
   \foreach \y in {0,1,2}
      \foreach \z in {0,1,2}{
           %#####################################################
           \ifthenelse{  \lengthtest{\x pt < 2pt}  }{
             \draw (\x,\y,\z) -- (\x+1,\y,\z);
             \shade[rotated axis,blackBall] (\x,\y,\z) circle (0.025cm); 
           }{}
           %#####################################################
           \ifthenelse{  \lengthtest{\y pt < 2pt}  }{
               \draw (\x,\y,\z) -- (\x,\y+1,\z);
               \shade[rotated axis,blackBall] (\x,\y,\z) circle (0.025cm);
           }{}
           %#####################################################
           \ifthenelse{  \lengthtest{\z pt < 2pt}  }{
               \draw (\x,\y,\z) -- (\x,\y,\z+1);
               \shade[rotated axis,blackBall] (\x,\y,\z) circle (0.025cm);
           }{}

}

\shade[rotated axis,blackBall] (1,0,1) circle (0.05cm); 
\shade[rotated axis,blackBall] (1,2,1) circle (0.05cm); 
\shade[rotated axis,blackBall] (0,1,1) circle (0.05cm); 
\shade[rotated axis,blackBall] (2,1,1) circle (0.05cm);
\shade[rotated axis,blackBall] (1,1,2) circle (0.05cm);
\shade[rotated axis,blackBall] (1,1,0) circle (0.05cm);

\shade[rotated axis,borderBall] (1,0,1) circle (0.1cm);
\shade[rotated axis,borderBall] (1,2,1) circle (0.1cm);
\shade[rotated axis,borderBall] (0,1,1) circle (0.1cm);
\shade[rotated axis,borderBall] (2,1,1) circle (0.1cm);
\shade[rotated axis,borderBall] (1,1,2) circle (0.1cm);
\shade[rotated axis,borderBall] (1,1,0) circle (0.1cm); 
\draw (1.1,1.15,1.05) node[scale=1.2, below] {\textbf{p}};   
\end{tikzpicture}
\end{document}