透视中的 3D 盒子

透视中的 3D 盒子

我正在尝试绘制透视的 3D 盒子,但没有成功。目的是将几个不同大小的盒子叠放在一起,如下所示:

在此处输入图片描述

这是我的 MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{perspective}

\begin{document}

\newcommand\simplecuboid[5]{%
    \draw[fill=#5!80!white]
        (tpp cs:x=0,y=0,z=#3+#4)
            -- (tpp cs:x=0,y=#2,z=#3+#4)
            -- (tpp cs:x=#1,y=#2,z=#3+#4)
            -- (tpp cs:x=#1,y=0,z=#3+#4) -- cycle;
    \draw[fill=gray]
        (tpp cs:x=0,y=0,z=#4)
            -- (tpp cs:x=0,y=0,z=#3+#4)
            -- (tpp cs:x=0,y=#2,z=#3+#4)
            -- (tpp cs:x=0,y=#2,z=#4) -- cycle;
    \draw[fill=gray!50!white]
        (tpp cs:x=0,y=0,z=#4)
            -- (tpp cs:x=0,y=0,z=#3+#4)
            -- (tpp cs:x=#1,y=0,z=#3+#4)
            -- (tpp cs:x=#1,y=0,z=#4) -- cycle;
    }

\begin{tikzpicture}[3d view, perspective={p={(20,15,0)},q={(0,20,0)}}]
    \simplecuboid{6}{12}{.2}{0}{gray}
    \simplecuboid{6}{12}{.2}{.2}{white}
    \simplecuboid{6}{10}{.2}{.4}{red}
\end{tikzpicture}

\end{document}

有人可以帮忙吗?

答案1

[opacity=.xx]想法来自于符号 1对该问题的评论这里。我修改了一些数字。不确定为什么只需要顶部框的一层为红色。我只是保留了你的想法,如下所示。希望它有所帮助。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{perspective}

\begin{document}

\newcommand\simplecuboid[5]{%
    \draw[fill=#5!80!white,opacity=0.5]
        (tpp cs:x=0,y=0,z=#3+#4)
            -- (tpp cs:x=0,y=#2,z=#3+#4)
            -- (tpp cs:x=#1,y=#2,z=#3+#4)
            -- (tpp cs:x=#1,y=0,z=#3+#4) -- cycle;
    \draw[fill=gray,opacity=0.3]
        (tpp cs:x=0,y=0,z=#4)
            -- (tpp cs:x=0,y=0,z=#3+#4)
            -- (tpp cs:x=0,y=#2,z=#3+#4)
            -- (tpp cs:x=0,y=#2,z=#4) -- cycle;
    \draw[fill=gray!50!white,opacity=0.7]
        (tpp cs:x=0,y=0,z=#4)
            -- (tpp cs:x=0,y=0,z=#3+#4)
            -- (tpp cs:x=#1,y=0,z=#3+#4)
            -- (tpp cs:x=#1,y=0,z=#4) -- cycle;
    }

\begin{tikzpicture}[3d view, perspective={p={(20,15,0)},q={(0,20,0)}}]
    \simplecuboid{6}{12}{.2}{0}{gray}
    \simplecuboid{6}{12}{.2}{.2}{white}
    \simplecuboid{6}{10}{.2}{.4}{red!80!white}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

我得到了它:

编辑:使用所建议的line join=round参数(非常感谢!)来改善角落。\draw@SandyG

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{perspective}

\begin{document}

\newcommand\simplecuboid[6]{% x = #1, y = #2, z = #3, h = #4, color = #5, fill opacity = #6
    %--> Bottom
    \draw[thin, fill=#5, fill opacity=#6, line join=round]
        (tpp cs:x=0,y=0,z=#4)
            % --(tpp cs:x=0,y=#2,z=#4)
            % --(tpp cs:x=#1,y=#2,z=#4)
            % --(tpp cs:x=#1,y=0,z=#4)
            % --cycle
        ;
    %--> Left
    \draw[thin, fill=#5!60!white, fill opacity=#6, line join=round]
        (tpp cs:x=0,y=0,z=#4)
            -- (tpp cs:x=0,y=0,z=#3+#4)
            -- (tpp cs:x=0,y=#2,z=#3+#4)
            -- (tpp cs:x=0,y=#2,z=#4)
            --cycle
            ;
    %--> Right
    \draw[thin, fill=#5!60!white, fill opacity=#6, line join=round]
        (tpp cs:x=#1,y=0,z=#4)
            --(tpp cs:x=#1,y=#2,z=#4)
            --(tpp cs:x=#1,y=#2,z=#3+#4)
            --(tpp cs:x=#1,y=0,z=#3+#4)
            --cycle
        ;
    %--> Back
    \draw[thin, fill=#5!45!white, fill opacity=#6, line join=round]
        (tpp cs:x=0,y=0,z=#4)
            -- (tpp cs:x=0,y=0,z=#3+#4)
            -- (tpp cs:x=#1,y=0,z=#3+#4)
            -- (tpp cs:x=#1,y=0,z=#4)
            --cycle
            ;
    %--> Front
    \draw[thin, fill=#5!45!white, fill opacity=#6, line join=round]
        (tpp cs:x=0,y=#2,z=#4)
            --(tpp cs:x=#1,y=#2,z=#4)
            --(tpp cs:x=#1,y=#2,z=#3+#4)
            --(tpp cs:x=0,y=#2,z=#3+#4)
            --cycle
        ;
    %--> Top
    \draw[thin, fill=#5!30!white, fill opacity=#6, line join=round]
        (tpp cs:x=0,y=0,z=#3+#4)
            -- (tpp cs:x=0,y=#2,z=#3+#4)
            -- (tpp cs:x=#1,y=#2,z=#3+#4)
            -- (tpp cs:x=#1,y=0,z=#3+#4)
            --cycle
            ;
    }

\begin{tikzpicture}[3d view={140}{12}, perspective={p={(-30,0,0)},q={(0,-30,0)}}, scale=0.4]
    \simplecuboid{6}{12.0}{.2}{0.0}{black}{1}
    \simplecuboid{6}{11.5}{.2}{0.2}{white}{.8}
    \simplecuboid{6}{11.0}{.2}{0.4}{red}{.8}
    \simplecuboid{6}{11.0}{.2}{0.6}{red}{.8}
    \simplecuboid{6}{10.5}{.2}{0.8}{white}{.8}
    \simplecuboid{6}{10.0}{.2}{1.0}{red}{.8}
    \simplecuboid{6}{10.0}{.2}{1.2}{red}{.8}
    \simplecuboid{6}{09.5}{.2}{1.4}{white}{.8}
    \simplecuboid{6}{09.0}{.2}{1.6}{blue}{.8}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容