Skip to content

Commit

Permalink
Implementing light range support in Metal
Browse files Browse the repository at this point in the history
  • Loading branch information
colincornaby committed Nov 27, 2024
1 parent 5cdc425 commit 6ae48c1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ vertex ColorInOut pipelineVertexShader(Vertex in [[stage_in]],
// Omni Light in all directions
const float3 v2l = lightSource->position.xyz - position.xyz;
const float distance = length(v2l);

if (distance > lightSource->range) {
continue;
}

direction.xyz = normalize(v2l);

direction.w = 1.f / (lightSource->constAtten + lightSource->linAtten * distance + lightSource->quadAtten * pow(distance, 2.f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ struct plMetalShaderLightSource
half4 specular;
simd::float3 direction;
simd::float4 spotProps; // (falloff, theta, phi)
__fp16 range;
__fp16 constAtten;
__fp16 linAtten;
__fp16 quadAtten;
Expand Down
7 changes: 7 additions & 0 deletions Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,8 @@ void plMetalPipeline::IEnableLight(size_t i, plLightInfo* light)
plDirectionalLightInfo* dirLight = nullptr;
plOmniLightInfo* omniLight = nullptr;
plSpotLightInfo* spotLight = nullptr;

const float maxRange = 32767.f;

if ((dirLight = plDirectionalLightInfo::ConvertNoRef(light)) != nullptr) {
hsVector3 lightDir = dirLight->GetWorldDirection();
Expand All @@ -2383,6 +2385,9 @@ void plMetalPipeline::IEnableLight(size_t i, plLightInfo* light)
fLights.lampSources[i].constAtten = 1.0f;
fLights.lampSources[i].linAtten = 0.0f;
fLights.lampSources[i].quadAtten = 0.0f;

fLights.lampSources[i].range = maxRange;

} else if ((omniLight = plOmniLightInfo::ConvertNoRef(light)) != nullptr) {
hsPoint3 pos = omniLight->GetWorldPosition();
fLights.lampSources[i].position = {pos.fX, pos.fY, pos.fZ, 1.0};
Expand All @@ -2392,6 +2397,8 @@ void plMetalPipeline::IEnableLight(size_t i, plLightInfo* light)
fLights.lampSources[i].constAtten = omniLight->GetConstantAttenuation();
fLights.lampSources[i].linAtten = omniLight->GetLinearAttenuation();
fLights.lampSources[i].quadAtten = omniLight->GetQuadraticAttenuation();

fLights.lampSources[i].range = omniLight->GetRadius();

if (!omniLight->GetProjection() && (spotLight = plSpotLightInfo::ConvertNoRef(omniLight)) != nullptr) {
hsVector3 lightDir = spotLight->GetWorldDirection();
Expand Down

0 comments on commit 6ae48c1

Please sign in to comment.