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 )
{
Vector baseColor, diffuseColor;
Vector end = trace->startpos + ( ( Vector )trace->endpos - ( Vector )trace->startpos ) * 1.1f;
Vector baseColor = vec3_invalid, diffuseColor;
const char *kind;
if ( trace->DidHitWorld() )
{
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
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
{
kind = "Static Prop";
// In this case we hit a static prop.
staticpropmgr->GetStaticPropMaterialColorAndLighting( trace, trace->hitbox - 1, diffuseColor, baseColor );
}
@ -117,20 +122,24 @@ void GetColorForSurface( trace_t *trace, Vector *color )
C_BaseEntity *pEnt = trace->m_pEnt;
if ( !pEnt )
{
Msg("Couldn't find surface in GetColorForSurface()\n");
color->x = 255;
color->y = 255;
color->z = 255;
return;
kind = "Null-Entity";
} else {
kind = "Entity";
ICollideable *pCollide = pEnt->GetCollideable();
int modelIndex = pCollide->GetCollisionModelIndex();
model_t* pModel = const_cast<model_t*>(modelinfo->GetModel( modelIndex ));
// Ask the model info about what we need to know
modelinfo->GetModelMaterialColorAndLighting( pModel, pCollide->GetCollisionOrigin(),
pCollide->GetCollisionAngles(), trace, diffuseColor, baseColor );
}
}
ICollideable *pCollide = pEnt->GetCollideable();
int modelIndex = pCollide->GetCollisionModelIndex();
model_t* pModel = const_cast<model_t*>(modelinfo->GetModel( modelIndex ));
// Ask the model info about what we need to know
modelinfo->GetModelMaterialColorAndLighting( pModel, pCollide->GetCollisionOrigin(),
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