[solved] How to add alpha/transparent map in textures lump?

Ask about editing graphics, sounds, models, music, etc here!
Shaders (GLSL) and SNDINFO questions also go here!

Moderators: GZDoom Developers, Raze Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

[solved] How to add alpha/transparent map in textures lump?

Post by Sir Robin »

I have 4 patches, I'm trying to build a sprite in the textures lump. These patches are an empty bottle, some liquid, a mask, and an alpha map. See the top row in the image for the four patches.
To create the sprite I have 4 steps - apply the bottle patch, apply the liquid patch, apply the mask (to correct the bottle over-drawn by the liquid), apply the alpha map to trim out that parts I don't want to see. See the bottom row in the image for the four steps.
I can get up to step 3, I don't know how to do step 4.

Code: Select all

Sprite "BOTRA0", 24, 33
{
	offset 12,33
	patch "EMPTY", 0, 0
	patch "LIQUIDR", 0, 26
	patch "MASK", 0, 0
	//need to apply alpha mask here
}
Edit: The answer is that there is no known way to do this in the textures lump, but a work-around exists by adding a custom shader. This only wroks in hardware mode. This was suggested by Caligari87, See the post below.
Last edited by Sir Robin on Tue Jan 11, 2022 9:49 pm, edited 1 time in total.
User avatar
Caligari87
Admin
Posts: 6195
Joined: Thu Feb 26, 2004 3:02 pm
Preferred Pronouns: He/Him

Re: How to add alpha/transparent map in textures lump?

Post by Caligari87 »

Unfortunately I'm pretty sure (would be happy to be proven wrong) that you cannot mask like this in TEXTURES.

What you need to do is apply the mask to your graphics individually in your image editor. Using paletted or truecolor PNG would be the easiest, but you can also use the transparent index for traditional Doom-type paletted graphics.

Of course, with the way you've constructed your graphics here, that may not work for your use case. The other option is to make a texture shader (yes they can apply to sprites too) which takes the alpha map as a uniform input and uses that to set the alpha of the rendered sprite.

8-)
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: How to add alpha/transparent map in textures lump?

Post by Sir Robin »

Caligari87 wrote:Unfortunately I'm pretty sure (would be happy to be proven wrong) that you cannot mask like this in TEXTURES.

What you need to do is apply the mask to your graphics individually in your image editor. Using paletted or truecolor PNG would be the easiest, but you can also use the transparent index for traditional Doom-type paletted graphics.

Of course, with the way you've constructed your graphics here, that may not work for your use case. The other option is to make a texture shader (yes they can apply to sprites too) which takes the alpha map as a uniform input and uses that to set the alpha of the rendered sprite.

8-)
Thanks for the reply. I'm actually simplifying it here for the example in the post. I've got 75 patches of liquid animations, if the liquid is still or swirling or bubbling or whatever, so if I multiply that by all the liquid levels and liquid colors I am going to have a ton of images. That's what I'm trying to avoid by build the sprites in the textures lump instead of using finished images.

Ok, so how do I go about the shader thing? I'm using true-color png files.
Gez
 
 
Posts: 17936
Joined: Fri Jul 06, 2007 3:22 pm

Re: How to add alpha/transparent map in textures lump?

Post by Gez »

I wonder if it isn't what you can do with CopyAlpha; but I've never tested it.
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: How to add alpha/transparent map in textures lump?

Post by Sir Robin »

Gez wrote:I wonder if it isn't what you can do with CopyAlpha; but I've never tested it.
That's what I thought too, but it's not literally copying the alpha channel. It's copying the pixel while considering the alpha channel. So I guess a better name would be CopyWithAlpha? meh...
User avatar
Caligari87
Admin
Posts: 6195
Joined: Thu Feb 26, 2004 3:02 pm
Preferred Pronouns: He/Him

Re: How to add alpha/transparent map in textures lump?

Post by Caligari87 »

75 patches, oof. That's a lot.

Okay, the bad news is that you'll need to have a separate shader definition for each one. The good news is that you can copypaste most of it.

THE FOLLOWING IS UNTESTED. The basics should be apparent but it may require some fiddling to actually get it working. I'm not in a place I can easily troubleshoot or test unfortunately.

GLDEFS (warm up that Ctrl+V key)

Code: Select all

HardwareShader Sprite POTSA0 { Shader "PotionShader.fp" Texture "mask" "PotionMask.png" }
HardwareShader Sprite POTSB0 { Shader "PotionShader.fp" Texture "mask" "PotionMask.png" }
HardwareShader Sprite POTSC0 { Shader "PotionShader.fp" Texture "mask" "PotionMask.png" }
// etc
PotionShader.fp (only need to do this once, thankfully)

Code: Select all

vec4 Process(vec4 color)
{
  vec2 texCoord = gl_TexCoord[0].st; // Get the coordinates of the current texel
  vec4 base = getTexel(texCoord); // Get the texel color from your base image
  vec4 m = texture(mask, texCoord) // Get the texel color from your mask uniform
  base.a = m.a // Set the alpha channel of the base to the alpha of the mask
  return base * color; // output multiplied by fog, etc
}
Hope this helps a little

8-)
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: How to add alpha/transparent map in textures lump?

Post by Sir Robin »

Caligari87 wrote:75 patches, oof. That's a lot.

Okay, the bad news is that you'll need to have a separate shader definition for each one. The good news is that you can copypaste most of it.

THE FOLLOWING IS UNTESTED. The basics should be apparent but it may require some fiddling to actually get it working. I'm not in a place I can easily troubleshoot or test unfortunately.

GLDEFS (warm up that Ctrl+V key)

Code: Select all

HardwareShader Sprite POTSA0 { Shader "PotionShader.fp" Texture "mask" "PotionMask.png" }
HardwareShader Sprite POTSB0 { Shader "PotionShader.fp" Texture "mask" "PotionMask.png" }
HardwareShader Sprite POTSC0 { Shader "PotionShader.fp" Texture "mask" "PotionMask.png" }
// etc
PotionShader.fp (only need to do this once, thankfully)

Code: Select all

vec4 Process(vec4 color)
{
  vec2 texCoord = gl_TexCoord[0].st; // Get the coordinates of the current texel
  vec4 base = getTexel(texCoord); // Get the texel color from your base image
  vec4 m = texture(mask, texCoord) // Get the texel color from your mask uniform
  base.a = m.a // Set the alpha channel of the base to the alpha of the mask
  return base * color; // output multiplied by fog, etc
}
Hope this helps a little

8-)
Awesome! Thanks! I will give this a try.
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: How to add alpha/transparent map in textures lump?

Post by Sir Robin »

Getting this:

Code: Select all

Execution could not continue.

Unable to load shader FlaskShader:
Init Shader 'FlaskShader':
Fragment shader:
ERROR: 0:14: 'base' : syntax error syntax error


Linking:
Attached fragment shader is not compiled.
my Gldef:

Code: Select all

HardwareShader Sprite UKRAA0 { Shader "Shaders/FlaskShader.fp" Texture "mask" "UFLASK77" }
I'm calling these flasks because I used potions and bottles for other things already
UFLASK77.png is the alpha mask file

FlaskShader.fp is unchanged:

Code: Select all

vec4 Process(vec4 color)
{
  vec2 texCoord = gl_TexCoord[0].st; // Get the coordinates of the current texel
  vec4 base = getTexel(texCoord); // Get the texel color from your base image
  vec4 m = texture(mask, texCoord) // Get the texel color from your mask uniform
  base.a = m.a // Set the alpha channel of the base to the alpha of the mask
  return base * color; // output multiplied by fog, etc
}
User avatar
Caligari87
Admin
Posts: 6195
Joined: Thu Feb 26, 2004 3:02 pm
Preferred Pronouns: He/Him

Re: How to add alpha/transparent map in textures lump?

Post by Caligari87 »

It looks like some semicolons are missing in the FlaskShader.fp file, that's my bad. Each line (before the // comments) needs to end with a semicolon. Try that.

8-)
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: How to add alpha/transparent map in textures lump?

Post by Sir Robin »

Caligari87 wrote:It looks like some semicolons are missing in the FlaskShader.fp file, that's my bad. Each line (before the // comments) needs to end with a semicolon. Try that.

8-)
That did it! It's working great! Thank you so much!

So this bottle alone has over 500 frames of animation, So this is much easier than having to write out all those files, and saves file space too.

So I noticed it said hardware shader, so went to check it in software mode. Funny thing, if I summon an actor with those sprites in the state frames, they are not transparent, but when I put them on the status bar they are. So I guess some routines obey the hardware shader even in software mode.
Gez
 
 
Posts: 17936
Joined: Fri Jul 06, 2007 3:22 pm

Re: How to add alpha/transparent map in textures lump?

Post by Gez »

Yes, the hardware/software dichotomy is really mostly for the 3D renderer (the game world). The 2D renderer (HUD, menus, and other interface bits) is always handled by the hardware.
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: How to add alpha/transparent map in textures lump?

Post by Sir Robin »

Gez wrote:Yes, the hardware/software dichotomy is really mostly for the 3D renderer (the game world). The 2D renderer (HUD, menus, and other interface bits) is always handled by the hardware.
Now that you mention that, it reminds me of something - a game I was modding about 10 years ago, I noticed that sprites in the 3d world would ignore the alpha channel, they used a color-key for transparency IIRC. But when you picked them up and put them in inventory, suddenly they are using the alpha channel. So I made an item I called the haunted helmet. Looks like a normal helmet in the world, but as soon as you drop it into inventory, a ghastly severed head would appear in it, because I drew that in the alpha channel. I never knew why that worked, but it makes sense now.

Return to “Assets (and other stuff)”