[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.

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

Postby Sir Robin » Mon Jan 10, 2022 11:14 am

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 allExpand view
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
Sir Robin
 
Joined: 22 Dec 2021
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: Intel (Modern GZDoom)

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

Postby Caligari87 » Mon Jan 10, 2022 11:27 am

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
Caligari87
User Accounts Assistant
 
Joined: 26 Feb 2004
Discord: Caligari87#3089
Github ID: caligari87

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

Postby Sir Robin » Mon Jan 10, 2022 12:02 pm

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.
User avatar
Sir Robin
 
Joined: 22 Dec 2021
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: Intel (Modern GZDoom)

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

Postby Gez » Mon Jan 10, 2022 12:12 pm

I wonder if it isn't what you can do with CopyAlpha; but I've never tested it.
Gez
 
 
 
Joined: 06 Jul 2007

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

Postby Sir Robin » Mon Jan 10, 2022 12:34 pm

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
Sir Robin
 
Joined: 22 Dec 2021
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: Intel (Modern GZDoom)

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

Postby Caligari87 » Mon Jan 10, 2022 12:40 pm

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 allExpand view
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 allExpand view
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
Caligari87
User Accounts Assistant
 
Joined: 26 Feb 2004
Discord: Caligari87#3089
Github ID: caligari87

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

Postby Sir Robin » Mon Jan 10, 2022 4:16 pm

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 allExpand view
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 allExpand view
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
 
Joined: 22 Dec 2021
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: Intel (Modern GZDoom)

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

Postby Sir Robin » Mon Jan 10, 2022 4:42 pm

Getting this:
Code: Select allExpand view
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 allExpand view
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 allExpand view
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
Sir Robin
 
Joined: 22 Dec 2021
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: Intel (Modern GZDoom)

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

Postby Caligari87 » Tue Jan 11, 2022 8:35 am

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
Caligari87
User Accounts Assistant
 
Joined: 26 Feb 2004
Discord: Caligari87#3089
Github ID: caligari87

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

Postby Sir Robin » Tue Jan 11, 2022 2:20 pm

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.
User avatar
Sir Robin
 
Joined: 22 Dec 2021
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: Intel (Modern GZDoom)

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

Postby Gez » Tue Jan 11, 2022 2:42 pm

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.
Gez
 
 
 
Joined: 06 Jul 2007

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

Postby Sir Robin » Tue Jan 11, 2022 10:48 pm

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.
User avatar
Sir Robin
 
Joined: 22 Dec 2021
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: Intel (Modern GZDoom)


Return to Assets (and other stuff)

Who is online

Users browsing this forum: No registered users and 0 guests