So I’m trying to hack in a Gouraud shading model by overriding the vertex colors at runtime. In the editor it works as expected but when I go to launch the project it doesn’t look as it should. Has anyone experimented with this technique or could help me?
Here is the whole function, it’s implemented as a blueprint function library and is fed all the point lights and static meshes per tick.
Here are the results in editor, correct.
Heres after being Launched.
#include "VertexColorLighting.h"
void UVertexColorLighting::PaintSMVertices(UStaticMeshComponent* SMComp, TArray<UPointLightComponent*> Lights)
{
if (!SMComp) return;
UStaticMesh* SM = SMComp->GetStaticMesh();
if (SM)
{
AActor* Owner = SMComp->GetOwner();
SMComp->SetLODDataCount(1, SMComp->LODData.Num());
FStaticMeshComponentLODInfo* LODInfo = &SMComp->LODData[0];
LODInfo->ReleaseOverrideVertexColorsAndBlock();
LODInfo->PaintedVertices.Empty();
LODInfo->OverrideVertexColors = new FColorVertexBuffer();
FStaticMeshLODResources& LodResources = SM->RenderData->LODResources[0];
TArray<FColor> ColorArray;
ColorArray.Reserve(LodResources.GetNumVertices() - 1);
if (SM->RenderData->LODResources.Num() > 0)
{
FPositionVertexBuffer* VertexBuffer = &SM->RenderData->LODResources[0].VertexBuffers.PositionVertexBuffer;
if (VertexBuffer)
{
const int32 VertexCount = VertexBuffer->GetNumVertices();
for (int32 Index = 0; Index < VertexCount; Index++)
{
const FVector VertexLocation = Owner->GetActorLocation() + Owner->GetTransform().TransformVector(VertexBuffer->VertexPosition(Index));
FColor Corrected(0.0, 0.0, 0.0);
for (int32 i = 0; i < Lights.Num(); i++)
{
if (FVector::PointsAreNear(Lights[i]->GetLightPosition(), VertexLocation, Lights[i]->AttenuationRadius))
{
const float LightDistance = FVector::Distance(Lights[i]->GetLightPosition(), VertexLocation);
const float Brightness = 1/(LightDistance /Lights[i]->AttenuationRadius * 3);
FLinearColor LColor = Lights[i]->LightColor;
Corrected.R = FMath::Clamp(Corrected.R + LColor.ToRGBE().R * Brightness, 0.0f, 255.0f);
Corrected.G = FMath::Clamp(Corrected.G + LColor.ToRGBE().G * Brightness, 0.0f, 255.0f);
Corrected.B = FMath::Clamp(Corrected.B + LColor.ToRGBE().B * Brightness, 0.0f, 255.0f);
Corrected.A = FMath::Clamp(Corrected.A + LColor.ToRGBE().A * Brightness, 0.0f, 255.0f);
}
}
ColorArray.Add(Corrected);
}
}
}
LODInfo->OverrideVertexColors->InitFromColorArray(ColorArray);
BeginInitResource(LODInfo->OverrideVertexColors);
SMComp->MarkRenderStateDirty();
}
}