YOUR ACCOUNT

Login or Register to post new topics or replies
Ramlyn
Ramlyn

Posts: 2931
Filters: 693
I'm doing my first steps in scripting.
Tonight, reading in our forum, I found some posts about blend modes, but I couldn't find anything specific about the basic structure of a blend.

Let's suppose I work on a Map Script.
I have three inputs and I want to blend them.
How would be the script?

( I'm sorry if it is an easy and silly question, but I think that the best way to learn is to see how the basic scripts are done )
  Details E-Mail
ThreeDee
Lost in Space

Posts: 1672
Filters: 112
You would take those three source inputs and use percentages to add the values together. Say you had R1, G1, B1, A1; R2, G2, B2, A2; R3, G3, B3, A3. To blend the Red channels in the proportion 30-20-50, would be:

R = R1*.30 + R2*.20 + R3*.50
  Details E-Mail
Sphinx.
Filter Optimizer

Posts: 1750
Filters: 39
  Details E-Mail
Ramlyn
Ramlyn

Posts: 2931
Filters: 693
Thanks ThreeDee. smile;)

So, ... if I let just "r = r1 + r2 ; r3", it should blend with 100% opacity.
And, because no particular blending mode was added, the blend should be normal.
Or not?

I did this : ( 5 inputs )

function prepare()
end;

function get_sample(x, y)
local r1, g1, b1, a1 = get_sample_map(x, y, INPUT01)
local r2, g2, b2, a2 = get_sample_map(x, y, INPUT02)
local r3, g3, b3, a3 = get_sample_map(x, y, INPUT03)
local r4, g4, b4, a4 = get_sample_map(x, y, INPUT04)
local r5, g5, b5, a5 = get_sample_map(x, y, INPUT05)
r = r1 + r2 + r3 + r4 + r5
g = g1 + g2 + g3 + g4 + g5
b = b1 + b2 + b3 + b4 + b5
a = a1 + a2 + a3 + a4 + a5
return r, g, b, a
end;

The result should be the same as a multiblend with 100 opacity. Instead... it is not. Why?
  Details E-Mail
Ramlyn
Ramlyn

Posts: 2931
Filters: 693
Thanks Sphinx. Yes, you're right.
I saw this page. But this is the case of only 2 sources with all blending modes.
I was instead trying to understand how a simple blend/multiblend work.
What do you think of my script?
The result is surely a blend of the 5 inputs but, normally the source on the top should cover the others, instead in this script it doesn't. There is surely some kind of problem, but I don't get it.
  Details E-Mail
Sphinx.
Filter Optimizer

Posts: 1750
Filters: 39
Yeah, there is a difference between the average or sum of multiple sources and blending (with an opacity factor). A real blending formula is not that simple really - It has to take into account opacity, alpha from foreground layer and alpha from background layer.

Even with multiple layers, you can still use the FF blending routines - you just have to "nest" things or think in intermediate results: start from the back and blend layer two onto layer 3. Consider that result your new "background" and blend layer one onto that.

With more layers it is really just the same.

Now the annoying thing about the FF routines / Lua is that the blends can't be called nested due to the multiple results... you can't pass the r,g,b,a result of the first blend onto the next directly, as the blend routine call is considered one parameter and not four by lua.

Btw if you don't want to use the build in blending, here is a lua version:
Code

function get_sample(x, y)
-- fetch inputs
local O = get_sample_grayscale(x, y, OPACITY)
local Ra, Ga, Ba, Aa = get_sample_map(x, y, FOREGROUND)
local Rb, Gb, Bb, Ab = get_sample_map(x, y, BACKGROUND)

-- blendmode callback
Ro = Ra + (get_sample_grayscale(Ra, Rb, BLEND_MODE) - Ra) * Ab
Go = Ga + (get_sample_grayscale(Ga, Gb, BLEND_MODE) - Ga) * Ab
Bo = Ba + (get_sample_grayscale(Ba, Bb, BLEND_MODE) - Ba) * Ab

-- combine foreground alpha and master opacity
Aa = O * Aa

-- blend individual channels   
local Ao = Aa + Ab * (1 - Aa)
if Ao > 0 then
local rAo = 1 / Ao

Rb = Rb * Ab
Gb = Gb * Ab
Bb = Bb * Ab

Ro = (Rb + (Ro - Rb) * Aa) * rAo
Go = (Gb + (Go - Gb) * Aa) * rAo
Bo = (Bb + (Bo - Bb) * Aa) * rAo

return Ro, Go, Bo, Ao
else
--undefined, returning something that makes slightly sense in FF context:
return Rb,Gb,Bb,Ab
end
end;
  Details E-Mail
Ramlyn
Ramlyn

Posts: 2931
Filters: 693
Thanks for the explanation and the lua version. smile;)
So.. we have no function prepare, this means no numerical inputs ( sliders ).
OPACITY is a Greyscale Map. FOREGROUND and BACKGROUND are Color Maps.
BLEND_MODE looks like another Greyscale Map.
But the script gives me error ( attempt to call a nil value ). Is it because of BLEND_MODE set as Greyscale Map?
  Details E-Mail
Sphinx.
Filter Optimizer

Posts: 1750
Filters: 39
Oh right.. I forgot about the custom blendmode stuff. Simply comment out that block, i.e. the three lines after -- blendmode callback
  Details E-Mail

Join Our Community!

Filter Forge has a thriving, vibrant, knowledgeable user community. Feel free to join us and have fun!

33,736 Registered Users
+14 new in 30 days!

153,582 Posts
+5 new in 7 days!

15,355 Topics
+5 new in 30 days!

Create an Account

Online Users Last minute:

16 unregistered users.