This document details the fundamental classes and data structures that form the foundation of ProBuilder's mesh representation and manipulation system.
Location: Runtime/Core/ProBuilderMesh.cs
ProBuilderMesh is the heart of ProBuilder, extending MonoBehaviour to provide a Unity component that manages all mesh data and operations.
- Mesh Data Storage: Maintains vertices, faces, edges, and UV coordinates
- Unity Integration: Bridges between ProBuilder's format and Unity's Mesh system
- Serialization: Handles saving/loading of mesh data
- Mesh Compilation: Converts internal format to Unity Mesh for rendering
// Core mesh elements
public Face[] faces // Array of face definitions
public Vector3[] positions // Vertex positions in local space
public SharedVertex[] sharedVertices // Vertex sharing information
// Material and UV data
public Vector2[] textures0 // Primary UV coordinates
public Material[] materials // Per-face materials
public AutoUnwrapSettings[] unwrapParameters // UV generation settingsToMesh(): Compiles ProBuilder format to Unity MeshRefresh(): Updates visual representationGetVertices(): Returns all vertex data as Vertex structsSetVertices(): Updates vertex data from Vertex array
Location: Runtime/Core/Face.cs
Represents a polygonal face composed of triangles with associated material and UV settings.
public class Face
{
int[] indexes; // Triangle indices (always triangulated)
int smoothingGroup; // For normal smoothing
AutoUnwrapSettings uv; // UV projection settings
Material material; // Face material
bool manualUV; // UV coordinate source
}- Triangle Storage: All faces are internally stored as triangles, even if originally quads
- Smoothing Groups: Faces sharing a smoothing group have averaged normals
- UV Modes: Faces can use automatic UV projection or manual coordinates
- Material Assignment: Each face can have its own material
Location: Runtime/Core/Edge.cs
A simple structure representing a connection between two vertices.
public struct Edge
{
public int a, b; // Vertex indices
public bool IsValid() // Checks if edge points to valid vertices
public static Edge Empty // Represents invalid edge (-1, -1)
}- Topology Queries: Finding adjacent faces, edge loops
- Selection Operations: Edge-based selection and manipulation
- Mesh Operations: Extrusion, loop cuts, beveling
Location: Runtime/Core/Vertex.cs
Comprehensive vertex structure containing all per-vertex attributes.
public struct Vertex
{
public Vector3 position; // 3D position
public Color color; // Vertex color
public Vector3 normal; // Surface normal
public Vector4 tangent; // Tangent vector (with handedness in w)
// UV coordinates (up to 4 channels)
public Vector2 uv0, uv2;
public Vector4 uv3, uv4;
}- Complete Attribute Set: Contains all data needed for rendering
- UV Channels: Supports multiple UV sets for advanced materials
- Vertex Colors: Per-vertex color information for special effects
Location: Runtime/Core/SharedVertex.cs
ProBuilder's sophisticated system for managing vertex sharing and topology.
Geometric Vertices: [A, B, C, D, E, F]
Shared Groups: [[A,D], [B,E], [C,F]]
Multiple mesh vertices can reference the same geometric position while maintaining different attributes (normals, UVs, colors).
public sealed class SharedVertex : IEnumerable<int>
{
int[] m_Vertices; // Array of vertex indices sharing position
}- Hard/Soft Edges: Control over normal smoothing
- UV Seams: Different UV coordinates at same position
- Efficient Operations: Topology queries and modifications
- Undo/Redo: Preserves sharing relationships
Location: Runtime/Core/WingedEdge.cs
Advanced data structure for efficient mesh topology navigation, based on the Winged Edge data structure.
public sealed class WingedEdge
{
public EdgeLookup edge; // The edge this wing represents
public Face face; // Connected face
public WingedEdge next; // Next edge in face
public WingedEdge previous; // Previous edge in face
public WingedEdge opposite; // Edge on adjacent face
}- Mesh Traversal: Navigate between adjacent faces and edges
- Topology Queries: Find edge loops, face neighbors
- Validation: Check for manifold geometry
- Complex Operations: Boolean operations, subdivision
The implementation follows the classic Winged Edge data structure described in:
- Winged Edge Wikipedia
- Original paper: "Winged edge polyhedron representation" by Bruce Baumgart (1975)
Location: Runtime/Core/AutoUnwrapSettings.cs
Controls automatic UV coordinate generation for faces.
public struct AutoUnwrapSettings
{
public bool useWorldSpace; // Use world vs local coordinates
public bool flipU, flipV; // UV flipping
public Vector2 scale; // UV scaling
public Vector2 offset; // UV offset
public float rotation; // UV rotation
public Anchor anchor; // Projection anchor point
}- Planar: Project along face normal
- Box: Six-sided projection
- Cylindrical: Wrap around cylinder
- Spherical: Spherical projection
Location: Runtime/Core/BuiltinMaterials.cs
Manages default materials for different render pipelines:
- Standard: Built-in render pipeline
- URP: Universal Render Pipeline
- HDRP: High Definition Render Pipeline
Location: Runtime/Core/SelectionPicker.cs
Handles 3D picking and selection of mesh elements using GPU-based selection.
- Render to Texture: Draw mesh with unique colors per element
- Mouse Query: Sample color at mouse position
- Color Decode: Convert color back to element index
- Accurate Selection: Pixel-perfect element picking
- Performance: GPU acceleration for complex meshes
- Occlusion: Automatically handles depth testing
Location: Runtime/Core/Math.cs
Mathematical functions specific to mesh operations:
- Plane/Ray intersections
- Point-in-polygon tests
- Distance calculations
- Geometric projections
Location: Runtime/Core/MeshUtility.cs
Helper functions for mesh data manipulation:
- Vertex welding and splitting
- Normal calculation
- Bounds computation
- Mesh validation
- Object Pooling: Reuse temporary collections
- Lazy Evaluation: Calculate expensive data on demand
- Dirty Flags: Track what needs recalculation
- Batch Operations: Process multiple elements together
- Spatial Indexing: Use KdTree for spatial queries
- Cache Friendly: Minimize object allocations in hot paths
- Begin Operation:
mesh.ToMesh()andmesh.Refresh() - Modify Data: Update vertices, faces, or topology
- Validate: Check mesh integrity
- Commit:
mesh.ToMesh()and refresh display
- Validation: Check for degenerate geometry
- Graceful Degradation: Fall back to simpler operations
- User Feedback: Provide actionable error messages
This foundation enables all higher-level operations while maintaining data integrity and performance.