Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Last active February 29, 2020 09:35
Show Gist options
  • Save PrimaryFeather/5983685 to your computer and use it in GitHub Desktop.
Save PrimaryFeather/5983685 to your computer and use it in GitHub Desktop.
Water distortion filter, implemented with Starling's "DisplacementMapFilter".
private function addDistortionTo(target:DisplayObject):void
{
var offset:Number = 0;
var scale:Number = Starling.contentScaleFactor;
var width:int = target.width;
var height:int = target.height;
var perlinData:BitmapData = new BitmapData(width * scale, height * scale, false);
perlinData.perlinNoise(200*scale, 20*scale, 2, 5, true, true, 0, true);
var dispMap:BitmapData = new BitmapData(perlinData.width, perlinData.height * 2, false);
dispMap.copyPixels(perlinData,perlinData.rect, new Point(0,0));
dispMap.copyPixels(perlinData,perlinData.rect, new Point(0,perlinData.height));
var texture:Texture = Texture.fromBitmapData(dispMap, false, false, scale);
var filter:DisplacementMapFilter = new DisplacementMapFilter(texture, null,
BitmapDataChannel.RED, BitmapDataChannel.RED, 40, 5);
target.filter = filter;
target.addEventListener("enterFrame", function(event:EnterFrameEvent):void
{
if (offset > height) offset -= height;
else offset += event.passedTime * 20;
filter.mapPoint.y = offset - height;
});
}
@Stortof
Copy link

Stortof commented Sep 28, 2018

Hi Daniel,
Thanks for the example.
I have a little issue with the rendering : each time offset > height, I have a little freeze on the animation... and I can't manage to run it smoothly...
Any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment