How to mask a texture

Blitz3D Forums/Blitz3D Programming/How to mask a texture

vinnicius(Posted 2017) [#1]
Hey guys.
I have decided to restart "The Last Hope" development. I will change the name by the way.

I am facing a little problem with textures.

I want to turn the black background of my grass to transparent (invisible).


My code:

(...)
Global textura_graminea = LoadTexture("world\graminea.png",4)
(...)
world(i,i)\graminea = LoadMesh("world\graminea_"+i+j+".3DS")
EntityFX      world(i,i)\graminea,16 (16 - disable backface culling)
EntityAlpha   world(i,i)\graminea,alpha_graminea# 
(...)


The result of the background is black. But alpha works (that is a little effect).
It worked very well before, but something is wrong...

What i have got:


The texture (the black IS Black):



RemiD(Posted 2017) [#2]
welcome back,

take a look a these codes examples to mask a texture manually :
http://www.blitzbasic.com/codearcs/codearcs.php?code=3290
of course you need to load/create the texture with the flags 1+4
also be aware that some textureblend modes and some entityblend/brushblend modes will alter how the final mesh/texture look


vinnicius(Posted 2017) [#3]
I found the problem. I am a Java programmer for years and things like this happens for everyone.

Something stupid! :D

"world(i,i)\graminea" should be "world(i,j)\graminea" and I forgot the "entityTexture..." :D

Sorry guys. Thank you.


RemiD(Posted 2017) [#4]
Well, some others suggestions :
->make sure that the texels you want to have masked are color 0,0,0
->make sure that you load the texture with the flags 1+4
->make sure that you have not used an incompatible textureblend
->make sure that your mesh/surface has not an incompatible entityblend/surfaceblend (you can check this by opening the b3d with Fragmotion)


vinnicius(Posted 2017) [#5]
I found the problem. I am a Java programmer for years and things like this happens for everyone.

Something stupid! :D

"world(i,i)\graminea" should be "world(i,j)\graminea" and I forgot the "entityTexture..." :D

Sorry guys. Thank you.

Santa Maria, RS - BRAZIL
Twitter: @aerovinni


Bobysait(Posted 2017) [#6]
With png, you need a mask (it must be exported with an alpha channel) else the back will stay black.

Else, you can edit the texture once it's loaded with the mask flag as you did (the "4" in the loadtexture)
traverse the pixels and set alpha to 0 when the pixel color is black.

Function ApplyMask(tex%, MaskR%=0,MaskG%=0,MaskB%=0, Frame%=0)
	Local maskRGB% = (MaskR Shl(16)) Or (MaskG Shl(8)) Or B
	Local w = TextureWidth(tex)-1
	Local h = TextureHeight(tex)-1
	Local PrevBuffer = GraphicsBuffer()
	SetBuffer TextureBuffer(tex,Frame)
	LockBuffer()
	For j = 0 To h
	For i = 0 To w
		If ((ReadPixelFast(i,j) And $FFFFFF) = MaskRGB)
			WritePixelFas(i,j, $000000)
		EndIf
	Next
	Next
	UnlockBuffer()
	SetBuffer PrevBuffer
End Function



vinnicius(Posted 2017) [#7]
Thank you Bobysait.

I have to learn how to export an animated mesh (trees) without the black. I can do it with static mesh, but it does not work on animated mesh.