Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411613 Posts in 69390 Topics- by 58447 Members - Latest Member: sinsofsven

May 09, 2024, 06:19:11 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsAS3: Dynamic outlines with variable thickness tutorial!
Pages: [1]
Print
Author Topic: AS3: Dynamic outlines with variable thickness tutorial!  (Read 8345 times)
Moczan
Guest
« on: August 15, 2012, 04:04:42 PM »

Hi! I want to start by saying that it's my first tutorial ever, so if I miss something or doesn't explain, don't be afraid to ask!

What you need to know before we start:
  • Basic knowledge of AS3
  • BitmapData class and copyPixels function

That's pretty much it. If you can display sprite from PNG file on your screen using AS3, soon you will be able to add outline to it.
I'm not really good with words, so I will mostly use code and images.
Let's start by creating some basic variables.



canvas is our 'main' BitmapData we will draw on. We will pass it to Bitmap object to display it on scree.
pikachu holds our graphics. I used [Embed] tag to add sprite to the project, that's where PikaGraphics come from.
outline is a very important object. It's a rectangle that defines color of our outline. It should be at least as big as our sprite (that's why I used pikachu's dimension creating it). With some tweaks to the code you could even create outlines with gradient!
p is a simple Point with sprite's position on screen.

The last thing to prepare is our Bitmap object, I pass canvas in to it, scale it up to x2 for better visibility and add it to stage.
At that point we are ready to display our Pokemon. Just add this simple line.



Result:



Ok, now, to the real job. If you've ever tried to display a sprite with transparency, you've probably seen those 2 parameters, that are always both null. Those are alphaBitmapData and alphaPoint. Those are used to define alphaMask.



Alpha mask let's you copy pixel's color from your source bitmapData, but alpha from other. We will use that, to copy our infamous pink rectangle and shift it to the left.



Result:



Ok, by now you probably know where it's going, we do that for the 3 other sides:



And get a full and clean outline!



"But Moczan, the topic clearly states that you posses the ancient power of outlines with variable thickness"
Fear not, we can easily do that by throwing our outline drawing code into the loop!



Results for the thickness of 2 and 3



Yep, it's that easy. It gets ugly when you try to bigger numbers, adding diagonal shift or caching result and using that as a new alpha mask helps with that a lot, but results may vary per sprite, and thickness of 1 or 2 is what's people want to do most of the time.

Bonus! source code
Logged
cactipus
Level 0
**

Cats are neat. Cacti are too.


View Profile WWW
« Reply #1 on: August 15, 2012, 07:17:15 PM »

Ah, nifty.
Nice tutorial.
Logged

Octopodes  > Octopus
Cactipodes > Cactipus
It just makes sense.
kamac
Level 10
*****


Notoriously edits his posts


View Profile
« Reply #2 on: August 16, 2012, 10:45:41 AM »

Looks good! I've been wondering how to do that last times without using shaders. Thanks!

Could we get source code as pastebin too? Thanks  Wink
Logged

Moczan
Guest
« Reply #3 on: August 16, 2012, 12:14:45 PM »

Looks good! I've been wondering how to do that last times without using shaders. Thanks!

Could we get source code as pastebin too? Thanks  Wink

http://pastebin.com/s9fugLAt Here you are Wink
Logged
kamac
Level 10
*****


Notoriously edits his posts


View Profile
« Reply #4 on: August 16, 2012, 12:51:41 PM »

Oh stop it, you.




Thanks a bunch.  Smiley
Logged

iopred
Level 0
**


Gutsch


View Profile
« Reply #5 on: November 20, 2012, 10:19:28 AM »

You can also use a GlowFilter with a very, very high strength. I'm not sure about the performance differences, but it allows outlines on the normal display list, without using BitmapData.
Logged
nikki
Level 10
*****


View Profile
« Reply #6 on: November 22, 2012, 02:53:52 PM »

yeah thats the first thing I thought too , glow filter with color black and thin 1/2/3 px 'spread' can't remember the actual parameter name.

but this solution is nice too, specically for bitmapped stuff
Logged
Moczan
Guest
« Reply #7 on: November 24, 2012, 06:44:18 AM »

Yep, very strong glow filter gives similar results, but I'm not sure if it won't give you anti-aliased outline which is not a desirable effect when making a game with pixelart graphics.
Logged
Drunken Monkeys
Level 0
**


Drunken Monkey (Kevin)


View Profile WWW
« Reply #8 on: December 13, 2013, 09:58:28 AM »

I know this is a very old topic, but I just implemented it in Flixel, and wanted to thank you.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic