Blitz3d AnimTexture Mask Help

Blitz3D Forums/Blitz3D Programming/Blitz3d AnimTexture Mask Help

Caton(Posted 2014) [#1]
I need help how do I mask a animtexture when animating mask as 255,255,255


Caton(Posted 2014) [#2]
I want it to mask the texture to 255,255,255


K(Posted 2014) [#3]
You don't...

Either mask 0,0,0 or...

What you really don't want to have to have to do is manually mask
255,255,255 as alpha of 00 in hex with WritePixel() ie:
FFFFFFF goes to FFFFFF0

So just mask it 0,0,0.


Guy Fawkes(Posted 2014) [#4]
Yes you do. Don't listen to K.

http://www.blitzbasic.com/codearcs/codearcs.php?code=1013


K(Posted 2014) [#5]
That's what I meant by a manual run-through.
Good find, I was just doing mine own when I
occasionally needed it.


Caton(Posted 2015) [#6]
It does't work when I do Frame=Frame+1 when I run that code it does not mask
Please help!!!!!!!!!


_PJ_(Posted 2015) [#7]
You need to ensure that you are using LoadAnimTexture for multiple animation frame textures.
Also, each frame of the texture may need to be addressed separately and each frame has its own buffer.

Unfortunately, there's no quick way to obtain the number of frames in a texture (this is typically going to be KNOWN to the developer anyway and so can be hardcoded where required)

Graphics3D 800,600
SetBuffer BackBuffer()

Local cam=CreateCamera()
PositionEntity cam,0,0,-10

Global FrameWidth=256
Global FrameHeight=256
Global Frames=5


Global sprite=CreateSprite()
Global tex=LoadAnimTexture("image1.png",4,FrameWidth,FrameHeight,1,Frames)

Local cube=CreateCube()
PositionEntity cube,0,0,2
ScaleEntity cube,6,2,1

Local frame=1
Local alpha=0

prepare_texture(tex,Frames)

While Not KeyHit(1)
	
	If KeyHit(2) Then texture_mask_colour(tex,0,255,0,Frames,55) ; will clear green colours within 55 either way, fading.
	
	If KeyHit(4) Then texture_mask_colour(tex,0,0,0,Frames,50) ; will clear black and values 55 close to it.
	
	If KeyHit(3) 
		alpha=Not(alpha)
		EntityAlpha sprite,0.5*alpha
	End If
	
	frame=MilliSecs()/100 Mod Frames
	
	EntityTexture sprite,tex,frame 
	
	UpdateWorld
	RenderWorld
	
	Text 0,0," Press 1 to mask green from the texture"
	Text 0,10," Press 3 to mask black from the texture"
	Text 0,20," Press 2 to turn on/off entityalpha "
	
	Flip
Wend

	
;--------------------------------------------------------------------
;  This function will clear a texture of all it's alpha information |
;--------------------------------------------------------------------
;parameters  =  texture : the texture you wish to clear alpha information from
Function prepare_texture(texture,totalframes=1)
	
	Local X
	Local Y
	Local RGB1
	Local r
	Local g
	Local b
	Local a
	Local RGB2
	
	Local Buffer
	
	Local frame
	
	For frame=1 To totalframes
		Buffer=TextureBuffer(texture,frame)
		
		LockBuffer Buffer
		
		For Y=0 To FrameHeight-1
			For X=0 To FrameWidth
				RGB1=ReadPixelFast(X,Y,Buffer)
				r=(RGB1 And $FF0000)Shr 16;separate out the red
				g=(RGB1 And $FF00) Shr 8;green
				b=RGB1 And $FF;and blue parts of the color
				a=(RGB1 And $FF000000)Shr 24
				
				a=255; remove any alpha information currently in the texture.
				
				RGB2= (a Shl 24) Or (r Shl 16) Or (g Shl 8) Or b; combine the ARGB back into a number
				
				WritePixelFast(X,Y,RGB2,Buffer); write the info back to the texture
			Next
		Next
		
		UnlockBuffer Buffer
	Next
	
End Function


;---------------------------------------------------------------------
;  This function will mask the passed across RGB of the texture also |
;  passed across. Do NOT pass across a value for flag if you only    |
;  want to mask an exact colour.                                     |
;---------------------------------------------------------------------
;parameters = texture   : the texture you wish to clear alpha information from
;           = r1        : the red value to mask
;           = g1        : the green value to mask
;           = b1        : the blue value to mask
;           = tolerance : the tolerance value. If set to 0, then the function will only mask the
;                         EXACT colours passed across. Higher value will mask values close to the colour.

Function texture_mask_colour(texture,r1,g1,b1,TotalFrames=1,tolerance=0)
	
	Local X
	Local Y
	Local RGB1
	Local RGB2
	Local Buffer
	Local r
	Local g
	Local b
	Local a
	Local Frame
	
	For Frame=1 To TotalFrames
		Buffer=TextureBuffer(texture,Frame)
		LockBuffer Buffer
			For Y=0 To FrameHeight-1
			For X=0 To FrameWidth-1
				RGB1=ReadPixelFast(X,Y,Buffer) ; read the RGB value from the texture
				r=(RGB1 And $FF0000)Shr 16;separate out the red
				g=(RGB1 And $FF00) Shr 8;green
				b=RGB1 And $FF;and blue parts of the color
				a=(RGB1 And $FF000000)Shr 24 ; extract the alpha
				
				If r>=r1-tolerance And r=<r1+tolerance And g=>g1-tolerance And g=<g1+tolerance And b=>b1-tolerance And b=<b1+tolerance Then; check RGB lies with the tolerance
					;temp=((Abs(r-r1)+Abs(g-g1)+Abs(b-b1))/3.0)*4.0 ; alpha the values based on the tolerance value
					a=0;temp
					
				End If
				
				RGB2= (a Shl 24) Or (r Shl 16) Or (g Shl 8) Or b ; combine the ARGB back into one value
				
				WritePixelFast(X,Y,RGB2,Buffer); write back to the texture
			Next
		Next
		UnlockBuffer Buffer
	Next
	
End Function