Reduce ghosts (#212)
Full ghost collision handling for sphere, capsule, and hull versus a single mesh or heightfield. This requires mesh to be built with edge identification. Optimized mesh builder. Removed B3_MAX_MESH_CONTACT_TRIANGLES. You will still hit performance problems for dense meshes. Fixes #184
This commit is contained in:
parent
3362633204
commit
38f2ae1e19
61 changed files with 17756 additions and 1492 deletions
|
|
@ -34,6 +34,16 @@ FetchContent_Declare(nfd
|
|||
)
|
||||
FetchContent_MakeAvailable(nfd)
|
||||
|
||||
# meshoptimizer for the s&box Ghost Collisions remesh toggle. Static lib, target meshoptimizer.
|
||||
set(MESHOPT_INSTALL OFF CACHE BOOL "" FORCE)
|
||||
FetchContent_Declare(meshoptimizer
|
||||
GIT_REPOSITORY https://github.com/zeux/meshoptimizer.git
|
||||
GIT_TAG v1.3
|
||||
GIT_SHALLOW TRUE
|
||||
GIT_PROGRESS TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(meshoptimizer)
|
||||
|
||||
# The shader compiler is only needed when recompiling shaders. A fresh clone
|
||||
# with BOX3D_BUILD_SHADERS=OFF builds from the committed headers and skips this.
|
||||
if(BOX3D_BUILD_SHADERS)
|
||||
|
|
@ -187,6 +197,7 @@ set(SAMPLE_FILES
|
|||
sample_determinism.cpp
|
||||
sample_events.cpp
|
||||
sample_geometry.cpp
|
||||
sample_ghost.cpp
|
||||
sample_issues.cpp
|
||||
sample_joint.cpp
|
||||
sample_manifold.cpp
|
||||
|
|
@ -220,7 +231,7 @@ target_include_directories(samples PRIVATE
|
|||
|
||||
# gfx: the renderer + Draw*/adapter. shared: human, utils (Random*), the
|
||||
# benchmark/determinism helpers the samples lean on.
|
||||
target_link_libraries(samples PRIVATE gfx shared imgui implot imguizmo sokol nfd)
|
||||
target_link_libraries(samples PRIVATE gfx shared imgui implot imguizmo sokol nfd meshoptimizer)
|
||||
|
||||
# MSVC auto-links winmm via a #pragma comment in main.cpp. MinGW ignores that
|
||||
# pragma, so link the timeBeginPeriod/timeEndPeriod library explicitly.
|
||||
|
|
|
|||
|
|
@ -50,11 +50,27 @@ typedef struct
|
|||
float radius;
|
||||
} DebugCapsule;
|
||||
|
||||
// Hull/mesh/heightfield. Scale only for mesh.
|
||||
// A slice of a mesh holding the triangles that share one material color.
|
||||
// A zero color draws with the shape color.
|
||||
typedef struct
|
||||
{
|
||||
MeshHandle handle;
|
||||
uint32_t color;
|
||||
} DebugMeshPart;
|
||||
|
||||
// Hull/mesh/heightfield. Scale only for mesh. A top-level mesh whose material
|
||||
// slots carry different custom colors is drawn as parts, one per color. The
|
||||
// slot colors are snapshot so a runtime material change rebuilds the parts.
|
||||
// The whole mesh handle is kept for highlight and material overrides.
|
||||
typedef struct
|
||||
{
|
||||
MeshHandle handle;
|
||||
b3Vec3 scale;
|
||||
const b3MeshData* data;
|
||||
uint32_t* slotColors;
|
||||
int slotCount;
|
||||
DebugMeshPart* parts;
|
||||
int partCount;
|
||||
} DebugMesh;
|
||||
|
||||
typedef struct
|
||||
|
|
@ -148,6 +164,138 @@ static const float kBodyTypeRoughness[3] = { 0.70f, 0.55f, 0.40f };
|
|||
static const float kDebugMaterialMetallic[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.85f };
|
||||
static const float kDebugMaterialRoughness[6] = { 0.50f, 0.85f, 0.65f, 0.95f, 0.30f, 0.35f };
|
||||
|
||||
static void ReleaseMeshParts( DebugMesh* geom )
|
||||
{
|
||||
for ( int i = 0; i < geom->partCount; ++i )
|
||||
{
|
||||
ReleaseMeshReference( geom->parts[i].handle );
|
||||
}
|
||||
free( geom->parts );
|
||||
free( geom->slotColors );
|
||||
geom->parts = NULL;
|
||||
geom->partCount = 0;
|
||||
geom->slotColors = NULL;
|
||||
geom->slotCount = 0;
|
||||
}
|
||||
|
||||
// Group the mesh triangles by the custom color of their material slot. Slots
|
||||
// sharing a color share a part, so a conveyor with many belt slots of one
|
||||
// color costs one draw. Nothing is split when every used slot has one color.
|
||||
static void BuildMeshParts( DebugShape* us )
|
||||
{
|
||||
DebugMesh* geom = &us->geom;
|
||||
ReleaseMeshParts( geom );
|
||||
|
||||
const b3MeshData* data = geom->data;
|
||||
const uint8_t* indices = data != NULL ? b3GetMeshMaterialIndices( data ) : NULL;
|
||||
const int slotCount = b3Shape_GetMeshMaterialCount( us->shapeId );
|
||||
if ( indices == NULL || slotCount <= 1 )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
geom->slotColors = (uint32_t*)malloc( (size_t)slotCount * sizeof( uint32_t ) );
|
||||
uint8_t* used = (uint8_t*)calloc( (size_t)slotCount, 1 );
|
||||
uint8_t* mask = (uint8_t*)malloc( (size_t)slotCount );
|
||||
uint32_t* colors = (uint32_t*)malloc( (size_t)slotCount * sizeof( uint32_t ) );
|
||||
if ( geom->slotColors == NULL || used == NULL || mask == NULL || colors == NULL )
|
||||
{
|
||||
free( used );
|
||||
free( mask );
|
||||
free( colors );
|
||||
free( geom->slotColors );
|
||||
geom->slotColors = NULL;
|
||||
return;
|
||||
}
|
||||
geom->slotCount = slotCount;
|
||||
|
||||
for ( int i = 0; i < slotCount; ++i )
|
||||
{
|
||||
geom->slotColors[i] = b3Shape_GetMeshSurfaceMaterial( us->shapeId, i ).customColor;
|
||||
}
|
||||
|
||||
for ( int t = 0; t < data->triangleCount; ++t )
|
||||
{
|
||||
const int slot = indices[t] < slotCount ? indices[t] : slotCount - 1;
|
||||
used[slot] = 1;
|
||||
}
|
||||
|
||||
int colorCount = 0;
|
||||
for ( int i = 0; i < slotCount; ++i )
|
||||
{
|
||||
if ( used[i] == 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
bool found = false;
|
||||
for ( int j = 0; j < colorCount; ++j )
|
||||
{
|
||||
if ( colors[j] == geom->slotColors[i] )
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( found == false )
|
||||
{
|
||||
colors[colorCount++] = geom->slotColors[i];
|
||||
}
|
||||
}
|
||||
|
||||
if ( colorCount > 1 )
|
||||
{
|
||||
geom->parts = (DebugMeshPart*)malloc( (size_t)colorCount * sizeof( DebugMeshPart ) );
|
||||
for ( int j = 0; geom->parts != NULL && j < colorCount; ++j )
|
||||
{
|
||||
for ( int i = 0; i < slotCount; ++i )
|
||||
{
|
||||
mask[i] = ( used[i] != 0 && geom->slotColors[i] == colors[j] ) ? 1 : 0;
|
||||
}
|
||||
|
||||
MeshHandle handle = FindOrAddMeshSubset( data, mask, slotCount );
|
||||
if ( IsMeshHandleValid( handle ) == false )
|
||||
{
|
||||
// Fall back to drawing the whole mesh in the shape color
|
||||
for ( int k = 0; k < geom->partCount; ++k )
|
||||
{
|
||||
ReleaseMeshReference( geom->parts[k].handle );
|
||||
}
|
||||
free( geom->parts );
|
||||
geom->parts = NULL;
|
||||
geom->partCount = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
geom->parts[geom->partCount].handle = handle;
|
||||
geom->parts[geom->partCount].color = colors[j];
|
||||
geom->partCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
free( used );
|
||||
free( mask );
|
||||
free( colors );
|
||||
}
|
||||
|
||||
// Rebuild the parts when a slot color changed since they were built
|
||||
static void RefreshMeshParts( DebugShape* us )
|
||||
{
|
||||
DebugMesh* geom = &us->geom;
|
||||
if ( geom->slotCount <= 1 )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for ( int i = 0; i < geom->slotCount; ++i )
|
||||
{
|
||||
if ( b3Shape_GetMeshSurfaceMaterial( us->shapeId, i ).customColor != geom->slotColors[i] )
|
||||
{
|
||||
BuildMeshParts( us );
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InitAdapter( void )
|
||||
{
|
||||
for ( int i = 0; i < BOX3D_USER_SHAPE_CAPACITY; ++i )
|
||||
|
|
@ -183,6 +331,7 @@ void ResetAdapterPool( void )
|
|||
DebugShape* us = &s_adapter.pool[i];
|
||||
if ( us->kind == Box3DUS_Hull || us->kind == Box3DUS_Mesh || us->kind == Box3DUS_HeightField )
|
||||
{
|
||||
ReleaseMeshParts( &us->geom );
|
||||
ReleaseMeshReference( us->geom.handle );
|
||||
}
|
||||
else if ( us->kind == Box3DUS_Compound )
|
||||
|
|
@ -532,11 +681,13 @@ static int CreateCompoundChild( const b3ChildShape* child, const DebugShape* par
|
|||
break;
|
||||
case b3_hullShape:
|
||||
us->kind = Box3DUS_Hull;
|
||||
us->geom = (DebugMesh){ 0 };
|
||||
us->geom.handle = handle;
|
||||
us->geom.scale = b3Vec3_one;
|
||||
break;
|
||||
case b3_meshShape:
|
||||
us->kind = Box3DUS_Mesh;
|
||||
us->geom = (DebugMesh){ 0 };
|
||||
us->geom.handle = handle;
|
||||
us->geom.scale = (b3Vec3){ child->mesh.scale.x, child->mesh.scale.y, child->mesh.scale.z };
|
||||
break;
|
||||
|
|
@ -603,6 +754,7 @@ static void* AdapterCreateDebugShape( const b3DebugShape* debugShape, void* cont
|
|||
DebugShape* us = &s_adapter.pool[index];
|
||||
us->kind = Box3DUS_Hull;
|
||||
PopulateCommonFields( us, debugShape );
|
||||
us->geom = (DebugMesh){ 0 };
|
||||
us->geom.handle = handle;
|
||||
us->geom.scale = b3Vec3_one;
|
||||
return us;
|
||||
|
|
@ -625,8 +777,11 @@ static void* AdapterCreateDebugShape( const b3DebugShape* debugShape, void* cont
|
|||
DebugShape* us = &s_adapter.pool[index];
|
||||
us->kind = Box3DUS_Mesh;
|
||||
PopulateCommonFields( us, debugShape );
|
||||
us->geom = (DebugMesh){ 0 };
|
||||
us->geom.handle = handle;
|
||||
us->geom.scale = (b3Vec3){ mesh->scale.x, mesh->scale.y, mesh->scale.z };
|
||||
us->geom.data = mesh->data;
|
||||
BuildMeshParts( us );
|
||||
return us;
|
||||
}
|
||||
|
||||
|
|
@ -647,6 +802,7 @@ static void* AdapterCreateDebugShape( const b3DebugShape* debugShape, void* cont
|
|||
DebugShape* us = &s_adapter.pool[index];
|
||||
us->kind = Box3DUS_HeightField;
|
||||
PopulateCommonFields( us, debugShape );
|
||||
us->geom = (DebugMesh){ 0 };
|
||||
us->geom.handle = handle;
|
||||
us->geom.scale = b3Vec3_one;
|
||||
return us;
|
||||
|
|
@ -740,6 +896,7 @@ static void DestroyDebugShape( void* userShape, void* context )
|
|||
}
|
||||
else if ( us->kind == Box3DUS_Hull || us->kind == Box3DUS_Mesh || us->kind == Box3DUS_HeightField )
|
||||
{
|
||||
ReleaseMeshParts( &us->geom );
|
||||
ReleaseMeshReference( us->geom.handle );
|
||||
}
|
||||
|
||||
|
|
@ -757,8 +914,8 @@ static void DestroyDebugShape( void* userShape, void* context )
|
|||
// center, capsule frame) layers on top of baseTransform, so the same path
|
||||
// serves a top-level shape (base = body transform) and a compound child
|
||||
// (base = body transform composed with the child transform).
|
||||
static void AppendResolvedShape( const DebugShape* us, b3Transform baseTransform, Vec4 c, float metallic, float roughness,
|
||||
TransparentShadowCast shadowCast, HighlightKind hk )
|
||||
static void AppendResolvedShape( const DebugShape* us, b3Transform baseTransform, Vec4 c, float metallic,
|
||||
float roughness, TransparentShadowCast shadowCast, HighlightKind hk )
|
||||
{
|
||||
if ( us->kind == Box3DUS_Sphere )
|
||||
{
|
||||
|
|
@ -782,7 +939,24 @@ static void AppendResolvedShape( const DebugShape* us, b3Transform baseTransform
|
|||
{
|
||||
MeshMaterialMode mode = us->isGround ? MESH_MATERIAL_MODE_GROUND_GRID : MESH_MATERIAL_MODE_SOLID;
|
||||
float cell = us->isGround ? BOX3D_GROUND_GRID_CELL_SIZE : 0.0f;
|
||||
AppendMesh( us->geom.handle, baseTransform, us->geom.scale, c, metallic, roughness, mode, cell, shadowCast );
|
||||
if ( us->geom.partCount > 0 && us->hasMaterial == false )
|
||||
{
|
||||
for ( int i = 0; i < us->geom.partCount; ++i )
|
||||
{
|
||||
const DebugMeshPart* part = &us->geom.parts[i];
|
||||
Vec4 pc = c;
|
||||
if ( part->color != 0 )
|
||||
{
|
||||
pc = HexColorToLinear( (b3HexColor)part->color );
|
||||
pc.w = c.w;
|
||||
}
|
||||
AppendMesh( part->handle, baseTransform, us->geom.scale, pc, metallic, roughness, mode, cell, shadowCast );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendMesh( us->geom.handle, baseTransform, us->geom.scale, c, metallic, roughness, mode, cell, shadowCast );
|
||||
}
|
||||
if ( hk != HIGHLIGHT_KIND_NONE )
|
||||
{
|
||||
AppendHighlightGeometry( us->geom.handle, baseTransform, us->geom.scale, hk );
|
||||
|
|
@ -790,9 +964,8 @@ static void AppendResolvedShape( const DebugShape* us, b3Transform baseTransform
|
|||
}
|
||||
}
|
||||
|
||||
// Context for the compound cull query. Carries the resolved material and the
|
||||
// body relative transform so each visited child appends exactly as the full
|
||||
// walk would.
|
||||
// Context for the compound cull query. Carries the resolved material and the body relative transform so each visited
|
||||
// child appends exactly as the full walk would.
|
||||
typedef struct
|
||||
{
|
||||
const DebugShape* parent;
|
||||
|
|
@ -842,6 +1015,11 @@ static void DrawShape( void* userShape, b3WorldTransform shapeTransform, b3HexCo
|
|||
|
||||
RefreshMaterialFromOverride( us );
|
||||
|
||||
if ( us->kind == Box3DUS_Mesh )
|
||||
{
|
||||
RefreshMeshParts( us );
|
||||
}
|
||||
|
||||
Vec4 c;
|
||||
float metallic;
|
||||
float roughness;
|
||||
|
|
@ -964,8 +1142,8 @@ static void DrawTransformFcn( b3WorldTransform transform, void* context )
|
|||
Vec4 blue = MakeVec4( 0.0f, 0.0f, 1.0f, 1.0f );
|
||||
OverlayAppendLine( origin, b3MulAdd( origin, L, basis.cx ), red, BOX3D_LINE_THICKNESS_PX, OVERLAY_THICKNESS_PIXELS,
|
||||
OVERLAY_OCCLUSION_HIDE );
|
||||
OverlayAppendLine( origin, b3MulAdd( origin, L, basis.cy ), green, BOX3D_LINE_THICKNESS_PX, OVERLAY_THICKNESS_PIXELS,
|
||||
OVERLAY_OCCLUSION_HIDE );
|
||||
OverlayAppendLine( origin, b3MulAdd( origin, L, basis.cy ), green, BOX3D_LINE_THICKNESS_PX,
|
||||
OVERLAY_THICKNESS_PIXELS, OVERLAY_OCCLUSION_HIDE );
|
||||
OverlayAppendLine( origin, b3MulAdd( origin, L, basis.cz ), blue, BOX3D_LINE_THICKNESS_PX, OVERLAY_THICKNESS_PIXELS,
|
||||
OVERLAY_OCCLUSION_HIDE );
|
||||
}
|
||||
|
|
@ -988,22 +1166,22 @@ static void DrawCapsuleFcn( b3Pos p1, b3Pos p2, float radius, b3HexColor color,
|
|||
{
|
||||
(void)context;
|
||||
|
||||
b3Vec3 e = b3SubPos(p2, p1);
|
||||
b3Vec3 e = b3SubPos( p2, p1 );
|
||||
float length = b3Length( e );
|
||||
if (length < FLT_EPSILON)
|
||||
if ( length < FLT_EPSILON )
|
||||
{
|
||||
DrawSphereEx( (b3WorldTransform){ p1, b3Quat_identity }, radius, HexColorAToVec4( color, alpha ), DEFAULT_METALLIC,
|
||||
DEFAULT_ROUGHNESS, TRANSPARENT_SHADOW_NONE );
|
||||
DrawSphereEx( (b3WorldTransform){ p1, b3Quat_identity }, radius, HexColorAToVec4( color, alpha ),
|
||||
DEFAULT_METALLIC, DEFAULT_ROUGHNESS, TRANSPARENT_SHADOW_NONE );
|
||||
return;
|
||||
}
|
||||
|
||||
b3Vec3 en = b3MulSV( 1.0f / length, e );
|
||||
b3WorldTransform transform;
|
||||
transform.p = b3OffsetPos( p1, b3MulSV( 0.5f, e ));
|
||||
transform.p = b3OffsetPos( p1, b3MulSV( 0.5f, e ) );
|
||||
transform.q = b3ComputeQuatBetweenUnitVectors( b3Vec3_axisX, en );
|
||||
|
||||
DrawCapsuleEx( transform, 0.5f * length, radius, HexColorAToVec4( color, alpha ), DEFAULT_METALLIC,
|
||||
DEFAULT_ROUGHNESS, TRANSPARENT_SHADOW_NONE );
|
||||
DEFAULT_ROUGHNESS, TRANSPARENT_SHADOW_NONE );
|
||||
}
|
||||
|
||||
static void DrawBoundsFcn( b3AABB aabb, b3HexColor color, void* context )
|
||||
|
|
|
|||
|
|
@ -420,11 +420,14 @@ static inline int MeshEdgeLess( const EdgeRecord* a, const EdgeRecord* b )
|
|||
return a->v1 < b->v1;
|
||||
}
|
||||
|
||||
static MeshHandle BuildMeshData( const b3MeshData* meshData )
|
||||
// A NULL slotMask builds every triangle. Otherwise only triangles whose material
|
||||
// slot has a nonzero mask entry are emitted, and their edges with them.
|
||||
static MeshHandle BuildMeshData( const b3MeshData* meshData, uint64_t hash, const uint8_t* slotMask, int slotCount )
|
||||
{
|
||||
const b3Vec3* verts = b3GetMeshVertices( meshData );
|
||||
const b3MeshTriangle* tris = b3GetMeshTriangles( meshData );
|
||||
const uint8_t* flags = b3GetMeshFlags( meshData );
|
||||
const uint8_t* materialIndices = slotMask != NULL ? b3GetMeshMaterialIndices( meshData ) : NULL;
|
||||
if ( !verts || !tris || meshData->triangleCount <= 0 )
|
||||
{
|
||||
fprintf( stderr, "error: mesh missing arrays or empty (hash=0x%016" PRIx64 ")\n", meshData->hash );
|
||||
|
|
@ -436,6 +439,15 @@ static MeshHandle BuildMeshData( const b3MeshData* meshData )
|
|||
|
||||
for ( int t = 0; t < meshData->triangleCount; ++t )
|
||||
{
|
||||
if ( materialIndices != NULL )
|
||||
{
|
||||
const int slot = materialIndices[t] < slotCount ? materialIndices[t] : slotCount - 1;
|
||||
if ( slotMask[slot] == 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const b3MeshTriangle tri = tris[t];
|
||||
const b3Vec3 a = verts[tri.index1];
|
||||
const b3Vec3 b = verts[tri.index2];
|
||||
|
|
@ -523,7 +535,7 @@ static MeshHandle BuildMeshData( const b3MeshData* meshData )
|
|||
eb.count = write + 1;
|
||||
}
|
||||
|
||||
const MeshHandle h = RegisterMesh( meshData->hash, buf.vertices, buf.vertexCount, buf.indices, buf.indexCount, "geom_mesh" );
|
||||
const MeshHandle h = RegisterMesh( hash, buf.vertices, buf.vertexCount, buf.indices, buf.indexCount, "geom_mesh" );
|
||||
|
||||
if ( IsMeshHandleValid( h ) )
|
||||
{
|
||||
|
|
@ -821,7 +833,37 @@ MeshHandle FindOrAddMesh( const b3MeshData* meshData )
|
|||
return existing;
|
||||
}
|
||||
|
||||
return BuildMeshData( meshData );
|
||||
return BuildMeshData( meshData, meshData->hash, NULL, 0 );
|
||||
}
|
||||
|
||||
MeshHandle FindOrAddMeshSubset( const b3MeshData* meshData, const uint8_t* slotMask, int slotCount )
|
||||
{
|
||||
if ( !meshData || meshData->hash == 0 || slotMask == NULL || slotCount <= 0 ||
|
||||
b3GetMeshMaterialIndices( meshData ) == NULL )
|
||||
{
|
||||
return FindOrAddMesh( meshData );
|
||||
}
|
||||
|
||||
// FNV-1a over the mask, seeded with the mesh hash
|
||||
uint64_t hash = meshData->hash ^ 0xcbf29ce484222325ull;
|
||||
for ( int i = 0; i < slotCount; ++i )
|
||||
{
|
||||
hash ^= slotMask[i] != 0 ? 1u : 0u;
|
||||
hash *= 0x100000001b3ull;
|
||||
}
|
||||
if ( hash == 0 || hash == meshData->hash )
|
||||
{
|
||||
hash += 1;
|
||||
}
|
||||
|
||||
MeshHandle existing = FindMesh( hash );
|
||||
if ( IsMeshHandleValid( existing ) )
|
||||
{
|
||||
AddMeshReference( existing );
|
||||
return existing;
|
||||
}
|
||||
|
||||
return BuildMeshData( meshData, hash, slotMask, slotCount );
|
||||
}
|
||||
|
||||
MeshHandle FindOrAddHeightField( const b3HeightFieldData* heightField )
|
||||
|
|
|
|||
|
|
@ -39,6 +39,12 @@ MeshHandle FindOrAddHull( const b3HullData* hull );
|
|||
// draw time. Two b3Mesh sharing the same b3MeshData share one entry.
|
||||
MeshHandle FindOrAddMesh( const b3MeshData* meshData );
|
||||
|
||||
// Acquire the subset of a mesh whose triangles sit in a material slot with a
|
||||
// nonzero slotMask entry. Keyed by the mesh hash mixed with the mask, so shapes
|
||||
// sharing a mesh and a slot grouping share one entry. Falls back to the whole
|
||||
// mesh when the mesh carries no material indices.
|
||||
MeshHandle FindOrAddMeshSubset( const b3MeshData* meshData, const uint8_t* slotMask, int slotCount );
|
||||
|
||||
// Acquire for b3HeightFieldData. The `scale` vector is part of the height
|
||||
// field's hash (folded into construction), so two heightfields with
|
||||
// different scales hash to different entries, per-instance scale stays
|
||||
|
|
|
|||
|
|
@ -40,7 +40,8 @@ EdgeOverlayParams GetDefaultEdgeParams( void )
|
|||
p.zBias = 1.0e-6f;
|
||||
p.convexColor = MakeVec4( 0.1f, 0.8f, 0.1f, 0.75f );
|
||||
p.concaveColor = MakeVec4( 0.8f, 0.10f, 0.10f, 0.75f );
|
||||
p.flatColor = MakeVec4( 0.6f, 0.6f, 0.6f, 0.5f );
|
||||
p.flatColor = MakeVec4( 0.6f, 0.6f, 0.6f, 0.2f );
|
||||
p.alphaScale = 1.0f;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
|
@ -106,9 +107,11 @@ void ShutdownEdges( void )
|
|||
s_edges.ready = false;
|
||||
}
|
||||
|
||||
static inline Vec4 Premultiply( Vec4 c )
|
||||
static inline Vec4 Premultiply( Vec4 c, float alphaScale )
|
||||
{
|
||||
return MakeVec4( c.x * c.w, c.y * c.w, c.z * c.w, c.w );
|
||||
float a = c.w * alphaScale;
|
||||
a = a < 1.0f ? a : 1.0f;
|
||||
return MakeVec4( c.x * a, c.y * a, c.z * a, a );
|
||||
}
|
||||
|
||||
// Per-batch policy: does the toggle want this batch drawn at all?
|
||||
|
|
@ -160,13 +163,13 @@ static void SubmitBatches( int width, int height, const Mat4* view, const Mat4*
|
|||
edge_ub_pass_t passMesh = { 0 };
|
||||
if ( params->showEdgeConvexity )
|
||||
{
|
||||
passMesh.convex_color = Premultiply( params->convexColor );
|
||||
passMesh.concave_color = Premultiply( params->concaveColor );
|
||||
passMesh.flat_color = Premultiply( params->flatColor );
|
||||
passMesh.convex_color = Premultiply( params->convexColor, params->alphaScale );
|
||||
passMesh.concave_color = Premultiply( params->concaveColor, params->alphaScale );
|
||||
passMesh.flat_color = Premultiply( params->flatColor, params->alphaScale );
|
||||
}
|
||||
else
|
||||
{
|
||||
Vec4 flat = Premultiply( params->flatColor );
|
||||
Vec4 flat = Premultiply( params->flatColor, params->alphaScale );
|
||||
passMesh.convex_color = flat;
|
||||
passMesh.concave_color = flat;
|
||||
passMesh.flat_color = flat;
|
||||
|
|
@ -175,7 +178,7 @@ static void SubmitBatches( int width, int height, const Mat4* view, const Mat4*
|
|||
edge_ub_pass_t passHull = { 0 };
|
||||
|
||||
Vec4 hullEdgeColor = { 0.5f, 0.5f, 0.5f, 0.5f };
|
||||
Vec4 hull = Premultiply( hullEdgeColor );
|
||||
Vec4 hull = Premultiply( hullEdgeColor, params->alphaScale );
|
||||
passHull.convex_color = hull;
|
||||
passHull.concave_color = hull;
|
||||
passHull.flat_color = hull;
|
||||
|
|
|
|||
|
|
@ -6,18 +6,14 @@
|
|||
// into two submission paths to match the silhouette quality of the
|
||||
// rest of the scene:
|
||||
//
|
||||
// Opaque path (RenderEdgesInMSAA): opaque-arena batches (meshes,
|
||||
// heightfields, opaque hulls if policy 1b is ever relaxed). Draws
|
||||
// inside the caller's currently-open MSAA scene pass, picking up the
|
||||
// same 4x MSAA as the opaque shapes. Pipeline tracks scene_target's
|
||||
// color/depth formats and sample_count.
|
||||
// Opaque path (RenderEdgesInMSAA): opaque-arena batches (meshes, heightfields, opaque hulls). Draws inside the caller's
|
||||
// currently-open MSAA scene pass, picking up the same 4x MSAA as the opaque shapes. Pipeline tracks scene_target's
|
||||
// color/depth formats and sample_count.
|
||||
//
|
||||
// Transparent path (RenderEdgesPostTransparent): transparent-arena batches
|
||||
// (hulls per policy 1b). Opens its own single-sample pass against the
|
||||
// resolved scene color + GTAO prepass depth, so edges sit on top of the
|
||||
// transparent draws that have already blended into the resolve. MSAA
|
||||
// isn't available here (the resolve already happened), the FS's
|
||||
// fwidth-based AA does what it can.
|
||||
// Transparent path (RenderEdgesPostTransparent): transparent-arena batches (just hulls). Opens its own single-sample
|
||||
// pass against the resolved scene color + GTAO prepass depth, so edges sit on top of the transparent draws that have
|
||||
// already blended into the resolve. MSAA isn't available here (the resolve already happened), the FS's fwidth-based AA
|
||||
// does what it can.
|
||||
|
||||
#pragma once
|
||||
|
||||
|
|
@ -43,6 +39,7 @@ typedef struct EdgeOverlayParams
|
|||
Vec4 convexColor; // straight (non-premultiplied) linear RGBA, premultiplied inside
|
||||
Vec4 concaveColor;
|
||||
Vec4 flatColor;
|
||||
float alphaScale; // multiplies the alpha of every edge color, clamped to 1
|
||||
} EdgeOverlayParams;
|
||||
|
||||
EdgeOverlayParams GetDefaultEdgeParams( void );
|
||||
|
|
|
|||
|
|
@ -126,6 +126,7 @@ void SampleContext::Save()
|
|||
fprintf( file, " \"drawDistance\": %g,\n", drawDistance );
|
||||
fprintf( file, " \"showHullEdges\": %s,\n", GetEdgeOverlayParams().showHulls ? "true" : "false" );
|
||||
fprintf( file, " \"showEdgeConvexity\": %s,\n", GetEdgeOverlayParams().showEdgeConvexity ? "true" : "false" );
|
||||
fprintf( file, " \"edgeAlpha\": %g,\n", GetEdgeOverlayParams().alphaScale );
|
||||
fprintf( file, " \"showMetrics\": %s,\n", showMetrics ? "true" : "false" );
|
||||
fprintf( file, " \"showProfile\": %s,\n", showProfile ? "true" : "false" );
|
||||
fprintf( file, " \"replayKeyframeBudgetMB\": %d,\n", replayKeyframeBudgetMB );
|
||||
|
|
@ -274,6 +275,12 @@ void SampleContext::Load()
|
|||
p.showEdgeConvexity = strncmp( s, "true", 4 ) == 0;
|
||||
SetEdgeOverlayParams( &p );
|
||||
}
|
||||
else if ( jsoneq( data, &tokens[i], "edgeAlpha" ) == 0 )
|
||||
{
|
||||
EdgeOverlayParams p = GetEdgeOverlayParams();
|
||||
p.alphaScale = b3ClampFloat( ParseFloat( data, tokens[i + 1] ), 0.0f, 4.0f );
|
||||
SetEdgeOverlayParams( &p );
|
||||
}
|
||||
else if ( jsoneq( data, &tokens[i], "showMetrics" ) == 0 )
|
||||
{
|
||||
const char* s = data + tokens[i + 1].start;
|
||||
|
|
@ -1376,8 +1383,52 @@ void Sample::DrawTextLine( const char* text, ... )
|
|||
char buffer[512];
|
||||
vsnprintf( buffer, sizeof( buffer ), text, args );
|
||||
va_end( args );
|
||||
DrawScreenString( m_textX, m_textLine, MakeColor( b3_colorWhite ), buffer );
|
||||
m_textLine += m_textIncrement;
|
||||
|
||||
// The text arena keeps 63 characters per string, so wrap long lines at spaces and indent the rest
|
||||
constexpr int capacity = 63;
|
||||
constexpr int indent = 2;
|
||||
const char* start = buffer;
|
||||
bool first = true;
|
||||
while ( true )
|
||||
{
|
||||
int width = first ? capacity : capacity - indent;
|
||||
int length = (int)strlen( start );
|
||||
int cut = length;
|
||||
if ( length > width )
|
||||
{
|
||||
cut = width;
|
||||
while ( cut > 0 && start[cut] != ' ' )
|
||||
{
|
||||
cut -= 1;
|
||||
}
|
||||
|
||||
if ( cut == 0 )
|
||||
{
|
||||
cut = width;
|
||||
}
|
||||
}
|
||||
|
||||
char line[capacity + 1];
|
||||
int offset = first ? 0 : indent;
|
||||
memset( line, ' ', offset );
|
||||
memcpy( line + offset, start, cut );
|
||||
line[offset + cut] = '\0';
|
||||
DrawScreenString( m_textX, m_textLine, MakeColor( b3_colorWhite ), line );
|
||||
m_textLine += m_textIncrement;
|
||||
|
||||
start += cut;
|
||||
while ( *start == ' ' )
|
||||
{
|
||||
start += 1;
|
||||
}
|
||||
|
||||
if ( *start == '\0' )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
|
||||
SampleEntry g_sampleEntries[MAX_SAMPLES];
|
||||
|
|
@ -1712,6 +1763,11 @@ static void DrawRenderMenu( SampleContext& ctx )
|
|||
ImGui::Separator();
|
||||
|
||||
ImGui::PushItemWidth( 8.0f * ImGui::GetFontSize() );
|
||||
if ( ImGui::SliderFloat( "Edge Alpha", &edgeParams.alphaScale, 0.0f, 4.0f, "%.2f" ) )
|
||||
{
|
||||
SetEdgeOverlayParams( &edgeParams );
|
||||
}
|
||||
|
||||
float ev = GetExposure();
|
||||
if ( ImGui::SliderFloat( "Exposure", &ev, -8.0f, 4.0f, "%.2f" ) )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2900,6 +2900,7 @@ public:
|
|||
e_random = 0,
|
||||
e_circle = 1,
|
||||
e_sliver = 2,
|
||||
e_grid = 3,
|
||||
};
|
||||
|
||||
static Sample* Create( SampleContext* context )
|
||||
|
|
@ -2923,6 +2924,7 @@ public:
|
|||
m_pointCount = 20;
|
||||
m_drawScale = 1.0f;
|
||||
m_drawLabels = true;
|
||||
m_hullFlags = 0;
|
||||
|
||||
Generate();
|
||||
}
|
||||
|
|
@ -2961,6 +2963,20 @@ public:
|
|||
m_inputPoints[i].originalIndex = i;
|
||||
}
|
||||
}
|
||||
else if ( m_sourceMode == e_grid )
|
||||
{
|
||||
// Every border point is collinear with its neighbors, so the hull keeps them only with
|
||||
// the collinear flag
|
||||
m_drawScale = 1.0f;
|
||||
m_inputCount = 36;
|
||||
|
||||
for ( int i = 0; i < m_inputCount; ++i )
|
||||
{
|
||||
m_inputPoints[i].p = { -5.0f + 2.0f * (float)( i % 6 ), -5.0f + 2.0f * (float)( i / 6 ) };
|
||||
m_inputPoints[i].separation = 0.0f;
|
||||
m_inputPoints[i].originalIndex = i;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// A near degenerate sliver with a duplicate pair. Welding and collinear removal have
|
||||
|
|
@ -2998,7 +3014,7 @@ public:
|
|||
scratch[i] = m_inputPoints[i];
|
||||
}
|
||||
|
||||
m_hullCount = b3Hull2D( scratch, m_inputCount, m_hullPoints );
|
||||
m_hullCount = b3Hull2D( scratch, m_inputCount, m_hullPoints, m_hullFlags );
|
||||
|
||||
for ( int i = 0; i < m_hullCount; ++i )
|
||||
{
|
||||
|
|
@ -3010,13 +3026,13 @@ public:
|
|||
|
||||
bool DrawControls() override
|
||||
{
|
||||
const char* modes[] = { "Random", "Circle", "Sliver" };
|
||||
const char* modes[] = { "Random", "Circle", "Sliver", "Grid" };
|
||||
if ( ImGui::Combo( "Source", &m_sourceMode, modes, IM_ARRAYSIZE( modes ) ) )
|
||||
{
|
||||
Generate();
|
||||
}
|
||||
|
||||
if ( m_sourceMode != e_sliver )
|
||||
if ( m_sourceMode == e_random || m_sourceMode == e_circle )
|
||||
{
|
||||
if ( ImGui::SliderInt( "Point Count", &m_pointCount, 4, m_pointCapacity ) )
|
||||
{
|
||||
|
|
@ -3033,6 +3049,8 @@ public:
|
|||
}
|
||||
|
||||
ImGui::SliderInt( "Target Count", &m_targetCount, 3, m_inputCount );
|
||||
ImGui::CheckboxFlags( "Keep Collinear", &m_hullFlags, b3_hull2DKeepCollinear );
|
||||
ImGui::CheckboxFlags( "Exact Merge", &m_hullFlags, b3_hull2DExactMerge );
|
||||
ImGui::Checkbox( "Draw Labels", &m_drawLabels );
|
||||
|
||||
return true;
|
||||
|
|
@ -3110,6 +3128,7 @@ public:
|
|||
int m_simplifiedCount;
|
||||
int m_targetCount;
|
||||
int m_pointCount;
|
||||
int m_hullFlags;
|
||||
bool m_drawLabels;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "gfx/debug_adapter.h"
|
||||
#include "gfx/draw.h"
|
||||
#include "imgui.h"
|
||||
#include "mesh_drop.h"
|
||||
#include "sample.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
|
@ -387,38 +388,9 @@ public:
|
|||
|
||||
static int sampleNeedleMesh = RegisterSample( "Continuous", "Needle Mesh", NeedleMesh::Create );
|
||||
|
||||
struct IndexPair
|
||||
{
|
||||
int index1;
|
||||
int index2;
|
||||
};
|
||||
|
||||
static inline IndexPair ConvertToPair( void* userData )
|
||||
{
|
||||
static_assert( sizeof( intptr_t ) >= 8 && sizeof( int ) == 4 );
|
||||
intptr_t value = (intptr_t)userData;
|
||||
int index1 = (int)value;
|
||||
int index2 = (int)( value >> 32 );
|
||||
return { index1, index2 };
|
||||
}
|
||||
|
||||
static inline void* ConvertToUserData( IndexPair pair )
|
||||
{
|
||||
intptr_t value = (intptr_t)pair.index2 << 32 | (intptr_t)pair.index1;
|
||||
return (void*)value;
|
||||
}
|
||||
|
||||
class MeshDrop : public Sample
|
||||
{
|
||||
public:
|
||||
enum ShapeType
|
||||
{
|
||||
e_box,
|
||||
e_capsule,
|
||||
e_cylinder,
|
||||
e_sphere,
|
||||
};
|
||||
|
||||
explicit MeshDrop( SampleContext* context )
|
||||
: Sample( context )
|
||||
{
|
||||
|
|
@ -428,20 +400,21 @@ public:
|
|||
GetGuiDraw()->forceScale = 0.1f;
|
||||
}
|
||||
|
||||
m_settings = GetDefaultMeshDropSettings();
|
||||
m_groundId = b3_nullBodyId;
|
||||
m_groundMesh = nullptr;
|
||||
m_groundAmplitude = 0.5f;
|
||||
CreateGround();
|
||||
|
||||
m_bodies = new b3BodyId[m_bodyCount];
|
||||
memset( m_bodies, 0, m_bodyCount * sizeof( b3BodyId ) );
|
||||
for ( int i = 0; i < MESH_DROP_BODY_COUNT; ++i )
|
||||
{
|
||||
m_bodies[i] = b3_nullBodyId;
|
||||
}
|
||||
|
||||
m_cylinder = b3CreateCylinder( 0.4f, 0.05f, 0.0f, 6 );
|
||||
m_shapeType = e_box;
|
||||
m_cylinder = CreateMeshDropCylinder();
|
||||
m_seed = 0;
|
||||
m_runCount = 0;
|
||||
m_failure = false;
|
||||
m_autoGenerate = false;
|
||||
m_collide = true;
|
||||
m_stepWhilePaused = true;
|
||||
|
||||
Generate();
|
||||
|
|
@ -449,7 +422,6 @@ public:
|
|||
|
||||
~MeshDrop() override
|
||||
{
|
||||
delete[] m_bodies;
|
||||
b3DestroyHull( m_cylinder );
|
||||
b3DestroyMesh( m_groundMesh );
|
||||
}
|
||||
|
|
@ -468,58 +440,13 @@ public:
|
|||
m_groundMesh = nullptr;
|
||||
}
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
m_groundId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
int gridCount = 40;
|
||||
float cellWidth = 1.0f;
|
||||
float rowHz = 0.1f;
|
||||
float columnHz = 0.2f;
|
||||
|
||||
m_groundMesh = b3CreateWaveMesh( gridCount, gridCount, cellWidth, m_groundAmplitude, rowHz, columnHz );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.filter.categoryBits = 1;
|
||||
b3CreateMeshShape( m_groundId, &shapeDef, m_groundMesh, b3Vec3_one );
|
||||
|
||||
float extent = 0.5f * gridCount * cellWidth;
|
||||
float halfHeight = 1.0f;
|
||||
|
||||
{
|
||||
b3Transform transform;
|
||||
transform.p = { 0.0f, halfHeight, -extent };
|
||||
transform.q = b3Quat_identity;
|
||||
b3BoxHull wallBox = b3MakeTransformedBoxHull( extent, halfHeight, 0.1f, transform );
|
||||
b3CreateHullShape( m_groundId, &shapeDef, &wallBox.base );
|
||||
}
|
||||
|
||||
{
|
||||
b3Transform transform;
|
||||
transform.p = { 0.0f, halfHeight, extent };
|
||||
transform.q = b3Quat_identity;
|
||||
b3BoxHull wallBox = b3MakeTransformedBoxHull( extent, halfHeight, 0.1f, transform );
|
||||
b3CreateHullShape( m_groundId, &shapeDef, &wallBox.base );
|
||||
}
|
||||
|
||||
{
|
||||
b3Transform transform;
|
||||
transform.p = { -extent, halfHeight, 0.0f };
|
||||
transform.q = b3Quat_identity;
|
||||
b3BoxHull wallBox = b3MakeTransformedBoxHull( 0.1f, halfHeight, extent, transform );
|
||||
b3CreateHullShape( m_groundId, &shapeDef, &wallBox.base );
|
||||
}
|
||||
|
||||
{
|
||||
b3Transform transform;
|
||||
transform.p = { extent, halfHeight, 0.0f };
|
||||
transform.q = b3Quat_identity;
|
||||
b3BoxHull wallBox = b3MakeTransformedBoxHull( 0.1f, halfHeight, extent, transform );
|
||||
b3CreateHullShape( m_groundId, &shapeDef, &wallBox.base );
|
||||
}
|
||||
m_groundMesh = CreateMeshDropMesh( m_settings.amplitude );
|
||||
m_groundId = CreateMeshDropGround( m_worldId, m_groundMesh );
|
||||
}
|
||||
|
||||
void Generate()
|
||||
{
|
||||
for ( int i = 0; i < m_bodyCount; ++i )
|
||||
for ( int i = 0; i < MESH_DROP_BODY_COUNT; ++i )
|
||||
{
|
||||
if ( B3_IS_NULL( m_bodies[i] ) )
|
||||
{
|
||||
|
|
@ -530,107 +457,53 @@ public:
|
|||
m_bodies[i] = b3_nullBodyId;
|
||||
}
|
||||
|
||||
// The soft contact solver cannot always resolve this small shape at 4 sub-steps.
|
||||
// Also this small shape has trouble moving advancing in the time of impact code
|
||||
// because the size is very close to the tolerances used.
|
||||
// b3BoxHull box = b3MakeBoxHull( 0.01f, 0.1f, 0.02f );
|
||||
b3BoxHull box = b3MakeBoxHull( 0.02f, 0.2f, 0.04f );
|
||||
b3Capsule capsule = { { 0.0f, -0.2f, 0.0f }, { 0.0f, 0.2f, 0.0f }, 0.05f };
|
||||
b3Sphere sphere = { b3Vec3_zero, 0.05f };
|
||||
|
||||
int bodyIndex = 0;
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.baseMaterial.rollingResistance = m_shapeType == e_capsule ? 0.4f : 0.1f;
|
||||
|
||||
// Don't allow shapes to collide with each other. This
|
||||
// makes isolating failures easier.
|
||||
if ( m_collide == false )
|
||||
{
|
||||
shapeDef.filter.categoryBits = 2;
|
||||
shapeDef.filter.maskBits = 1;
|
||||
}
|
||||
|
||||
g_randomSeed = (uint32_t)b3GetTicks();
|
||||
|
||||
bool simulateAll = true;
|
||||
// g_randomSeed = 1910133196;
|
||||
// A non-zero seed replays a generation reported by the mesh_soak tool
|
||||
m_seed = m_replaySeed != 0 ? m_replaySeed : (uint32_t)b3GetTicks();
|
||||
|
||||
m_runCount += 1;
|
||||
m_stepCount = 0;
|
||||
printf( "run %d seed %u\n", m_runCount, g_randomSeed );
|
||||
printf( "run %d seed %u\n", m_runCount, m_seed );
|
||||
|
||||
for ( int i = 0; i < m_gridCount; ++i )
|
||||
MeshDropSettings settings = m_settings;
|
||||
if ( m_solo[0] >= 0 && m_solo[1] >= 0 )
|
||||
{
|
||||
for ( int j = 0; j < m_gridCount; ++j )
|
||||
{
|
||||
b3Vec3 linearVelocity = RandomVec3Uniform( -1.0f, 1.0f );
|
||||
b3Vec3 angularVelocity = RandomVec3Uniform( -5.0f, 5.0f );
|
||||
|
||||
if ( simulateAll || ( i == 19 && j == 20 ) )
|
||||
{
|
||||
bodyDef.position = { 0.5f * ( i - 0.5f * m_gridCount ), 5.0f, 0.5f * ( j - 0.5f * m_gridCount ) };
|
||||
bodyDef.linearVelocity = linearVelocity;
|
||||
bodyDef.angularVelocity = angularVelocity;
|
||||
// bodyDef.angularDamping = 0.1f;
|
||||
bodyDef.userData = ConvertToUserData( { i, j } );
|
||||
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
if ( m_shapeType == e_box )
|
||||
{
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
}
|
||||
else if ( m_shapeType == e_capsule )
|
||||
{
|
||||
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
|
||||
}
|
||||
else if ( m_shapeType == e_cylinder )
|
||||
{
|
||||
b3CreateHullShape( bodyId, &shapeDef, m_cylinder );
|
||||
}
|
||||
else
|
||||
{
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
}
|
||||
|
||||
m_bodies[bodyIndex] = bodyId;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_bodies[bodyIndex] = b3_nullBodyId;
|
||||
}
|
||||
bodyIndex += 1;
|
||||
}
|
||||
settings.soloIndex = MESH_DROP_GRID_COUNT * m_solo[0] + m_solo[1];
|
||||
}
|
||||
|
||||
assert( bodyIndex == m_bodyCount );
|
||||
CreateMeshDropBodies( m_worldId, settings, m_seed, m_cylinder, m_bodies );
|
||||
ResetMeshDropDepth( &m_depth, settings, m_cylinder );
|
||||
}
|
||||
|
||||
bool DrawControls() override
|
||||
{
|
||||
const char* shapeTypes[] = { "box", "capsule", "cylinder", "sphere" };
|
||||
int shapeType = (int)m_shapeType;
|
||||
if ( ImGui::Combo( "Type", &shapeType, shapeTypes, IM_ARRAYSIZE( shapeTypes ) ) )
|
||||
int shapeType = (int)m_settings.shape;
|
||||
if ( ImGui::Combo( "Type", &shapeType, g_meshDropShapeNames, e_meshDropShapeCount ) )
|
||||
{
|
||||
m_stepCount = 0;
|
||||
m_shapeType = ShapeType( shapeType );
|
||||
m_settings.shape = MeshDropShape( shapeType );
|
||||
Generate();
|
||||
}
|
||||
|
||||
if ( ImGui::SliderFloat( "Amplitude", &m_groundAmplitude, 0.0f, 1.0f ) )
|
||||
if ( ImGui::SliderFloat( "Amplitude", &m_settings.amplitude, 0.0f, 1.0f ) )
|
||||
{
|
||||
CreateGround();
|
||||
Generate();
|
||||
}
|
||||
|
||||
if ( ImGui::Checkbox( "Collide", &m_collide ) )
|
||||
if ( ImGui::Checkbox( "Collide", &m_settings.collide ) )
|
||||
{
|
||||
Generate();
|
||||
}
|
||||
|
||||
ImGui::InputScalar( "Seed", ImGuiDataType_U32, &m_replaySeed );
|
||||
|
||||
// Drop only the body at these indices, -1 for all
|
||||
if ( ImGui::InputInt2( "Solo", m_solo ) )
|
||||
{
|
||||
m_solo[0] = b3ClampInt( m_solo[0], -1, MESH_DROP_GRID_COUNT - 1 );
|
||||
m_solo[1] = b3ClampInt( m_solo[1], -1, MESH_DROP_GRID_COUNT - 1 );
|
||||
}
|
||||
|
||||
if ( ImGui::Button( "Generate" ) )
|
||||
{
|
||||
Generate();
|
||||
|
|
@ -647,8 +520,14 @@ public:
|
|||
|
||||
void Step() override
|
||||
{
|
||||
int stepCount = m_stepCount;
|
||||
Sample::Step();
|
||||
|
||||
if ( m_stepCount != stepCount )
|
||||
{
|
||||
UpdateMeshDropDepth( m_worldId, &m_depth );
|
||||
}
|
||||
|
||||
{
|
||||
PickRay pickRay = m_camera->BuildPickRay( m_context->mouseX, m_context->mouseY );
|
||||
|
||||
|
|
@ -657,13 +536,25 @@ public:
|
|||
if ( result.hit )
|
||||
{
|
||||
b3BodyId bodyId = b3Shape_GetBody( result.shapeId );
|
||||
void* userData = b3Body_GetUserData( bodyId );
|
||||
IndexPair pair = ConvertToPair( userData );
|
||||
DrawTextLine( "indices: (%d, %d)\n", pair.index1, pair.index2 );
|
||||
int index = GetMeshDropBodyIndex( b3Body_GetUserData( bodyId ) );
|
||||
if ( index >= 0 )
|
||||
{
|
||||
DrawTextLine( "indices: (%d, %d)", index / MESH_DROP_GRID_COUNT, index % MESH_DROP_GRID_COUNT );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for ( int i = 0; i < m_bodyCount && m_failure == false; i += 1 )
|
||||
DrawTextLine( "seed %u", m_seed );
|
||||
|
||||
if ( m_depth.maxIndex >= 0 )
|
||||
{
|
||||
int index = m_depth.maxIndex;
|
||||
DrawTextLine( "deepest: (%d, %d) %.1f slop at step %d", index / MESH_DROP_GRID_COUNT, index % MESH_DROP_GRID_COUNT,
|
||||
m_depth.maxDepth / B3_LINEAR_SLOP, m_depth.maxStep );
|
||||
}
|
||||
|
||||
float tunnelHeight = GetMeshDropTunnelHeight();
|
||||
for ( int i = 0; i < MESH_DROP_BODY_COUNT && m_failure == false; i += 1 )
|
||||
{
|
||||
b3BodyId bodyId = m_bodies[i];
|
||||
if ( B3_IS_NULL( bodyId ) )
|
||||
|
|
@ -672,19 +563,14 @@ public:
|
|||
}
|
||||
|
||||
b3Pos massCenter = b3Body_GetWorldCenter( bodyId );
|
||||
if ( massCenter.y < -2.0f )
|
||||
if ( massCenter.y < tunnelHeight )
|
||||
{
|
||||
IndexPair pair = ConvertToPair( b3Body_GetUserData( bodyId ) );
|
||||
printf( "index1: %d - index2: %d\n", pair.index1, pair.index2 );
|
||||
printf( "index1: %d - index2: %d\n", i / MESH_DROP_GRID_COUNT, i % MESH_DROP_GRID_COUNT );
|
||||
printf( "(%6.2f, %6.2f, %6.2f)\n", massCenter.x, massCenter.y, massCenter.z );
|
||||
m_context->pause = true;
|
||||
m_failure = true;
|
||||
m_autoGenerate = false;
|
||||
}
|
||||
|
||||
// This is for debugging the fallback inner sphere
|
||||
// b3Sphere sphere = {massCenter, 0.015f};
|
||||
// DrawSphere( m_scene, b3Transform_identity, sphere, b3_colorLightGray );
|
||||
}
|
||||
|
||||
if ( m_autoGenerate )
|
||||
|
|
@ -709,13 +595,13 @@ public:
|
|||
for ( int i = 0; i < n; ++i )
|
||||
{
|
||||
b3World_Step( m_worldId, timeStep, m_context->subStepCount );
|
||||
UpdateMeshDropDepth( m_worldId, &m_depth );
|
||||
}
|
||||
|
||||
m_stepCount += n;
|
||||
}
|
||||
|
||||
int maxSteps = m_shapeType == e_capsule ? 3000 : 1000;
|
||||
if ( m_stepCount > maxSteps )
|
||||
if ( m_stepCount > GetMeshDropStepLimit( m_settings.shape ) )
|
||||
{
|
||||
m_context->pause = true;
|
||||
m_failure = true;
|
||||
|
|
@ -729,20 +615,18 @@ public:
|
|||
return new MeshDrop( context );
|
||||
}
|
||||
|
||||
static constexpr int m_gridCount = 32;
|
||||
// static constexpr int m_gridCount = 3;
|
||||
static constexpr int m_bodyCount = m_gridCount * m_gridCount;
|
||||
|
||||
MeshDropSettings m_settings;
|
||||
MeshDropDepth m_depth;
|
||||
b3MeshData* m_groundMesh;
|
||||
float m_groundAmplitude;
|
||||
b3BodyId m_groundId;
|
||||
b3HullData* m_cylinder;
|
||||
b3BodyId* m_bodies;
|
||||
ShapeType m_shapeType;
|
||||
b3BodyId m_bodies[MESH_DROP_BODY_COUNT];
|
||||
uint32_t m_seed;
|
||||
int m_runCount;
|
||||
bool m_failure;
|
||||
bool m_autoGenerate;
|
||||
bool m_collide;
|
||||
uint32_t m_replaySeed = 0;
|
||||
int m_solo[2] = { -1, -1 };
|
||||
};
|
||||
|
||||
static int sampleMeshDrop = RegisterSample( "Continuous", "Mesh Drop", MeshDrop::Create );
|
||||
|
|
|
|||
3749
samples/sample_ghost.cpp
Normal file
3749
samples/sample_ghost.cpp
Normal file
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -2320,6 +2320,8 @@ public:
|
|||
m_upperSteeringDegrees = 45.0f;
|
||||
m_maxSteeringTorque = 5.0f;
|
||||
m_targetSteeringDegrees = 0.0f;
|
||||
m_wheelType = WheelType::sphere;
|
||||
m_cylinderSides = 16;
|
||||
|
||||
b3BodyId groundId;
|
||||
{
|
||||
|
|
@ -2371,15 +2373,10 @@ public:
|
|||
b3CreateParallelJoint( m_worldId, ¶llelJointDef );
|
||||
}
|
||||
|
||||
shapeDef.density = 2.0f;
|
||||
shapeDef.baseMaterial.friction = 3.0f;
|
||||
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.allowFastRotation = true;
|
||||
bodyDef.rotation = b3ComputeQuatBetweenUnitVectors( b3Vec3_axisY, b3Vec3_axisZ );
|
||||
|
||||
// b3HullData* hull = b3CreateCylinder( 0.25f, 0.4f, 0.0f, 16 );
|
||||
|
||||
b3WheelJointDef jointDef = b3DefaultWheelJointDef();
|
||||
jointDef.base.bodyIdA = m_chassisId;
|
||||
jointDef.base.localFrameA.q = b3ComputeQuatBetweenUnitVectors( b3Vec3_axisX, b3Vec3_axisY );
|
||||
|
|
@ -2401,15 +2398,11 @@ public:
|
|||
jointDef.lowerSteeringLimit = B3_PI / 180.0f * m_lowerSteeringDegrees;
|
||||
jointDef.upperSteeringLimit = B3_PI / 180.0f * m_upperSteeringDegrees;
|
||||
|
||||
b3Sphere sphere = { b3Vec3_zero, 0.4f };
|
||||
|
||||
{
|
||||
bodyDef.position = { 1.5f, 2.0f, 0.8f };
|
||||
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
// b3CreateHullShape( bodyId, &shapeDef, hull );
|
||||
m_wheelIds[0] = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
jointDef.base.bodyIdB = bodyId;
|
||||
jointDef.base.bodyIdB = m_wheelIds[0];
|
||||
jointDef.base.localFrameA.p = { 1.5f, -0.5f, 0.8f };
|
||||
jointDef.enableSteering = true;
|
||||
jointDef.enableSpinMotor = false;
|
||||
|
|
@ -2418,11 +2411,9 @@ public:
|
|||
|
||||
{
|
||||
bodyDef.position = { 1.5f, 2.0f, -0.8f };
|
||||
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
// b3CreateHullShape( bodyId, &shapeDef, hull );
|
||||
m_wheelIds[1] = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
jointDef.base.bodyIdB = bodyId;
|
||||
jointDef.base.bodyIdB = m_wheelIds[1];
|
||||
jointDef.base.localFrameA.p = { 1.5f, -0.5f, -0.8f };
|
||||
jointDef.enableSteering = true;
|
||||
jointDef.enableSpinMotor = false;
|
||||
|
|
@ -2431,11 +2422,9 @@ public:
|
|||
|
||||
{
|
||||
bodyDef.position = { -1.5f, 2.0f, 0.8f };
|
||||
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
// b3CreateHullShape( bodyId, &shapeDef, hull );
|
||||
m_wheelIds[2] = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
jointDef.base.bodyIdB = bodyId;
|
||||
jointDef.base.bodyIdB = m_wheelIds[2];
|
||||
jointDef.base.localFrameA.p = { -1.5f, -0.5f, 0.8f };
|
||||
jointDef.enableSteering = false;
|
||||
jointDef.enableSpinMotor = true;
|
||||
|
|
@ -2444,18 +2433,21 @@ public:
|
|||
|
||||
{
|
||||
bodyDef.position = { -1.5f, 2.0f, -0.8f };
|
||||
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
// b3CreateHullShape( bodyId, &shapeDef, hull );
|
||||
m_wheelIds[3] = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
jointDef.base.bodyIdB = bodyId;
|
||||
jointDef.base.bodyIdB = m_wheelIds[3];
|
||||
jointDef.base.localFrameA.p = { -1.5f, -0.5f, -0.8f };
|
||||
jointDef.enableSteering = false;
|
||||
jointDef.enableSpinMotor = true;
|
||||
m_rearRightId = b3CreateWheelJoint( m_worldId, &jointDef );
|
||||
}
|
||||
|
||||
// b3DestroyHull( hull );
|
||||
for ( int i = 0; i < 4; ++i )
|
||||
{
|
||||
m_wheelShapeIds[i] = b3_nullShapeId;
|
||||
}
|
||||
|
||||
CreateWheelShapes();
|
||||
|
||||
m_camera->m_thirdPerson = false;
|
||||
|
||||
|
|
@ -2474,6 +2466,59 @@ public:
|
|||
b3DestroyHeightField( m_heightField );
|
||||
}
|
||||
|
||||
void CreateWheelShapes()
|
||||
{
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.density = 2.0f;
|
||||
shapeDef.baseMaterial.friction = 3.0f;
|
||||
|
||||
float radius = 0.4f;
|
||||
float width = 0.25f;
|
||||
|
||||
b3HullData* cylinder = nullptr;
|
||||
if ( m_wheelType == WheelType::cylinder )
|
||||
{
|
||||
float w = 1.5f * width;
|
||||
cylinder = b3CreateCylinder( w, radius, -0.5f * w, m_cylinderSides );
|
||||
}
|
||||
|
||||
for ( int i = 0; i < 4; ++i )
|
||||
{
|
||||
if ( B3_IS_NON_NULL( m_wheelShapeIds[i] ) )
|
||||
{
|
||||
b3DestroyShape( m_wheelShapeIds[i], false );
|
||||
}
|
||||
|
||||
b3BodyId bodyId = m_wheelIds[i];
|
||||
|
||||
switch ( m_wheelType )
|
||||
{
|
||||
case WheelType::sphere:
|
||||
{
|
||||
b3Sphere sphere = { b3Vec3_zero, radius };
|
||||
m_wheelShapeIds[i] = b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
}
|
||||
break;
|
||||
|
||||
case WheelType::capsule:
|
||||
{
|
||||
b3Capsule capsule = { { 0.0f, -0.5f * width, 0.0f }, { 0.0f, 0.5f * width, 0.0f }, radius };
|
||||
m_wheelShapeIds[i] = b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
|
||||
}
|
||||
break;
|
||||
|
||||
case WheelType::cylinder:
|
||||
m_wheelShapeIds[i] = b3CreateHullShape( bodyId, &shapeDef, cylinder );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( cylinder != nullptr )
|
||||
{
|
||||
b3DestroyHull( cylinder );
|
||||
}
|
||||
}
|
||||
|
||||
void Keyboard( int key, int action, int mods ) override
|
||||
{
|
||||
if ( key == KEY_T && action == ACTION_PRESS )
|
||||
|
|
@ -2484,6 +2529,32 @@ public:
|
|||
|
||||
bool DrawControls() override
|
||||
{
|
||||
ImGui::Text( "Wheels" );
|
||||
if ( ImGui::RadioButton( "Sphere", m_wheelType == WheelType::sphere ) )
|
||||
{
|
||||
m_wheelType = WheelType::sphere;
|
||||
CreateWheelShapes();
|
||||
}
|
||||
|
||||
if ( ImGui::RadioButton( "Capsule", m_wheelType == WheelType::capsule ) )
|
||||
{
|
||||
m_wheelType = WheelType::capsule;
|
||||
CreateWheelShapes();
|
||||
}
|
||||
|
||||
if ( ImGui::RadioButton( "Cylinder", m_wheelType == WheelType::cylinder ) )
|
||||
{
|
||||
m_wheelType = WheelType::cylinder;
|
||||
CreateWheelShapes();
|
||||
}
|
||||
|
||||
if ( ImGui::SliderInt( "Sides", &m_cylinderSides, 3, 32 ) && m_wheelType == WheelType::cylinder )
|
||||
{
|
||||
CreateWheelShapes();
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
ImGui::Text( "Suspension" );
|
||||
if ( ImGui::SliderFloat( "Min##Suspension", &m_lowerTranslation, -10.0f, 10.0f, "%.1f" ) )
|
||||
{
|
||||
|
|
@ -2659,8 +2730,19 @@ public:
|
|||
return new Driving( context );
|
||||
}
|
||||
|
||||
enum class WheelType
|
||||
{
|
||||
sphere,
|
||||
capsule,
|
||||
cylinder,
|
||||
};
|
||||
|
||||
b3HeightFieldData* m_heightField;
|
||||
b3BodyId m_chassisId;
|
||||
b3BodyId m_wheelIds[4];
|
||||
b3ShapeId m_wheelShapeIds[4];
|
||||
WheelType m_wheelType;
|
||||
int m_cylinderSides;
|
||||
b3JointId m_frontLeftId;
|
||||
b3JointId m_frontRightId;
|
||||
b3JointId m_rearLeftId;
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -10,9 +10,13 @@
|
|||
|
||||
#include "box3d/box3d.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <float.h>
|
||||
#include <imgui.h>
|
||||
#include <queue>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
|
||||
enum class ShapeType
|
||||
{
|
||||
|
|
@ -1621,3 +1625,4 @@ public:
|
|||
};
|
||||
|
||||
static int sampleHollowBox = RegisterSample( "Mesh", "Hollow Box", HollowBox::Create );
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue