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

improve: lighting system #577

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
104 changes: 61 additions & 43 deletions src/client/lightview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,18 @@
#include <framework/graphics/drawpoolmanager.h>

LightView::LightView(const Size& size, const uint16_t tileSize) : m_pool(g_drawPool.get(DrawPoolType::LIGHT)) {
resize(size, tileSize);
g_mainDispatcher.addEvent([&] {
m_texture = std::make_shared<Texture>(m_lightData.mapSize);
m_texture->setSmooth(true);
});

m_thread = std::thread([this]() {
std::unique_lock lock(m_pool->getMutex());
m_condition.wait(lock, [this]() -> bool {
updatePixels();
return m_texture == nullptr;
});
});
}

void LightView::resize(const Size& size, const uint16_t tileSize) {
std::scoped_lock l(m_pool->getMutex());
m_lightData.mapSize = size;
m_lightData.tileSize = tileSize;
m_lightData.tiles.resize(size.area());
if (m_pixels.size() < 4u * m_lightData.mapSize.area())
m_pixels.resize(m_lightData.mapSize.area() * 4);
if (m_texture)
m_texture->setupSize(m_lightData.mapSize);
g_mainDispatcher.addEvent([this, size] {
m_texture = std::make_shared<Texture>(size);
m_texture->setSmooth(true);
});

g_drawPool.use(DrawPoolType::LIGHT);
g_drawPool.addAction([this] {
Expand All @@ -68,19 +56,38 @@ void LightView::resize(const Size& size, const uint16_t tileSize) {
});
}

void LightView::resize(const Size& size, const uint16_t tileSize) {
std::scoped_lock l(m_pool->getMutex());

m_mapSize = size;
m_tileSize = tileSize;

for (auto& lightData : m_lightData) {
lightData.tiles.resize(size.area());
lightData.lights.clear();
}

m_pixels.resize(size.area() * 4);

if (m_texture)
m_texture->setupSize(m_mapSize);
}

void LightView::addLightSource(const Point& pos, const Light& light, float brightness)
{
if (light.intensity == 0)
return;

if (!m_lightData.lights.empty()) {
auto& prevLight = m_lightData.lights.back();
auto& lightData = m_lightData[m_currentLightData];

if (!lightData.lights.empty()) {
auto& prevLight = lightData.lights.back();
if (prevLight.pos == pos && prevLight.color == light.color) {
prevLight.intensity = std::max<uint8_t>(prevLight.intensity, light.intensity);
return;
}
}
m_lightData.lights.emplace_back(pos, light.intensity, light.color, std::min<float>(brightness, g_drawPool.getOpacity()));
lightData.lights.emplace_back(pos, light.intensity, light.color, std::min<float>(brightness, g_drawPool.getOpacity()));

stdext::hash_union(m_updatedHash, pos.hash());
stdext::hash_combine(m_updatedHash, light.intensity);
Expand All @@ -92,9 +99,11 @@ void LightView::addLightSource(const Point& pos, const Light& light, float brigh

void LightView::resetShade(const Point& pos)
{
size_t index = (pos.y / m_lightData.tileSize) * m_lightData.mapSize.width() + (pos.x / m_lightData.tileSize);
if (index >= m_lightData.tiles.size()) return;
m_lightData.tiles[index] = m_lightData.lights.size();
auto& lightData = m_lightData[m_currentLightData];

size_t index = (pos.y / m_tileSize) * m_mapSize.width() + (pos.x / m_tileSize);
if (index >= lightData.tiles.size()) return;
lightData.tiles[index] = lightData.lights.size();
}

void LightView::draw(const Rect& dest, const Rect& src)
Expand All @@ -104,12 +113,15 @@ void LightView::draw(const Rect& dest, const Rect& src)
if (m_updatedHash != m_hash) {
m_hash = m_updatedHash;
m_updatedHash = 0;
m_threadLightData = m_lightData;

std::scoped_lock l(m_pool->getMutex());
if (++m_currentLightData > 1) m_currentLightData = 0;
m_condition.notify_one();
}

m_lightData.lights.clear();
m_lightData.tiles.assign(m_lightData.mapSize.area(), {});
auto& lightData = m_lightData[m_currentLightData];
lightData.lights.clear();
lightData.tiles.assign(m_mapSize.area(), {});
}

void LightView::updateCoords(const Rect& dest, const Rect& src) {
Expand All @@ -124,31 +136,37 @@ void LightView::updateCoords(const Rect& dest, const Rect& src) {

m_coords.clear();
m_coords.addRect(RectF(m_dest.left(), m_dest.top(), m_dest.width(), m_dest.height()),
RectF(static_cast<float>(offset.x) / m_lightData.tileSize, static_cast<float>(offset.y) / m_lightData.tileSize,
static_cast<float>(size.width()) / m_lightData.tileSize, static_cast<float>(size.height()) / m_lightData.tileSize));
RectF(static_cast<float>(offset.x) / m_tileSize, static_cast<float>(offset.y) / m_tileSize,
static_cast<float>(size.width()) / m_tileSize, static_cast<float>(size.height()) / m_tileSize));
}

void LightView::updatePixels() {
const size_t lightSize = m_threadLightData.lights.size();

for (int x = 0; x < m_threadLightData.mapSize.width(); ++x) {
for (int y = 0; y < m_threadLightData.mapSize.height(); ++y) {
Point pos(x * m_threadLightData.tileSize + m_threadLightData.tileSize / 2, y * m_threadLightData.tileSize + m_threadLightData.tileSize / 2);
int index = (y * m_threadLightData.mapSize.width() + x);
int colorIndex = index * 4;
m_pixels[colorIndex] = m_threadLightData.globalLightColor.r();
m_pixels[colorIndex + 1] = m_threadLightData.globalLightColor.g();
m_pixels[colorIndex + 2] = m_threadLightData.globalLightColor.b();
const auto& lightData = m_lightData[m_currentLightData ? 0 : 1];

const size_t lightSize = lightData.lights.size();

const int mapWidth = m_mapSize.width();
const int mapHeight = m_mapSize.height();

for (int x = -1; ++x < mapWidth;) {
for (int y = -1; ++y < mapHeight;) {
const Point pos(x * m_tileSize + m_tileSize / 2, y * m_tileSize + m_tileSize / 2);
const int index = (y * mapWidth + x);
const int colorIndex = index * 4;
m_pixels[colorIndex] = m_globalLightColor.r();
m_pixels[colorIndex + 1] = m_globalLightColor.g();
m_pixels[colorIndex + 2] = m_globalLightColor.b();
m_pixels[colorIndex + 3] = 255; // alpha channel
for (size_t i = m_threadLightData.tiles[index]; i < lightSize; ++i) {
const auto& light = m_threadLightData.lights[i];
float distance = std::sqrt((pos.x - light.pos.x) * (pos.x - light.pos.x) +
(pos.y - light.pos.y) * (pos.y - light.pos.y));
distance /= m_threadLightData.tileSize;
for (size_t i = lightData.tiles[index]; i < lightSize; ++i) {
const auto& light = lightData.lights[i];
const float distance = (std::sqrt((pos.x - light.pos.x) * (pos.x - light.pos.x) +
(pos.y - light.pos.y) * (pos.y - light.pos.y))) / m_tileSize;

float intensity = (-distance + light.intensity) * 0.2f;
if (intensity < 0.01f) continue;
if (intensity > 1.0f) intensity = 1.0f;
Color lightColor = Color::from8bit(light.color) * intensity;

const auto& lightColor = Color::from8bit(light.color) * intensity;
m_pixels[colorIndex] = std::max<int>(m_pixels[colorIndex], lightColor.r());
m_pixels[colorIndex + 1] = std::max<int>(m_pixels[colorIndex + 1], lightColor.g());
m_pixels[colorIndex + 2] = std::max<int>(m_pixels[colorIndex + 2], lightColor.b());
Expand Down
13 changes: 8 additions & 5 deletions src/client/lightview.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class LightView : public LuaObject

void setGlobalLight(const Light& light)
{
std::scoped_lock l(m_pool->getMutex());
m_isDark = light.intensity < 250;
m_lightData.globalLightColor = Color::from8bit(light.color, light.intensity / static_cast<float>(UINT8_MAX));
m_globalLightColor = Color::from8bit(light.color, light.intensity / static_cast<float>(UINT8_MAX));
}

bool isDark() const { return m_isDark; }
Expand All @@ -62,11 +63,8 @@ class LightView : public LuaObject

struct LightData
{
Size mapSize;
uint16_t tileSize{ 0 };
std::vector<size_t> tiles;
std::vector<TileLight> lights;
Color globalLightColor{ Color::white };
};

void updateCoords(const Rect& dest, const Rect& src);
Expand All @@ -76,12 +74,17 @@ class LightView : public LuaObject

size_t m_hash{ 0 }, m_updatedHash{ 0 };

Size m_mapSize;
uint16_t m_tileSize{ 32 };
Color m_globalLightColor{ Color::white };

DrawPool* m_pool{ nullptr };

Rect m_dest, m_src;
CoordsBuffer m_coords;
TexturePtr m_texture;
LightData m_lightData, m_threadLightData;
LightData m_lightData[2];
std::atomic_uint8_t m_currentLightData{ 0 };

std::thread m_thread;
std::condition_variable m_condition;
Expand Down
Loading