创建一个与透明度变化相匹配的雾效果。

我正在尝试在开发的地图工具中实现“战争迷雾(fog-of-war)”。

对于每个网格单元,如果一个单元被“迷雾”遮盖,颜色将设置为灰色乘以单元当前的颜色。否则,单元的颜色设置为单元通常应该的颜色。

以下是我的draw函数。

for x = 0, (self.gridSize - 1) * self.scale, self.scale do
        for y = 0, (self.gridSize - 1) * self.scale, self.scale do
            local mouseX, mouseY = camera:mousepos()

            local curCell = self:getCell(x, y, true)
            local state = Grid:getState(x, y, true)

            if fog and curCell.fogged then
                local color = multiplyColors(curCell.color, {100,100,100,10})
                color[4] = 150
                if curCell:getState() == 1 then
                    love.graphics.setColor(color)
                else
                    love.graphics.setColor({100, 100, 100, 100})
                end
                love.graphics.rectangle('fill', x, y, self.scale, self.scale)
            elseif math.floor(mouseX / self.scale) == math.floor(x / self.scale) and math.floor(mouseY / self.scale) == math.floor(y / self.scale) then
                love.graphics.setColor(255, 0, 0, 30)
                love.graphics.rectangle('fill', x, y, self.scale, self.scale)

            elseif state == 1 then
                love.graphics.setColor(curCell.color)
                love.graphics.rectangle('fill', x, y, self.scale, self.scale)

            elseif state == 0 and self.bGridLines then
                love.graphics.setColor(100, 100, 100, 10)
                love.graphics.rectangle('line', x, y, self.scale, self.scale)
            end
        end
    end

这是multiplyColors函数:

function multiplyColors(c1, c2)
    local newColor = {}

    newColor[1] = math.floor((c1[1] * c2[1]) / 255)
    newColor[2] = math.floor((c1[2] * c2[2]) / 255)
    newColor[3] = math.floor((c1[3] * c2[3]) / 255)

    return newColor

end

虽然这样创建了一个看起来不错的效果,但我需要能够设置迷雾的不透明度。

例如,如果我将color[4] = 150改为color[4] = 255,则期望的输出结果应该类似于这个,而不是当我改变那行时实际得到的结果。同样,将行更改为color[4] = 0会产生这个(虽然看起来有点凌乱),而不是类似于这个的结果

这是完整存储库的链接:https://github.com/camdenb/DnD-Map-Creator

谢谢!

点赞
用户2726734
用户2726734

一个简单的方法来在你的雾中得到不同的透明度水平是通过在循环体外绘制整个屏幕的雾:

love.graphics.setBackgoundColor(100, 100, 100) -- 雾,无处不在

之后,如果一个单元被雾笼罩,那么就使用更低的alpha值来绘制它,从而暴露下面的雾。

   if fogged and curCell.fogged then
      local c = curCell.color
      -- 您可以更改100以获得不同数量的雾
      -- 0表示全部雾,255表示无雾
      love.graphics.setColor(c[1], c[2], c[3], 100)
      -- 更多的绘制代码...
   end

您可以尝试不同的混合模式。 LÖVE wiki 中有一些示例,可在将雾背景与上面绘制的单元混合时获得不同的效果。

2015-02-05 19:38:13