1

How to get unique color with opacity (color mix in the union set region/overlapping region) for each of the faces/surfaces of the cuboid?

The screen shot below does not depict the outcome of my code, but the desired outcome.

Preferred outcome

\documentclass[border=5pt]{standalone}
\usepackage{tikz} 
\usetikzlibrary{quotes,arrows.meta}
\begin{document}
\begin{tikzpicture}[every edge quotes/.append style={auto, ultra thick, text=black}]
\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{5}
\pgfmathsetmacro{\cubez}{5}
\draw [draw=black, every edge/.append style={draw=black, thick, densely dashed, opacity=.25}]
(0,0,0) [fill=green!40,opacity=0.8] coordinate (o) -- ++(-\cubex,0,0) coordinate (a) -- ++(0,- 
\cubey,0) [fill=blue!25,opacity=0.8] coordinate (b) edge coordinate [pos=1] (g) ++(0,0,-\cubez)  -- 
++(\cubex,0,0) coordinate (c) -- cycle (o) -- ++(0,0,-\cubez) coordinate (d) -- ++(0,-\cubey,0) coordinate (e) edge (g) -- (c) -- cycle
(o) -- (a) -- ++(0,0,-\cubez) coordinate (f) edge (g) -- (d) -- cycle;
\end{tikzpicture}
\end{document}
rakatex
  • 489
  • 2
  • 13
  • @Schrödinger's cat, that sample diagram is just an illustration, I want each side of the cuboid to have a different color, if there is an overlap of two region, in that region I want the mix of the colour, for example if the color on the right face is yellow and the color on the bottom is green, then the overlapped region, i.e, bottom right should produce mix of these colors. Just like union in the Venn diagram. – rakatex May 15 '20 at 19:09
  • 1
    @ Schrödinger's cat added the information. Thank you. – rakatex May 15 '20 at 19:16

1 Answers1

2

This is certainly an overkill answer. Yet it is an attempt to achieve 3d ordering independently of the package used to obtain orthographic projections. The fill colors are stored in keys like xy face/.style={fill=orange}.

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{perspective,3d,fpu}
\makeatletter
\pgfmathdeclarefunction{screendepth}{3}{%
\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathparse{%
((\the\pgf@yx/1cm)*(\the\pgf@zy/1cm)-(\the\pgf@yy/1cm)*(\the\pgf@zx/1cm))*(#1)+
((\the\pgf@zx/1cm)*(\the\pgf@xy/1cm)-(\the\pgf@xx/1cm)*(\the\pgf@zy/1cm))*(#2)+
((\the\pgf@xx/1cm)*(\the\pgf@yy/1cm)-(\the\pgf@yx/1cm)*(\the\pgf@xy/1cm))*(#3)}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\pgfmathdeclarefunction{totalthree}{3}{%
\pgfmathparse{#1+#2+#3}}
\pgfmathdeclarefunction{direction}{3}{%
\begingroup%
\pgfmathparse{int(#1==0)}%
\ifnum\pgfmathresult=1
 \pgfmathparse{int(#2==0)}%
 \ifnum\pgfmathresult=1
  \edef\pgfmathresult{z}%
 \else
  \edef\pgfmathresult{y}%
 \fi
\else
 \edef\pgfmathresult{x}%
\fi
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\makeatother
\begin{document}
\begin{tikzpicture}[3d view={120}{15},line join=round,fill opacity=0.8,
    xy face/.style={fill=orange},yx face/.style={fill=yellow},
    xz face/.style={fill=blue},zx face/.style={fill=cyan},
    yz face/.style={fill=red},zy face/.style={fill=magenta}]
\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{5}
\pgfmathsetmacro{\cubez}{5}
\def\pft#1#2;{\edef\planex{\csname cube#1\endcsname}%
\edef\planey{\csname cube#2\endcsname}}
\foreach \X/\Y in {xy/{(0,0,1)},yx/{(0,0,-1)},xz/{(0,1,0)},zx/{(0,-1,0)},yz/{(1,0,0)},zy/{(-1,0,0)}}
{\pgfmathsetmacro{\myproj}{screendepth\Y}
\ifdim\myproj pt<0pt
\pgfmathsetmacro{\mytot}{totalthree\Y}
\pgfmathsetmacro{\mydir}{direction\Y}
\edef\myshift{\csname cube\mydir\endcsname}
\expandafter\pft\X;
\begin{scope}[style/.expanded={canvas is \X\space plane at \mydir={\mytot*0.5*\myshift}}]
 \draw[dashed,style/.expanded=\X\space face]
  (-\planex/2,-\planey/2) rectangle (\planex/2,\planey/2);
\end{scope}
\fi}
\foreach \X/\Y in {xy/{(0,0,1)},yx/{(0,0,-1)},xz/{(0,1,0)},zx/{(0,-1,0)},yz/{(1,0,0)},zy/{(-1,0,0)}}
{\pgfmathsetmacro{\myproj}{screendepth\Y}
\ifdim\myproj pt>0pt
\pgfmathsetmacro{\mytot}{totalthree\Y}
\pgfmathsetmacro{\mydir}{direction\Y}
\edef\myshift{\csname cube\mydir\endcsname}
\expandafter\pft\X;
\begin{scope}[style/.expanded={canvas is \X\space plane at \mydir={\mytot*0.5*\myshift}}]
 \draw[style/.expanded=\X\space face]
  (-\planex/2,-\planey/2) rectangle (\planex/2,\planey/2);
\end{scope}
\fi}
\end{tikzpicture}
\end{document}

enter image description here

You can change the view and dimensions at will. For instance, for 3d view={30}{15} and \pgfmathsetmacro{\cubey}{3} one gets

enter image description here

There exist certainly simpler possibilities like e.g. in this thread, some of which are not orthographic projections, or also here, which is specific to tikz-3dplot, say.