 1. ## Layer Rather Than Add?

Hi,

I have been making a few simple HWA effects recently and one problem i have is combining multiple elements. I can get them to all show ok with something like this for the return -

Code:
```return
tex2D(Part01,float2(In1.x,In1.y)) * Mix01 +
tex2D(Part02,float2(In2.x,In2.y)) * Mix02 +
tex2D(Part03,float2(In3.x,In3.y)) * Mix03 +
tex2D(Part04,float2(In4.x,In4.y)) * Mix04;```
But then if they overlap it adds the colors so the lightness grows rather than just layering as normal over the last. I have tried a few things like without the + and just using , etc but i can't seem to be able to do this.

Does anyone know how to do this and display each element/layer above the last correctly?  Reply With Quote

2. ## Re: Layer Rather Than Add?

Well, there might be some handy functions available to do this, I'm not an HLSL dictionary - But you basically have to blend the foreground over the background based on the foreground's alpha.

Something like this:
PHP Code:
``` //A: Lower, B: Upper float4 Mix(float4 A, float4 B) {  float4 Output;  Output.rgb = A.rgb+(B.rgb-A.rgb)*B.a;  Output.a = A.a+B.a;  return Output; } float4 Col1 = tex2D(Part01,In1); float4 Col2 = tex2D(Part02,In2); //Col2 over Col1 float4 NewCol = Mix(Col1,Col2);  ```
Honestly I'm not 100% sure that it will work, but it should.

Why are you using multiple 'In' variables by the way? Are you sure that you need them?
Also, float2(In2.x,In2.y) is the same as just writing In2!  Reply With Quote

3. ## Re: Layer Rather Than Add?

I tried the code but it did not seem to work unfortunately. I use in like that because i have a X and Y offset for each image used. The code you have seems to use colors but this was image and 4 of them.

Other than all the control inputs and the pixel width and height (and also changing the input names from part to image now also) the main code was -

Code:
```float4 ps_main(in float2 In1 : TEXCOORD0,
in float2 In2 : TEXCOORD0,
in float2 In3 : TEXCOORD0,
in float2 In4 : TEXCOORD0) : COLOR0
{
In1.x += fPixelWidth+OffsetX1;
In1.y += fPixelHeight+OffsetY1;

In2.x += fPixelWidth+OffsetX2;
In2.y += fPixelHeight+OffsetY2;

In3.x += fPixelWidth+OffsetX3;
In3.y += fPixelHeight+OffsetY3;

In4.x += fPixelWidth+OffsetX4;
In4.y += fPixelHeight+OffsetY4;

return
tex2D(Image01,float2(In1.x,In1.y)) * Mix01 +
tex2D(Image02,float2(In2.x,In2.y)) * Mix02 +
tex2D(Image03,float2(In3.x,In3.y)) * Mix03 +
tex2D(Image04,float2(In4.x,In4.y)) * Mix04;
}```
It works fine in terms of using them all but it adds the colors, the solution you post might work but that seems to be different for 2 inputs and also color based.

I have only just started to code HWA effects recently though so probably making lots of mistakes in my coding but it's working just not fully how i want yet.   Reply With Quote

4. ## Re: Layer Rather Than Add?

try dividing:
Code:
```	return
(tex2D(Image01,float2(In1.x,In1.y)) * Mix01 +
tex2D(Image02,float2(In2.x,In2.y)) * Mix02 +
tex2D(Image03,float2(In3.x,In3.y)) * Mix03 +
tex2D(Image04,float2(In4.x,In4.y)) * Mix04) / 4.0;```  Reply With Quote

5. ## Re: Layer Rather Than Add?

It sounds like you want Lerp(). Lerp takes three parameters, blending between two using the 3rd.  Reply With Quote

6. ## Re: Layer Rather Than Add?

xyzzy -

Thanks but that does not work, it just seems to set down the transparency for all the layers.

Dynasoft -

I don't think Lerp() is what is needed but i might be wrong, i don't to blend 2 layers and also i wanted to add a transparency setting for each layer (Mix01 etc). That and the offset for each layer is why this seems it would not be what i want but maybe there is a certain way it could do this?

-----

Just to give a better example of what i mean, say if the input images were all just circles with the same size and position but with a different color for each.

Layer 1 - Red Circle
Layer 2 - Blue Circle
Layer 3 - Green Circle
Layer 4 - Orange Circle

So if they all had no offset and the mix set to 1 (fully visible) then you would just see a Orange Circle. Then if you set all to have a Mix of 0 but the first which had a 1 you would just see a Red Circle. Then if you set the mix of Blue Circle (Layer 2) to 0.5 you would see a purple like circle and then if you set it to 1 instead you would only see the Blue Circle.

So basically each layer just goes over the top of the last with no mix or blending etc unless it was set for a layer. That is the reason i set it up like that but using + at the return part of the code doesn't seem to work for layering like how i want.  Reply With Quote

7. ## Re: Layer Rather Than Add?

Lerp does that. You lerp each image over the one below, so that a lerp param of 0 gives you the one below, a lerp param of 1 gives you the image above, and a lerp param of 0.5 gives you a 50:50 blend.  Reply With Quote

8. ## Re: Layer Rather Than Add? Originally Posted by Dynasoft
Lerp does that. You lerp each image over the one below, so that a lerp param of 0 gives you the one below, a lerp param of 1 gives you the image above, and a lerp param of 0.5 gives you a 50:50 blend.
Thanks, this works   Reply With Quote

9. ## Re: Layer Rather Than Add? Originally Posted by Atom
I am not sure how to use this correctly when i use -

Code:
```O1 = lerp(tex2D(Image01,float2(In1.x,In1.y)), tex2D(Image02,float2(In2.x,In2.y)), 1.0);

return O1;```
It shows nothing, other attempts would show a single input but as white, if possible could you give a basic example of how to merge with lerp() to do what i need.
That's correct, it should work.

Perhaps your texture samples contain no alpha? Does just a texture sample alone work?

EDIT: Ha you changed you post.  Reply With Quote

10. ## Re: Layer Rather Than Add?

Yeah sorry you must have read it before i edited   Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•