Welcome, Guest.
Username: Password: Remember me

TOPIC: How to add png transparency control to shader?

How to add png transparency control to shader? 4 months 3 weeks ago #1

So I have created a shader file that adds a png HUD overlay to the screen. I would like to add transparency control and transparency control for individual colour channels but I have no idea how to do it.
This is my overlay shader so far;
NAMESPACE_ENTER(CFX)

#include CFX_SETTINGS_DEF

texture HudTex	< string source = "ReShade/CustomFX/Textures/CFX_hud.png"; > {Width = 1920; Height = 1080; Format = RGBA8;};
sampler	HudColor 	{ Texture = HudTex; };

float4 PS_Hud(float4 vpos : SV_Position, float2 texcoord : TEXCOORD) : SV_Target
{
	float4 hud = tex2D(HudColor, texcoord);
	return lerp(tex2D(RFX_backbufferColor, texcoord),hud,hud.a);
}

technique Hud_Tech <bool enabled = RFX_Start_Enabled; int toggle = RFX_ToggleKey; >
{
	pass HudPass
	{
		VertexShader = RFX_VS_PostProcess;
		PixelShader = PS_Hud;
	}
}

#include CFX_SETTINGS_UNDEF

NAMESPACE_LEAVE()
Last Edit: 4 months 3 weeks ago by Lazmarr.
The administrator has disabled public write access.

How to add png transparency control to shader? 4 months 3 weeks ago #2

  • crosire
  • crosire's Avatar
  • Offline
  • Posts: 2437
  • Thank you received: 1384
There are many ways to do transparency: additive blending, over blending, a mix of multiple blending operators, ...
Your code looks alright for the most part (except that you may want to return a float3 and discard the alpha channel of both the backbuffer and the overlay image after lerping). Check if the alpha channel of the image file was read correctly (ReShade may not understand all PNG formats):
return float4(hud.aaa, 1.0);
Cheers, crosire =)
The administrator has disabled public write access.

How to add png transparency control to shader? 4 months 3 weeks ago #3

Thanks for the quick reply crossire.
I probably should've mentioned that I barely know what any of this stuff means. I found the majority of this code online and only edited the bits I need.
I'm not really sure what backbuffer and lerping is.
I have added an alpha channel to my hud png as it did not contain one.
I shall try to add your code snippet in but I am not entirely sure where it goes :S
Edit: Ok the overlay worked fine, the PNG transparency was read correctly, until I repalced the;
return lerp(tex2D(RFX_backbufferColor, texcoord),hud,hud.a);
with
return float4(hud.aaa, 1.0);
The transparency became black and all other colour became white.
Is this code you provided to adjust transparency?
I appreciate the help you are giving me :)
Last Edit: 4 months 3 weeks ago by Lazmarr.
The administrator has disabled public write access.

How to add png transparency control to shader? 4 months 3 weeks ago #4

  • crosire
  • crosire's Avatar
  • Offline
  • Posts: 2437
  • Thank you received: 1384
The code was to check whether the alpha channel exists. If the transparency already works fine, why do you need help?
Cheers, crosire =)
The administrator has disabled public write access.

How to add png transparency control to shader? 4 months 3 weeks ago #5

Sorry, I shall attach an image of what is happening.
With the code I posted this is what it looks like;


With the code to check alpha this is what happens;


The white (HUD elements) on this image is what needs to adjust in transparency :/
I don't know how to add code to the one I posted that will be able to only adjust the transparency of those HUD elements.
Last Edit: 4 months 3 weeks ago by Lazmarr.
The administrator has disabled public write access.

How to add png transparency control to shader? 4 months 3 weeks ago #6

  • crosire
  • crosire's Avatar
  • Offline
  • Posts: 2437
  • Thank you received: 1384
You misunderstood me. I first thought you couldn't get transparency to work at all and therefore posted some code to verify the image file.

If you want to modify individual channels, here's a standalone shader for ReShade 3.0 to give you that:
texture BackBufferTex : COLOR;
texture HudColorTex < source = "hud.png"; > { Width = 1920; Height = 1080; Format = RGBA8; };
sampler BackBuffer { Texture = BackBufferTex; };
sampler HudColor { Texture = HudColorTex; };

uniform float3 TransparencyControl <
	ui_type = "drag";
	ui_min = 0.0; ui_max = 1.0;
> = float3(1.0, 1.0, 1.0);

void PostProcessVS(uint id : SV_VertexID, out float4 position : SV_Position, out float2 texcoord : TEXCOORD)
{
	texcoord.x = (id == 2) ? 2.0 : 0.0;
	texcoord.y = (id == 1) ? 2.0 : 0.0;
	position = float4(texcoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}
float3 HudPS(float4 vpos : SV_Position, float2 texcoord : TEXCOORD) : SV_Target
{
	float4 hud = tex2D(HudColor, texcoord);
	float3 color = tex2D(BackBuffer, texcoord).rgb;

	return lerp(color, hud.rgb, hud.aaa * TransparencyControl);
}

technique HUD
{
	pass
	{
		VertexShader = PostProcessVS;
		PixelShader = HudPS;
	}
}

An alternative using hardware blending (which could be faster, too requires ReShade 3.0):
texture HudColorTex < source = "hud.png"; > { Width = 1920; Height = 1080; Format = RGBA8; };
sampler HudColor { Texture = HudColorTex; };

uniform float TransparencyControl <
	ui_type = "drag";
	ui_min = 0.0; ui_max = 1.0;
> = 1.0;

void PostProcessVS(uint id : SV_VertexID, out float4 position : SV_Position, out float2 texcoord : TEXCOORD)
{
	texcoord.x = (id == 2) ? 2.0 : 0.0;
	texcoord.y = (id == 1) ? 2.0 : 0.0;
	position = float4(texcoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}
float4 HudPS(float4 vpos : SV_Position, float2 texcoord : TEXCOORD) : SV_Target
{
	float4 hud = tex2D(HudColor, texcoord);

	return float4(hud.rgb, hud.a * TransparencyControl);
}

technique HUD
{
	pass
	{
		VertexShader = PostProcessVS;
		PixelShader = HudPS;
		ClearRenderTargets = false;
		BlendEnable = true;
		BlendOp = ADD;
		SrcBlend = SRCALPHA;
		DestBlend = INVSRCALPHA;
	}
}
Cheers, crosire =)
Last Edit: 4 months 3 weeks ago by crosire.
The administrator has disabled public write access.

How to add png transparency control to shader? 4 months 3 weeks ago #7

Thanks for the reply, I have tried to implement the shader code you have given me into my hud.h shader file. However, they don't seem to be working. I am not sure what I am doing wrong :(
The first one reshade loads the shader but the overlay does not appear. I have tried changing the source for the texture and changing values for the transparency control but neither have any effect.
The second one gives a red message "unrecognised pass state clear render targets"
Instead of changing the individual channels. Is it possible to change the transparency of the overlay inestead? As I feel that may be simpler to implement instead of messing with channels and the alpha :S
The administrator has disabled public write access.