Add improved Global SDF quality and precision of rasterization

This commit is contained in:
Wojtek Figat
2025-11-04 13:27:21 +01:00
parent 5d17d2509d
commit 85b134b7be
4 changed files with 14 additions and 2 deletions

BIN
Content/Editor/Primitives/Cube.flax (Stored with Git LFS)

Binary file not shown.

View File

@@ -366,9 +366,11 @@ bool ModelTool::GenerateModelSDF(Model* inputModel, const ModelData* modelData,
return true;
ModelBase::SDFData sdf;
sdf.WorldUnitsPerVoxel = METERS_TO_UNITS(0.1f) / Math::Max(resolutionScale, 0.0001f); // 1 voxel per 10 centimeters
#if 0
const float boundsMargin = sdf.WorldUnitsPerVoxel * 0.5f; // Add half-texel margin around the mesh
bounds.Minimum -= boundsMargin;
bounds.Maximum += boundsMargin;
#endif
const Float3 size = bounds.GetSize();
Int3 resolution(Float3::Ceil(Float3::Clamp(size / sdf.WorldUnitsPerVoxel, 4, 256)));
Float3 uvwToLocalMul = size;

View File

@@ -142,8 +142,10 @@ void CS_RasterizeTriangle(uint3 DispatchThreadId : SV_DispatchThreadID)
int voxelIndex = GetVoxelIndex(voxelCoord);
float3 voxelPos = GetVoxelPos(voxelCoord);
float distance = SignedDistancePointToTriangle(voxelPos, v0, v1, v2);
#if 0
if (distance < -10.0f) // TODO: find a better way to reject negative distance from degenerate triangles that break SDF shape
distance = abs(distance);
#endif
InterlockedMin(SDF[voxelIndex], FloatFlip3(distance));
}
}

View File

@@ -52,6 +52,14 @@ uint IFloatFlip3(uint f2)
float DistancePointToEdge(float3 p, float3 x0, float3 x1, out float3 n)
{
// Hack to swap to ensure the order is correct (.x only for simplicity)
if (x0.x > x1.x)
{
float3 temp = x0;
x0 = x1;
x1 = temp;
}
float3 x10 = x1 - x0;
float t = dot(x1 - p, x10) / dot(x10, x10);