8

I want to draw a cuboid in another transparent cuboid with TikZ. Therefore I've created a \newcommand to draw a cuboid.

\documentclass[11pt, halfparskip]{scrartcl}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}

\begin{document}
\newcommand{\cuboid}[7]{
    % bottom
    \draw[#7] (#1, #2, #3) -- ++(#4, 0, 0) -- ++(0, 0, #6) -- ++(-#4, 0, 0) -- cycle;
    % back
    \draw[#7] (#1, #2, #3) -- ++(#4, 0, 0) -- ++(0, #5, 0) -- ++(-#4, 0, 0) -- cycle;
    % left
    \draw[#7] (#1, #2, #3) -- ++(0, 0, #6) -- ++(0, #5, 0) -- ++(0, 0, -#6) -- cycle;
    % right
    \draw[#7] (#1+#4, #2, #3) -- ++(0, 0, #6) -- ++(0, #5, 0) -- ++(0, 0, -#6) -- cycle;
    % front
    \draw[#7] (#1, #2, #3+#6) -- ++(#4, 0, 0) -- ++(0, #5, 0) -- ++(-#4, 0, 0) -- cycle;
    % top
    \draw[#7] (#1, #2+#5, #3) -- ++(#4, 0, 0) -- ++(0, 0, #6) -- ++(-#4, 0, 0) -- cycle;
}
\begin{tikzpicture}


\tikzstyle{bottomfill} = [thick, fill=lightgray, fill opacity=.5]
\tikzstyle{camerafill} = [thick, fill=blue!20]

\cuboid{0}{0}{0}{4}{4}{4}{bottomfill}
\cuboid{1}{1}{1}{1}{1}{1}{camerafill}

\draw[->, thick] (2, 1.5, 1.5) -- ++(4, 0, 0);

\end{tikzpicture}
\end{document}

But the result looks strange (The inner cuboid seems to be outside the transparent cuboid; compare the cuboid and and the arrow of the second image)

enter image description here

The result looks right if I'm adding the planes in the right z-order (but that's exhausting)

\documentclass[11pt, halfparskip]{scrartcl}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\tikzstyle{bottomfill} = [thick, fill=lightgray, fill opacity=.5]
\tikzstyle{camerafill} = [thick, fill=blue!20]

% 1 bottom
\draw[bottomfill] (0, 0, 0) -- ++(4, 0, 0) -- ++(0, 0, 4) -- ++(-4, 0, 0) -- cycle;
% 1 back
\draw[bottomfill] (0, 0, 0) -- ++(4, 0, 0) -- ++(0, 4, 0) -- ++(-4, 0, 0) -- cycle;
% 1 left
\draw[bottomfill] (0, 0, 0) -- ++(0, 0, 4) -- ++(0, 4, 0) -- ++(0, 0, -4) -- cycle;

% 2 bottom
\draw[camerafill] (1, 1, 1) -- ++(1, 0, 0) -- ++(0, 0, 1) -- ++(-1, 0, 0) -- cycle;
% 2 back
\draw[camerafill] (1, 1, 1) -- ++(1, 0, 0) -- ++(0, 1, 0) -- ++(-1, 0, 0) -- cycle;
% 2 left
\draw[camerafill] (1, 1, 1) -- ++(0, 0, 1) -- ++(0, 1, 0) -- ++(0, 0, -1) -- cycle;
% 2 right
\draw[camerafill] (1+1, 1, 1) -- ++(0, 0, 1) -- ++(0, 1, 0) -- ++(0, 0, -1) -- cycle;
% 2 front
\draw[camerafill] (1, 1, 1+1) -- ++(1, 0, 0) -- ++(0, 1, 0) -- ++(-1, 0, 0) -- cycle;
% 2 top
\draw[camerafill] (1, 1+1, 1) -- ++(1, 0, 0) -- ++(0, 0, 1) -- ++(-1, 0, 0) -- cycle;

\draw[thick] (2, 1.5, 1.5) -- ++(2, 0, 0);

% 1 right
\draw[bottomfill] (0+4, 0, 0) -- ++(0, 0, 4) -- ++(0, 4, 0) -- ++(0, 0, -4) -- cycle;
% 1 front
\draw[bottomfill] (0, 0, 0+4) -- ++(4, 0, 0) -- ++(0, 4, 0) -- ++(-4, 0, 0) -- cycle;
% 1 top
\draw[bottomfill] (0, 0+4, 0) -- ++(4, 0, 0) -- ++(0, 0, 4) -- ++(-4, 0, 0) -- cycle;

\draw[->, thick] (4, 1.5, 1.5) -- ++(2, 0, 0);
\end{tikzpicture}
\end{document}

With the result

enter image description here

My question: is there a possibility in TikZ to draw an object in a transparent object with without adding the shapes in the right z-order?

8

What about an Asymptote solution?

\documentclass{standalone}
\usepackage{asymptote}

\begin{document}

\begin{asy}[width=8cm,height=8cm]
import three;

currentprojection=perspective(3,6.5,5);
currentlight=(3,7,8);

surface cube1 = scale3(1)*shift((-0.5,-0.5,-0.5))*unitcube;
surface cube2 = scale3(2)*shift((-0.5,-0.5,-0.5))*unitcube;

draw(cube1,lightgrey);
draw(cube2,lightblue+opacity(0.2));

\end{asy}
\end{document}

Compile with pdflatex file.tex, asy file-1.asy and a few more pdflatex.

enter image description here

| improve this answer | |
  • Super, that's what I was looking for! Asymptote seems to have better 3d-capabilities than TikZ. There are also nice 3d-examples for TikZ on texample.net but they often use some tricks. – ClemensK Mar 6 '13 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.