答案1
灵感来自链接答案“绘制不同形状的文本”我找到了一个解决方案。它计算纹理到单个三角形的仿射映射,并在单位纹理空间中进行裁剪和绘制。不幸的是,由于计算精度和范围有限,该解决方案并不可靠。根据三角形的不同,可能会出现错误或只是消失的三角形。
\documentclass{article}
\usepackage{tikz}
% Get X and Y of a coordinate as separated variables.
% Modified solution from: https://tex.stackexchange.com/a/58590/103072
\makeatletter
\newcommand{\gettikzxy}[3]{%
\path #1;%
\edef#2{ \strip@pt\pgf@x }%
\edef#3{ \strip@pt\pgf@y }%
}
\makeatother
% Command to draw one textured triangle
% #1-#3 are coordinates of the triangle
% #4-#6 are texture coordinates in [0,1]^2 of the image
% where (0,0) is bottom left
% #7 is the name of the image
\newcommand\texturedtriangle[7]{%
% Decode all the coordinates into nice single numbers
\gettikzxy{#1}{\ax}{\ay}
\gettikzxy{#2}{\bx}{\by}
\gettikzxy{#3}{\cx}{\cy}
\gettikzxy{#4}{\tax}{\tay}
\gettikzxy{#5}{\tbx}{\tby}
\gettikzxy{#6}{\tcx}{\tcy}
\gettikzxy{(1,1)}{\ux}{\uy}
% Compute the required affine transformation
\pgfmathsetmacro\det{\tax*(\tcy-\tby) + \tay*(\tbx-\tcx) - \tbx*\tcy + \tby*\tcx}
\pgfmathsetmacro\aa{(\ax*(\tcy-\tby) + \tay*(\bx-\cx) - \bx*\tcy + \tby*\cx)/\det}
\pgfmathsetmacro\ba{(\ax*(\tbx-\tcx) + \tax*(\cx-\bx) - \tbx*\cx + \bx*\tcx)/\det}
\pgfmathsetmacro\ab{(\ay*(\tcy-\tby) + \tay*(\by-\cy) - \by*\tcy + \tby*\cy)/\det}
\pgfmathsetmacro\bb{(\ay*(\tbx-\tcx) + \tax*(\cy-\by) - \tbx*\cy + \by*\tcx)/\det}
\pgfmathsetmacro\tx{\ax - (\tax*\aa + \tay*\ba)}
\pgfmathsetmacro\ty{\ay - (\tax*\ab + \tay*\bb)}
\pgflowlevelobj{
\pgfsettransformentries{\aa}{\ab}{\ba}{\bb}{\tx pt}{\ty pt}
}{
% We are inside the texture space here...
\clip #4 -- #5 -- #6 -- cycle;
% Draw a unit sized image (from (0,0) to (1,1) if no scale)
\node at (0.5,0.5) {\includegraphics[width=\ux pt, height=\uy pt]{#7}};
}
}
\begin{document}
\begin{tikzpicture}[scale=2]
\texturedtriangle{(0,0)}{(2,0)}{(0.5,2)} {(0,0)}{(1,0)}{(0,1)} {dragon}
\texturedtriangle{(2.5,1.5)}{(2,0)}{(0.5,2)} {(1,1)}{(1,0)}{(0,1)} {dragon}
\texturedtriangle{(2.5,1.5)}{(1,3)}{(0.5,2)} {(1,1)}{(1,0)}{(0,1)} {dragon}
\texturedtriangle{(-0.5,2)}{(1,3)}{(0.5,2)} {(1,1)}{(1,0)}{(0,1)} {dragon}
\draw (0,0) -- (2,0) -- (0.5,2) -- cycle;
\draw (2.5,1.5) -- (2,0) -- (0.5,2) -- cycle;
\draw (2.5,1.5) -- (1,3) -- (0.5,2) -- cycle;
\draw (-0.5,2) -- (1,3) -- (0.5,2) -- cycle;
\end{tikzpicture}
\end{document}