Paint the Alphachannel

Blitz3D Forums/Blitz3D Programming/Paint the Alphachannel

AJirenius(Posted 2006) [#1]
Is there a way to only paint the alphachannel on an image?
This is what Im trying to achieve:



I want to have a 2 meshes on top of each other, one containing the background texture and one containing the white texture (snow).
I want to be able to edit the alphachannel on the white texture so it becomes transparent.
Is it possible to edit only the alphachannel on an image like this in B3D? Otherwise I need to find other solutions...


Matty(Posted 2006) [#2]
Only by doing so directly with writepixel/writepixelfast but in blitz3d these commands are not very quick, but at least it is possible.


AJirenius(Posted 2006) [#3]
So you are saying I cannot copy a small black/white pic to an image's alphachannel but only pixel it by hand?
Sounds strange... can you widen your answer a little bit?


Ross C(Posted 2006) [#4]
Yeah, when copying an image, it will copy with no alpha, because the copying commands are all 2d. So, you would need to quickly read from the image and write to the other image. Writing a few pixels isn't too slow. Make sure you lock the buffer and do it all in one shot though.

For more speed, tranfer the data you want to read, into an array. Accessing image buffers is slow for reading, but more so for writing. You should be ok though if it's just a small amount as i say.

When reading writing a pixel using WritePixelFast, you will need to translate the ARGB number given, so you can write the alpha.

Try this, it should help:

	RGB1=ReadPixelFast(loop,loop1,TextureBuffer(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
			
	a=0; PUT THE ALPHA VALUE YOU WANT TO WRITE HERE!

	newrgb= (a Shl 24) Or (r Shl 16) Or (g Shl 8) Or b; combine the ARGB back into a number

	WritePixelFast(loop,loop1,newrgb,TextureBuffer(texture)); write the info back to the texture


This will read from the texture buffer, take the ARGB value and split it up to Alpha, Red, Green and Blue. It will read the Red, Green and Blue. You need to write those values back exactly how they were read. Just change the alpha value to what you want. I hope that helps some


AJirenius(Posted 2006) [#5]
Thanks man!
However this will not be fast enough for the effect Im trying to make (I got 20 santas running around on a texture with 1024*1024 size and they will create footsteps on the texture on every step).
I guess I have to create something less fancy :(

Thanks anyway.


So, now you all know Im trying to make footsteps. Is there any other way I could create the same effect? Is there a way to copy a footstepshaped piece of the background and apply to the white texture?
I guess Sprites is not to be considered as there will be a lot of footsteps in the end.


Ross C(Posted 2006) [#6]
What you COULD do, is try copyrecting the footprint texture. You could have a texture overlaid on top, have it painted white, and set to add mode. Now, simple copy black footprints to the texture. They should show the texture below. If that's no good what about....

Have the white texture at the bottom and a blank texture on top. Everytime you want a footprint texture, draw the rocky ground texture to the blank texture, in the shape of a footprint.


jfk EO-11110(Posted 2006) [#7]
I'd say use a simple decals (sprites or quads) for this. Use eg. 200 decals, this should be more than enough. Then simply fade out about the 50 oldest of them.

you can do this easily if you use a looped index counter:

position footstep(index) somewhere
set alpha of index-51 to index-1 to 1.0 to 0.0, all other 1.0
(need to check if index-i<zero and in that case add 200...)
that's about it


AJirenius(Posted 2006) [#8]
Thanks for all your advices. Unfortunately decals wont do it as it is actually not just about footsteps.
Im creating a snowball-war game where you run around, rolling balls on the ground, scraping up snow to make balls, gets hit and slide over ground...
All this will impact the snow underneath and as snow are removed/diminished it will become harder to make snowballs.

So at this stage of design we cannot hae any quads that will fade out :(

Now, I think I will try to paint the oerlay texture black (0,0,0) and set it to mask.
But still I face the same (?) problem. Can it be done copying just the footprint (not a rect) on the texture. I mean, first I want it rotated and then when two footsteps are joining each other I dont want the new one to oerlap the other with somme white square thingie.


SoggyP(Posted 2006) [#9]
Hello.

I don't know the quantity of snow you're displaying but couldn't you create mesh-hugging sprites of snow that get picked up by the player/get mushed into ground underneath?

You could perhaps have a completely opaque 'piece' of snow that hasn't been walked over (mmmm, virgin snow) that holds 100% of snow available for that 'piece' to one that has been walked over a few times which is more transparent to one that has finally be scooped up or been completely trodden underfoot so has 0% snow available.

Having overlapping 'pieces' would also make for more realistic appearance of footsteps appearing - you could have pressure based snow removal, eg, the faster someone moves the quicker the snow disappears.

I suppose it depends on the granularity of snow that you'd want.

Goodbye.


Ross C(Posted 2006) [#10]
By copyrect, i mean, simply copying a large area to one texture, with one command. I still don't see Pixel writing to be THAT slow for your uses. You should test it :o)


mr.keks(Posted 2006) [#11]
i'd suggest, you use jfk's method and take another array/texture for saving the state of the snow.

if you want to have the underground visible, you could first render the underground, then only enable zbufferwriting, render foot step decals slightly above the terrain (so that they increment the zvalue) and then render the snow.. but, um, i also think, that's way too much passes and doesn't look any good ^^.

if you just want to darken the terrain there where your footsteps are, put a texture layer or decals with blend mode 2 over your terrain.

or, my last idea:
if you just want simple white snow, you can also put a blend mode 3 layer over your background layer. it is white, if there's snow, and it's black if there are foot steps. this will unfortunately dissable your lightning, but you can get it back, by multiplying a lightmap or a cubemap in diffuse illumination mode over it.


Ross C(Posted 2006) [#12]
Yep, your last way is the way i was *trying* to suggest earlier. I don't think i put it across correctly :o)

I still say pixel writing isn't that slow, if used moderately.


AJirenius(Posted 2006) [#13]
Thank you all for all your great suggestions. I think I might have found a way to do this. It's a compromise but still gets the job done.

You'll see the result later on (I hope)


jfk EO-11110(Posted 2006) [#14]
uh, I'd like to add a note here, I don't see a problem in using writepixelfast if you only have to draw a few footsteps every frame. You need to use the textureflag 256 tho. Then simply set the alphabyte of the wanted texels to zero.


AJirenius(Posted 2006) [#15]
Yes, I would probably go with writepixel as well if it just were footsteps but there's gonna be snowballs rolling on the ground, scraping on the ground to make snowballs, people sliding on the ground being hit by snowballs and people making "angels" being hit by snowballs.

I will try the writepixelfast but I'm depending on lot of "frametime" for other purposes on this one.


Subirenihil(Posted 2006) [#16]



AJirenius(Posted 2006) [#17]
The reason why I got stuck with B3D was the fact that the community in here are more helpful than in any other dev.community I've ever seen.

Wonderful proof of this.

Thank you very much all of you (especially Subirenihil who totally convinced my stubborn head of the way to go)


AJirenius(Posted 2006) [#18]
Ok, it seems like I will combine all the ideas from you and it works well. Youll see soon enough (I hope)

Just wondering about the writepixelfast contra copyrect

How many pixels must you dot before you find CopyRect a faster solution?

Since I will make these small footsteps in about 14 pixels Im thinking about pixing them instead of copyrect from a tilemap of rotated premade footsteps.


jfk EO-11110(Posted 2006) [#19]
You may hold them in arrays with xy index, so you won't have to readpixel them from the tilemap and/or prerotated images.

It's pretty slow to toggle between reading and writing pixels. A lot faster to write only using data taken from RAM and not VRAM, eg. Arrays or Banks.

Ehrm, never testes the speed diffrence of writepixelfast and copyrect. Should be easy to write a test program.


AJirenius(Posted 2006) [#20]
Yes, as it is just a few pixels I will just manually write in their positiondata in an array and not use any read at all. I was just lazy asking in here as there already were a thread (mine) about this and I would guess someone already tested this sometime and could give me an answer.

:)


AJirenius(Posted 2006) [#21]
Heres a quick view of the result with Copyrect. 16 Santas creates their own steps.



mr.keks(Posted 2006) [#22]
great! :)

but two things:
1) is there no way to have the foot steps somehow smoother i.e. antialiased? if you copyrect 'em from somewhere, i guess you have them rotated and saved in images or something similar before using them. so why can't you save an antialias rotated version then? this shouldn't be a problem when copyrecting, should it? (ok maybe, when two foots teps intersect it could look strange ^^.)

2. you definitely have to put a blendmode 2 light map over all this. shading will improve the look rapidly.