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:
parent
52f1a254ad
commit
ef8ef0187a
39 changed files with 16639 additions and 12086 deletions
|
|
@ -31,6 +31,7 @@ enum
|
|||
KEY_LEFT_SHIFT = SAPP_KEYCODE_LEFT_SHIFT,
|
||||
KEY_LEFT_BRACKET = SAPP_KEYCODE_LEFT_BRACKET,
|
||||
KEY_RIGHT_BRACKET = SAPP_KEYCODE_RIGHT_BRACKET,
|
||||
KEY_PAUSE = SAPP_KEYCODE_PAUSE,
|
||||
|
||||
MOD_SHIFT = SAPP_MODIFIER_SHIFT,
|
||||
MOD_CTRL = SAPP_MODIFIER_CTRL,
|
||||
|
|
|
|||
|
|
@ -387,27 +387,28 @@ static void BuildIcosphere( float ( *outVerts )[3], uint32_t* outIndices )
|
|||
}
|
||||
|
||||
// Capsule proxy for shadows
|
||||
// 12-slice cylinder + two hemispherical caps with 2 latitude rings each
|
||||
// (alpha at pi/6 and pi/3, plus a single pole). Verts are encoded with two
|
||||
// vec3s, in_axis (scaled by halfLength) and in_radial (scaled by radius),
|
||||
// so the same proxy mesh works for any (halfLength, radius) pair.
|
||||
// Lat/long mesh: CAPSULE_SLICES azimuthal slices around the long axis, a
|
||||
// cylinder body, and two hemispherical caps with CAPSULE_CAP_RINGS interior
|
||||
// latitude rings plus a pole each. Verts are encoded with two vec3s, in_axis
|
||||
// (scaled by halfLength) and in_radial (scaled by radius), so the same proxy
|
||||
// mesh works for any (halfLength, radius) pair.
|
||||
//
|
||||
// Layout (74 vertices, 144 tris):
|
||||
// 0..11 body ring -X in_axis=(-1,0,0), in_radial=(0, cos beta, sin beta)
|
||||
// 12..23 body ring +X in_axis=(+1,0,0), in_radial=(0, cos beta, sin beta)
|
||||
// 24..35 +X cap ring alpha=pi/6 in_axis=(+1,0,0), in_radial=(sin alpha, cos alpha cos beta, cos alpha sin beta)
|
||||
// 36..47 +X cap ring alpha=pi/3 ditto with alpha=pi/3
|
||||
// 48 +X pole in_axis=(+1,0,0), in_radial=(1, 0, 0)
|
||||
// 49..60 -X cap ring alpha=pi/6 in_axis=(-1,0,0), in_radial=(-sin alpha, cos alpha cos beta, cos alpha sin beta)
|
||||
// 61..72 -X cap ring alpha=pi/3 ditto
|
||||
// 73 -X pole in_axis=(-1,0,0), in_radial=(-1, 0, 0)
|
||||
// An inscribed facet mesh casts a shadow up to 1 - cos(halfAz) cos(halfLat)
|
||||
// too small and its outline shows the slices. Scaling the radial vectors so
|
||||
// facets straddle the true surface halves the max silhouette error, at 32
|
||||
// slices it lands under 0.5% of radius, below a cascade texel.
|
||||
//
|
||||
// Caps share their equator ring with the body's end ring (the body ring
|
||||
// at +X is the +X cap's alpha=0 equator, etc.), saves 24 verts and avoids
|
||||
// any seam in the depth render.
|
||||
#define CAPSULE_SLICES 12
|
||||
#define CAPSULE_VERTICES 74
|
||||
#define CAPSULE_INDICES ( 144 * 3 )
|
||||
// Vertex layout:
|
||||
// - body ring -X, then body ring +X (these double as the caps' equators)
|
||||
// - +X cap: interior rings equator->pole order, then the pole
|
||||
// - -X cap: same
|
||||
//
|
||||
// Caps share their equator ring with the body's end ring, saves a ring of
|
||||
// verts per cap and avoids any seam in the depth render.
|
||||
#define CAPSULE_SLICES 32
|
||||
#define CAPSULE_CAP_RINGS 7
|
||||
#define CAPSULE_VERTICES ( 2 * CAPSULE_SLICES + 2 * ( CAPSULE_CAP_RINGS * CAPSULE_SLICES + 1 ) )
|
||||
#define CAPSULE_INDICES ( 12 * CAPSULE_SLICES * ( CAPSULE_CAP_RINGS + 1 ) )
|
||||
|
||||
typedef struct CapsuleVertex
|
||||
{
|
||||
|
|
@ -432,136 +433,122 @@ typedef struct CapsuleVertex
|
|||
static void BuildCapsuleProxy( CapsuleVertex* outVerts, uint32_t* outIndices )
|
||||
{
|
||||
const float TWO_PI = 6.28318530717958647692f;
|
||||
const float ALPHA1 = TWO_PI / 12.0f; // pi/6
|
||||
const float ALPHA2 = 2.0f * TWO_PI / 12.0f; // pi/3
|
||||
|
||||
// Quad centers of an inscribed lat/long mesh dip below the true surface
|
||||
// by cos(halfAz) cos(halfLat). Scale all radials so facets straddle the
|
||||
// surface, halving the max silhouette error. Uniform scale keeps the
|
||||
// proxy a true capsule of radius scale * r, no seam at the equators.
|
||||
const float halfAz = B3_PI / (float)CAPSULE_SLICES;
|
||||
const float halfLat = 0.25f * B3_PI / (float)( CAPSULE_CAP_RINGS + 1 );
|
||||
const float scale = 2.0f / ( 1.0f + cosf( halfAz ) * cosf( halfLat ) );
|
||||
|
||||
// Vertices
|
||||
int v = 0;
|
||||
// 0..11: body ring -X
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i, ++v )
|
||||
// Body rings, -X then +X. These double as the caps' equators.
|
||||
for ( int side = 0; side < 2; ++side )
|
||||
{
|
||||
const float beta = TWO_PI * (float)i / (float)CAPSULE_SLICES;
|
||||
outVerts[v].axis[0] = -1.0f;
|
||||
const float sx = ( side == 0 ) ? -1.0f : +1.0f;
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i, ++v )
|
||||
{
|
||||
const float beta = TWO_PI * (float)i / (float)CAPSULE_SLICES;
|
||||
outVerts[v].axis[0] = sx;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = 0.0f;
|
||||
outVerts[v].radial[1] = scale * cosf( beta );
|
||||
outVerts[v].radial[2] = scale * sinf( beta );
|
||||
}
|
||||
}
|
||||
// Caps, +X then -X: interior latitude rings equator->pole, then the pole.
|
||||
// Ring r sits at latitude alpha = r * (pi/2) / (rings + 1).
|
||||
for ( int side = 0; side < 2; ++side )
|
||||
{
|
||||
const float sx = ( side == 0 ) ? +1.0f : -1.0f;
|
||||
for ( int r = 1; r <= CAPSULE_CAP_RINGS; ++r )
|
||||
{
|
||||
const float alpha = 0.5f * B3_PI * (float)r / (float)( CAPSULE_CAP_RINGS + 1 );
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i, ++v )
|
||||
{
|
||||
const float beta = TWO_PI * (float)i / (float)CAPSULE_SLICES;
|
||||
outVerts[v].axis[0] = sx;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = scale * sx * sinf( alpha );
|
||||
outVerts[v].radial[1] = scale * cosf( alpha ) * cosf( beta );
|
||||
outVerts[v].radial[2] = scale * cosf( alpha ) * sinf( beta );
|
||||
}
|
||||
}
|
||||
outVerts[v].axis[0] = sx;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = 0.0f;
|
||||
outVerts[v].radial[1] = cosf( beta );
|
||||
outVerts[v].radial[2] = sinf( beta );
|
||||
outVerts[v].radial[0] = scale * sx;
|
||||
outVerts[v].radial[1] = 0.0f;
|
||||
outVerts[v].radial[2] = 0.0f;
|
||||
++v;
|
||||
}
|
||||
// 12..23: body ring +X
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i, ++v )
|
||||
{
|
||||
const float beta = TWO_PI * (float)i / (float)CAPSULE_SLICES;
|
||||
outVerts[v].axis[0] = +1.0f;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = 0.0f;
|
||||
outVerts[v].radial[1] = cosf( beta );
|
||||
outVerts[v].radial[2] = sinf( beta );
|
||||
}
|
||||
// 24..35: +X cap ring alpha=pi/6
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i, ++v )
|
||||
{
|
||||
const float beta = TWO_PI * (float)i / (float)CAPSULE_SLICES;
|
||||
outVerts[v].axis[0] = +1.0f;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = sinf( ALPHA1 );
|
||||
outVerts[v].radial[1] = cosf( ALPHA1 ) * cosf( beta );
|
||||
outVerts[v].radial[2] = cosf( ALPHA1 ) * sinf( beta );
|
||||
}
|
||||
// 36..47: +X cap ring alpha=pi/3
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i, ++v )
|
||||
{
|
||||
const float beta = TWO_PI * (float)i / (float)CAPSULE_SLICES;
|
||||
outVerts[v].axis[0] = +1.0f;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = sinf( ALPHA2 );
|
||||
outVerts[v].radial[1] = cosf( ALPHA2 ) * cosf( beta );
|
||||
outVerts[v].radial[2] = cosf( ALPHA2 ) * sinf( beta );
|
||||
}
|
||||
// 48: +X pole
|
||||
outVerts[v].axis[0] = +1.0f;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = +1.0f;
|
||||
outVerts[v].radial[1] = 0.0f;
|
||||
outVerts[v].radial[2] = 0.0f;
|
||||
++v;
|
||||
// 49..60: -X cap ring alpha=pi/6
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i, ++v )
|
||||
{
|
||||
const float beta = TWO_PI * (float)i / (float)CAPSULE_SLICES;
|
||||
outVerts[v].axis[0] = -1.0f;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = -sinf( ALPHA1 );
|
||||
outVerts[v].radial[1] = cosf( ALPHA1 ) * cosf( beta );
|
||||
outVerts[v].radial[2] = cosf( ALPHA1 ) * sinf( beta );
|
||||
}
|
||||
// 61..72: -X cap ring alpha=pi/3
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i, ++v )
|
||||
{
|
||||
const float beta = TWO_PI * (float)i / (float)CAPSULE_SLICES;
|
||||
outVerts[v].axis[0] = -1.0f;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = -sinf( ALPHA2 );
|
||||
outVerts[v].radial[1] = cosf( ALPHA2 ) * cosf( beta );
|
||||
outVerts[v].radial[2] = cosf( ALPHA2 ) * sinf( beta );
|
||||
}
|
||||
// 73: -X pole
|
||||
outVerts[v].axis[0] = -1.0f;
|
||||
outVerts[v].axis[1] = 0.0f;
|
||||
outVerts[v].axis[2] = 0.0f;
|
||||
outVerts[v].radial[0] = -1.0f;
|
||||
outVerts[v].radial[1] = 0.0f;
|
||||
outVerts[v].radial[2] = 0.0f;
|
||||
++v;
|
||||
|
||||
// Indices
|
||||
int index = 0;
|
||||
|
||||
// Body: ring -X (verts 0..11) to ring +X (verts 12..23). For slice i
|
||||
// and i+1, the outward face winds from -X[i] up the slice direction
|
||||
// (beta increasing) to -X[i+1], across to +X[i+1], down to +X[i].
|
||||
// Body: ring -X to ring +X. For slice i and i+1, the outward face winds
|
||||
// from -X[i] up the slice direction (beta increasing) to -X[i+1], across
|
||||
// to +X[i+1], down to +X[i].
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i )
|
||||
{
|
||||
const int next = ( i + 1 ) % CAPSULE_SLICES;
|
||||
const int mxi = i; // ring -X
|
||||
const int mxn = next;
|
||||
const int pxi = 12 + i; // ring +X
|
||||
const int pxn = 12 + next;
|
||||
CAPSULE_QUAD( mxi, mxn, pxn, pxi );
|
||||
CAPSULE_QUAD( i, next, CAPSULE_SLICES + next, CAPSULE_SLICES + i );
|
||||
}
|
||||
|
||||
// +X cap: equator (ring +X, 12..23) -> alpha=pi/6 ring (24..35) -> alpha=pi/3 (36..47) -> pole (48).
|
||||
// Going outward, alpha increases. For slice i to i+1, the outward face
|
||||
// winds equator[i] -> equator[i+1] -> ring1[i+1] -> ring1[i].
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i )
|
||||
// +X cap: quad bands march outward from the equator (the +X body ring)
|
||||
// through the latitude rings, then a fan to the pole. Going outward the
|
||||
// face winds prev[i] -> prev[i+1] -> cur[i+1] -> cur[i].
|
||||
{
|
||||
const int next = ( i + 1 ) % CAPSULE_SLICES;
|
||||
CAPSULE_QUAD( 12 + i, 12 + next, 24 + next, 24 + i );
|
||||
CAPSULE_QUAD( 24 + i, 24 + next, 36 + next, 36 + i );
|
||||
// Fan to pole: tri (ring2[i], ring2[i+1], pole).
|
||||
outIndices[index++] = (uint32_t)( 36 + i );
|
||||
outIndices[index++] = (uint32_t)( 36 + next );
|
||||
outIndices[index++] = (uint32_t)48;
|
||||
int prev = CAPSULE_SLICES;
|
||||
int cur = 2 * CAPSULE_SLICES;
|
||||
for ( int r = 0; r < CAPSULE_CAP_RINGS; ++r )
|
||||
{
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i )
|
||||
{
|
||||
const int next = ( i + 1 ) % CAPSULE_SLICES;
|
||||
CAPSULE_QUAD( prev + i, prev + next, cur + next, cur + i );
|
||||
}
|
||||
prev = cur;
|
||||
cur += CAPSULE_SLICES;
|
||||
}
|
||||
const int pole = cur;
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i )
|
||||
{
|
||||
const int next = ( i + 1 ) % CAPSULE_SLICES;
|
||||
outIndices[index++] = (uint32_t)( prev + i );
|
||||
outIndices[index++] = (uint32_t)( prev + next );
|
||||
outIndices[index++] = (uint32_t)pole;
|
||||
}
|
||||
}
|
||||
|
||||
// -X cap: equator (ring -X, 0..11) -> alpha=pi/6 ring (49..60) -> alpha=pi/3 (61..72) -> pole (73).
|
||||
// Winding flipped relative to +X cap because the outward normal points
|
||||
// toward -X, the "outside-front" side of each quad swaps slice order.
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i )
|
||||
// -X cap: winding flipped relative to +X because the outward normal
|
||||
// points toward -X, the "outside-front" side of each quad swaps slice
|
||||
// order.
|
||||
{
|
||||
const int next = ( i + 1 ) % CAPSULE_SLICES;
|
||||
CAPSULE_QUAD( 0 + next, 0 + i, 49 + i, 49 + next );
|
||||
CAPSULE_QUAD( 49 + next, 49 + i, 61 + i, 61 + next );
|
||||
// Fan to pole: tri (ring2[i+1], ring2[i], pole), opposite winding to +X.
|
||||
outIndices[index++] = (uint32_t)( 61 + next );
|
||||
outIndices[index++] = (uint32_t)( 61 + i );
|
||||
outIndices[index++] = (uint32_t)73;
|
||||
int prev = 0;
|
||||
int cur = 2 * CAPSULE_SLICES + CAPSULE_CAP_RINGS * CAPSULE_SLICES + 1;
|
||||
for ( int r = 0; r < CAPSULE_CAP_RINGS; ++r )
|
||||
{
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i )
|
||||
{
|
||||
const int next = ( i + 1 ) % CAPSULE_SLICES;
|
||||
CAPSULE_QUAD( prev + next, prev + i, cur + i, cur + next );
|
||||
}
|
||||
prev = cur;
|
||||
cur += CAPSULE_SLICES;
|
||||
}
|
||||
const int pole = cur;
|
||||
for ( int i = 0; i < CAPSULE_SLICES; ++i )
|
||||
{
|
||||
const int next = ( i + 1 ) % CAPSULE_SLICES;
|
||||
outIndices[index++] = (uint32_t)( prev + next );
|
||||
outIndices[index++] = (uint32_t)( prev + i );
|
||||
outIndices[index++] = (uint32_t)pole;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1681,6 +1668,7 @@ static void DrawScene( int targetWidth, int targetHeight, const FrameInput* fram
|
|||
const sg_backend backend = sg_query_backend();
|
||||
const float uvYSign = ( backend == SG_BACKEND_GLCORE || backend == SG_BACKEND_GLES3 ) ? 1.0f : -1.0f;
|
||||
const Vec4 cascadeFarViewZ = MakeVec4( GetCascadeFarViewZ( 0 ), GetCascadeFarViewZ( 1 ), GetCascadeFarViewZ( 2 ), uvYSign );
|
||||
const Vec4 cascadePcfScale = MakeVec4( GetCascadePcfScale( 0 ), GetCascadePcfScale( 1 ), GetCascadePcfScale( 2 ), 0.0f );
|
||||
Mat4 cascadeMatrices[3];
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
{
|
||||
|
|
@ -1726,6 +1714,7 @@ static void DrawScene( int targetWidth, int targetHeight, const FrameInput* fram
|
|||
up.sun_color = sunColor;
|
||||
up.flags[0] = frame->debugMode;
|
||||
up.cascade_far_view_z = cascadeFarViewZ;
|
||||
up.cascade_pcf_scale = cascadePcfScale;
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
up.cascade_matrices[i] = cascadeMatrices[i];
|
||||
up.view = frame->view;
|
||||
|
|
@ -1774,6 +1763,7 @@ static void DrawScene( int targetWidth, int targetHeight, const FrameInput* fram
|
|||
sup.view_proj = viewProj;
|
||||
sup.view = frame->view;
|
||||
sup.cascade_far_view_z = cascadeFarViewZ;
|
||||
sup.cascade_pcf_scale = cascadePcfScale;
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
sup.cascade_matrices[i] = cascadeMatrices[i];
|
||||
for ( int i = 0; i < 9; ++i )
|
||||
|
|
@ -1817,6 +1807,7 @@ static void DrawScene( int targetWidth, int targetHeight, const FrameInput* fram
|
|||
cup.view_proj = viewProj;
|
||||
cup.view = frame->view;
|
||||
cup.cascade_far_view_z = cascadeFarViewZ;
|
||||
cup.cascade_pcf_scale = cascadePcfScale;
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
cup.cascade_matrices[i] = cascadeMatrices[i];
|
||||
for ( int i = 0; i < 9; ++i )
|
||||
|
|
@ -1869,6 +1860,7 @@ static void DrawScene( int targetWidth, int targetHeight, const FrameInput* fram
|
|||
gup.grid_offset = MakeVec4( frame->gridWrap.x, frame->gridWrap.y, frame->drawOrigin.x, frame->drawOrigin.z );
|
||||
gup.view = frame->view;
|
||||
gup.cascade_far_view_z = cascadeFarViewZ;
|
||||
gup.cascade_pcf_scale = cascadePcfScale;
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
gup.cascade_matrices[i] = cascadeMatrices[i];
|
||||
for ( int i = 0; i < 9; ++i )
|
||||
|
|
@ -2279,6 +2271,7 @@ static void DrawTransparentIntoResolve( int width, int height, const FrameInput*
|
|||
const sg_backend backend = sg_query_backend();
|
||||
const float uvYSign = ( backend == SG_BACKEND_GLCORE || backend == SG_BACKEND_GLES3 ) ? 1.0f : -1.0f;
|
||||
const Vec4 cascadeFarViewZ = MakeVec4( GetCascadeFarViewZ( 0 ), GetCascadeFarViewZ( 1 ), GetCascadeFarViewZ( 2 ), uvYSign );
|
||||
const Vec4 cascadePcfScale = MakeVec4( GetCascadePcfScale( 0 ), GetCascadePcfScale( 1 ), GetCascadePcfScale( 2 ), 0.0f );
|
||||
Mat4 cascadeMatrices[3];
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
{
|
||||
|
|
@ -2313,6 +2306,7 @@ static void DrawTransparentIntoResolve( int width, int height, const FrameInput*
|
|||
cubePass.sun_color = sunColor;
|
||||
cubePass.flags[0] = frame->debugMode;
|
||||
cubePass.cascade_far_view_z = cascadeFarViewZ;
|
||||
cubePass.cascade_pcf_scale = cascadePcfScale;
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
{
|
||||
cubePass.cascade_matrices[i] = cascadeMatrices[i];
|
||||
|
|
@ -2335,6 +2329,7 @@ static void DrawTransparentIntoResolve( int width, int height, const FrameInput*
|
|||
spherePass.view_proj = view_proj;
|
||||
spherePass.view = frame->view;
|
||||
spherePass.cascade_far_view_z = cascadeFarViewZ;
|
||||
spherePass.cascade_pcf_scale = cascadePcfScale;
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
spherePass.cascade_matrices[i] = cascadeMatrices[i];
|
||||
for ( int i = 0; i < 9; ++i )
|
||||
|
|
@ -2351,6 +2346,7 @@ static void DrawTransparentIntoResolve( int width, int height, const FrameInput*
|
|||
capsulePass.view_proj = view_proj;
|
||||
capsulePass.view = frame->view;
|
||||
capsulePass.cascade_far_view_z = cascadeFarViewZ;
|
||||
capsulePass.cascade_pcf_scale = cascadePcfScale;
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
capsulePass.cascade_matrices[i] = cascadeMatrices[i];
|
||||
for ( int i = 0; i < 9; ++i )
|
||||
|
|
@ -2368,6 +2364,7 @@ static void DrawTransparentIntoResolve( int width, int height, const FrameInput*
|
|||
geomPass.grid_offset = MakeVec4( frame->gridWrap.x, frame->gridWrap.y, frame->drawOrigin.x, frame->drawOrigin.z );
|
||||
geomPass.view = frame->view;
|
||||
geomPass.cascade_far_view_z = cascadeFarViewZ;
|
||||
geomPass.cascade_pcf_scale = cascadePcfScale;
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
geomPass.cascade_matrices[i] = cascadeMatrices[i];
|
||||
for ( int i = 0; i < 9; ++i )
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ extern "C"
|
|||
#endif
|
||||
|
||||
#define SHADOW_CASCADE_COUNT 3
|
||||
#define SHADOW_RESOLUTION 2048
|
||||
#define SHADOW_RESOLUTION 4096
|
||||
|
||||
// Default cascade far split, view-space distance from the camera. Scenes
|
||||
// smaller than this keep the default range, larger ones widen it.
|
||||
|
|
@ -87,6 +87,13 @@ Mat4 GetCascadeMatrix( int cascade );
|
|||
// these to pick which cascade to sample.
|
||||
float GetCascadeFarViewZ( int cascade );
|
||||
|
||||
// PCF tap-offset multiplier for a cascade, in (0, 1]. The kernel is
|
||||
// authored in texels of cascade 0, farther cascades cover more world per
|
||||
// texel, so their tap offsets shrink by the texel-size ratio. This keeps
|
||||
// the penumbra a constant world width, without it the shadow edge blur
|
||||
// visibly jumps at every cascade boundary.
|
||||
float GetCascadePcfScale( int cascade );
|
||||
|
||||
// Bound by lit pipelines once for sampling across all cascades. The view
|
||||
// is the depth array's full-array sampling view. Per-cascade selection
|
||||
// happens inside the shader by passing `cascade` as the layer index.
|
||||
|
|
|
|||
Loading…
Reference in a new issue