SDF3DToolkit is a Unity toolkit for working with Signed Distance Fields (SDFs) in 3D space. This toolkit provides utilities for creating, manipulating, and querying SDFs, which are useful for various applications such as procedural content generation, physics simulations, and more.
- SdfNode Class: Represents a node in the SDF structure, providing methods for setting parameters, copying nodes, and calculating bounds and intersections.
- Distance Field Management: Handles the creation and release of render textures used as distance fields.
- Voxel Data Handling: Manages voxel data for distance fields and provides methods for querying signed distances and gradients.
- Clone or download the repository.
- Copy the
SDF3DToolkit
folder into your Unity project's assets folder.
To create an SdfNode
, you need a RenderTexture
, a transformation matrix, and a voxel size:
RenderTexture distanceField = new RenderTexture(width, height, depth);
Matrix4x4 matrix = Matrix4x4.identity;
float voxelSize = 1.0f;
SdfNode sdfNode = new SdfNode(distanceField, matrix, voxelSize);
You can create a copy of an existing SdfNode
:
SdfNode copy = sdfNode.Copy();
You can get the local and world bounds of an SdfNode
:
Bounds localBounds = sdfNode.GetLocalBounds();
Bounds worldBounds = sdfNode.GetWorldBounds();
To check if two SdfNode
instances intersect and get the intersection bounds:
SdfNode otherNode = new SdfNode(otherDistanceField, otherMatrix, otherVoxelSize);
if (sdfNode.IntersectsBounds(otherNode, out Bounds intersectionBounds))
{
// Intersection detected
}
To get the signed distance at a specific world position:
Vector3 worldPos = new Vector3(1.0f, 2.0f, 3.0f);
float signedDistance = sdfNode.SD(worldPos);
To calculate the gradient of the signed distance field at a specific world position:
Vector3 gradient = sdfNode.Gradient(worldPos);
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request.