Implement GetColorForSurface() failure workaround

This function is used to color impact particles.
On Linux I've noticed that this function sometimes is not successful on
retrieving the surface color, leaving an odd color to render the
particles with. The engine function TraceLineMaterialAndLighting() even
has a boolean return value indicating success.
GetModelMaterialColorAndLighting() does not though, but I still observe
failures (the color parameter is not modified). The color is initialized
with an invalid value. If it detects that retrieving the color failed
(engine function said so or the invalid value was left in place), this
now hamfistedly assumes a lightish grey color, but at least still
correctly (presumably) incorporates lighting information.
When this situation is detected, a warning is also printed to the
console. Because why not.
This commit is contained in:
Alexander 'z33ky' Hirsch 2024-09-05 23:35:46 +02:00
parent 471a840ed9
commit e9c45e5235

View File

@ -95,18 +95,23 @@ extern PMaterialHandle g_Material_Spark;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void GetColorForSurface( trace_t *trace, Vector *color ) void GetColorForSurface( trace_t *trace, Vector *color )
{ {
Vector baseColor, diffuseColor; Vector baseColor = vec3_invalid, diffuseColor;
Vector end = trace->startpos + ( ( Vector )trace->endpos - ( Vector )trace->startpos ) * 1.1f; const char *kind;
if ( trace->DidHitWorld() ) if ( trace->DidHitWorld() )
{ {
if ( trace->hitbox == 0 ) if ( trace->hitbox == 0 )
{ {
kind = "World";
Vector end = trace->startpos + ( trace->endpos - trace->startpos ) * 1.1f;
// If we hit the world, then ask the world for the fleck color // If we hit the world, then ask the world for the fleck color
engine->TraceLineMaterialAndLighting( trace->startpos, end, diffuseColor, baseColor ); if ( !engine->TraceLineMaterialAndLighting( trace->startpos, end, diffuseColor, baseColor ) ) {
baseColor = vec3_invalid; // Make sure this wasn't modified
}
} }
else else
{ {
kind = "Static Prop";
// In this case we hit a static prop. // In this case we hit a static prop.
staticpropmgr->GetStaticPropMaterialColorAndLighting( trace, trace->hitbox - 1, diffuseColor, baseColor ); staticpropmgr->GetStaticPropMaterialColorAndLighting( trace, trace->hitbox - 1, diffuseColor, baseColor );
} }
@ -117,13 +122,9 @@ void GetColorForSurface( trace_t *trace, Vector *color )
C_BaseEntity *pEnt = trace->m_pEnt; C_BaseEntity *pEnt = trace->m_pEnt;
if ( !pEnt ) if ( !pEnt )
{ {
Msg("Couldn't find surface in GetColorForSurface()\n"); kind = "Null-Entity";
color->x = 255; } else {
color->y = 255; kind = "Entity";
color->z = 255;
return;
}
ICollideable *pCollide = pEnt->GetCollideable(); ICollideable *pCollide = pEnt->GetCollideable();
int modelIndex = pCollide->GetCollisionModelIndex(); int modelIndex = pCollide->GetCollisionModelIndex();
model_t* pModel = const_cast<model_t*>(modelinfo->GetModel( modelIndex )); model_t* pModel = const_cast<model_t*>(modelinfo->GetModel( modelIndex ));
@ -132,6 +133,14 @@ void GetColorForSurface( trace_t *trace, Vector *color )
modelinfo->GetModelMaterialColorAndLighting( pModel, pCollide->GetCollisionOrigin(), modelinfo->GetModelMaterialColorAndLighting( pModel, pCollide->GetCollisionOrigin(),
pCollide->GetCollisionAngles(), trace, diffuseColor, baseColor ); pCollide->GetCollisionAngles(), trace, diffuseColor, baseColor );
} }
}
if ( baseColor == vec3_invalid )
{
Warning( "Couldn't find surface color of %s\n", kind );
baseColor = Vector( .5f, .5f, .5f );
diffuseColor = engine->GetLightForPoint( trace->endpos, true );
}
//Get final light value //Get final light value
color->x = pow( diffuseColor[0], 1.0f/2.2f ) * baseColor[0]; color->x = pow( diffuseColor[0], 1.0f/2.2f ) * baseColor[0];