Debian Codegraph @

texmf-dist/doc/generic/pgf/pgfmanual-en-library-perspective.tex

raw
1
2 \section{Three Point Perspective Drawing Library}
3
4 \noindent\emph{by Max Snippe}
5
6 \begin{tikzlibrary}{perspective}
7 This library provides tools for perspective drawing with one, two, or three
8 vanishing points.
9 \end{tikzlibrary}
10
11
12 \subsection{Coordinate Systems}
13
14 \begin{coordinatesystem}{three point perspective}
15 The |three point perspective| coordinate system is very similar to the |xyz|
16 coordinate system, save that it will display the provided coordinates with a
17 perspective projection.
18 %
19 \begin{key}{/tikz/cs/x=\meta{number} (initially 0)}
20 The $x$ component of the coordinate. Should be given \emph{without} unit.
21 \end{key}
22 %
23 \begin{key}{/tikz/cs/y=\meta{number} (initially 0)}
24 Same as |x|.
25 \end{key}
26 %
27 \begin{key}{/tikz/cs/z=\meta{number} (initially 0)}
28 Same as |x|.
29 \end{key}
30 \end{coordinatesystem}
31
32 \begin{coordinatesystem}{tpp}
33 The |tpp| coordinate system is an alias for the |three point perspective|
34 coordinate system.
35 \end{coordinatesystem}
36
37
38 \subsection{Setting the view}
39
40 \begin{key}{/tikz/3d view=\marg{azimuth}\marg{elevation}
41 (default \{-30\}\{15\})}
42 With the |3d view| option, the projection of the 3D coordinates on the 2D page
43 is defined. It is determined by rotating the coordinate system by
44 $-\meta{azimuth}$ around the $z$-axis, and by \meta{elevation} around the
45 (new) $x$-axis, as shown below.
46
47 \begin{tikzpicture}[
48 viewpoint/.pic={
49 \draw (22.5:0.45) -- (0,0) -- (-22.5:0.45);
50 \draw (0,0) ++ (22.5:0.35) arc (22.5:-22.5:0.35);
51 \draw (0.225,0) circle (0.02 and 0.09);
52 }]
53 \begin{scope}[3d view={-20}{20}]
54 \draw[->] (-3,0,0) -- (3,0,0) node[pos=1.05]{x};
55 \draw[->] (0,-3,0) -- (0,3,0) node[pos=1.05]{y};
56 \draw[->] (0,0,-1) -- (0,0,3) node[pos=1.05]{z};
57
58 \pgfmathsetmacro\az{50}
59 \begin{scope}[canvas is xy plane at z=0]
60 \draw[->] (0,0) ++(0,-2) arc (-90:-90+\az:2) coordinate[pos=0.5](az);
61 \draw (az) -- ++(-90+\az/2:1) node[below]{\meta{azimuth}};
62 \draw[dashed] (0,0) -- ++(-90+\az:3);
63 \end{scope}
64 \begin{scope}[rotate around z=\az]
65 \pgfmathsetmacro\el{50}
66 \begin{scope}[canvas is yz plane at x=0]
67 \draw[->] (0,0) ++(-2.5,0) arc (180:180-\el:2.5)
68 coordinate[pos=0.5](el);
69 \draw (el) -- ++(180-\el/2:1) node[above]{\meta{elevation}};
70 \draw[dashed] (0,0) --
71 pic[solid,sloped,transform shape,pos=1.2]{viewpoint} ++(180-\el:3);
72 \end{scope}
73 \end{scope}
74 \end{scope}
75 \end{tikzpicture}
76
77 For example, when both \meta{azimuth} and \meta{elevation} are 0$^\circ$,
78 $+z$ will be pointing upward, and $+x$ will be pointing right. The default is
79 as shown below.
80 %
81 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
82 \begin{tikzpicture}[3d view]
83 \draw[->] (-1,0,0) -- (1,0,0) node[pos=1.1]{x};
84 \draw[->] (0,-1,0) -- (0,1,0) node[pos=1.1]{y};
85 \draw[->] (0,0,-1) -- (0,0,1) node[pos=1.1]{z};
86 \end{tikzpicture}
87 \end{codeexample}
88 \end{key}
89
90 \begin{stylekey}{/tikz/isometric view}
91 A special kind of |3d view| is isometric, which can be set with the
92 |isometric view| style. It simply sets |3d view={-45}{35.26}|. The value for
93 \meta{elevation} is determined with $\arctan(1/\sqrt{2})$. In isometric
94 projection the angle between any pair of axes is 120$^\circ$, as shown below.
95 %
96 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
97 \begin{tikzpicture}[isometric view]
98 \draw[->] (-1,0,0) -- (1,0,0) node[pos=1.1]{x};
99 \draw[->] (0,-1,0) -- (0,1,0) node[pos=1.1]{y};
100 \draw[->] (0,0,-1) -- (0,0,1) node[pos=1.1]{z};
101 \end{tikzpicture}
102 \end{codeexample}
103 \end{stylekey}
104
105
106 \subsection{Defining the perspective}
107
108 In this section, the following example cuboid will be used with various
109 scaling. As a reference, the axes will be shown too, without perspective
110 projection.
111 %
112 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
113 \newcommand\simplecuboid[3]{%
114 \fill[gray!80!white] (tpp cs:x=0,y=0,z=#3)
115 -- (tpp cs:x=0,y=#2,z=#3)
116 -- (tpp cs:x=#1,y=#2,z=#3)
117 -- (tpp cs:x=#1,y=0,z=#3) -- cycle;
118 \fill[gray] (tpp cs:x=0,y=0,z=0)
119 -- (tpp cs:x=0,y=0,z=#3)
120 -- (tpp cs:x=0,y=#2,z=#3)
121 -- (tpp cs:x=0,y=#2,z=0) -- cycle;
122 \fill[gray!50!white] (tpp cs:x=0,y=0,z=0)
123 -- (tpp cs:x=0,y=0,z=#3)
124 -- (tpp cs:x=#1,y=0,z=#3)
125 -- (tpp cs:x=#1,y=0,z=0) -- cycle;}
126 \newcommand{\simpleaxes}[3]{%
127 \draw[->] (-0.5,0,0) -- (#1,0,0) node[pos=1.1]{x};
128 \draw[->] (0,-0.5,0) -- (0,#2,0) node[pos=1.1]{y};
129 \draw[->] (0,0,-0.5) -- (0,0,#3) node[pos=1.1]{z};}
130
131 \begin{tikzpicture}[3d view]
132 \simplecuboid{2}{2}{2}
133 \simpleaxes{2}{2}{2}
134 \end{tikzpicture}
135 \end{codeexample}
136
137 \begin{codeexample}[setup code,hidden]
138 \newcommand\simplecuboid[3]{%
139 \fill[gray!80!white] (tpp cs:x=0,y=0,z=#3)
140 -- (tpp cs:x=0,y=#2,z=#3)
141 -- (tpp cs:x=#1,y=#2,z=#3)
142 -- (tpp cs:x=#1,y=0,z=#3) -- cycle;
143 \fill[gray] (tpp cs:x=0,y=0,z=0)
144 -- (tpp cs:x=0,y=0,z=#3)
145 -- (tpp cs:x=0,y=#2,z=#3)
146 -- (tpp cs:x=0,y=#2,z=0) -- cycle;
147 \fill[gray!50!white] (tpp cs:x=0,y=0,z=0)
148 -- (tpp cs:x=0,y=0,z=#3)
149 -- (tpp cs:x=#1,y=0,z=#3)
150 -- (tpp cs:x=#1,y=0,z=0) -- cycle;}
151
152 \newcommand{\simpleaxes}[3]{%
153 \draw[->] (-0.5,0,0) -- (#1,0,0) node[pos=1.1]{x};
154 \draw[->] (0,-0.5,0) -- (0,#2,0) node[pos=1.1]{y};
155 \draw[->] (0,0,-0.5) -- (0,0,#3) node[pos=1.1]{z};}
156 \end{codeexample}
157
158 \begin{key}{/tikz/perspective=\meta{vanishing points}
159 (default p=\{(10,0,0)\},q=\{(0,10,0)\},r=\{(0,0,20)\})}
160 The `strength' of the perspective can be determined by setting the location of
161 the vanishing points. The default values have a stronger perspective towards
162 $x$ and $y$ than towards $z$, as shown below.
163 %
164 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
165 \begin{tikzpicture}[3d view,perspective]
166 \simplecuboid{2}{2}{2}
167 \simpleaxes{2}{2}{2}
168 \end{tikzpicture}
169 \end{codeexample}
170 %
171 From this example it also shows that the maximum dimensions of the cuboid are
172 no longer 2 by 2 by 2. This is inherent to the perspective projection.
173 %
174 \begin{key}{/tikz/perspective/p=\marg{x,y,z} (initially (0,0,0))}
175 The location of the vanishing point that determines the `strength' of the
176 perspective in $x$-direction can be set with the |p| key.
177 %
178 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
179 \begin{tikzpicture}[
180 3d view,
181 perspective={
182 p = {(5,0,0)}}]
183 \simplecuboid{2}{2}{2}
184 \simpleaxes{2}{2}{2}
185 \end{tikzpicture}
186 \end{codeexample}
187 %
188 Note also that when only |p| is provided, the perspective in $y$ and $z$
189 direction is turned off.
190
191 To turn off the perspective in $x$-direction, one must set the $x$ component
192 of |p| to \texttt{0} (e.g.\ |p={(0,a,b)}|, where \texttt{a} and \texttt{b}
193 can be any number and will be ignored). Or one can provide |q| and |r| and
194 omit |p|.
195
196 By changing the $y$ and $z$ components of |p|, one can achieve various
197 effects.
198 %
199 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
200 \begin{tikzpicture}[
201 3d view,
202 perspective={
203 p = {(5,0,1)}}]
204 \simplecuboid{2}{2}{2}
205 \simpleaxes{2}{2}{2}
206 \end{tikzpicture}
207 \end{codeexample}
208 %
209 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
210 \begin{tikzpicture}[
211 3d view,
212 perspective={
213 p = {(5,1,0)}}]
214 \simplecuboid{2}{2}{2}
215 \simpleaxes{2}{2}{2}
216 \end{tikzpicture}
217 \end{codeexample}
218 %
219 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
220 \begin{tikzpicture}[
221 3d view,
222 perspective={
223 p = {(5,1,1)}}]
224 \simplecuboid{2}{2}{2}
225 \simpleaxes{2}{2}{2}
226 \end{tikzpicture}
227 \end{codeexample}
228 %
229 \end{key}
230 %
231 \begin{key}{/tikz/perspective/q=\marg{x,y,z} (initially (0,0,0))}
232 Similar to |p|, but can be turned off by setting its $y$ component to
233 \texttt{0}.
234 %
235 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
236 \begin{tikzpicture}[
237 3d view,
238 perspective={
239 q = {(0,5,0)}}]
240 \simplecuboid{2}{2}{2}
241 \simpleaxes{2}{2}{2}
242 \end{tikzpicture}
243 \end{codeexample}
244 %
245 \end{key}
246 %
247 \begin{key}{/tikz/perspective/r=\marg{x,y,z} (initially (0,0,0))}
248 Similar to |p|, but can be turned off by setting its $z$ component to
249 \texttt{0}.
250 %
251 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
252 \begin{tikzpicture}[
253 3d view,
254 perspective={
255 r = {(0,0,5)}}]
256 \simplecuboid{2}{2}{2}
257 \simpleaxes{2}{2}{2}
258 \end{tikzpicture}
259 \end{codeexample}
260 %
261 \end{key}
262 \end{key}
263
264
265 \subsection{Shortcomings}
266
267 Currently a number of things are not working, mostly due to the fact that PGF
268 uses a 2D coordinate system underwater, and perspective projection is a
269 non-linear affine transformation which needs to be aware of all three
270 coordinates. These three coordinates are currently lost when processing a 3D
271 coordinate.
272 The issues include, but possibly are not limited to:
273 %
274 \begin{itemize}
275 \item Keys like |shift|, |xshift|, |yshift| are not working
276 \item Keys like |rotate around x|, |rotate around y|, and |rotate around z|
277 are not working
278 \item Units are not working
279 \item Most keys from the |3d| library are unsupported, e.g.\ all the
280 |canvas is .. plane| keys.
281 \end{itemize}
282
283
284 \subsection{Examples}
285
286 An |r| that lies `below' your drawing can mimic a macro effect.
287 %
288 \nopagebreak
289 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
290 \begin{tikzpicture}[
291 isometric view,
292 perspective={
293 p = {(8,0,0)},
294 q = {(0,8,0)},
295 r = {(0,0,-8)}}]
296
297 \simplecuboid{2}{2}{2}
298
299 \end{tikzpicture}
300 \end{codeexample}
301
302 A peculiar phenomenon inherent to perspective drawing, is that however great
303 your coordinate will become in the direction of the vanishing point, it will
304 never reach it.
305 %
306 \nopagebreak
307 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
308 \begin{tikzpicture}[
309 isometric view,
310 perspective={
311 p = {(4,0,0)},
312 q = {(0,4,0)}}]
313
314 \node[fill=red,circle,inner sep=1.5pt,label=above:p] at (4,0,0){};
315
316 \foreach \i in {0,...,100}{
317 \filldraw[fill = gray] (tpp cs:x=\i,y=0,z=0)
318 -- (tpp cs:x=\i+0.5,y=0,z=0)
319 -- (tpp cs:x=\i+0.5,y=2,z=0)
320 -- (tpp cs:x=\i,y=2,z=0)
321 -- cycle;}
322 \end{tikzpicture}
323 \end{codeexample}
324
325 Even for simple examples, the added perspective might add another `dimension' to
326 your drawing. In this case, two vanishing points give a more intuitive result
327 then three would.
328 %
329 \nopagebreak
330 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
331 \begin{tikzpicture}[
332 scale=0.7,
333 3d view,
334 perspective={
335 p = {(20,0,0)},
336 q = {(0,20,0)}}]
337
338 \filldraw[fill=brown] (tpp cs:x=0,y=0,z=0)
339 -- (tpp cs:x=0,y=4,z=0)
340 -- (tpp cs:x=0,y=4,z=2)
341 -- (tpp cs:x=0,y=2,z=4)
342 -- (tpp cs:x=0,y=0,z=2) -- cycle;
343 \filldraw[fill=red!70!black] (tpp cs:x=0,y=0,z=2)
344 -- (tpp cs:x=5,y=0,z=2)
345 -- (tpp cs:x=5,y=2,z=4)
346 -- (tpp cs:x=0,y=2,z=4) -- cycle;
347 \filldraw[fill=brown!80!white] (tpp cs:x=0,y=0,z=0)
348 -- (tpp cs:x=0,y=0,z=2)
349 -- (tpp cs:x=5,y=0,z=2)
350 -- (tpp cs:x=5,y=0,z=0) -- cycle;
351 \end{tikzpicture}
352 \end{codeexample}
353
354 With the vanishing points nearby, the distortion of parallel lines becomes very
355 strong. This might lead to \texttt{Dimension too large} errors.
356 %
357 \nopagebreak
358 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
359 \begin{tikzpicture}[
360 3d view,
361 perspective={
362 p = {(2,0,0)},
363 q = {(0,2,0)},
364 r = {(0,0,2)}},
365 scale=4,
366 vanishing point/.style={fill,circle,inner sep=2pt}]
367
368 \simplecuboid{3}{1}{2}
369
370 \node[vanishing point,label = right:p] (p) at (2,0,0){};
371 \node[vanishing point,label = left:q] (q) at (0,2,0){};
372 \node[vanishing point,label = above:r] (r) at (0,0,2){};
373
374 \begin{scope}[dotted]
375 \foreach \y in {0,1}{
376 \foreach \z in {0,2}{
377 \draw (tpp cs:x=0,y=\y,z=\z) -- (p.center);}}
378 \foreach \x in {0,3}{
379 \foreach \z in {0,2}{
380 \draw (tpp cs:x=\x,y=0,z=\z) -- (q.center);}}
381 \foreach \x in {0,3}{
382 \foreach \y in {0,1}{
383 \draw (tpp cs:x=\x,y=\y,z=0) -- (r.center);}}
384 \end{scope}
385 \end{tikzpicture}
386 \end{codeexample}
387
388 % A more complex example.
389 \iffalse
390 Of course these examples can become as complex as desired, but as with any 3D
391 drawing using \tikzname, the order of drawing commands is important and can
392 become increasingly more complex.
393 %
394 \nopagebreak
395 \begin{codeexample}[preamble={\usetikzlibrary{perspective}}]
396 \begin{tikzpicture}[
397 cycle of vertices/.style 2 args={
398 insert path={
399 foreach \i [count=\j,evaluate=\j as \k using
400 {ifthenelse(\j==1,"","-- "}] in {#2}{\k (vert-#1-\i)} -- cycle}},
401 scale=0.7,
402 line join=round,
403 bottom/.style={draw=white!50!black,fill=white!40!black},
404 front/.style={draw=white!50!black,fill=black},
405 side/.style={draw=white!50!black,fill=white!80!black},
406 ]
407 \begin{scope}[
408 3d view={-20}{0},
409 perspective={
410 p = {(20,0,0)},
411 q = {(0,20,0)},
412 r = {(5,1,50)},
413 }]
414 \path foreach \x/\y/\z [count=\i] in {
415 3.5/2.0/0.0,3.5/2.0/4.0,6.0/2.0/4.0,6.5/2.0/3.5,6.5/2.0/0.5,6.0/2.0/0.0,
416 4.5/2.0/1.0,4.5/2.0/3.0,5.5/2.0/3.0,5.5/2.0/1.0,3.5/0.0/0.0,3.5/0.0/4.0,
417 6.0/0.0/4.0,6.5/0.0/3.5,6.5/0.0/0.5,6.0/0.0/0.0,4.5/0.0/1.0,4.5/0.0/3.0,
418 5.5/0.0/3.0,5.5/0.0/1.0%
419 }{(tpp cs:x=\x,y=\y,z=\z) coordinate[name=vert-D-\i]};
420 \filldraw[front,cycle of vertices={D}{1,...,6},
421 cycle of vertices={D}{7,10,9,8}];
422 \filldraw[side,cycle of vertices={D}{10,9,19,20}];
423 \filldraw[bottom,cycle of vertices={D}{8,9,19,18}];
424 \filldraw[front,cycle of vertices={D}{11,...,16},
425 cycle of vertices={D}{17,20,19,18}];
426 \filldraw[side,cycle of vertices={D}{1,2,12,11}];
427 % '3'
428 \path foreach \x/\y/\z [count=\i] in {
429 0.0/2.0/0.0,0.0/2.0/1.0,2.0/2.0/1.0,2.0/2.0/1.5,0.0/2.0/1.5,0.0/2.0/2.5,
430 2.0/2.0/2.5,2.0/2.0/3.0,0.0/2.0/3.0,0.0/2.0/4.0,3.0/2.0/4.0,3.0/2.0/0.0,
431 0.0/0.0/0.0,0.0/0.0/1.0,2.0/0.0/1.0,2.0/0.0/1.5,0.0/0.0/1.5,0.0/0.0/2.5,
432 2.0/0.0/2.5,2.0/0.0/3.0,0.0/0.0/3.0,0.0/0.0/4.0,3.0/0.0/4.0,3.0/0.0/0.0%
433 }{(tpp cs:x=\x,y=\y,z=\z) coordinate[name=vert-3-\i]};
434 \filldraw[front,cycle of vertices={3}{1,...,12}];
435 \filldraw[side,cycle of vertices={3}{3,4,16,15}];
436 \filldraw[side,cycle of vertices={3}{7,8,20,19}];
437 \filldraw[side,cycle of vertices={3}{1,2,14,13}];
438 \filldraw[side,cycle of vertices={3}{5,6,18,17}];
439 \filldraw[side,cycle of vertices={3}{9,10,22,21}];
440 \filldraw[bottom,cycle of vertices={3}{4,5,17,16}];
441 \filldraw[bottom,cycle of vertices={3}{8,9,21,20}];
442 \filldraw[front,cycle of vertices={3}{13,...,24}];
443 \end{scope}
444 \end{tikzpicture}
445 \end{codeexample}
446 \fi