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
|