Unpremultiply PNG alpha

Discuss suggestions to improve the Axel library.

Unpremultiply PNG alpha

Postby Bennett » Fri Feb 22, 2013 6:42 am

Hi there.

By default, photoshop exports transparent PNGs with premultiplied alpha, which gives soft-edged sprites a black halo.

It would be good if the shader that Axel uses could 'unpremultiply' the alpha.

I saw this code snippet online, although it doesn't work as given in Axel:

Code: Select all
"tex ft0, v1, fs0 <2d,repeat,linear> \n" + //sample texture
         "div ft0.rgb, ft0.rgb, ft0.a \n" +  // un-premultiply png
         "mov oc, ft0"


Oh also: I'm using the camera projection stuff to smoothly zoom in and out (one of the main reasons to use axel instead of flixel). It would be nice if we could turn mipmapping on for sprites and tilemaps, so that this would look a little smoother. And I guess it would also be nice if you could make it so that UI elements would not scale along with the game (just as you can stop them from scrolling with the camera)

Also, the current code to prevent drawing a sprite when it's offscreen doesn't work when you zoom out smoothly.
Last edited by Bennett on Fri Feb 22, 2013 8:08 am, edited 2 times in total.
Bennett
Private
 
Posts: 8
Joined: Fri Feb 22, 2013 6:40 am

Re: Unpremultiply PNG alpha

Postby Bennett » Fri Feb 22, 2013 7:53 am

eventually got this working with:

Code: Select all
"tex ft0, v0, fs0 <2d,linear,mipnone>", //was <2d,nearest,mipnone>
"div ft0.xyz, ft0.xyz, ft0.w", //unpremultply png alpha
"mov oc, ft0"


It breaks the camera fadeout code though, since that seems to depend on premultiplying alpha.

Also: another suggestion: allow AxSprites to have pixel-snapping enabled or disabled. If you're using Axel with a physics engine, as I am, it's annoying to have bodies oscillate between pixels.
Bennett
Private
 
Posts: 8
Joined: Fri Feb 22, 2013 6:40 am

Re: Unpremultiply PNG alpha

Postby Arkeus » Fri Feb 22, 2013 11:22 am

Another method is to set the blend mode of sprites where you have premultiplied alpha to AxBlendMode.TRANSPARENT_SPRITE. That was the temporary fix I put in for the issue (though you can no longer correct use alpha with the sprite). I'll have to try out the shader you found to see how that affects these things, since it might be useful.

As for pixel snapping, I still haven't found a solution that doesn't have terrible edge cases without rounding before drawing.
Image
User avatar
Arkeus
Site Admin
 
Posts: 363
Joined: Mon Mar 26, 2012 12:43 am

Re: Unpremultiply PNG alpha

Postby Bennett » Fri Feb 22, 2013 12:04 pm

Thanks, I hadn't noticed the transparent sprite blend mode - I'll try it out.

Is there a best way to go about smooth zooming? Ideally sprites would zoom in accordance with their scroll setting, I guess?
Bennett
Private
 
Posts: 8
Joined: Fri Feb 22, 2013 6:40 am

Re: Unpremultiply PNG alpha

Postby Arkeus » Fri Feb 22, 2013 4:14 pm

Bennett wrote:Thanks, I hadn't noticed the transparent sprite blend mode - I'll try it out.

Is there a best way to go about smooth zooming? Ideally sprites would zoom in accordance with their scroll setting, I guess?


The last time I zoomed on the fly, I simply changed Ax.zoom based on Ax.dt and it worked well. As for scroll setting, that will affect their position, but shouldn't affect how much they zoom. Things with scroll other than 1 might do weird things, because as you zoom in, you're seeing less, so things will have scrolled farther. Also, by default UI elements would also zoom and no longer fit the screen normally (but if their scroll is set to 0, their upper left corners would be in the same places, which would look weird when zooming in). You can see things that shouldn't zoom to not zoom by changing "zooms" to false for them. Note that zooms = false is similar to changing the position of a parent group, it will cause collision not to function correctly, so it should only be used for things such as UI elements.
Image
User avatar
Arkeus
Site Admin
 
Posts: 363
Joined: Mon Mar 26, 2012 12:43 am

Re: Unpremultiply PNG alpha

Postby Bennett » Fri Feb 22, 2013 4:24 pm

Hmm, I tried that but it only jumps between whole-integer amounts of zoom. I guess I've missed something here.
Bennett
Private
 
Posts: 8
Joined: Fri Feb 22, 2013 6:40 am

Re: Unpremultiply PNG alpha

Postby Arkeus » Fri Feb 22, 2013 6:48 pm

Oh, looking at changelog for 0.9.2 "Zooming is now required to be at integer levels". Like the whole rounding of sprite positions, there were so many problems for the typical use case for axel, that for now I simply disabled it. Without rounding there's so many annoying cases where things like float arithmetic errors cause things to appear wrong (a pixel of space between 2 things that should be next to each other, etc). I think these are things that will be worked out in time, but unfortunately time is what I lack these days.

You could try checking how the zoom stuff changed in 0.9.2, I suspect it's a really simple rounding change you'd have to undo. Line 698 of Ax.as has "Ax.worldZoom = Math.ceil(worldZoom);", so I'd start by seeing if removing that will make it work. It might be that any issue caused by floating point zoom levels won't be an issue for you.
Image
User avatar
Arkeus
Site Admin
 
Posts: 363
Joined: Mon Mar 26, 2012 12:43 am

Re: Unpremultiply PNG alpha

Postby Bennett » Sat Feb 23, 2013 12:18 am

Ok, thanks!
Bennett
Private
 
Posts: 8
Joined: Fri Feb 22, 2013 6:40 am


Return to Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest

cron