Okay, creating a detailed 3D tractor in TikZ is a fun challenge! It involves layering shapes and using a perspective projection. Here's a TikZ code that draws a stylized red tractor with a 3D effect.
I'll make it somewhat blocky but recognizable.
latex
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,shapes.geometric,arrows.meta} % Added arrows.meta for steering wheel
\begin{document}
\begin{tikzpicture}[
scale=1.5,
% --- Define 3D perspective ---
% x goes right-ish, y goes into the page (depth), z goes up
x={({0.9cm},{0.1cm})}, % Leans slightly back
y={({-0.4cm},{0.3cm})}, % Into the page and slightly up
z={({0cm},{1cm})},
% --- Styles ---
tractor_body_main/.style={fill=red!70!black, draw=black, thick},
tractor_body_secondary/.style={fill=red!90!black, draw=black, thick},
tractor_engine/.style={fill=gray!60!black, draw=black, thick},
tire/.style={fill=black!80!white, draw=black!70!white, very thick},
rim/.style={fill=gray!40, draw=gray!60, thick},
hub/.style={fill=gray!60, draw=gray!80, thick},
window/.style={fill=blue!20!cyan!30!white, draw=blue!50!black, opacity=0.7},
exhaust/.style={fill=gray!70!black, draw=black, thick},
light/.style={fill=yellow!70!orange, draw=orange!50!black, circle, inner sep=0pt}
]
% --- Coordinates for key points (approximate centers or corners) ---
\coordinate (O) at (0,0,0); % Origin, roughly under front axle center
% --- Dimensions ---
\def\rearWheelRadius{1.2}
\def\rearWheelWidth{0.6}
\def\frontWheelRadius{0.7}
\def\frontWheelWidth{0.4}
\def\axleSeparation{3.5} % Distance between front and rear axles
\def\wheelGauge{2.2} % Distance between wheels on the same axle (center to center)
% --- Helper for drawing wheels ---
% #1: x offset, #2: y offset (depth), #3: z offset (height)
% #4: radius, #5: width
% #6: rim color suffix (e.g., !80!black or empty)
\newcommand{\drawWheel}[6]{
\begin{scope}[shift={(#1,#2,#3)}]
% Outer tire (far side)
\filldraw[tire] (0, {#5/2}, 0) ellipse ({#4} and {#4*0.8}); % Ellipse for 3D effect
% Rim (far side)
\filldraw[rim] (0, {#5/2 - #5*0.05}, 0) ellipse ({#4*0.6} and {#4*0.6*0.8});
% Hub (far side)
\filldraw[hub] (0, {#5/2 - #5*0.1}, 0) ellipse ({#4*0.2} and {#4*0.2*0.8});
% Tire connecting surface (only if width > 0)
\ifdim #5 pt > 0 pt
\fill[black!70!white]
({#4}, {#5/2}, 0)
arc (90:270:{#4} and {#4*0.8}) --
({-#4}, -{#5/2}, 0)
arc (270:90:{#4} and {#4*0.8}) -- cycle;
\draw[black!60!white, very thick] ({#4}, {#5/2}, 0) arc (90:270:{#4} and {#4*0.8});
\draw[black!60!white, very thick] ({#4}, -{#5/2}, 0) arc (90:270:{#4} and {#4*0.8});
\fi
% Outer tire (near side)
\filldraw[tire] (0, -{#5/2}, 0) ellipse ({#4} and {#4*0.8});
% Rim (near side)
\filldraw[rim#6] (0, -{#5/2 + #5*0.05}, 0) ellipse ({#4*0.6} and {#4*0.6*0.8});
% Hub (near side)
\filldraw[hub#6] (0, -{#5/2 + #5*0.1}, 0) ellipse ({#4*0.2} and {#4*0.2*0.8});
\end{scope}
}
% --- Draw Components (from back/far to front/near for correct layering) ---
% Far Rear Wheel
\drawWheel{\axleSeparation}{\wheelGauge/2}{\rearWheelRadius}{\rearWheelRadius}{\rearWheelWidth}{!80!black};
% Far Front Wheel
\drawWheel{0}{\wheelGauge/2}{\frontWheelRadius}{\frontWheelRadius}{\frontWheelWidth}{!80!black};
% Main Body & Engine Block
% Define chassis points (bottom plane)
\coordinate (chassis_FL) at (-0.5, \wheelGauge/2-0.3, \frontWheelRadius*0.8);
\coordinate (chassis_FR) at (-0.5, -\wheelGauge/2+0.3, \frontWheelRadius*0.8);
\coordinate (chassis_RL) at (\axleSeparation+0.5, \wheelGauge/2-0.3, \frontWheelRadius*0.8); % Rear extends a bit
\coordinate (chassis_RR) at (\axleSeparation+0.5, -\wheelGauge/2+0.3, \frontWheelRadius*0.8);
% Engine block (top plane, relative to chassis)
\coordinate (engine_FL_top) at ($(chassis_FL)+(0,0,1.5)$);
\coordinate (engine_FR_top) at ($(chassis_FR)+(0,0,1.5)$);
\coordinate (engine_RL_top) at ($(chassis_FL)+(\axleSeparation*0.7,0,1.5)$); % Engine not full length
\coordinate (engine_RR_top) at ($(chassis_FR)+(\axleSeparation*0.7,0,1.5)$);
% Draw Engine Sides and Top (simplified cuboid)
% Far side
\filldraw[tractor_body_secondary] (chassis_FL) -- (engine_FL_top) -- (engine_RL_top) -- ($(chassis_FL)+(\axleSeparation*0.7,0,0)$) -- cycle;
% Top
\filldraw[tractor_body_main] (engine_FL_top) -- (engine_FR_top) -- (engine_RR_top) -- (engine_RL_top) -- cycle;
% Front
\filldraw[tractor_body_main] (chassis_FL) -- (chassis_FR) -- (engine_FR_top) -- (engine_FL_top) -- cycle;
% Cabin Base (on top of rear chassis/engine part)
\def\cabinHeight{2}
\def\cabinWidth{1.8}
\def\cabinDepth{1.5} % x-direction length
\def\cabinRearOffset{\axleSeparation*0.6} % Start cabin here
\coordinate (cabin_base_FL) at (\cabinRearOffset, \cabinWidth/2-0.2, \frontWheelRadius*0.8+1.5);
\coordinate (cabin_base_FR) at (\cabinRearOffset, -\cabinWidth/2+0.2, \frontWheelRadius*0.8+1.5);
\coordinate (cabin_base_RL) at (\cabinRearOffset+\cabinDepth, \cabinWidth/2-0.2, \frontWheelRadius*0.8+1.5);
\coordinate (cabin_base_RR) at (\cabinRearOffset+\cabinDepth, -\cabinWidth/2+0.2, \frontWheelRadius*0.8+1.5);
\coordinate (cabin_top_FL) at ($(cabin_base_FL)+(0,0,\cabinHeight)$);
\coordinate (cabin_top_FR) at ($(cabin_base_FR)+(0,0,\cabinHeight)$);
\coordinate (cabin_top_RL) at ($(cabin_base_RL)+(0,0,\cabinHeight)$);
\coordinate (cabin_top_RR) at ($(cabin_base_RR)+(0,0,\cabinHeight)$);
% Slanted front for cabin roof
\coordinate (cabin_top_FL_slant) at ($(cabin_top_FL)+(-0.3,0,0.2)$);
\coordinate (cabin_top_FR_slant) at ($(cabin_top_FR)+(-0.3,0,0.2)$);
% Cabin Far Side
\filldraw[tractor_body_secondary] (cabin_base_FL) -- (cabin_top_FL_slant) -- (cabin_top_RL) -- (cabin_base_RL) -- cycle;
% Cabin Roof
\filldraw[tractor_body_main] (cabin_top_FL_slant) -- (cabin_top_FR_slant) -- (cabin_top_RR) -- (cabin_top_RL) -- cycle;
% Cabin Back
\filldraw[tractor_body_main] (cabin_base_RL) -- (cabin_base_RR) -- (cabin_top_RR) -- (cabin_top_RL) -- cycle;
% Near Rear Wheel
\drawWheel{\axleSeparation}{-\wheelGauge/2}{\rearWheelRadius}{\rearWheelRadius}{\rearWheelWidth}{};
% Near Front Wheel
\drawWheel{0}{-\wheelGauge/2}{\frontWheelRadius}{\frontWheelRadius}{\frontWheelWidth}{};
% Near side of engine
\filldraw[tractor_body_main] (chassis_FR) -- (engine_FR_top) -- (engine_RR_top) -- ($(chassis_FR)+(\axleSeparation*0.7,0,0)$) -- cycle;
% Near Cabin Side
\filldraw[tractor_body_main] (cabin_base_FR) -- (cabin_top_FR_slant) -- (cabin_top_RR) -- (cabin_base_RR) -- cycle;
% Cabin Front (visible part)
\filldraw[tractor_body_main] (cabin_base_FL) -- (cabin_base_FR) -- (cabin_top_FR_slant) -- (cabin_top_FL_slant) -- cycle;
% Windows
% Far side window
\path[window]
($(cabin_base_FL)!0.2!(cabin_top_FL_slant)$) --
($(cabin_base_FL)!0.8!(cabin_top_FL_slant) + (0.1,0,0)$) --
($(cabin_top_RL)!0.8!(cabin_base_RL) + (-0.1,0,0)$) --
($(cabin_top_RL)!0.2!(cabin_base_RL)$) -- cycle;
% Near side window
\path[window]
($(cabin_base_FR)!0.2!(cabin_top_FR_slant) + (0.1,0,0)$) -- % small offset for frame
($(cabin_base_FR)!0.8!(cabin_top_FR_slant) + (0.1,0,0)$) --
($(cabin_top_RR)!0.8!(cabin_base_RR) + (-0.1,0,0)$) --
($(cabin_top_RR)!0.2!(cabin_base_RR)$) -- cycle;
% Front window
\path[window]
($(cabin_base_FL)!0.3!(cabin_top_FL_slant) + (0,0.1,0)$) --
($(cabin_base_FR)!0.3!(cabin_top_FR_slant) + (0,-0.1,0)$) --
($(cabin_base_FR)!0.7!(cabin_top_FR_slant) + (0,-0.1,0)$) --
($(cabin_base_FL)!0.7!(cabin_top_FL_slant) + (0,0.1,0)$) -- cycle;
% Exhaust Pipe
\def\exhaustRadius{0.15}
\def\exhaustHeight{1.5}
\coordinate (exhaustBase) at ($(engine_FL_top)!(0.2)! (engine_FR_top) + (0.5,0,0.1)$); % On near side of hood
% Far ellipse
\filldraw[exhaust] ($(exhaustBase)+(0,0,\exhaustHeight)$) ellipse ({\exhaustRadius} and {\exhaustRadius*0.5});
% Sides
\filldraw[exhaust] ($(exhaustBase)+(-\exhaustRadius,0,0)$) rectangle ($(exhaustBase)+(\exhaustRadius,0,\exhaustHeight)$);
% Near ellipse (top)
\filldraw[exhaust, fill=gray!90!black] ($(exhaustBase)+(0,0,\exhaustHeight)$) ellipse ({\exhaustRadius} and {\exhaustRadius*0.5});
% Bottom ellipse
\filldraw[exhaust] (exhaustBase) ellipse ({\exhaustRadius} and {\exhaustRadius*0.5});
% Steering Wheel (simple)
\coordinate (steeringColumnTop) at ($(cabin_base_FR) + (0.3, 0.4, 1)$); % Inside cabin, near side
\draw[thick, gray!70!black] ($(steeringColumnTop) - (0.1,0.1,0.7)$) -- (steeringColumnTop);
\draw[thick, fill=gray!50!black, draw=black] (steeringColumnTop) circle (0.35);
\draw[thick, gray!70!black] (steeringColumnTop) -- ($(steeringColumnTop) + (0,0,0.35)$); % Spokes
\draw[thick, gray!70!black] (steeringColumnTop) -- ($(steeringColumnTop) + (0.25,0,-0.25)$);
\draw[thick, gray!70!black] (steeringColumnTop) -- ($(steeringColumnTop) + (-0.25,0,-0.25)$);
% Front Lights (simple circles)
\node[light, minimum size=0.4cm] at ($(engine_FL_top)!(0.5)!(chassis_FL) + (-0.05,0.1,0)$) {};
\node[light, minimum size=0.4cm] at ($(engine_FR_top)!(0.5)!(chassis_FR) + (-0.05,-0.1,0)$) {};
% Grill lines (optional detail)
\foreach \i in {1,...,4} {
\draw[gray!50!black] ($(chassis_FL)!\i/5!(engine_FL_top) + (-0.02,0,0)$) -- ($(chassis_FR)!\i/5!(engine_FR_top) + (-0.02,0,0)$);
}
\end{tikzpicture}
\end{document}
Explanation and How to Compile:
Preamble:
\documentclass[tikz,border=10pt]{standalone}: Creates a PDF that fits the drawing.
calc: For coordinate calculations (e.g., ($(A)+(B)$)).
fadings: Not used in this version, but good for advanced shading.
shapes.geometric: For shapes like ellipse.
arrows.meta: For potentially better arrowheads if needed (used for steering wheel spokes here).
\begin{tikzpicture}[...]:
scale=1.5: Adjusts the overall size.
x={...}, y={...}, z={...}: This is crucial for the 3D perspective.
x={(0.9cm,0.1cm)}: The x-unit vector. For every 1 unit in x, move 0.9cm right and 0.1cm up on the page.
y={(-0.4cm,0.3cm)}: The y-unit vector (depth). For every 1 unit in y, move 0.4cm left and 0.3cm up.
z={(0cm,1cm)}: The z-unit vector (height). For every 1 unit in z, move 1cm up.
You can play with these values to change the viewing angle.
Styles: Styles like tractor_body_main, tire, rim, window, etc., are defined to make the code cleaner and easier to modify colors/appearances.
Coordinates and Dimensions:
\coordinate (O) at (0,0,0);: Defines an origin.
\def\rearWheelRadius{...} etc.: Defines dimensions for reusability.
\drawWheel Command:
A custom command to draw a wheel (tire, rim, hub) with perspective. It draws two ellipses for the near and far sides of the tire and rim and connects them.
The #4*0.8 in ellipse ({#4} and {#4*0.8}) makes the ellipses slightly squashed vertically, which is typical for perspective views of circles.
Drawing Components:
Layering: Components are generally drawn from "far" to "near" so that closer objects correctly obscure parts of farther objects.
To Customize:
Colors: Change the fill values in the style definitions.
Perspective: Modify the x, y, z vectors in the tikzpicture options.
Shape/Proportions: Adjust the \def dimensions and the coordinates used for drawing the body and cabin parts.
Details: Add more elements like mudguards, a seat, steps, etc.
This code provides a solid foundation. True 3D modeling in TikZ can get very complex, but this approach gives a convincing illusion with manageable code.