How do you create (createtexture) a masked texture?

Started by Randall_Flagg, January 11, 2025, 20:10:39

Previous topic - Next topic

Randall_Flagg

Hi All,

After my last post I can see that there's some great experts here so I thought I'd ask about the other issue that I am having.

If I load in a texture and set the mask flag (4) then black areas on the texture are masked/transparent.

If I create a texture with the mask flag set, black areas of the texture are still visible as black areas and are not transparent.

What purpose does the mask flag serve using the createtexture command and do you need to carry out additional steps to get the created texture masked?

Many thanks in advance for any replies.

Matty

You need to use the writepixel commands to set the areas you want to be transparent to be transparent on a texture you create at runtime.

That typically means you have to first read the pixel from the texture, using readpixel, convert the resulting integer into its rgb components, check if it's black and then write it back out with an alpha of 0 for those regions...like this:

lockbuffer texturebuffer(tex)
for y = 0 to textureheight(tex) - 1
  for x = 0 to texturewidth(tex) - 1
    rgb = readpixelfast(x,y,texturebuffer(tex))
    rgb_alpha = (rgb shr 24) and 255 ;this won't be zero
    rgb_red = (rgb shr 16) and 255
    rgb_green = (rgb shr 8) and 255
    rgb_blue = (rgb and 255)
  if rgb_red = 0 and rgb_green = 0 and rgb_blue = 0 then
    ;set alpha to zero
    writepixelfast x,y,0,texturebuffer(tex)
  endif
  next
next
unlockbuffer texturebuffer(tex)

Note the smiley face is meant to be the number 8 and a close bracket, unfortunately it got translated by the forum codes into a smiley face.


Randall_Flagg

Quote from: Matty on January 11, 2025, 20:18:39You need to use the writepixel commands to set the areas you want to be transparent to be transparent on a texture you create at runtime.

That typically means you have to first read the pixel from the texture, using readpixel, convert the resulting integer into its rgb components, check if it's black and then write it back out with an alpha of 0 for those regions...like this:

lockbuffer texturebuffer(tex)
for y = 0 to textureheight(tex) - 1
  for x = 0 to texturewidth(tex) - 1
    rgb = readpixelfast(x,y,texturebuffer(tex))
    rgb_alpha = (rgb shr 24) and 255 ;this won't be zero
    rgb_red = (rgb shr 16) and 255
    rgb_green = (rgb shr 8) and 255
    rgb_blue = (rgb and 255)
  if rgb_red = 0 and rgb_green = 0 and rgb_blue = 0 then
    ;set alpha to zero
    writepixelfast x,y,0,texturebuffer(tex)
  endif
  next
next
unlockbuffer texturebuffer(tex)

Note the smiley face is meant to be the number 8 and a close bracket, unfortunately it got translated by the forum codes into a smiley face.
Quote from: RemiD on January 11, 2025, 20:56:04https://www.syntaxbomb.com/graphics-40/bb-writeread-color-alpha-of-each-texel-of-a-texture-with-different-commands-by-r/
post #1

Great thanks for the replies. Yes this makes sense. So reading all of the textels and then setting any black ones (or any RGB value you desire) to 0 alpha.

Many thanks for the valuable info.