From 5643cd81ff07fd0497e3bfcdf04f6425cc8a2e7f Mon Sep 17 00:00:00 2001 From: Erin Catto Date: Fri, 25 Sep 2026 14:56:44 -0700 Subject: [PATCH] Remove compound mesh child material limit. (#172) --- docs/compound.md | 6 +- include/box3d/types.h | 36 ++++--- samples/sample_compound.cpp | 3 + shared/benchmarks.c | 2 +- src/compound.c | 98 +++++++++++------- src/contact.h | 5 +- src/convex_manifold.c | 2 +- src/mesh_contact.c | 5 +- src/shape.c | 2 +- test/test_compound.c | 192 +++++++++++++++++++++++++++++++++++- test/test_recording.c | 21 ++++ 11 files changed, 314 insertions(+), 58 deletions(-) diff --git a/docs/compound.md b/docs/compound.md index 82d79fe..f3d5465 100644 --- a/docs/compound.md +++ b/docs/compound.md @@ -55,10 +55,8 @@ b3Compound* compound = b3CreateCompound(&def); can be freed immediately after the call. Mesh children share the mesh pointer rather than cloning triangle data — the -`b3MeshData` must remain valid for the lifetime of the compound. Triangle -materials are limited to `B3_MAX_COMPOUND_MESH_MATERIALS` (4) slots per mesh -child; if your mesh needs more materials, attach it as a standalone mesh shape -on the static body instead. +`b3MeshData` must remain valid for the lifetime of the compound. A compound's +shared material table holds at most 64K unique materials. ## The b3Compound Structure diff --git a/include/box3d/types.h b/include/box3d/types.h index a06689e..fb88971 100644 --- a/include/box3d/types.h +++ b/include/box3d/types.h @@ -2481,11 +2481,7 @@ typedef struct b3CompoundDef } b3CompoundDef; /// The baked compound version depends on the tree, mesh, and hull versions. -#define B3_COMPOUND_VERSION ( 0xB11DCE70FAD5622Bull ^ B3_DYNAMIC_TREE_VERSION ^ B3_MESH_VERSION ^ B3_HULL_VERSION ) - -/// Meshes used in compounds have limited space for materials. If you have -/// a mesh with many materials, you can use it outside of the compound. -#define B3_MAX_COMPOUND_MESH_MATERIALS 4 +#define B3_COMPOUND_VERSION ( 0x7A2F41C9E6D0B358ull ^ B3_DYNAMIC_TREE_VERSION ^ B3_MESH_VERSION ^ B3_HULL_VERSION ) /// The data for a baked compound shape. This is a potentially large yet highly optimized /// data structure. It can contain thousands of child shapes, yet at runtime it populates @@ -2556,7 +2552,10 @@ typedef struct b3CompoundCapsule b3Capsule capsule; /// Index to a shared material. - int materialIndex; + uint16_t materialIndex; + + /// Padding for determinism. + uint16_t padding; } b3CompoundCapsule; /// A hull that lives in a compound. @@ -2569,7 +2568,10 @@ typedef struct b3CompoundHull b3Transform transform; /// Index to a shared material. - int materialIndex; + uint16_t materialIndex; + + /// Padding for determinism. + uint16_t padding; } b3CompoundHull; /// A mesh with non-uniform scale that lives in a compound. @@ -2585,10 +2587,12 @@ typedef struct b3CompoundMesh b3Vec3 scale; /// This is used to access the surface material from b3GetCompoundMaterials. - /// Requires an extra level of indirection. The triangle material index - /// is clamped to B3_MAX_COMPOUND_MESH_MATERIALS. + /// Requires an extra level of indirection. /// materialIndex = materialIndices[triangle->materialIndex] - int materialIndices[B3_MAX_COMPOUND_MESH_MATERIALS]; + const uint16_t* materialIndices; + + /// The number of materials. 1 for convex hapes. + int materialCount; } b3CompoundMesh; /// A sphere that lives in a compound. @@ -2598,7 +2602,10 @@ typedef struct b3CompoundSphere b3Sphere sphere; /// Index to a shared material. - int materialIndex; + uint16_t materialIndex; + + /// Padding for determinism. + uint16_t padding; } b3CompoundSphere; /// Child shape of a compound @@ -2617,8 +2624,11 @@ typedef struct b3ChildShape b3Transform transform; /// Material indices. Index 0 is used for convex shapes. - /// todo limit to 64K? - int materialIndices[B3_MAX_COMPOUND_MESH_MATERIALS]; + const uint16_t* materialIndices; + + /// The number of materials. This is one for convex shapes. For + /// meshes it is determined by the maximum triangle material index. + int materialCount; /// The shape type (union tag). b3ShapeType type; diff --git a/samples/sample_compound.cpp b/samples/sample_compound.cpp index 636d378..30954d6 100644 --- a/samples/sample_compound.cpp +++ b/samples/sample_compound.cpp @@ -545,6 +545,7 @@ public: assert( capsuleIndex < capsuleCapacity ); capsules[capsuleIndex].capsule = { p1, p2, radius }; capsules[capsuleIndex].material = material; + capsules[capsuleIndex].material.userMaterialId = 1; capsuleIndex += 1; } else @@ -552,6 +553,7 @@ public: assert( sphereIndex < capsuleCapacity ); spheres[sphereIndex].sphere = { p1, radius }; spheres[sphereIndex].material = material; + spheres[sphereIndex].material.userMaterialId = 2; sphereIndex += 1; } } @@ -559,6 +561,7 @@ public: hulls[hullIndex].hull = &box.base; hulls[hullIndex].transform = transform; hulls[hullIndex].material = material; + hulls[hullIndex].material.userMaterialId = 3; hullIndex += 1; } diff --git a/shared/benchmarks.c b/shared/benchmarks.c index b2880e6..2cd3baa 100644 --- a/shared/benchmarks.c +++ b/shared/benchmarks.c @@ -908,7 +908,7 @@ void GetConvexPileCapacity( b3Capacity* capacity ) #ifdef NDEBUG capacity->dynamicShapeCount = 5120; capacity->dynamicBodyCount = 5120; - capacity->contactCount = 50 * 1024; + capacity->contactCount = 60 * 1024; #endif } diff --git a/src/compound.c b/src/compound.c index 38b5571..8d704e8 100644 --- a/src/compound.c +++ b/src/compound.c @@ -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; diff --git a/src/contact.h b/src/contact.h index f1dd98c..c4388b3 100644 --- a/src/contact.h +++ b/src/contact.h @@ -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 ); diff --git a/src/convex_manifold.c b/src/convex_manifold.c index bcc34a0..ac2909e 100644 --- a/src/convex_manifold.c +++ b/src/convex_manifold.c @@ -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 ); diff --git a/src/mesh_contact.c b/src/mesh_contact.c index 43a50f9..67c9b6f 100644 --- a/src/mesh_contact.c +++ b/src/mesh_contact.c @@ -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 ); diff --git a/src/shape.c b/src/shape.c index 1f6f38b..951e2c3 100644 --- a/src/shape.c +++ b/src/shape.c @@ -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 diff --git a/test/test_compound.c b/test/test_compound.c index 6ff10a3..e76dbb5 100644 --- a/test/test_compound.c +++ b/test/test_compound.c @@ -6,6 +6,7 @@ // b3CollideMoverAndCompound, b3GetCompoundChild, b3QueryCompound are internal #include "compound.h" +#include "box3d/box3d.h" #include "box3d/collision.h" #include "box3d/math_functions.h" @@ -180,7 +181,7 @@ static int CompoundMaterialDistinct( void ) for ( int i = 0; i < 3; ++i ) { b3CompoundCapsule cc = b3GetCompoundCapsule( c, i ); - ENSURE( cc.materialIndex >= 0 && cc.materialIndex < 3 ); + ENSURE( cc.materialIndex < 3 ); ENSURE( mats[cc.materialIndex].userMaterialId == (uint64_t)( i + 1 ) ); } @@ -719,6 +720,193 @@ static int CompoundMeshMaterialRemap( void ) return 0; } +// Six separated upward facing triangles, one per material index 0 to 5 +static b3MeshData* MakeSixMaterialMesh( void ) +{ + b3Vec3 vertices[18]; + int32_t indices[18]; + uint8_t materialIndices[6]; + + for ( int i = 0; i < 6; ++i ) + { + float x = 3.0f * (float)i; + vertices[3 * i + 0] = (b3Vec3){ x - 1.0f, 0.0f, -1.0f }; + vertices[3 * i + 1] = (b3Vec3){ x, 0.0f, 1.0f }; + vertices[3 * i + 2] = (b3Vec3){ x + 1.0f, 0.0f, -1.0f }; + + indices[3 * i + 0] = 3 * i + 0; + indices[3 * i + 1] = 3 * i + 1; + indices[3 * i + 2] = 3 * i + 2; + + materialIndices[i] = (uint8_t)i; + } + + b3MeshDef def = { 0 }; + def.vertices = vertices; + def.stride = sizeof( b3Vec3 ); + def.indices = indices; + def.materialIndices = materialIndices; + def.vertexCount = 18; + def.triangleCount = 6; + + return b3CreateMesh( &def, NULL, 0 ); +} + +static int CompoundMeshManyMaterials( void ) +{ + // The capsule material bakes first so the mesh map is not the identity + b3SurfaceMaterial capsuleMat = MakeMaterial( 0.4f, 1000 ); + + b3MeshData* md = MakeSixMaterialMesh(); + ENSURE( md->materialCount == 6 ); + + b3SurfaceMaterial meshMats[6]; + for ( int i = 0; i < 6; ++i ) + { + meshMats[i] = MakeMaterial( 0.2f + 0.1f * (float)i, (uint64_t)( i + 1 ) ); + } + + b3CompoundCapsuleDef cap = { .capsule = { { -10.0f, 0.0f, 0.0f }, { -9.0f, 0.0f, 0.0f }, 0.25f }, .material = capsuleMat }; + b3CompoundMeshDef mesh = { + .meshData = md, + .transform = b3Transform_identity, + .scale = { 1.0f, 1.0f, 1.0f }, + .materials = meshMats, + .materialCount = 6, + }; + + b3CompoundDef def = { + .capsules = &cap, + .capsuleCount = 1, + .meshes = &mesh, + .meshCount = 1, + }; + b3CompoundData* c = b3CreateCompound( &def ); + ENSURE( c != NULL ); + ENSURE( c->materialCount == 7 ); + + const b3SurfaceMaterial* table = b3GetCompoundMaterials( c ); + + b3ChildShape capChild = b3GetCompoundChild( c, 0 ); + ENSURE( capChild.type == b3_capsuleShape ); + ENSURE( capChild.materialCount == 1 ); + ENSURE( table[capChild.materialIndices[0]].userMaterialId == 1000 ); + + b3ChildShape meshChild = b3GetCompoundChild( c, 1 ); + ENSURE( meshChild.type == b3_meshShape ); + ENSURE( meshChild.materialCount == 6 ); + for ( int k = 0; k < 6; ++k ) + { + ENSURE( table[meshChild.materialIndices[k]].userMaterialId == (uint64_t)( k + 1 ) ); + } + + // Sixth triangle, material index 5 + const uint8_t* triangleMaterials = b3GetMeshMaterialIndices( md ); + b3RayCastInput ray = { .origin = { 15.0f, 5.0f, 0.0f }, .translation = { 0.0f, -10.0f, 0.0f }, .maxFraction = 1.0f }; + b3CastOutput out = b3RayCastCompound( c, &ray ); + ENSURE( out.hit == true ); + ENSURE( out.childIndex == 1 ); + int rawIndex = triangleMaterials[out.triangleIndex]; + ENSURE( rawIndex >= 4 ); + ENSURE( table[out.materialIndex].userMaterialId == (uint64_t)( rawIndex + 1 ) ); + + b3DestroyCompound( c ); + b3DestroyMesh( md ); + return 0; +} + +// Flat quad with every triangle on material index 5, so the mesh has six materials +static b3MeshData* MakeFlatMaterial5Mesh( void ) +{ + b3Vec3 vertices[4] = { + { -5.0f, 0.0f, -5.0f }, + { -5.0f, 0.0f, 5.0f }, + { 5.0f, 0.0f, -5.0f }, + { 5.0f, 0.0f, 5.0f }, + }; + int32_t indices[6] = { 0, 1, 3, 3, 2, 0 }; + uint8_t materialIndices[2] = { 5, 5 }; + + b3MeshDef def = { 0 }; + def.vertices = vertices; + def.stride = sizeof( b3Vec3 ); + def.indices = indices; + def.materialIndices = materialIndices; + def.vertexCount = 4; + def.triangleCount = 2; + + return b3CreateMesh( &def, NULL, 0 ); +} + +static uint64_t s_compoundContactMaterialId; + +static float CompoundContactFrictionCallback( float frictionA, uint64_t userMaterialIdA, float frictionB, + uint64_t userMaterialIdB ) +{ + if ( userMaterialIdA == 6 || userMaterialIdB == 6 ) + { + s_compoundContactMaterialId = 6; + } + return 0.5f * ( frictionA + frictionB ); +} + +static int CompoundMeshContactMaterial( void ) +{ + s_compoundContactMaterialId = 0; + + b3WorldDef worldDef = b3DefaultWorldDef(); + b3WorldId worldId = b3CreateWorld( &worldDef ); + b3World_SetFrictionCallback( worldId, CompoundContactFrictionCallback ); + + b3BodyDef groundBodyDef = b3DefaultBodyDef(); + groundBodyDef.type = b3_staticBody; + b3BodyId groundBodyId = b3CreateBody( worldId, &groundBodyDef ); + + b3MeshData* md = MakeFlatMaterial5Mesh(); + ENSURE( md->materialCount == 6 ); + + b3SurfaceMaterial meshMats[6]; + for ( int i = 0; i < 6; ++i ) + { + meshMats[i] = MakeMaterial( 0.5f, (uint64_t)( i + 1 ) ); + } + + b3CompoundMeshDef meshChild = { + .meshData = md, + .transform = b3Transform_identity, + .scale = { 1.0f, 1.0f, 1.0f }, + .materials = meshMats, + .materialCount = 6, + }; + b3CompoundDef compoundDef = { .meshes = &meshChild, .meshCount = 1 }; + b3CompoundData* compound = b3CreateCompound( &compoundDef ); + ENSURE( compound != NULL ); + + b3ShapeDef groundShapeDef = b3DefaultShapeDef(); + b3CreateBakedCompoundShape( groundBodyId, &groundShapeDef, compound ); + + b3BodyDef sphereBodyDef = b3DefaultBodyDef(); + sphereBodyDef.type = b3_dynamicBody; + sphereBodyDef.position = (b3Pos){ 0.0f, 3.0f, 0.0f }; + b3BodyId sphereBodyId = b3CreateBody( worldId, &sphereBodyDef ); + b3ShapeDef sphereShapeDef = b3DefaultShapeDef(); + sphereShapeDef.density = 1.0f; + b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f }; + b3CreateSphereShape( sphereBodyId, &sphereShapeDef, &sphere ); + + for ( int i = 0; i < 120; ++i ) + { + b3World_Step( worldId, 1.0f / 60.0f, 4 ); + } + + ENSURE( s_compoundContactMaterialId == 6 ); + + b3DestroyWorld( worldId ); + b3DestroyCompound( compound ); + b3DestroyMesh( md ); + return 0; +} + static int CompoundOverlap( void ) { b3SurfaceMaterial mat = b3DefaultSurfaceMaterial(); @@ -1141,6 +1329,8 @@ int CompoundTest( void ) RUN_SUBTEST( CompoundShapeCastMiss ); RUN_SUBTEST( CompoundShapeCastHullNormalRotation ); RUN_SUBTEST( CompoundMeshMaterialRemap ); + RUN_SUBTEST( CompoundMeshManyMaterials ); + RUN_SUBTEST( CompoundMeshContactMaterial ); RUN_SUBTEST( CompoundOverlap ); RUN_SUBTEST( CompoundOverlapTransformed ); diff --git a/test/test_recording.c b/test/test_recording.c index 3c6274a..42acb95 100644 --- a/test/test_recording.c +++ b/test/test_recording.c @@ -1154,10 +1154,30 @@ static int AllOps( void ) b3CompoundSphereDef compSphere; compSphere.sphere = (b3Sphere){ { 0.0f, 0.0f, 0.0f }, 1.0f }; compSphere.material = b3DefaultSurfaceMaterial(); + + // Mesh child with five materials so the compound material map is recorded + b3MeshData* compoundMeshData = b3CreateGridMesh( 1, 5, 1.0f, 5, false ); + ENSURE( compoundMeshData != NULL ); + ENSURE( compoundMeshData->materialCount == 5 ); + b3SurfaceMaterial compoundMeshMaterials[5]; + for ( int i = 0; i < 5; ++i ) + { + compoundMeshMaterials[i] = b3DefaultSurfaceMaterial(); + } + b3CompoundMeshDef compMesh; + memset( &compMesh, 0, sizeof( compMesh ) ); + compMesh.meshData = compoundMeshData; + compMesh.transform = b3Transform_identity; + compMesh.scale = (b3Vec3){ 1.0f, 1.0f, 1.0f }; + compMesh.materials = compoundMeshMaterials; + compMesh.materialCount = 5; + b3CompoundDef compoundDef; memset( &compoundDef, 0, sizeof( compoundDef ) ); compoundDef.spheres = &compSphere; compoundDef.sphereCount = 1; + compoundDef.meshes = &compMesh; + compoundDef.meshCount = 1; b3CompoundData* compound = b3CreateCompound( &compoundDef ); ENSURE( compound != NULL ); b3ShapeDef compoundShapeDef = b3DefaultShapeDef(); @@ -1501,6 +1521,7 @@ static int AllOps( void ) b3DestroyMesh( swapMeshData ); b3DestroyHeightField( hf ); b3DestroyCompound( compound ); + b3DestroyMesh( compoundMeshData ); const uint8_t* recData = b3Recording_GetData( rec ); int recSize = b3Recording_GetSize( rec );