menu_bookDocumentation

s&box SnapGrid: World-space snap grid overlay

calendar_today May 12, 2026 schedule ~3 min read person PatrickJr verified 50

SnapGrid: World-Space Snap Grid Overlay

SnapGrid is a sealed class that manages a world-space snap grid overlay projected onto a surface plane. It is used by the weld tool to provide visual feedback for snapping positions to a grid on surfaces.

Type Signature

CSHARP
public sealed class SnapGrid
{
    public float CellSize { get; set; }
    public float MaskRadius { get; set; }
    public float GridSize { get; set; }
    public Vector3 LastSnapWorldPos { get; private set; }

    public SnapGrid(float cellSize = 4f, float maskRadius = 64f, float gridSize = 48f);
    public void Update(SceneWorld world, GameObject hoveredObject, Vector3 aimWorldPos, Vector3 hitNormalWorld);
    public void Hide();
    public void Destroy();
    
    public static (int cx, int cy, Vector3 snapPos, float snapU, float snapV, bool snapAxisX, bool snapAxisY) ComputeSnap(
        Vector3 faceOriginWs, Vector3 faceRightWs, Vector3 faceUpWs, float cellSize, Vector3 aimPosWs);
}

Properties

Methods

Constructor

Creates a new SnapGrid with optional parameters for cell size, mask radius, and grid size.

Update(SceneWorld world, GameObject hoveredObject, Vector3 aimWorldPos, Vector3 hitNormalWorld)

Called every frame when the weld tool hovers a valid object. After calling, read LastSnapWorldPos for a snapped position. This method:
  • Creates or updates the scene object for rendering
  • Recalculates the plane when the surface normal or hovered object changes
  • Aligns the grid with the object's natural rotation
  • Scales down cell size to fit within the object's face
  • Computes the nearest snap line and updates the shader attributes

Hide()

Hides the overlay by disabling rendering and resetting the plane state.

Destroy()

Destroys the underlying scene object and cleans up resources.

ComputeSnap(...)

Static method that returns the nearest grid corner index and its world-space position, given the face plane in world space and the aim world position. Returns a tuple with:
  • cx, cy: Grid corner indices
  • snapPos: Snapped world position
  • snapU, snapV: UV coordinates on the grid
  • snapAxisX, snapAxisY: Whether snapping to X or Y axis

Behavior

  • The grid aligns with the object's natural rotation on every face, including tilted surfaces
  • Cell size automatically scales down to fit within the object's face
  • Near corners snap to the intersection and show both lines
  • Uses model-local bounds when available for tight, rotation-aware OBB calculation
  • Falls back to world AABB from colliders when model bounds are unavailable
  • Supports holding use to lock the plane while moving

Usage

CSHARP
// Create a snap grid for the weld tool
var snapGrid = new SnapGrid(cellSize: 4f, maskRadius: 64f, gridSize: 48f);

// Update every frame while hovering an object
snapGrid.Update(Scene.SceneWorld, hoveredObject, aimWorldPos, hitNormalWorld);

// Get the snapped position
var snappedPos = snapGrid.LastSnapWorldPos;

// Hide when not hovering
snapGrid.Hide();

// Clean up when done
snapGrid.Destroy();

Notes

  • Uses a custom shader (shaders/snap_grid.shader) for rendering
  • Implements SceneDynamicObject for overlay rendering with depth
  • Has a cheat convar snapgridno_fade to disable fade masking
  • The grid projects the object's up vector onto the face plane for natural orientation
Was this helpful?