Quantcast
Channel: Tikz : texture cube faces using png image - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by alexwlchan for Tikz : texture cube faces using png image

$
0
0

There are several questions on the site which address filling a shape with an image in TikZ, for example:

We can adapt those solutions to your 3D shape. We use \clip to restrict to the front square of the cube, then add the image, and finally we draw the border around it.

I’ve replaced the line:

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

with

\def\frontsquare{(Q0)--(Q1)--(Q3)--(Q2)--cycle}\begin{scope}    \clip \frontsquare;    \node {\includegraphics{grass.jpg}};\end{scope}\draw[black, thick] \frontsquare;

First I define \frontsquare to the area you want to fill. The \clip contained in the scope restricts everything else we draw to within that area, until \end{scope}. When I use \includegraphics to add an image, only the part of the image that is within the front square. The \draw command then adds a border.

This is what the resulting cube looks like:

enter image description here

and here’s the complete code:

\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}

Viewing all articles
Browse latest Browse all 3

Trending Articles