feat: Make inverse square falloff a toggleable feature

This commit is contained in:
Vsevolod Kremianskii 2021-02-15 19:04:36 +07:00
parent 56ec59b93a
commit b757066244
6 changed files with 24 additions and 10 deletions

View file

@ -798,7 +798,7 @@ bool Game::handleKeyDown(const SDL_KeyboardEvent &event) {
case SDLK_F3:
if (_options.developer) {
setFeatureEnabled(Feature::SelfIllumAsLights, !isFeatureEnabled(Feature::SelfIllumAsLights));
setFeatureEnabled(Feature::InverseSquareFalloff, !isFeatureEnabled(Feature::InverseSquareFalloff));
}
return true;

View file

@ -28,8 +28,9 @@ namespace render {
static unordered_map<Feature, bool> g_features {
{ Feature::PBR, false },
{ Feature::HDR, false },
{ Feature::SelfIllumAsLights, false },
{ Feature::DynamicRoomLighting, false }
{ Feature::InverseSquareFalloff, false },
{ Feature::DynamicRoomLighting, false },
{ Feature::SelfIllumAsLights, false }
};
bool isFeatureEnabled(Feature feature) {

View file

@ -24,8 +24,9 @@ namespace render {
enum class Feature {
PBR,
HDR,
SelfIllumAsLights,
DynamicRoomLighting
InverseSquareFalloff,
DynamicRoomLighting,
SelfIllumAsLights
};
bool isFeatureEnabled(Feature feature);

View file

@ -64,6 +64,7 @@ const int FEATURE_SHADOWS = 0x200;
const int FEATURE_BILLBOARD = 0x400;
const int FEATURE_WATER = 0x800;
const int FEATURE_HDR = 0x1000;
const int FEATURE_INVSQRFALLOFF = 0x2000;
struct Light {
vec4 position;
@ -693,11 +694,17 @@ void main() {
vec3 radiance = uLights[i].color.rgb;
if (uLights[i].position.w == 1.0) {
float D = length(uLights[i].position.xyz - fragPosition);
D *= D;
float R = uLights[i].radius;
R *= R;
float attenuation = uLights[i].multiplier * (R / (R + D));
float attenuation;
if (isFeatureEnabled(FEATURE_INVSQRFALLOFF)) {
float distance = length(uLights[i].position.xyz - fragPosition);
attenuation = 1.0 / (distance * distance);
} else {
float D = length(uLights[i].position.xyz - fragPosition);
D *= D;
float R = uLights[i].radius;
R *= R;
attenuation = uLights[i].multiplier * (R / (R + D));
}
radiance *= attenuation;
}

View file

@ -77,6 +77,7 @@ struct UniformFeatureFlags {
static constexpr int billboard = 0x400;
static constexpr int water = 0x800;
static constexpr int hdr = 0x1000;
static constexpr int invSqrFalloff = 0x2000;
};
struct GeneralUniforms {

View file

@ -237,6 +237,10 @@ void ModelNodeSceneNode::renderSingle(bool shadowPass) {
shaderLight.multiplier = lights[i]->multiplier();
shaderLight.radius = lights[i]->radius();
}
if (isFeatureEnabled(Feature::InverseSquareFalloff)) {
uniforms.general.featureMask |= UniformFeatureFlags::invSqrFalloff;
}
}
if (diffuseTexture) {