experiment: auto-conversion of object spread lights to hybrid spread/point lights

This commit is contained in:
Kae 2024-03-26 07:31:33 +11:00
parent 77d7f8eb81
commit c484fab32d
22 changed files with 95 additions and 61 deletions

Binary file not shown.

View File

@ -1,21 +1,6 @@
-- unused for now
local function modLight(light)
for i = 1, #light do
light[i] = light[i] * 0.4
end
end
function patch(object, path)
if object.lightColor then
modLight(object.lightColor)
object.pointLight = true
return object;
elseif object.lightColors then
for i, v in pairs(object.lightColors) do
modLight(v)
end
object.pointLight = true
if object.pointLight ~= true and (object.lightColor or object.lightColors) then
object.lightType = "PointAsSpread"
return object;
end
end

View File

@ -11,8 +11,8 @@ if assets.image("/cursors/cursors.png"):size()[1] == 64 then
end
-- Add object patches
--local objects = assets.byExtension("object")
--local path = "/objects/opensb/object.patch.lua"
--for i = 1, #objects do
-- assets.patch(objects[i], path)
--end
local objects = assets.byExtension("object")
local path = "/objects/opensb/object.patch.lua"
for i = 1, #objects do
assets.patch(objects[i], path)
end

View File

@ -1,12 +1,13 @@
#include "StarCellularLightArray.hpp"
#include "StarInterpolation.hpp"
// just specializing these in a cpp file so I can iterate on them without recompiling like 40 files!!
namespace Star {
template <>
void CellularLightArray<ScalarLightTraits>::calculatePointLighting(size_t xmin, size_t ymin, size_t xmax, size_t ymax) {
float perBlockObstacleAttenuation = 1.0f / m_pointMaxObstacle;
float perBlockAirAttenuation = 1.0f / m_pointMaxAir;
float pointPerBlockObstacleAttenuation = 1.0f / m_pointMaxObstacle;
float pointPerBlockAirAttenuation = 1.0f / m_pointMaxAir;
for (PointLight light : m_pointLights) {
if (light.position[0] < 0 || light.position[0] > m_width - 1 || light.position[1] < 0 || light.position[1] > m_height - 1)
@ -14,8 +15,10 @@ void CellularLightArray<ScalarLightTraits>::calculatePointLighting(size_t xmin,
float maxIntensity = ScalarLightTraits::maxIntensity(light.value);
Vec2F beamDirection = Vec2F(1, 0).rotate(light.beamAngle);
float perBlockObstacleAttenuation = light.asSpread ? 1.0f / m_spreadMaxObstacle : pointPerBlockObstacleAttenuation;
float perBlockAirAttenuation = light.asSpread ? 1.0f / m_spreadMaxAir : pointPerBlockAirAttenuation;
float maxRange = maxIntensity * m_pointMaxAir;
float maxRange = maxIntensity * (light.asSpread ? m_spreadMaxAir : m_pointMaxAir);
// The min / max considering the radius of the light
size_t lxmin = std::floor(std::max<float>(xmin, light.position[0] - maxRange));
size_t lymin = std::floor(std::max<float>(ymin, light.position[1] - maxRange));
@ -40,7 +43,7 @@ void CellularLightArray<ScalarLightTraits>::calculatePointLighting(size_t xmin,
continue;
Vec2F direction = relativeLightPosition / distance;
if (light.beam > 0.0f) {
if (light.beam > 0.0001f) {
attenuation += (1.0f - light.beamAmbience) * clamp(light.beam * (1.0f - direction * beamDirection), 0.0f, 1.0f);
if (attenuation >= 1.0f)
continue;
@ -54,12 +57,21 @@ void CellularLightArray<ScalarLightTraits>::calculatePointLighting(size_t xmin,
float circularizedPerBlockObstacleAttenuation = perBlockObstacleAttenuation / max(fabs(direction[0]), fabs(direction[1]));
float blockAttenuation = lineAttenuation(blockPos, light.position, circularizedPerBlockObstacleAttenuation, remainingAttenuation);
attenuation += blockAttenuation;
// Apply single obstacle boost (determine single obstacle by one
// block unit of attenuation).
attenuation += blockAttenuation + min(blockAttenuation, circularizedPerBlockObstacleAttenuation) * m_pointObstacleBoost;
if (!light.asSpread)
attenuation += min(blockAttenuation, circularizedPerBlockObstacleAttenuation) * m_pointObstacleBoost;
if (attenuation < 1.0f)
setLight(x, y, lvalue + ScalarLightTraits::subtract(light.value, attenuation));
if (attenuation < 1.0f) {
auto newLight = ScalarLightTraits::subtract(light.value, attenuation);
if (ScalarLightTraits::maxIntensity(newLight) > 0.0001f) {
if (light.asSpread)
setLight(x, y, lvalue + newLight * 0.25f);
else
setLight(x, y, lvalue + newLight);
}
}
}
}
}
@ -67,8 +79,8 @@ void CellularLightArray<ScalarLightTraits>::calculatePointLighting(size_t xmin,
template <>
void CellularLightArray<ColoredLightTraits>::calculatePointLighting(size_t xmin, size_t ymin, size_t xmax, size_t ymax) {
float perBlockObstacleAttenuation = 1.0f / m_pointMaxObstacle;
float perBlockAirAttenuation = 1.0f / m_pointMaxAir;
float pointPerBlockObstacleAttenuation = 1.0f / m_pointMaxObstacle;
float pointPerBlockAirAttenuation = 1.0f / m_pointMaxAir;
for (PointLight light : m_pointLights) {
if (light.position[0] < 0 || light.position[0] > m_width - 1 || light.position[1] < 0 || light.position[1] > m_height - 1)
@ -76,8 +88,10 @@ void CellularLightArray<ColoredLightTraits>::calculatePointLighting(size_t xmin,
float maxIntensity = ColoredLightTraits::maxIntensity(light.value);
Vec2F beamDirection = Vec2F(1, 0).rotate(light.beamAngle);
float perBlockObstacleAttenuation = light.asSpread ? 1.0f / m_spreadMaxObstacle : pointPerBlockObstacleAttenuation;
float perBlockAirAttenuation = light.asSpread ? 1.0f / m_spreadMaxAir : pointPerBlockAirAttenuation;
float maxRange = maxIntensity * m_pointMaxAir;
float maxRange = maxIntensity * (light.asSpread ? m_spreadMaxAir : m_pointMaxAir);
// The min / max considering the radius of the light
size_t lxmin = std::floor(std::max<float>(xmin, light.position[0] - maxRange));
size_t lymin = std::floor(std::max<float>(ymin, light.position[1] - maxRange));
@ -116,12 +130,21 @@ void CellularLightArray<ColoredLightTraits>::calculatePointLighting(size_t xmin,
float circularizedPerBlockObstacleAttenuation = perBlockObstacleAttenuation / max(fabs(direction[0]), fabs(direction[1]));
float blockAttenuation = lineAttenuation(blockPos, light.position, circularizedPerBlockObstacleAttenuation, remainingAttenuation);
attenuation += blockAttenuation;
// Apply single obstacle boost (determine single obstacle by one
// block unit of attenuation).
attenuation += blockAttenuation + min(blockAttenuation, circularizedPerBlockObstacleAttenuation) * m_pointObstacleBoost;
if (!light.asSpread)
attenuation += min(blockAttenuation, circularizedPerBlockObstacleAttenuation) * m_pointObstacleBoost;
if (attenuation < 1.0f)
setLight(x, y, lvalue + ColoredLightTraits::subtract(light.value, attenuation));
if (attenuation < 1.0f) {
auto newLight = ColoredLightTraits::subtract(light.value, attenuation);
if (ColoredLightTraits::maxIntensity(newLight) > 0.0001f) {
if (light.asSpread)
setLight(x, y, lvalue + newLight * 0.25f);
else
setLight(x, y, lvalue + newLight);
}
}
}
}
}

View File

@ -56,6 +56,7 @@ public:
float beam;
float beamAngle;
float beamAmbience;
bool asSpread;
};
void setParameters(unsigned spreadPasses, float spreadMaxAir, float spreadMaxObstacle,

View File

@ -109,12 +109,12 @@ void CellularLightingCalculator::addSpreadLight(Vec2F const& position, Vec3F con
m_lightArray.left().addSpreadLight({arrayPosition, light});
}
void CellularLightingCalculator::addPointLight(Vec2F const& position, Vec3F const& light, float beam, float beamAngle, float beamAmbience) {
void CellularLightingCalculator::addPointLight(Vec2F const& position, Vec3F const& light, float beam, float beamAngle, float beamAmbience, bool asSpread) {
Vec2F arrayPosition = position - Vec2F(m_calculationRegion.min());
if (m_monochrome)
m_lightArray.right().addPointLight({arrayPosition, light.max(), beam, beamAngle, beamAmbience});
m_lightArray.right().addPointLight({arrayPosition, light.max(), beam, beamAngle, beamAmbience, asSpread});
else
m_lightArray.left().addPointLight({arrayPosition, light, beam, beamAngle, beamAmbience});
m_lightArray.left().addPointLight({arrayPosition, light, beam, beamAngle, beamAmbience, asSpread});
}
void CellularLightingCalculator::calculate(Image& output) {

View File

@ -136,7 +136,7 @@ public:
void setCellIndex(size_t cellIndex, Vec3F const& light, bool obstacle);
void addSpreadLight(Vec2F const& position, Vec3F const& light);
void addPointLight(Vec2F const& position, Vec3F const& light, float beam, float beamAngle, float beamAmbience);
void addPointLight(Vec2F const& position, Vec3F const& light, float beam, float beamAngle, float beamAmbience, bool asSpread = false);
// Finish the calculation, and put the resulting color data in the given
// output image. The image will be reset to the size of the region given in

View File

@ -320,7 +320,7 @@ void ItemDrop::render(RenderCallback* renderCallback) {
void ItemDrop::renderLightSources(RenderCallback* renderCallback) {
LightSource light;
light.pointLight = false;
light.type = LightType::Spread;
light.color = Vec3F::filled(20.f / 255.f);
light.position = position();
renderCallback->addLightSource(std::move(light));

View File

@ -3,6 +3,12 @@
namespace Star {
EnumMap<LightType> const LightTypeNames{
{LightType::Spread, "Spread"},
{LightType::Point, "Point"},
{LightType::PointAsSpread, "PointAsSpread"}
};
void LightSource::translate(Vec2F const& pos) {
position += pos;
}
@ -10,7 +16,7 @@ void LightSource::translate(Vec2F const& pos) {
DataStream& operator<<(DataStream& ds, LightSource const& lightSource) {
ds.write(lightSource.position);
ds.write(lightSource.color);
ds.write(lightSource.pointLight);
ds.write(lightSource.type);
ds.write(lightSource.pointBeam);
ds.write(lightSource.beamAngle);
ds.write(lightSource.beamAmbience);
@ -21,7 +27,7 @@ DataStream& operator<<(DataStream& ds, LightSource const& lightSource) {
DataStream& operator>>(DataStream& ds, LightSource& lightSource) {
ds.read(lightSource.position);
ds.read(lightSource.color);
ds.read(lightSource.pointLight);
ds.read(lightSource.type);
ds.read(lightSource.pointBeam);
ds.read(lightSource.beamAngle);
ds.read(lightSource.beamAmbience);

View File

@ -2,14 +2,22 @@
#include "StarVector.hpp"
#include "StarDataStream.hpp"
#include "StarBiMap.hpp"
namespace Star {
enum class LightType : uint8_t {
Spread = 0,
Point = 1,
PointAsSpread = 2 // Point with spread-like range
};
extern EnumMap<LightType> const LightTypeNames;
struct LightSource {
Vec2F position;
Vec3F color;
bool pointLight;
LightType type;
// pointBeam of 0.0 means light has no beam component, as pointBeam goes up,
// the dropoff from the beamAngle becomes faster and faster.
float pointBeam;

View File

@ -717,7 +717,7 @@ List<LightSource> NetworkedAnimator::lightSources(Vec2F const& translate) const
lightSources.append(LightSource{
position + translate,
color.toRgbF(),
pair.second.pointLight,
pair.second.pointLight ? LightType::Point : LightType::Spread,
pair.second.pointBeam,
pointAngle,
pair.second.beamAmbience

View File

@ -262,7 +262,7 @@ List<LightSource> Object::lightSources() const {
LightSource lightSource;
lightSource.position = position() + centerOfTile(orientation->lightPosition);
lightSource.color = color.toRgbF();
lightSource.pointLight = m_config->pointLight;
lightSource.type = m_config->lightType;
lightSource.pointBeam = m_config->pointBeam;
lightSource.beamAngle = orientation->beamAngle;
lightSource.beamAmbience = m_config->beamAmbience;

View File

@ -495,7 +495,10 @@ ObjectConfigPtr ObjectDatabase::readConfig(String const& path) {
objectConfig->lightColors[pair.first] = jsonToColor(pair.second);
}
objectConfig->pointLight = config.getBool("pointLight", false);
if (auto lightType = config.optString("lightType"))
objectConfig->lightType = LightTypeNames.getLeft(*lightType);
else
objectConfig->lightType = (LightType)config.getBool("pointLight", false);
objectConfig->pointBeam = config.getFloat("pointBeam", 0.0f);
objectConfig->beamAmbience = config.getFloat("beamAmbience", 0.0f);

View File

@ -131,7 +131,7 @@ struct ObjectConfig {
bool interactive;
StringMap<Color> lightColors;
bool pointLight;
LightType lightType;
float pointBeam;
float beamAmbience;
Maybe<PeriodicFunction<float>> lightFlickering;

View File

@ -379,7 +379,7 @@ void Projectile::renderLightSources(RenderCallback* renderCallback) {
if (renderable.is<LightSource>())
renderCallback->addLightSource(renderable.get<LightSource>());
}
renderCallback->addLightSource({position(), m_config->lightColor.toRgbF(), m_config->pointLight, 0.0f, 0.0f, 0.0f});
renderCallback->addLightSource({position(), m_config->lightColor.toRgbF(), m_config->lightType, 0.0f, 0.0f, 0.0f});
}
Maybe<Json> Projectile::receiveMessage(ConnectionId sendingConnection, String const& message, JsonArray const& args) {
@ -825,7 +825,7 @@ void Projectile::processAction(Json const& action) {
m_pendingRenderables.append(LightSource{
position(),
jsonToColor(parameters.get("color")).toRgbF(),
parameters.getBool("pointLight", true),
(LightType)parameters.getBool("pointLight", true),
0.0f,
0.0f,
0.0f

View File

@ -118,7 +118,10 @@ ProjectileConfigPtr ProjectileDatabase::readConfig(String const& path) {
projectileConfig->lightColor = jsonToColor(config.get("lightColor", JsonArray{0, 0, 0}));
projectileConfig->lightPosition = jsonToVec2F(config.get("lightPosition", JsonArray{0, 0}));
projectileConfig->pointLight = config.getBool("pointLight", false);
if (auto lightType = config.optString("lightType"))
projectileConfig->lightType = LightTypeNames.getLeft(*lightType);
else
projectileConfig->lightType = (LightType)config.getBool("pointLight", false);
projectileConfig->persistentAudio = config.getString("persistentAudio", "");

View File

@ -68,7 +68,7 @@ struct ProjectileConfig {
Color lightColor;
Vec2F lightPosition;
bool pointLight = false;
LightType lightType = LightType::Spread;
String persistentAudio;

View File

@ -1652,10 +1652,15 @@ void WorldClient::lightingCalc() {
for (auto const& light : lights) {
Vec2F position = m_geometry.nearestTo(Vec2F(m_lightingCalculator.calculationRegion().min()), light.position);
if (light.pointLight)
m_lightingCalculator.addPointLight(position, light.color, light.pointBeam, light.beamAngle, light.beamAmbience);
else {
if (light.type == LightType::Spread)
m_lightingCalculator.addSpreadLight(position, light.color);
else {
if (light.type == LightType::PointAsSpread) {
// hybrid (used for auto-converted object lights) - 75% spread, 25% point (2nd is applied elsewhere)
m_lightingCalculator.addSpreadLight(position, light.color * 0.75f);
m_lightingCalculator.addPointLight(position, light.color, light.pointBeam, light.beamAngle, light.beamAmbience, true);
} else // fully additive point light
m_lightingCalculator.addPointLight(position, light.color, light.pointBeam, light.beamAngle, light.beamAmbience);
}
}

View File

@ -445,10 +445,10 @@ namespace WorldImpl {
for (auto const& entity : entityMap->entityQuery(RectF(lighting.calculationRegion()))) {
for (auto const& light : entity->lightSources()) {
Vec2F position = worldGeometry.nearestTo(Vec2F(lighting.calculationRegion().min()), light.position);
if (light.pointLight)
lighting.addPointLight(position, light.color.sum() / 3.0f, light.pointBeam, light.beamAngle, light.beamAmbience);
else
if (light.type == LightType::Spread)
lighting.addSpreadLight(position, light.color.sum() / 3.0f);
else
lighting.addPointLight(position, light.color.sum() / 3.0f, light.pointBeam, light.beamAngle, light.beamAmbience);
}
}

View File

@ -54,7 +54,7 @@ List<LightSource> InspectionTool::lightSources() const {
float angle = world()->geometry().diff(owner()->aimPosition(), owner()->position()).angle();
LightSource lightSource;
lightSource.pointLight = true;
lightSource.type = LightType::Point;
lightSource.position = owner()->position() + owner()->handPosition(hand(), m_lightPosition - m_handPosition);
lightSource.color = m_lightColor.toRgbF();
lightSource.pointBeam = m_beamWidth;

View File

@ -239,7 +239,7 @@ List<LightSource> Flashlight::lightSources() const {
float angle = world()->geometry().diff(owner()->aimPosition(), owner()->position()).angle();
LightSource lightSource;
lightSource.pointLight = true;
lightSource.type = LightType::Point;
lightSource.position = owner()->position() + owner()->handPosition(hand(), (m_lightPosition - m_handPosition) / TilePixels);
lightSource.color = m_lightColor.toRgbF();
lightSource.pointBeam = m_beamWidth;

View File

@ -77,7 +77,7 @@ LuaAnimationComponent<Base>::LuaAnimationComponent() {
m_lightSources.append({
lightSourceTable.get<Vec2F>("position"),
lightSourceTable.get<Color>("color").toRgbF(),
lightSourceTable.get<Maybe<bool>>("pointLight").value(),
(LightType)lightSourceTable.get<Maybe<bool>>("pointLight").value(),
lightSourceTable.get<Maybe<float>>("pointBeam").value(),
lightSourceTable.get<Maybe<float>>("beamAngle").value(),
lightSourceTable.get<Maybe<float>>("beamAmbience").value()