Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
});
}
@JokerHuang

This comment has been minimized.

Copy link

@JokerHuang JokerHuang commented Sep 25, 2013

great!

@nicotroia

This comment has been minimized.

Copy link

@nicotroia nicotroia commented Sep 25, 2013

thanks

@zhanghaocong

This comment has been minimized.

Copy link

@zhanghaocong zhanghaocong commented Sep 28, 2013

cool!

@Stortof

This comment has been minimized.

Copy link

@Stortof 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