Remove compound mesh child material limit. (#172)

This commit is contained in:
Erin Catto 2026-09-25 14:56:44 -07:00 • committed by GitHub
commit 5643cd81ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 314 additions and 58 deletions

View file

@ -32,17 +32,25 @@ typedef struct b3HullInstance
{
b3Transform transform;
uint32_t hullOffset;
uint32_t materialIndex;
uint16_t materialIndex;
uint16_t padding;
} b3HullInstance;
_Static_assert( sizeof( b3HullInstance ) == sizeof( b3Transform ) + sizeof( uint32_t ) + 2 * sizeof( uint16_t ),
"review padding" );
typedef struct b3MeshInstance
{
b3Transform transform;
b3Vec3 scale;
uint32_t meshOffset;
uint32_t materialIndices[B3_MAX_COMPOUND_MESH_MATERIALS];
uint32_t materialOffset;
uint32_t materialCount;
} b3MeshInstance;
_Static_assert( sizeof( b3CompoundCapsule ) == sizeof( b3Capsule ) + 2 * sizeof( uint16_t ), "review padding" );
_Static_assert( sizeof( b3CompoundSphere ) == sizeof( b3Sphere ) + 2 * sizeof( uint16_t ), "review padding" );
static inline b3TreeNode* b3GetCompoundNodes( b3CompoundData* compound )
{
if ( compound->nodeOffset == 0 )
@ -122,10 +130,8 @@ b3CompoundMesh b3GetCompoundMesh( const b3CompoundData* compound, int index )
result.meshData = (const b3MeshData*)( (intptr_t)compound + meshOffset );
result.transform = meshInstances[index].transform;
result.scale = meshInstances[index].scale;
for ( int i = 0; i < B3_MAX_COMPOUND_MESH_MATERIALS; ++i )
{
result.materialIndices[i] = meshInstances[index].materialIndices[i];
}
result.materialIndices = (const uint16_t*)( (intptr_t)compound + meshInstances[index].materialOffset );
result.materialCount = (int)meshInstances[index].materialCount;
return result;
}
@ -148,11 +154,13 @@ b3ChildShape b3GetCompoundChild( const b3CompoundData* compound, int childIndex
// Capsule?
if ( 0 <= childIndex && childIndex < compound->capsuleCount )
{
b3CompoundCapsule compoundCapsule = b3GetCompoundCapsule( compound, childIndex );
B3_ASSERT( compound->capsuleOffset > 0 );
const b3CompoundCapsule* capsules = (const b3CompoundCapsule*)( (intptr_t)compound + compound->capsuleOffset );
return (b3ChildShape){
.capsule = compoundCapsule.capsule,
.capsule = capsules[childIndex].capsule,
.transform = b3Transform_identity,
.materialIndices = { compoundCapsule.materialIndex },
.materialIndices = &capsules[childIndex].materialIndex,
.materialCount = 1,
.type = b3_capsuleShape,
};
}
@ -161,11 +169,14 @@ b3ChildShape b3GetCompoundChild( const b3CompoundData* compound, int childIndex
// Hull?
if ( 0 <= childIndex && childIndex < compound->hullCount )
{
b3CompoundHull compoundHull = b3GetCompoundHull( compound, childIndex );
B3_ASSERT( compound->hullOffset > 0 );
const b3HullInstance* hullInstances = (const b3HullInstance*)( (intptr_t)compound + compound->hullOffset );
uint32_t hullOffset = hullInstances[childIndex].hullOffset;
return (b3ChildShape){
.hull = compoundHull.hull,
.transform = compoundHull.transform,
.materialIndices = { compoundHull.materialIndex },
.hull = (const b3HullData*)( (intptr_t)compound + hullOffset ),
.transform = hullInstances[childIndex].transform,
.materialIndices = &hullInstances[childIndex].materialIndex,
.materialCount = 1,
.type = b3_hullShape,
};
}
@ -175,8 +186,6 @@ b3ChildShape b3GetCompoundChild( const b3CompoundData* compound, int childIndex
if ( 0 <= childIndex && childIndex < compound->meshCount )
{
b3CompoundMesh compoundMesh = b3GetCompoundMesh( compound, childIndex );
const int* m = compoundMesh.materialIndices;
_Static_assert( B3_MAX_COMPOUND_MESH_MATERIALS == 4, "too many materials in compound mesh" );
return (b3ChildShape){
.mesh =
@ -185,7 +194,8 @@ b3ChildShape b3GetCompoundChild( const b3CompoundData* compound, int childIndex
.scale = compoundMesh.scale,
},
.transform = compoundMesh.transform,
.materialIndices = { m[0], m[1], m[2], m[3] },
.materialIndices = compoundMesh.materialIndices,
.materialCount = compoundMesh.materialCount,
.type = b3_meshShape,
};
}
@ -195,11 +205,13 @@ b3ChildShape b3GetCompoundChild( const b3CompoundData* compound, int childIndex
// Sphere?
{
b3CompoundSphere compoundSphere = b3GetCompoundSphere( compound, childIndex );
B3_ASSERT( compound->sphereOffset > 0 );
const b3CompoundSphere* spheres = (const b3CompoundSphere*)( (intptr_t)compound + compound->sphereOffset );
return (b3ChildShape){
.sphere = compoundSphere.sphere,
.sphere = spheres[childIndex].sphere,
.transform = b3Transform_identity,
.materialIndices = { compoundSphere.materialIndex },
.materialIndices = &spheres[childIndex].materialIndex,
.materialCount = 1,
.type = b3_sphereShape,
};
}
@ -308,6 +320,10 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
b3SurfaceMaterial* materials = b3AllocZero( materialCapacity * sizeof( b3SurfaceMaterial ) );
int materialCount = 0;
int meshMaterialTotal = materialCapacity - convexCount;
uint16_t* meshMaterialStaging = b3AllocZero( meshMaterialTotal * sizeof( uint16_t ) );
int meshMaterialCursor = 0;
for ( int i = 0; i < def->capsuleCount; ++i )
{
const b3CompoundCapsuleDef* capsuleDef = def->capsules + i;
@ -318,7 +334,7 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
// Get the shared material index.
int materialIndex = materialItr.data->val;
capsuleInstances[i].materialIndex = materialIndex;
capsuleInstances[i].materialIndex = (uint16_t)materialIndex;
// Is this a new material?
if ( materialIndex == materialCount )
@ -355,7 +371,7 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
// Get the shared material index.
int materialIndex = materialItr.data->val;
hullInstances[i].materialIndex = materialIndex;
hullInstances[i].materialIndex = (uint16_t)materialIndex;
// Is this a new material?
if ( materialIndex == materialCount )
@ -410,9 +426,9 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
// No effort to share mesh materials. It would be easier to do if the number of materials was limited.
B3_ASSERT( meshData->materialCount == meshDef->materialCount );
int meshMaterialCount = b3MinInt( meshDef->materialCount, B3_MAX_COMPOUND_MESH_MATERIALS );
int meshMaterialStart = meshMaterialCursor;
for ( int j = 0; j < meshMaterialCount; ++j )
for ( int j = 0; j < meshDef->materialCount; ++j )
{
// Look for an existing material.
b3MaterialMap_itr materialItr =
@ -420,7 +436,8 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
// Get the shared material index.
int materialIndex = materialItr.data->val;
meshInstances[i].materialIndices[j] = materialIndex;
meshMaterialStaging[meshMaterialCursor] = (uint16_t)materialIndex;
meshMaterialCursor += 1;
// Is this a new material?
if ( materialIndex == materialCount )
@ -440,6 +457,9 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
meshInstances[i].transform = def->meshes[i].transform;
meshInstances[i].scale = def->meshes[i].scale;
meshInstances[i].materialOffset = (uint32_t)meshMaterialStart;
meshInstances[i].materialCount = (uint32_t)meshDef->materialCount;
// The offset isn't known yet, so store the index of the shared mesh.
meshInstances[i].meshOffset = sharedMeshIndex;
@ -467,7 +487,7 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
// Get the shared material index.
int materialIndex = materialItr.data->val;
sphereInstances[i].materialIndex = materialIndex;
sphereInstances[i].materialIndex = (uint16_t)materialIndex;
// Is this a new material?
if ( materialIndex == materialCount )
@ -482,6 +502,7 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
}
B3_ASSERT( materialCount <= materialCapacity );
B3_ASSERT( materialCount <= UINT16_MAX );
B3_ASSERT( tree.proxyCount > 0 );
b3DynamicTree_Rebuild( &tree, true );
@ -522,6 +543,9 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
// Array of mesh instances
byteCount += b3AlignUp8( meshCount * sizeof( b3MeshInstance ) );
int meshMaterialMapOffset = (int)byteCount;
byteCount += b3AlignUp8( meshMaterialTotal * sizeof( uint16_t ) );
// Packed shared mesh blobs
for ( int i = 0; i < sharedMeshCount; ++i )
{
@ -621,11 +645,20 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
int sharedIndex = meshInstances[i].meshOffset;
B3_ASSERT( 0 <= sharedIndex && sharedIndex < sharedMeshCount );
meshInstances[i].meshOffset = sharedMeshes[sharedIndex].meshOffset;
uint32_t materialStart = meshInstances[i].materialOffset;
meshInstances[i].materialOffset = (uint32_t)meshMaterialMapOffset + materialStart * (uint32_t)sizeof( uint16_t );
}
b3MeshInstance* destinationMeshInstances = (b3MeshInstance*)( (intptr_t)compound + meshArrayOffset );
memcpy( destinationMeshInstances, meshInstances, meshCount * sizeof( b3MeshInstance ) );
if ( meshMaterialTotal > 0 )
{
uint16_t* destinationMeshMaterials = (uint16_t*)( (intptr_t)compound + meshMaterialMapOffset );
memcpy( destinationMeshMaterials, meshMaterialStaging, meshMaterialTotal * sizeof( uint16_t ) );
}
for ( int i = 0; i < sharedMeshCount; ++i )
{
int offset = sharedMeshes[i].meshOffset;
@ -651,6 +684,7 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
b3Free( meshInstances, meshCount * sizeof( b3MeshInstance ) );
b3Free( sphereInstances, sphereCount * sizeof( b3CompoundSphere ) );
b3Free( materials, materialCapacity * sizeof( b3SurfaceMaterial ) );
b3Free( meshMaterialStaging, meshMaterialTotal * sizeof( uint16_t ) );
b3DynamicTree_Destroy( &tree );
return compound;
@ -822,9 +856,8 @@ static float b3CompoundRayCastCallback( const b3RayCastInput* input, int proxyId
case b3_meshShape:
{
output = b3RayCastMesh( &child.mesh, &localInput );
B3_ASSERT( 0 <= output.materialIndex );
int childMaterialIndex = b3MinInt( output.materialIndex, B3_MAX_COMPOUND_MESH_MATERIALS - 1 );
output.materialIndex = child.materialIndices[childMaterialIndex];
B3_ASSERT( 0 <= output.materialIndex && output.materialIndex < child.materialCount );
output.materialIndex = child.materialIndices[output.materialIndex];
}
break;
@ -899,9 +932,8 @@ static float b3CompoundShapeCastCallback( const b3BoxCastInput* input, int proxy
case b3_meshShape:
{
output = b3ShapeCastMesh( &child.mesh, &localInput );
B3_ASSERT( 0 <= output.materialIndex );
int childMaterialIndex = b3MinInt( output.materialIndex, B3_MAX_COMPOUND_MESH_MATERIALS - 1 );
output.materialIndex = child.materialIndices[childMaterialIndex];
B3_ASSERT( 0 <= output.materialIndex && output.materialIndex < child.materialCount );
output.materialIndex = child.materialIndices[output.materialIndex];
}
break;
@ -1184,8 +1216,8 @@ static bool b3CompoundMoverCallback( int proxyId, uint64_t userData, void* conte
planes[i].point = b3TransformPoint( child.transform, planes[i].point );
planes[i].childIndex = childIndex;
int childMaterialIndex = b3MinInt( planes[i].materialIndex, B3_MAX_COMPOUND_MESH_MATERIALS - 1 );
planes[i].materialIndex = child.materialIndices[childMaterialIndex];
B3_ASSERT( 0 <= planes[i].materialIndex && planes[i].materialIndex < child.materialCount );
planes[i].materialIndex = child.materialIndices[planes[i].materialIndex];
}
moverContext->planeCount += planeCount;

View file

@ -180,5 +180,6 @@ void b3DestroyContact( b3World* world, b3Contact* contact, bool wakeBodies );
bool b3UpdateContact( b3World* world, int workerIndex, b3Contact* contact, b3Shape* shapeA, b3Vec3 localCenterA, b3WorldTransform xfA,
b3Shape* shapeB, b3Vec3 localCenterB, b3WorldTransform xfB, bool isFast, b3Arena arena );
bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact, const b3Shape* shapeA, const int* materialMap,
b3WorldTransform xfA, const b3Shape* shapeB, b3WorldTransform xfB, bool isFast, b3Arena arena );
bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact, const b3Shape* shapeA,
const uint16_t* materialMap, b3WorldTransform xfA, const b3Shape* shapeB, b3WorldTransform xfB,
bool isFast, b3Arena arena );

View file

@ -1435,7 +1435,7 @@ static inline int b3ArcCanReach( float a, float b, float c, float length, float
#define NV ( B3_MAX_HULL_VERTICES + B3_SIMD_WIDTH )
// SIMD separating axis test based on an implementation developed by Cairn Overturf.
// See his article: https://cairno.substack.com/p/improvements-to-the-separating-axis
// See his article: https://cairnc.github.io/posts/improvements-to-the-separating-axis/
b3AxisQuery b3ComputeSeparatingAxis( const b3HullData* hullA, const b3HullData* hullB, b3Transform xfB, bool earlyReturn )
{
b3Matrix3 R = b3MakeMatrixFromQuat( xfB.q );

View file

@ -524,8 +524,9 @@ typedef struct b3Cluster
int pointCount;
} b3Cluster;
bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact, const b3Shape* shapeA, const int* materialMap,
b3WorldTransform xfA, const b3Shape* shapeB, b3WorldTransform xfB, bool isFast, b3Arena arena )
bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact, const b3Shape* shapeA,
const uint16_t* materialMap, b3WorldTransform xfA, const b3Shape* shapeB, b3WorldTransform xfB,
bool isFast, b3Arena arena )
{
B3_ASSERT( shapeA->type == b3_meshShape || shapeA->type == b3_heightShape );
B3_UNUSED( workerIndex );

View file

@ -2413,7 +2413,7 @@ uint64_t b3GetShapeUserMaterialId( const b3Shape* shape, int childIndex, int tri
{
const uint8_t* indices = b3GetMeshMaterialIndices( child.mesh.data );
int meshMaterialIndex = indices != NULL ? indices[triangleIndex] : 0;
meshMaterialIndex = b3ClampInt( meshMaterialIndex, 0, B3_MAX_COMPOUND_MESH_MATERIALS - 1 );
B3_ASSERT( 0 <= meshMaterialIndex && meshMaterialIndex < child.materialCount );
materialIndex = child.materialIndices[meshMaterialIndex];
}
else