Tikz:使用 png 影像紋理立方體

Tikz:使用 png 影像紋理立方體

我想使用 Tikz 繪製一個立方體,立方體的某些面具有紋理(紋理將是 PNG 圖像)

我目前的程式碼如下:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{3d,calc}
\tikzset{persp/.style={scale=3.0,x={(-0.8cm,-0.4cm)},y={(0.8cm,-0.4cm)}, z={(0cm,1cm)}},points/.style={fill=white,draw=black,thick}}

\begin{document}
\begin{tikzpicture}[persp]\def\i{-15}
    \pgfmathparse{cos(\i)}\let\ci\pgfmathresult
    \pgfmathparse{sin(\i)}\let\si\pgfmathresult 
    \coordinate (Ocube) at (0,0,0);
    \coordinate (Xcube) at (\ci,\si,0);
    \coordinate (Ycube) at (-\si,\ci,0);
    \coordinate (Zcube) at (0,0,1);
    \coordinate (C0) at ($(Ocube)-(Xcube)-(Ycube)-(Zcube)$);
    \coordinate (C1) at ($(Ocube)+(Xcube)-(Ycube)-(Zcube)$);
    \coordinate (C2) at ($(Ocube)-(Xcube)+(Ycube)-(Zcube)$);
    \coordinate (C3) at ($(Ocube)+(Xcube)+(Ycube)-(Zcube)$);
    \coordinate (C4) at ($(Ocube)-(Xcube)-(Ycube)+(Zcube)$);
    \coordinate (C5) at ($(Ocube)+(Xcube)-(Ycube)+(Zcube)$);
    \coordinate (C6) at ($(Ocube)-(Xcube)+(Ycube)+(Zcube)$);
    \coordinate (C7) at ($(Ocube)+(Xcube)+(Ycube)+(Zcube)$);
    \fill[black!20, draw=black, dashed,opacity=0.3] (C0)--(C1)--(C3)--(C2)--cycle;
    \fill[black!20, draw=black, dashed,opacity=0.3] (C0)--(C1)--(C5)--(C4)--cycle;
    \fill[black!20, draw=black, dashed,opacity=0.3] (C0)--(C2)--(C6)--(C4)--cycle;
    \fill[black!20, draw=black, thick,opacity=0.3] (C4)--(C5)--(C7)--(C6)--cycle;
    \fill[black!20, draw=black, thick,opacity=0.3] (C2)--(C3)--(C7)--(C6)--cycle;
    \fill[black!20, draw=black, thick,opacity=0.3] (C1)--(C3)--(C7)--(C5)--cycle;   
    \coordinate (Q0) at ($(Ocube)-.8*(Xcube)+(Ycube)-.8*(Zcube)$);
    \coordinate (Q1) at ($(Ocube)+.8*(Xcube)+(Ycube)-.8*(Zcube)$);
    \coordinate (Q2) at ($(Ocube)-.8*(Xcube)+(Ycube)+.8*(Zcube)$);
    \coordinate (Q3) at ($(Ocube)+.8*(Xcube)+(Ycube)+.8*(Zcube)$);
    \fill[white!20, draw=black, thick,opacity=0.3] (Q0)--(Q1)--(Q3)--(Q2)--cycle;
\end{tikzpicture}
\end{document}

在最後一行,我想繪製圖像“face.png”,而不是填充白色(白色!20)。這張圖必須變形才能真實貼合臉部。

有人可以幫我這樣做嗎?

答案1

不完全是問題的答案,但很有趣。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}
  \draw[yellow,yslant=0.5] (0,0) rectangle (2,2);
  \node[yslant=0.5,anchor=center,opacity=0.3]  at (1,1.5) {\includegraphics[width=2cm,height=2cm]{example-image-a}};
  \draw[magenta,yslant=-0.5] (-2,2) rectangle (0,0);
  \node[yslant=-0.5,anchor=center,opacity=0.3]  at (-1,1.5) {\includegraphics[width=2cm,height=2cm]{example-image-b}};
  \begin{scope}[on background layer]
  \draw[orange,yslant=-0.5] (0,2) rectangle (2,4);
  \node[yslant=-0.5,anchor=center,xshift=0cm,yshift=1cm,opacity=0.3]  at (1,1.5) {\includegraphics[width=2cm,height=2cm]{example-image-b}};
  \draw[green,yslant=0.5,,xshift=-2cm,yshift=2cm] (0,0) rectangle (2,2);
  \node[yslant=0.5,anchor=center,xshift=-2cm,yshift=2cm,opacity=0.3]  at (1,1.5) {\includegraphics[width=2cm,height=2cm]{example-image-a}};
\draw[blue,yslant=-0.5,xslant=1] (-2,0) rectangle (0,2);
\node[yslant=-0.5,anchor=center,xshift=0cm,yshift=2cm,xslant=1,opacity=0.3]  at (-0,-1) {\includegraphics[width=2cm,height=2cm]{example-image-a}};
  \end{scope}
  \draw[red,yslant=-0.5,xslant=1] (-4,2) rectangle (-2,4);
\node[yslant=-0.5,anchor=center,xshift=0cm,yshift=4cm,xslant=1,opacity=0.3]  at (-0,-1) {\includegraphics[width=2cm,height=2cm]{example-image-a}};

\end{tikzpicture}
\end{document}

在此輸入影像描述

答案2

該網站上有幾個問題涉及在 TikZ 中用圖像填充形狀,例如:

我們可以根據您的 3D 形狀調整這些解決方案。我們使用\clip限制為立方體的前面的正方形,然後添加圖像,最後我們在它周圍繪製邊框。

我已經更換了該行:

\fill[white!20, draw=black, thick,opacity=0.3] (Q0)--(Q1)--(Q3)--(Q2)--cycle;

\def\frontsquare{(Q0)--(Q1)--(Q3)--(Q2)--cycle}

\begin{scope}
    \clip \frontsquare;
    \node {\includegraphics{grass.jpg}};
\end{scope}

\draw[black, thick] \frontsquare;

首先我定義\frontsquare你想要填滿的區域。\clip範圍中包含的 限制了我們在該區域內繪製的所有其他內容,直到\end{scope}。當我用來\includegraphics添加圖像時,僅添加位於前面正方形內的圖像部分。然後該\draw命令添加邊框。

產生的立方體如下所示:

在此輸入影像描述

這是完整的程式碼:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{3d,calc}
\tikzset{persp/.style={scale=3.0,x={(-0.8cm,-0.4cm)},y={(0.8cm,-0.4cm)}, z={(0cm,1cm)}},points/.style={fill=white,draw=black,thick}}

\begin{document}
\begin{tikzpicture}[persp]\def\i{-15}
    \pgfmathparse{cos(\i)}\let\ci\pgfmathresult
    \pgfmathparse{sin(\i)}\let\si\pgfmathresult 
    \coordinate (Ocube) at (0,0,0);
    \coordinate (Xcube) at (\ci,\si,0);
    \coordinate (Ycube) at (-\si,\ci,0);
    \coordinate (Zcube) at (0,0,1);
    \coordinate (C0) at ($(Ocube)-(Xcube)-(Ycube)-(Zcube)$);
    \coordinate (C1) at ($(Ocube)+(Xcube)-(Ycube)-(Zcube)$);
    \coordinate (C2) at ($(Ocube)-(Xcube)+(Ycube)-(Zcube)$);
    \coordinate (C3) at ($(Ocube)+(Xcube)+(Ycube)-(Zcube)$);
    \coordinate (C4) at ($(Ocube)-(Xcube)-(Ycube)+(Zcube)$);
    \coordinate (C5) at ($(Ocube)+(Xcube)-(Ycube)+(Zcube)$);
    \coordinate (C6) at ($(Ocube)-(Xcube)+(Ycube)+(Zcube)$);
    \coordinate (C7) at ($(Ocube)+(Xcube)+(Ycube)+(Zcube)$);
    \fill[black!20, draw=black, dashed,opacity=0.3] (C0)--(C1)--(C3)--(C2)--cycle;
    \fill[black!20, draw=black, dashed,opacity=0.3] (C0)--(C1)--(C5)--(C4)--cycle;
    \fill[black!20, draw=black, dashed,opacity=0.3] (C0)--(C2)--(C6)--(C4)--cycle;
    \fill[black!20, draw=black, thick,opacity=0.3] (C4)--(C5)--(C7)--(C6)--cycle;
    \fill[black!20, draw=black, thick,opacity=0.3] (C2)--(C3)--(C7)--(C6)--cycle;
    \fill[black!20, draw=black, thick,opacity=0.3] (C1)--(C3)--(C7)--(C5)--cycle;   
    \coordinate (Q0) at ($(Ocube)-.8*(Xcube)+(Ycube)-.8*(Zcube)$);
    \coordinate (Q1) at ($(Ocube)+.8*(Xcube)+(Ycube)-.8*(Zcube)$);
    \coordinate (Q2) at ($(Ocube)-.8*(Xcube)+(Ycube)+.8*(Zcube)$);
    \coordinate (Q3) at ($(Ocube)+.8*(Xcube)+(Ycube)+.8*(Zcube)$);

    \def\frontsquare{(Q0)--(Q1)--(Q3)--(Q2)--cycle}

    \begin{scope}
        \clip \frontsquare;
        \node {\includegraphics{grass.jpg}};
    \end{scope}

    \draw[black, thick] \frontsquare;
\end{tikzpicture}
\end{document}

相關內容