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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const float maxRange = 32767.f;
constexpr float kMaxRange = 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fLights.lampSources[i].range = maxRange;
fLights.lampSources[i].range = kMaxRange;


} 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