Ghost collision improvements (#61)

- flag to disable hull-mesh speculative collision
- faster atomics #55 
- fix triangle vs hull logic
- fix #58 and #59
- improved shadows in samples app, a bit more expensive
This commit is contained in:
Erin Catto 2026-07-08 23:53:06 -07:00 • committed by GitHub
commit ef8ef0187a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 16639 additions and 12086 deletions

View file

@ -40,6 +40,7 @@ static struct
sg_sampler sampler; // comparison sampler (LESS_EQUAL, linear PCF)
Mat4 cascadeMatrices[SHADOW_CASCADE_COUNT]; // light-space view-proj per cascade
float cascadeFarViewZ[SHADOW_CASCADE_COUNT]; // far end (positive view-Z) per cascade
float cascadeRadius[SHADOW_CASCADE_COUNT]; // bounding-sphere radius = ortho half-extent
bool inPass; // safety against unbalanced begin/end
} s_shadow;
@ -119,7 +120,7 @@ static void FrustumSliceCornersWorld( const Mat4* viewInv, const Mat4* proj, flo
// post-translation. This pins the cascade to a fixed grid in light
// space, small camera motions no longer cause the shadow texels to
// drift relative to the casters.
static Mat4 FitCascade( const Mat4* viewInv, const Mat4* proj, b3Vec3 dirToSun, float nearZ, float farZ )
static Mat4 FitCascade( const Mat4* viewInv, const Mat4* proj, b3Vec3 dirToSun, float nearZ, float farZ, float* radiusOut )
{
b3Vec3 corners[8];
FrustumSliceCornersWorld( viewInv, proj, nearZ, farZ, corners );
@ -145,6 +146,7 @@ static Mat4 FitCascade( const Mat4* viewInv, const Mat4* proj, b3Vec3 dirToSun,
{
radius = 0.01f;
}
*radiusOut = radius;
// Avoid a degenerate up vector when the sun is straight up/down.
b3Vec3 up = b3Vec3_axisY;
@ -184,6 +186,12 @@ void InitShadows( void )
s_shadow.splitFar = SHADOW_SPLIT_FAR;
s_shadow.splitLambda = SplitLambdaForRange( s_shadow.splitNear, s_shadow.splitFar );
// Sane ratios if a getter runs before the first FitShadows
for ( int i = 0; i < SHADOW_CASCADE_COUNT; ++i )
{
s_shadow.cascadeRadius[i] = 1.0f;
}
sg_image_desc desc = { 0 };
desc.type = SG_IMAGETYPE_ARRAY;
desc.usage.depth_stencil_attachment = true;
@ -285,7 +293,7 @@ void FitShadows( const Mat4* viewInv, const Mat4* proj )
for ( int i = 0; i < SHADOW_CASCADE_COUNT; ++i )
{
s_shadow.cascadeMatrices[i] = FitCascade( viewInv, proj, s_shadow.sunDir, splits[i], splits[i + 1] );
s_shadow.cascadeMatrices[i] = FitCascade( viewInv, proj, s_shadow.sunDir, splits[i], splits[i + 1], &s_shadow.cascadeRadius[i] );
s_shadow.cascadeFarViewZ[i] = splits[i + 1];
}
}
@ -334,6 +342,20 @@ float GetCascadeFarViewZ( int cascade )
return s_shadow.cascadeFarViewZ[cascade];
}
float GetCascadePcfScale( int cascade )
{
if ( cascade < 0 || cascade >= SHADOW_CASCADE_COUNT )
{
return 1.0f;
}
// Texel world size is proportional to the cascade's ortho half-extent,
// so the radius ratio is the texel-size ratio. The floor keeps the
// shrunken kernel at least ~2 texels wide so bilinear smoothing still
// gets some support in the far cascades.
const float scale = s_shadow.cascadeRadius[0] / s_shadow.cascadeRadius[cascade];
return b3ClampFloat( scale, 0.25f, 1.0f );
}
sg_view GetShadowSampleView( void )
{
return s_shadow.sampleView;