Basics
This section explains how the 2D/3D Paint asset works in Unity, giving you the essential knowledge to start painting on both 2D and 3D objects efficiently.
Core Workflow
- Material and Texture Handling:
- The asset clones the source material and swaps out the original texture for a RenderTexture;
- This cloned texture is where all the painting happens, enabling high-performance rendering directly on the GPU.
- Input Processing:
- The asset reads input data (mouse, touch, stylus, or VR controller) and passes it to the RaycastController, which computes the exact position on the object where the painting should occur;
- Each painting interaction involves calculating two points: where the user last touched (previous frame) and where they are currently touching (current frame). The paint is then applied between these two points.
- Layer-Based Painting:
- Each layer you paint on is stored as a separate RenderTexture. These layers provide the flexibility to modify or erase strokes without affecting the underlying textures. You can think of layers as transparent canvases stacked on top of one another.
Key Components and Terminology
To understand how 2D/3D Paint works, it’s important to get familiar with a few key components:
- RenderTextures: The core of the painting system. Different textures are created for various purposes:
Active Layer Texture
: The primary layer where your brush strokes are applied;Active Layer Temp Texture
: Temporary storage for strokes that will later be baked into the final texture;Input Texture
: Temporarily stores user input, either for real-time painting (Default Mode) or during the duration of a stroke (Additive Mode);Combined Texture
: A composite of all active layers, giving you the final visual result;Unwrapped Texture
(exists in World Paint Space forMeshRenderer
andSkinnedMeshRenderer
components): A texture that maps the 3D object’s surface for UV-based painting.
- Raycasting:
- The system determines where on the object the brush should be applied by using raycasting, which calculates intersections between the user’s input and the 3D model;
- The asset features an optimized raycasting system that bypasses the need for colliders, making the process faster and more efficient;
- You can further enhance raycast performance with Unity’s Job System for multithreading, especially useful when working with complex models with many vertices.
- Brush:
- The brush is a RenderTexture that is rendered onto the active layer’s RenderTexture. You can adjust its size, texture, opacity, hardness color and angle. These parameters give you full control over the look of your strokes;
- Brush size and scale are relative to the layer or object being painted, which ensures consistent results.
Paint Modes
2D/3D Paint offers two distinct painting modes:
-
Default Mode:
- This mode applies paint in real-time, with every stroke being baked into the texture immediately;
- It’s best suited for rapid and continuous painting tasks where each stroke should instantly affect the final texture.
-
Additive Mode:
- In this mode, paint is applied more gradually, blending colors and opacity with greater accuracy. Each stroke is stored in the Input Texture and only baked into the layer once the user completes the stroke (e.g., on mouse up);
- Ideal for precise blending and detailed work where smooth transitions between strokes are required.
Paint Spaces
2D/3D Paint allows you to work in two main painting spaces:
- UV Space:
- Painting in UV space applies color to a 2D texture map based on the model’s UV coordinates, ensuring precise control over texture placement.
- World Space:
- In world space, the brush paints directly on the 3D model using its position in the scene, allowing seamless application across surfaces.
Undo/Redo System
2D/3D Paint has an undo/redo feature to track and reverse any changes to layers or textures.
- Layer Changes: The system stores a record of texture changes after each stroke (e.g., after every OnMouseUp event);
- Parameter Changes: Adjustments to layer properties such as name, index, and opacity are also stored;
- Memory Consideration: Note that undoing a texture change can consume a significant amount of memory, especially with larger textures (e.g., a 1024x1024 texture requires 4MB of memory). You can configure this feature in the asset’s settings to limit or disable it.
Input Support
The asset supports a variety of input devices for painting, including:
- Mouse;
- Touch devices (e.g., tablets or phones);
- Pen/stylus devices (like Apple Pen);
- VR controllers.
You can switch between Unity’s legacy Input Manager or the newer Input System Package depending on your project needs.