YOUR ACCOUNT

Login or Register to post new topics or replies
James
James
Posts: 676
Filters: 46
I have made various modules to use so far but there is one thing i have had problems doing which is multiple alphas. Basically what i want to know is how do you layer multiple things together without effecting the colors with multiply or additions etc?

Currently i having done things like return r, g, b, mask with the mask being generated before and as the alpha which works fine if you just want a single layer with a alpha mask but i have yet to work out how to do this with more than 1 layer.

Things like -

return ((r1+r2)+(r3+r4))/4, ((g1+g2)+(g3+g4))/4, ((b1+b2)+(b3+b4))/4, ((a1+a2)+(a3+a4))/4

and a load of other things don't seem to work correctly so if possible does anyone have any tips on how to do layering multiple images/inputs with alpha masks correctly?
  Details E-Mail
Mike Blackney

Posts: 375
Filters: 57
Bad answer, but it depends on what you want to do.

Your code there is doing an average. Add four layers and divide by four. So doing an average of the alpha seems to make sense.

What you need is a rule set. Do you want something like Photoshop, where layer order matters? If so, then first you need to do the operations in order.

If what you want is something like Photoshop's multiply layer (where A is the bottom layer and B is the top) then what it does is take pixel color A, multiply it by pixel color B and then call this color Result. Then interpolate between A and Result, using the alpha of layer B.
  Details E-Mail
Vladimir Golovin
Administrator
Posts: 3446
Filters: 55
Quote
Mike Blackney wrote:
Do you want something like Photoshop, where layer order matters?


We're planning to expose Filter Forge's blend function, both HDR and LDR versions, with all available blending modes, but I don't know when it will happen. Most likely, we'll add it after the official release of v2.0.
  Details E-Mail
James
James
Posts: 676
Filters: 46
Well what i basically mean is just layer like photoshop etc would do but without a layer mode applied so basically to put one above the last yet keep a different alpha for each.

So a example would be if i had say a solid red circle over transparent. Doing this as a single thing would be simple and the return for example like -

return r1, g1, b1, circlemask

Then above that a different layer with a solid blue square over transparent that is scaled down to appear within the red circle. As a single thing would be like -

return r2, g2, b2, squaremask

So basically what i would want is to see a solid blue square over a solid red circle with a transparent background. I could easily make 2 separate scripts and blend them as nodes but i want to do it all within 1 basically. The problem is i can't figure it out to get these results as it's like how you say i am getting some sort of average like you suggest using code like i posted before and it effects the colors with any maths i try. I am guessing if it's possible i am just not sure on the maths needed to mix between them yet, what mike suggests seems like the thing needed but i am not sure about the correct code to do it.

Reading what Vladimir just said though it seems blending/layering above the last is not possible right now?

Maybe if it is possible someone could post a basic example of stacking 4 layers each with a input and alpha input or something as that would show me exactly what i need i am guessing. Thanks for the help everyone
  Details E-Mail
Sphinx.
Filter Optimizer

Posts: 1750
Filters: 39
hi James

According to http://en.wikipedia.org/wiki/Alpha_compositing:


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)

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

-- blend individual channels
local Ao = Aa + Ab * (1 - Aa)

local Ro = (Ra * Aa + (Rb * Ab) * (1 - Aa)) / Ao
local Go = (Ga * Aa + (Gb * Ab) * (1 - Aa)) / Ao
local Bo = (Ba * Aa + (Bb * Ab) * (1 - Aa)) / Ao

return Ro, Go, Bo, Ao
end;

not at all optimized or anything.. but there you have it, alpha preserving blending
  Details E-Mail
James
James
Posts: 676
Filters: 46
This code seems to work well and shows the method for Alpha compositing i needed. Thanks Sphinx smile:D

Edit -

By the way talking about optimizations i am just wondering how optimized is Lua in comparison to standard nodes? Would rendering times be cut when using lua code if it replaced a task needing many standard nodes or does it have not much effect on the time it takes to render?
  Details E-Mail
Vladimir Golovin
Administrator
Posts: 3446
Filters: 55
Quote
James wrote:
how optimized is Lua in comparison to standard nodes? Would rendering times be cut when using lua code if it replaced a task needing many standard nodes


Generally, standard components are much faster, but connections between them introduce a slight overhead. It is difficult to say which approach would work better -- it heavily depends on the task.
  Details E-Mail
Sphinx.
Filter Optimizer

Posts: 1750
Filters: 39
Quote
Generally, standard components are much faster, but connections between them introduce a slight overhead. It is difficult to say which approach would work better -- it heavily depends on the task.


Indeed! For my test case (mixed alphas and opacity), native blend rendered at 0.17 secs and the above function rendered at 0.72 secs.

I tried optimizing a few bits here and there, but the lua parser seem to do some tricks by its own.. I only managed to tweak out a few milisecs.

Here is the "optimized" version:
Quote
function get_sample(x, y)
-- fetch inputs
local O = get_sample_grayscale(x, y, OPACITY)
if O > 0 then
local Ra, Ga, Ba, Aa = get_sample_map(x, y, FOREGROUND)
-- combine foreground alpha and master opacity
Aa = O * Aa

if Aa > 0 then
if Aa < 1 then
local Rb, Gb, Bb, Ab = get_sample_map(x, y, BACKGROUND)
if Ab > 0 then
if Ab < 1 then

-- blend individual channels
local Ao = Aa + Ab * (1 - Aa)
local R_Ao = 1 / Ao
Ab = Ab * (1 - Aa) * R_Ao
Aa = Aa * R_Ao
return Ra * Aa + (Rb * Ab), Ga * Aa + (Gb * Ab), Ba * Aa + (Bb * Ab), Ao
else
return Rb + (Ra - Rb) * Aa, Gb + (Ga - Gb) * Aa, Bb + (Ba - Bb) * Aa, 1
end
else
return Ra, Ga, Ba, Aa
end
else
return Ra, Ga, Ba, Aa
end
else
return get_sample_map(x, y, BACKGROUND)
end
else
return get_sample_map(x, y, BACKGROUND)
end
end;


  Details E-Mail
Sphinx.
Filter Optimizer

Posts: 1750
Filters: 39
  Details E-Mail

Join Our Community!

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

33,711 Registered Users
+18 new in 30 days!

153,531 Posts
+39 new in 30 days!

15,347 Topics
+72 new in year!

Create an Account

Online Users Last minute:

20 unregistered users.