# 3D Blending textures question

## Recommended Posts

Hi Guys,

I am playing with projective texturing which I now have working well.

However, I am having troubles figuring out how to blend the original texture with the projective texture. I am trying to make it so the projective texture is painted on top of the original texture.

I have come close with this

textureColor = TextureStandard.Sample(SamplerWrap, input.textureCoord);

float4 texMixed = (projectionColor * projectionColor.a + textureColor * (1 - projectionColor.a));

color *= texMixed;

But this is giving me the following output;

The skull should be a solid black, and the greens are a bit washed out outside of the projected area.

Any advice on how to cleanly project the skull on to the body would be truly appreciated.

##### Share on other sites

What you have there is the typical "over" blending operator, commonly used for transparent rendering or for combining layers in photoshop. It's essentially a linear interpolation of the two colors based on an alpha value, in fact you can re-write it as this:

float3 texMixed = lerp(textureColor.xyz, projectionColor.xyz, projectionColor.a);


This blend operation will result in only your projected texture color being visible when alpha is 1.0, so I'm guessing the alpha isn't actually 1 in your cases. I would double-check the value of the alpha channel in your texture (RenderDoc is great for this), and perhaps try hard-coding the alpha value to 1 in your shader to see what happens.

##### Share on other sites

Hi MJP, thanks for the reply.

I just tried that snippet and it gave me the same result unfortunately.

float3 texMixed = lerp(textureColor.xyz, projectionColor.xyz, projectionColor.a);

float4 texOverlay = float4(texMixed, 1.0f);

color *= texOverlay;

Checked with the VS graphical debugger and the skull image has an alpha of 1.0 in the black areas and 0.0 in the transparent area. So the desired area is fully opaque.

Thanks again for your help so far

##### Share on other sites

So you still do not set proper blending operation

https://www.braynzarsoft.net/viewtutorial/q16390-13-blending

##### Share on other sites

Hi _WeirdCat_ This is my blend state here.

	ID3D11BlendState* d3dBlendState = NULL;
D3D11_BLEND_DESC omDesc;
ZeroMemory(&omDesc, sizeof(D3D11_BLEND_DESC));
omDesc.RenderTarget[0].BlendEnable = true;
omDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
omDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
omDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
omDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
omDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

##### Share on other sites

This is what I have now, but it isn't quite there. The colours are now correct, with the exception of the darker green areas.

textureColor = TextureStandard.Sample(SamplerWrap, input.textureCoord);
float4 texOverlay = (projectionColor * projectionColor.a + textureColor * (1.0f - projectionColor.a));

color *= textureColor;
color += (diffuseColor * diffuseIntensity);
color *= saturate(textureColor * texOverlay);

return color;

Edited by DividedByZero

##### Share on other sites

I should have tried with a coloured texture earlier, as it is much more apparent with this.

##### Share on other sites
3 hours ago, DividedByZero said:

color *= textureColor; color += (diffuseColor * diffuseIntensity); color *= saturate(textureColor * texOverlay);

This seems odd to me..

You multiply by texturecolor, add diffuse - and then multiply again by texturecolor * texoverlay(?!) ...

Shouldn't you just multiply color by texturecolor once??

##### Share on other sites
21 hours ago, vinterberg said:

This seems odd to me..

You multiply by texturecolor, add diffuse - and then multiply again by texturecolor * texoverlay(?!) ...

Shouldn't you just multiply color by texturecolor once??

Probably, but if I do the image becomes washed out and along with that the same symptom remains.

Seems that it probably is a blend mode issue, but I can't seem to figure out, for the life of me, what parameters I need to put in to get a 'normal' (using Photoshop terminology) blend. I.e. The new image gets pasted over the original image.

##### Share on other sites

Never did get this working properly 🙁

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 14
• 12
• 29
• 11
• ### Forum Statistics

• Total Topics
634868
• Total Posts
3019710
×