texture mask's color

Blitz3D Forums/Blitz3D Programming/texture mask's color

Cousin Gilgamesh(Posted 2005) [#1]
I'm using the texture flags to mask some textures, and I find it extremely annoying that 0,0,0, the masked color, is extremely prevolent in images I'm trying to mask. instead of using 1,1,1 for black in my pictures, is there any way I can change which color is masked? It would be helpful if anyone knew how I would go about doing this. Thanks a lot.


John Blackledge(Posted 2005) [#2]
MaskImage handle,red,green,blue


Matty(Posted 2005) [#3]
Maskimage only works for images not textures (ie 2d not 3d).


Cousin Gilgamesh(Posted 2005) [#4]
is there anything else I can do?


ozak(Posted 2005) [#5]
Doesn't the alpha channel work for 3D textures?


jfk EO-11110(Posted 2005) [#6]
you need to write a little function that will set your alpha channel. I'd do something lke this:

MaskTexture(mytexture,1,1,1)
...
function MaskTexture(tex,r,g,b)
 setbuffer texturebuffer(tex)
 lockbuffer()
 for y=0 to textureheight(tex)-1
  for x=0 to texturewidth(tex)-1
   rgb=readpixelfast(x,y) and $ffffff
   r2=(rgb and $ff0000) shr 16
   g2=(rgb and $ff00) shr 8
   b2=(rgb and $ff)
   alpha=255
   if (r=r2) and (g=g2) and (b=b2) then
    alpha=0
   endif
   argb= (alpha shl 24) or rgb
   writepixelfast x,y,argb
  next
 next
 unlockbuffer()
 setbuffer backbuffer()
end function

just from my head, I hope there ain't no typos.


Shifty Geezer(Posted 2005) [#7]
Note, as I found out, this doesn't work in 16 bit colour - the graphics get screwed. I'm afraid I don't have the 16but friendly code sorted yet as I'm working on other aspects to my program.


jfk EO-11110(Posted 2005) [#8]
Maybe you can make it work with a color that can be divided by 16, eg: 16,16,16 instead of 1,1,1. Not sure, but there's some way, some raster 32 bit is converted to 16 bit, and some bit values remain the same.


Shifty Geezer(Posted 2005) [#9]
Doesn't 16 bit representation vary on different hardwares? So one card might have 5,6,5 and another 5,5,5,1? OR are they all 5,6,5? If what you suggest would work, this suggests the minimum value 'step' for a colour would be 256/32 = 8 for R and B, and 256/64 = 4 for G. In which case colours that divide uniformly into 16 bit would be (a*8),(b*4),(c*8) such as 80,44,72 and 0,4,8?

Even is the hardware varies in colour representation, using colours in steps of 8 should work on all hardwares, just with the loss of resolution in whatever colour is 6 bit.


DH(Posted 2005) [#10]
Doesn't 16 bit representation vary on different hardwares


No, 0,0,0 is the same accross the board..

Your issue is that you might be using a jpg or lossy compression format for your texture, in which case 0,0,0 in the image might not be 0,0,0 after decompression the image.


jfk EO-11110(Posted 2005) [#11]
I think this wasn't the point, I guess he meant 1,1,1 didn't work in 16 Bit Mode. Well as Shifty said, steps of 8 should work, that's even better than steps of 16, as I suggested. I only remember this was discussed a few years ago and there was a solution.


Liberator(Posted 2005) [#12]
Really simple way to fix this, you need to use a .bmp file with a alpha layer. Just use the alpha texture flag and not the Masked. Just pop open photoshop, add an alpha channel, make the stuff you wanna see black, the stuff you don't want to see white, and your problem is solved. Works with 16, and 32 bit images.

And for all of you that are going to yell about a .bmp file not having the ability to support alpha information - try it and run it in blitz. It's flawless.


Cousin Gilgamesh(Posted 2005) [#13]
hmm... I have Jasc Paint shop, but it's able to do a lot of the same things. I'll take a look around and see if I can do this. thanks a lot!


DH(Posted 2005) [#14]
Using Jasc I found the following way to make an alpha image (I have PSP 8 min you):

Create your mask, then merge all layers. Create a seperate image with your 'actua' image and merge all layers.

In the mask image, right click on the layer and select the mask->from image option. Tell it to create the mask from the source values. It will create a mask layer. Hide it (click on the eye to hide it). and select the other layer (non-mask) for drawing operations.

Goto your actual image and select it and copy it. Go back to the 'mask' image and paste the contents on the normal layer. No unhide the mask layer and go to file->export->png optimizer. From there goto tranparancy tab and select the 'range' of values rather than the 1 or 0 option for the transparancy.

After you hit ok, it will ask where you want to save the image (As a .png file). and there you go, a png alpha image.

(it took me 5 hours to figure this out with PSP where as photoshop it's pretty straight forward). If you ever get the chance to pick up a copy of photoshop do it! You won't regret it!


Shifty Geezer(Posted 2005) [#15]
In PSP seven if you have your mask image you can fairly easily save it as an aplhoa channel. Select Masks > New > From Image and select the mask image. This creates the mask. Then do Masks > Save to Alpha Chhannel > New Channel , and you can change it's name to Alpha or leave it as Selection#1 - the name makes no odds. Finally save your image as a .png and in the Optimiser, select Transparency > Alpha Channel > Existing image transparency. It's pretty straightforward to set up single colour transparencies too with the PSP optimiser.

The .png alpha channel idea unfortunately doesn't work if you're creating your own textures, such as I am by copying areas of an image to sprite textures. Though it certainly might work for Gilgamesh from the sounds of it.


jfk EO-11110(Posted 2005) [#16]
Using Alpha Transparency will probably only open a new pandora box: Z-Order problems. So when somebody is using the Mask Flag 4, he usualy has a good reason to do this.

But since the masked textures use mipmapping and filtering, they normaly fade to black on their contours. Even worse, when the mesh is far away from the camera, this effect is getting stronger. We have seen this a lot with Grass Quads using the Mask flag. THey where kind of outlined with a black border.

So there was no way around Writepixelfast & Co. But the good news is: you can neutralize this black outline syndrom completely! All you have to do is: Ask if the RGB Color of a pixel is your Transparency Color (0,0,0 by default). If so, set the alpha byte to 0 and the RGB byte to something green (for grass), if it's not then set the alpha byte to 255 and don't alter the rgb. This way the grass texture will fade to green and it' slooking real good this way.

The determination of the best Color for the outline effect can even be automatized, just parse all pixels of a texture and calculate the average RGB color.


t3K|Mac(Posted 2005) [#17]
anyone having a working example for this issue? i try to load a mesh with loadmesh() and the texture is applied automatically. so i still have my black borders around. some guy wrote that using 32 bit alpha (png, tga) will do the trick. i tried this, but i failed. i always get the ugly black border. i need a mesh, bbcode and a texture. please help ;)


skidracer(Posted 2005) [#18]
Try my KeyImage routine to clean up your black edges.


t3K|Mac(Posted 2005) [#19]
looks good, but as the texture is automatically loaded within loadmesh(), i have no texturehandle. see what i mean?


Ross C(Posted 2005) [#20]
Look at the GetSurfaceBrush command help in the docs. Perfect example to getting the texture used. Remember and free the brush created in this process though, as you'll have two copies of it.


t3K|Mac(Posted 2005) [#21]
that sounds good. i'll try. i think this is a solution for my problem.


t3K|Mac(Posted 2005) [#22]
whoohow! it works! jfk, you are my hero ;)