feat: Enhance background opacity handling and optimize pixel rendering for transparency

This commit is contained in:
2026-01-05 16:51:17 +00:00
parent 114208ab64
commit af5d4d0a02
2 changed files with 22 additions and 0 deletions

View File

@@ -196,11 +196,16 @@ function Context:applyBackgroundOpacity(color)
-- For full transparency (opacity = 0), set alpha to 0
-- For partial transparency, multiply the existing alpha
local result = {color[1], color[2], color[3], color[4] or 1.0}
local originalAlpha = result[4]
if self.opacity == 0 then
result[4] = 0
else
result[4] = result[4] * self.opacity
end
if lua_debug then
print(string.format("[GUI] applyBackgroundOpacity: opacity=%.2f, original alpha=%.2f, new alpha=%.2f",
self.opacity, originalAlpha, result[4]))
end
return result
end
@@ -208,6 +213,12 @@ function Context:pushRect(rect, params)
params = params or {}
-- Only apply opacity to the background color, not borders or other elements
local color = params.color and self:applyBackgroundOpacity(params.color) or params.color
-- Skip drawing background rect entirely if it would be fully transparent
if color and color[4] == 0 and (not params.borderColor or params.borderWidth == 0) then
return
end
local command = {
type = "rect",
x = rect.x,

View File

@@ -5,6 +5,7 @@
#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iterator>
#include <stdexcept>
@@ -479,12 +480,22 @@ GuiRenderer::GuiRenderer(VkDevice device, VkPhysicalDevice physicalDevice, VkFor
const auto& pixels = canvas_->Pixels();
size_t pixelCount = static_cast<size_t>(canvasWidth_) * canvasHeight_;
uint8_t* dest = reinterpret_cast<uint8_t*>(stagingMapped_);
// Clear destination to fully transparent so pixels with alpha=0 don't overwrite the scene
std::memset(dest, 0, pixelCount * 4);
for (size_t i = 0; i < pixelCount; ++i) {
size_t offset = i * 4;
uint8_t r = pixels[offset];
uint8_t g = pixels[offset + 1];
uint8_t b = pixels[offset + 2];
uint8_t a = pixels[offset + 3];
// Skip fully transparent pixels to avoid overwriting the 3D scene
if (a == 0) {
continue;
}
switch (swapchainFormat_) {
case VK_FORMAT_B8G8R8A8_UNORM:
case VK_FORMAT_B8G8R8A8_SRGB: