Sine-wave Distortion Effect for Actors (Behavior code now linked)

Hectate

  • *
  • Posts: 4643
Edit: see notes at the bottom of this post as well.
Sine-wave Distortion for Actors (for Stencyl 3.0, all platforms)
Attached to this post This Pastebin file is an Actor Behavior that allows you to apply a sine-wave warping/distortion effect to the actor's image. Some explanation of the settings available are below, but you will probably have to do some tests to get it to look as desired for your particular scenario.
Note that this is designed to work on single-frame actors and will probably not function as expected with animated (multi-frame) actors' images. Scaling images has been reported to also break the effect.
Eventually, I aim to see about implementing this as a scene behavior to apply to the screen on demand.

As the behavior is now on Pastebin, everyone should be able to add it to their game. To do so, create a new Actor Behavior, in code mode. Name it whatever you like, and the Classname field can be whatever is there already. From inside the code mode behavior editor, paste the entire contents of the pastebin file into the behavior. You need to completely replace everything that was in there previously by default. After it is in there, press the "Refresh Attributes" button.

If you feel compelled, you can still use the PNG image attached as the icon for the behavior.
Note that if you happen to have another behavior with the exact same classname (see line 61) Stencyl will give you errors when you compile. This can happen if you create a duplicate of a code mode behavior, so keep that in mind.

Attributes
Orientation
This number attribute sets the "orientation" of the waves. Specifically, setting it to 0 (zero) will result in the behavior modifying rows of pixels in the actor's image. Any other value will cause it to modify columns of pixels of the image. This also means that the waves appear to move toward the top when set to 0, or to the right otherwise.
Note that you can reverse the direction of motion; see the Frameskip attribute below.

Direction
This number attribute sets the "direction" of the waves' motion. Specifically, setting it to 0 (zero) will result in the behavior to shift the rows/columns (as determined by the orientation value) in a left/right motion. Any other value will cause the rows/columns to shift in an up/down motion.

Frameskip
A number value that allows you to set the speed of the wave motion. A value of 0 (zero) will result in no motion, although the effect will still be present. Positive values will move the waves "upward" or "rightward" and negative values will move "downward" or "leftward". The further from zero the value, the greater the speed of motion.

Frequency
This number value sets the width of the wave (technically, peak-to-peak measurement). Counter-intuitively, a value of 0 (zero) represents the entire width or height of the image (motion without distortion). A value of 2 is the maximum recommended, and appears to represent every-other row/column. The ideal value for a particular use will be closely related to the Amplitude value, but values of .01 to .5 seem to produce good results. This value can also be negative, to mirror the effect.

Amplitude
This number value is the maximum number of pixels the wave will shift a row or column. Since the edges of the image are not redrawn or rendered transparent, some degree of transparency padding (equal to the amplitude, for example), or the use of similar colors at the edge, will help avoid visible "stretching" issues with higher amplitude settings.

Credits
Special thanks to Irock who provided the request and inspiration for this behavior. Without him it would not exist.
The algorithm for the sine-wave motion itself comes from Earthbound Battle Backgrounds. While the sine-wave formula is well documented, having an existing version to work with greatly simplified the task.
Some of the code from Rob's Set Screenshot To Actor was extremely helpful in accessing the BitmapData and ensuring cross-platform compatibility.
MdotEdot also performed some device testing for the initial version.
Thanks to all the above. Enjoy, everyone!


Quote from: original post
Last night and this morning I was working on a way to distort an Actor's BitmapData in a sine-wave pattern. This is the result. I'll share the behavior eventually once I clean it up some.

http://www.stencyl.com/game/play/24401
For other people:
http://pastebin.com/hR71TKYA

Name your new Actor Behavior "Distortion", select Code Mode,  and give it a classname of "Design_12_12_Distortion" (from line 62 of the above pastebin file). If you use a different classname, you'll have to alter that line to match instead.

Make sure you click "Refresh Attributes" after pasting the file into the behavior. All other instructions are valid. Let me know if this fails as I'm short on testing time right now.

« Last Edit: March 31, 2015, 02:20:23 pm by Hectate »
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

dtrungle

  • *
  • Posts: 1938
Neat effect Hectate. Another effect usable for Stencyl is always good.  :)

pyxelbit

  • *
  • Posts: 83
is it possible to distort each color channel different? like for a stereoscopic 3D effect?

Hectate

  • *
  • Posts: 4643
Hrm, from previous work with the color channels I think it would probably be possible. You'd have to split the image into the separate channels, shift each channel as desired, and then recombine them. It wouldn't be stereoscopic though.

To get a real stereoscopic effect you'd have better luck rendering in a real 3D program (Blender3D, for example) and then exporting. I'm sure there's even scripts already out there to produce the images you need, rather than rendering each eye and then combining afterward.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

NobodyX

  • *
  • Posts: 1227
This is glorious.

Hectate

  • *
  • Posts: 4643
MdotEdot was kind enough to test this on multiple devices. All the devices below ran the demo successfully.


Also, I updated the Arcade demo with an additional scene that was to test transparency on smaller actors' images. Looks great to me, although if you want it to look good you would have some transparent pixels around the edges to pad them. A number equal to the amplitude of the sine wave would be sufficient.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.


Jon

  • *
  • Posts: 17516
This is terrific stuff and a perfect fit when combined with the Image/Bitmap API we'll add in post-3.0.

Hectate

  • *
  • Posts: 4643
Glad everyone likes it!

First post has now been updated with the behavior attached and some instructions.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

irock

  • *
  • Posts: 2864
I had way more fun reading the first post than anyone ever should reading a script description.

How difficult would animation support be to implement? It's not as important, but applying the effect to scrolling backgrounds would be cool too.

http://youtu.be/-Z1iXaCrRiQ
http://youtu.be/iaL8m6dGros
http://youtu.be/KpmyQUAAG00
http://youtu.be/HTMiMseZSJM

Nonetheless, you did a great job on this and I can't wait to make some cool trippy backgrounds for FlapBound.

NobodyX

  • *
  • Posts: 1227
This is terrific stuff and a perfect fit when combined with the Image/Bitmap API we'll add in post-3.0.
Exactly what I was thinking. :)

irock

  • *
  • Posts: 2864
Augh, for whatever reason I can't import the resource. Tried both the File menu and dragging into the application. Can you upload it to Forge?

irock

  • *
  • Posts: 2864
Actually, something's definitely up. I tried uploading an older version you sent to me via a game, I didn't get an "upload complete" confirmation, it showed up on Forge with a ? icon and I can't download it.



Tried uploading an actor with it attached to Forge and it downloaded without the behavior.

Strange.

Gintonic

  • Posts: 69
Thank you for that great behavior. Unfortunately I also can't import it :(

Hectate

  • *
  • Posts: 4643
Very strange. Maybe code-mode behaviors don't export well and we've just never tested it?

Either way, I'll figure a way to share it.

Edit: Yeah, I can't import it either. Very odd. The log viewer just throws an error, either from the file menu > import or drag-n-dropping.
Quote from: log viewer
[LOG] Action: Import Resource...
[LOG] Dropping into Snippet Page
[ERR] java.lang.NullPointerException
[LOG] Dragged In: C:\Users\shmitchn\Desktop\Distort Actor Sine Wave.png
[LOG] Dropping into Snippet Page
[ERR] java.lang.NullPointerException

Edit: reported in 3.0 bugs section: http://community.stencyl.com/index.php/topic,27347.0.html

I'll post the code and some instructions soon.

« Last Edit: February 18, 2014, 06:33:49 am by Hectate »
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.