Control of texture mask
Blitz3D Forums/Blitz3D Programming/Control of texture mask
| ||
I want to be able to select which pixels are masked, and I want masked pixels to be able to be other colors than pure black. I want to use a color you can actually see to select the mask, and then color the masked pixels with an averge hue of the non-masked pixels. My trees look cel-shaded right now because of the black masked pixels creeping in. |
| ||
Halo, Could you not define ANY color to be the mask, as long as the RGB values are identical (like blue or green screening)? Maybe some guassian blurring after the masking would make things look better? That;s a photoshop trick, don't know if you can do it in Blitz though. |
| ||
The following (untested sorry) should fix up your masking issues, it won't do your very edge pixels but with masks you should always have a 1 pixel frame of transparent pixels for the benefit of antialiased edges (who needs multipass rendering...). Let me know if it's broken and post a test image (I'd go with hot pink for the keycolor as i'm sure you don't feature any such gay colors in your games).Function KeyImage(texture,keycolor) oldbuffer=GraphicsBuffer() buffer=TextureBuffer(texture) SetBuffer buffer LockBuffer buffer w=GraphicsWidth() h=GraphicsHeight() keycolor=keycolor And $fcfcfc ; first pass set key For y=0 To h-1 For x=0 To w-1 c=ReadPixelFast(x,y) If (c And $fcfcfc)<>keycolor c=c Or $ff000000 Else c=0 EndIf WritePixelFast x,y,c Next Next ; second pass fix edges For y=1 To h-2 For x=1 To w-2 c=ReadPixelFast(x,y) If c=0 t=0 For yy=-1 To 1 For xx=-1 To 1 c=ReadPixelFast(x+xx,y+yy) If c&$ffffff t=((t And $fefefe) Shr 1)+((c And $fefefe) Shr 1) EndIf Next Next WritePixelFast x+xx,y+yy,t EndIf Next Next UnlockBuffer buffer SetBuffer oldbuffer End Function |
| ||
. |
| ||
Haven't tested it yet, I'll let you know what I find. I know you can change black pixels to another color, and keep the mask, and I know you can change non-blck pixels to black and create a mask, but if you change non-black pixels to black, and then to another color, you lose the mask. |
| ||
Are you actually changing the pixel alpha value, or just changing the color to and from black? |
| ||
c=ReadPixelFast(x,y) If (c And $fcfcfc)<>keycolor c=c Or $ff000000 Else c=0 EndIf WritePixelFast x,y,c seems like he did alter the alpha byte. But I agree with halo, the almost-cellshaded effect can be real ugly, especially when the mesh is far away. So I would use the average color for the mask, then simply replace this color on the non-masked pixels with something very similar. THe only problem is with 16 Bit textures, probably this won't work correctly when there is only a single bit diffrence from mask color and other stuff. not sure of that. |
| ||
Sweet! Thanks, Simon. You can take the average of the non-masked pixels, and color the masked pixels that color. |
| ||
It should work fine with 16 bit (see the $fcfcfc masking I do), or to be exact 15 bit textures as 1 bit gets allocated as the mask bit. ReadPixel converts the mask bit in textures so it will always be present in the top bit of the rgba value returned hence the ability of the second pass to check for zero as all solid black pixels will have at least the top bit set in the returned rgba value. off the top of my head Function CalcRGBA(r,g,b,a) return b or (g shl 8) or (r shl 16) or (a shl 24) End Function and Function GetAlpha(rgba) return rgba shr 24 End Function |
| ||
r,g,b needs to be the other way around, unless textures are a special case..(dunno)Function CalcRGBA(r,g,b,a) return b or (g shl 8) or (r shl 16) or (a shl 24) End Function |
| ||
Ta |