Page 1 of 1

[1.1.76] What is the blending mode for LuaRendering color?

Posted: Sun Jan 22, 2023 11:21 am
by Honktown
Expectation: Rendering's color to behave like tint, and the default blendmode to be: normal.
Here is code used to draw squares (more complicated than it needs to be, I know), and something must be selected:

Code: Select all

/c
local player = game.player
local surface = player.surface
local selected = player.selected
local position = selected.position
local X0 = position.x
local Y0 = position.y
local leftmost = -10
local rightmost = 10
local top = -10
local bottom = 10

--[[these aren't 100% accurate]]
local square_size = .5 --[[size of square relative to tile]]
local frequency = 1 --[[ at most 1]]
local occupancy = 1 --[[how much of the square is taken up]]
local period = square_size / frequency

local width_half = occupancy * square_size / 2
local height_half = occupancy * square_size / 2

local DX = (rightmost - leftmost)
local DY = (bottom - top)

for dy = top, bottom, period do
	for dx = leftmost, rightmost, period do
		local a = (dx - leftmost) / DX
		local w = (dy - top) / DY

		local force_a = true
		if force_a then
			a = 1
		end
		rendering.draw_rectangle{
			color = {w, w, w, a},
			filled = true,
			left_top = {X0 - width_half + dx, Y0 - height_half + dy},
			right_bottom = {X0 + width_half + dx, Y0 + height_half + dy},
			surface = surface,
			time_to_live = 180,
		}
	end
end
Results for alpha 1, which is full opacity (which the wiki still lists as transparency):
alpha_forced_1.png
alpha_forced_1.png (342.45 KiB) Viewed 567 times
The result is sensible: alpha is fully opaque and draws 100% over the initial color.
Result = Active_RGB * Active_Alpha + Background_RGB * ( 1 - Active_Alpha )
Active Alpha 1:
Result = Active_RGB

Now, alpha force 0:
alpha_forced_0.png
alpha_forced_0.png (403.55 KiB) Viewed 567 times
The result makes no sense. Alpha 0 is fully transparent for tinting, and
Result = Active_RGB * Active_Alpha + Background_RGB * ( 1 - Active_Alpha )
Clearly results in:
Result = Background_RGB

The non-forced code, where whiteness increases from left to right, and alpha increases from top to bottom:
alpha_x_y.png
alpha_x_y.png (455.07 KiB) Viewed 567 times
This is clear that *just* changing the color results in an increase in opacity, the more white the color is. This is not the same blendmode as normal.

Re: [1.1.76] What is the blending mode for LuaRendering color?

Posted: Sun Jan 22, 2023 12:27 pm
by posila
Oh, it seems you spent lot of time on this, I am sorry 🙈
Honktown wrote: ↑
Sun Jan 22, 2023 11:21 am
Expectation: Rendering's color to behave like tint, and the default blendmode to be: normal.
Rendering's color behaves the same as tint does.
Honktown wrote: ↑
Sun Jan 22, 2023 11:21 am
This is clear that *just* changing the color results in an increase in opacity, the more white the color is. This is not the same blendmode as normal.
It is the Factorio's "normal" blend mode.

The wiki is incorrect in what normal blend mode is. It is:

Code: Select all

Result = Active_RGB + Background_RGB * ( 1 - Active_Alpha )
In most of Factorio, colors are assumed to be pre-multiplied with alpha. Sprites are being pre-multiplied on loading (unless premul_alpha is set to false). To satisfy this assumption, you need to set your color to { w*a, w*a, w*a, a }

See also: https://www.factorio.com/blog/post/fff-172