Skip to content

Commit 8cc311c

Browse files
AlpyneDreamsmisyltoad
authored andcommitted
Add shading to debug view
1 parent 3ae7588 commit 8cc311c

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

vphysics_jolt/vjolt_debugrender.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ static ConVar vjolt_debugrender_picture_in_picture( "vjolt_debugrender_picture_i
3131
static ConVar vjolt_debugrender_clear_rt( "vjolt_debugrender_clear_rt", "1" );
3232
static ConVar vjolt_debugrender_clear_depth( "vjolt_debugrender_clear_depth", "1" );
3333
static ConVar vjolt_debugrender_wireframe( "vjolt_debugrender_wireframe", "0" );
34-
static ConVar vjolt_debugrender_color_mode( "vjolt_debugrender_color_mode", "instance", 0, "One of instance, shape_type, motion_type, sleep, island, material." );
34+
static ConVar vjolt_debugrender_color_mode("vjolt_debugrender_color_mode", "instance", 0, "One of instance, shape_type, motion_type, sleep, island, material.");
35+
static ConVar vjolt_debugrender_shaded( "vjolt_debugrender_shaded", "1", 0, "Use shading in the debug overlay. Requires map restart to take effect." );
3536
#endif
3637

3738
//-------------------------------------------------------------------------------------------------
@@ -72,6 +73,13 @@ void JoltPhysicsDebugRenderer::DrawTriangle( JPH::Vec3Arg inV1, JPH::Vec3Arg inV
7273
//DrawTriangle_Internal( JPH::Float3( inV1.GetX(), inV1.GetY(), inV1.GetZ() ), JPH::Float3( inV2.GetX(), inV2.GetY(), inV2.GetZ() ), JPH::Float3( inV3.GetX(), inV3.GetY(), inV3.GetZ() ), inColor );
7374
}
7475

76+
//-----------------------------------------------------------------------------
77+
static float LightPlane( Vector& Normal )
78+
{
79+
static Vector Light = Vector( 1.0f, 2.0f, 3.0f ).Normalized();
80+
return 0.65f + ( 0.35f * DotProduct( Normal, Light ) );
81+
}
82+
7583
JoltPhysicsDebugRenderer::Batch JoltPhysicsDebugRenderer::CreateTriangleBatch( const Triangle* inTriangles, int inTriangleCount )
7684
{
7785
#ifndef VJOLT_USE_PHYSICS_DEBUG_OVERLAY
@@ -105,10 +113,21 @@ JoltPhysicsDebugRenderer::Batch JoltPhysicsDebugRenderer::CreateTriangleBatch( c
105113
for (int idx = 0; idx < vertCount; ++idx)
106114
{
107115
int i = firstVertex + idx;
116+
117+
Vector color{
118+
float( inVertices[i].mColor.r ) / 255.f,
119+
float( inVertices[i].mColor.g ) / 255.f,
120+
float( inVertices[i].mColor.b ) / 255.f,
121+
};
122+
Vector normal{ inVertices[i].mNormal.x, inVertices[i].mNormal.y, inVertices[i].mNormal.z };
123+
124+
if ( vjolt_debugrender_shaded.GetBool() )
125+
color *= LightPlane( normal );
126+
108127
meshBuilder.Position3f ( inVertices[i].mPosition.x * JoltToSource::Factor, inVertices[i].mPosition.y * JoltToSource::Factor, inVertices[i].mPosition.z * JoltToSource::Factor );
109-
meshBuilder.Normal3f ( inVertices[i].mNormal.x, inVertices[i].mNormal.y, inVertices[i].mNormal.z );
128+
meshBuilder.Normal3fv ( normal.Base() );
110129
meshBuilder.TexCoord2f ( 0, inVertices[i].mUV.x, inVertices[i].mUV.y );
111-
meshBuilder.Color4Packed( inVertices[i].mColor.mU32 );
130+
meshBuilder.Color3fv ( color.Base() );
112131
meshBuilder.AdvanceVertex();
113132
}
114133
}
@@ -143,10 +162,20 @@ JoltPhysicsDebugRenderer::Batch JoltPhysicsDebugRenderer::CreateTriangleBatch( c
143162
{
144163
for (int i = 0; i < inVertexCount; ++i)
145164
{
165+
Vector color {
166+
float(inVertices[i].mColor.r) / 255.f,
167+
float(inVertices[i].mColor.g) / 255.f,
168+
float(inVertices[i].mColor.b) / 255.f,
169+
};
170+
Vector normal { inVertices[i].mNormal.x, inVertices[i].mNormal.y, inVertices[i].mNormal.z };
171+
172+
if (vjolt_debugrender_shaded.GetBool())
173+
color *= LightPlane(normal);
174+
146175
meshBuilder.Position3f ( inVertices[i].mPosition.x * JoltToSource::Factor, inVertices[i].mPosition.y * JoltToSource::Factor, inVertices[i].mPosition.z * JoltToSource::Factor );
147-
meshBuilder.Normal3f ( inVertices[i].mNormal.x, inVertices[i].mNormal.y, inVertices[i].mNormal.z );
176+
meshBuilder.Normal3fv ( normal.Base() );
148177
meshBuilder.TexCoord2f ( 0, inVertices[i].mUV.x, inVertices[i].mUV.y );
149-
meshBuilder.Color4Packed( inVertices[i].mColor.mU32 );
178+
meshBuilder.Color3fv ( color.Base() );
150179
meshBuilder.AdvanceVertex();
151180
}
152181

0 commit comments

Comments
 (0)