Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing light range support in Metal #1638

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 8 additions & 2 deletions Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,9 @@ void plMetalPipeline::IEnableLight(size_t i, plLightInfo* light)
plDirectionalLightInfo* dirLight = nullptr;
plOmniLightInfo* omniLight = nullptr;
plSpotLightInfo* spotLight = nullptr;

const float maxRange = 32767.f;
colincornaby marked this conversation as resolved.
Show resolved Hide resolved
fLights.lampSources[i].range = maxRange;

if ((dirLight = plDirectionalLightInfo::ConvertNoRef(light)) != nullptr) {
hsVector3 lightDir = dirLight->GetWorldDirection();
Expand All @@ -2383,15 +2386,18 @@ 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;

colincornaby marked this conversation as resolved.
Show resolved Hide resolved
} else if ((omniLight = plOmniLightInfo::ConvertNoRef(light)) != nullptr) {
hsPoint3 pos = omniLight->GetWorldPosition();
fLights.lampSources[i].position = {pos.fX, pos.fY, pos.fZ, 1.0};

// TODO: Maximum Range

fLights.lampSources[i].constAtten = omniLight->GetConstantAttenuation();
fLights.lampSources[i].linAtten = omniLight->GetLinearAttenuation();
fLights.lampSources[i].quadAtten = omniLight->GetQuadraticAttenuation();

if (omniLight->GetRadius() != 0.f) {
fLights.lampSources[i].range = omniLight->GetRadius();
}

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