From 42404f0af178bb98abe437d273e3d5b48ecc3dd7 Mon Sep 17 00:00:00 2001 From: Erin Catto Date: Wed, 11 Nov 2026 10:18:21 -0800 Subject: [PATCH] heightfield tree benchmark --- benchmark/main.c | 3 ++ samples/sample_benchmark.cpp | 12 +++-- shared/benchmarks.c | 95 +++++++++++++++++++++++++++++++----- shared/benchmarks.h | 3 ++ 4 files changed, 98 insertions(+), 15 deletions(-) diff --git a/benchmark/main.c b/benchmark/main.c index 00747f8..d8ec5ce 100644 --- a/benchmark/main.c +++ b/benchmark/main.c @@ -239,6 +239,9 @@ int main( int argc, char** argv ) { "trees100", NULL, CreateTrees100, DestroyTrees, NULL, 500 }, { "trees50", NULL, CreateTrees50, DestroyTrees, NULL, 500 }, { "trees25", NULL, CreateTrees25, DestroyTrees, NULL, 500 }, + { "trees100hf", NULL, CreateTrees100HeightField, DestroyTrees, NULL, 500 }, + { "trees50hf", NULL, CreateTrees50HeightField, DestroyTrees, NULL, 500 }, + { "trees25hf", NULL, CreateTrees25HeightField, DestroyTrees, NULL, 500 }, { "village", GetVillageCapacity, CreateVillage, DestroyVillage, StepVillage, VILLAGE_STEP_COUNT }, { "washer", GetWasherCapacity, CreateWasher, NULL, NULL, 1000 }, //{ "smash", CreateSmash, NULL, 300 }, diff --git a/samples/sample_benchmark.cpp b/samples/sample_benchmark.cpp index d70c28e..5482088 100644 --- a/samples/sample_benchmark.cpp +++ b/samples/sample_benchmark.cpp @@ -693,20 +693,25 @@ public: if ( m_gridSize == 100 ) { - CreateTrees100( m_worldId ); + m_useHeightField ? CreateTrees100HeightField( m_worldId ) : CreateTrees100( m_worldId ); } else if ( m_gridSize == 50 ) { - CreateTrees50( m_worldId ); + m_useHeightField ? CreateTrees50HeightField( m_worldId ) : CreateTrees50( m_worldId ); } else { - CreateTrees25( m_worldId ); + m_useHeightField ? CreateTrees25HeightField( m_worldId ) : CreateTrees25( m_worldId ); } } bool DrawControls() override { + if ( ImGui::Checkbox( "Height Field", &m_useHeightField ) ) + { + Generate(); + } + if ( ImGui::RadioButton( "100cm", &m_gridSize, 100 ) ) { Generate(); @@ -731,6 +736,7 @@ public: } int m_gridSize; + bool m_useHeightField = false; }; static int sampleFallingTrees = RegisterSample( "Benchmark", "Falling Trees", BenchmarkFallingTrees::Create ); diff --git a/shared/benchmarks.c b/shared/benchmarks.c index 402b14a..259160a 100644 --- a/shared/benchmarks.c +++ b/shared/benchmarks.c @@ -676,19 +676,15 @@ void CreateWasher( b3WorldId worldId ) struct { b3MeshData* meshData; + b3HeightFieldData* heightField; } g_treeData; -static void CreateTrees( b3WorldId worldId, int scale ) +static void CreateTrees( b3WorldId worldId, int scale, bool useHeightField ) { memset( &g_treeData, 0, sizeof( g_treeData ) ); // float tilt = 0.15f * B3_PI; float tilt = 0.0f * B3_PI; - b3BodyDef bodyDef = b3DefaultBodyDef(); - bodyDef.position = (b3Pos){ 0.0f, 0.0f, 0.0f }; - bodyDef.rotation = b3MakeQuatFromAxisAngle( (b3Vec3){ 1.0f, 0.0f, 0.0 }, tilt ); - b3BodyId groundId = b3CreateBody( worldId, &bodyDef ); - int xCount = scale * 150; // int zCount = BENCHMARK_DEBUG ? 100 : 400; int zCount = scale * 200; @@ -698,9 +694,60 @@ static void CreateTrees( b3WorldId worldId, int scale ) float rowHz = 0.05f; float columnHz = 0.1f; - g_treeData.meshData = b3CreateWaveMesh( xCount, zCount, cellWidth, amplitude, rowHz, columnHz ); + b3BodyDef bodyDef = b3DefaultBodyDef(); + bodyDef.position = (b3Pos){ 0.0f, 0.0f, 0.0f }; + bodyDef.rotation = b3MakeQuatFromAxisAngle( (b3Vec3){ 1.0f, 0.0f, 0.0 }, tilt ); + if ( useHeightField ) + { + b3Vec3 corner = { -0.5f * cellWidth * xCount, 0.0f, -0.5f * cellWidth * zCount }; + corner = b3RotateVector( bodyDef.rotation, corner ); + bodyDef.position = (b3Pos){ corner.x, corner.y, corner.z }; + } + b3BodyId groundId = b3CreateBody( worldId, &bodyDef ); + bodyDef.position = (b3Pos){ 0.0f, 0.0f, 0.0f }; + b3ShapeDef shapeDef = b3DefaultShapeDef(); - b3CreateMeshShape( groundId, &shapeDef, g_treeData.meshData, b3Vec3_one ); + + if ( useHeightField ) + { + int countX = xCount + 1; + int countZ = zCount + 1; + float* heights = malloc( countX * countZ * sizeof( float ) ); + uint8_t* materialIndices = calloc( xCount * zCount, sizeof( uint8_t ) ); + + float omegaX = 2.0f * B3_PI * columnHz * cellWidth; + float omegaZ = 2.0f * B3_PI * rowHz * cellWidth; + for ( int iz = 0; iz < countZ; ++iz ) + { + float sz = b3ComputeCosSin( omegaZ * iz ).sine; + for ( int ix = 0; ix < countX; ++ix ) + { + float sx = b3ComputeCosSin( omegaX * ix ).sine; + heights[iz * countX + ix] = sx * sz; + } + } + + b3HeightFieldDef hfDef = { 0 }; + hfDef.heights = heights; + hfDef.materialIndices = materialIndices; + hfDef.scale = (b3Vec3){ cellWidth, amplitude, cellWidth }; + hfDef.countX = countX; + hfDef.countZ = countZ; + hfDef.globalMinimumHeight = -1.0f; + hfDef.globalMaximumHeight = 1.0f; + hfDef.clockwiseWinding = false; + + g_treeData.heightField = b3CreateHeightField( &hfDef ); + free( heights ); + free( materialIndices ); + + b3CreateHeightFieldShape( groundId, &shapeDef, g_treeData.heightField ); + } + else + { + g_treeData.meshData = b3CreateWaveMesh( xCount, zCount, cellWidth, amplitude, rowHz, columnHz ); + b3CreateMeshShape( groundId, &shapeDef, g_treeData.meshData, b3Vec3_one ); + } bodyDef.type = b3_dynamicBody; bodyDef.sleepThreshold = 0.2f; @@ -767,22 +814,46 @@ static void CreateTrees( b3WorldId worldId, int scale ) void CreateTrees25( b3WorldId worldId ) { - CreateTrees( worldId, 4 ); + CreateTrees( worldId, 4, false ); } void CreateTrees50( b3WorldId worldId ) { - CreateTrees( worldId, 2 ); + CreateTrees( worldId, 2, false ); } void CreateTrees100( b3WorldId worldId ) { - CreateTrees( worldId, 1 ); + CreateTrees( worldId, 1, false ); +} + +void CreateTrees25HeightField( b3WorldId worldId ) +{ + CreateTrees( worldId, 4, true ); +} + +void CreateTrees50HeightField( b3WorldId worldId ) +{ + CreateTrees( worldId, 2, true ); +} + +void CreateTrees100HeightField( b3WorldId worldId ) +{ + CreateTrees( worldId, 1, true ); } void DestroyTrees( void ) { - b3DestroyMesh( g_treeData.meshData ); + if ( g_treeData.meshData != NULL ) + { + b3DestroyMesh( g_treeData.meshData ); + } + + if ( g_treeData.heightField != NULL ) + { + b3DestroyHeightField( g_treeData.heightField ); + } + memset( &g_treeData, 0, sizeof( g_treeData ) ); } diff --git a/shared/benchmarks.h b/shared/benchmarks.h index 9218aac..29f1530 100644 --- a/shared/benchmarks.h +++ b/shared/benchmarks.h @@ -24,6 +24,9 @@ void ResetGroundShapeId( void ); void CreateTrees25( b3WorldId worldId ); void CreateTrees50( b3WorldId worldId ); void CreateTrees100( b3WorldId worldId ); +void CreateTrees25HeightField( b3WorldId worldId ); +void CreateTrees50HeightField( b3WorldId worldId ); +void CreateTrees100HeightField( b3WorldId worldId ); void DestroyTrees( void ); void CreateMeshDropBenchmark( b3WorldId worldId ); void DestroyMeshDropBenchmark( void );