perturbation effect study
learned from <Direct3D ShaderX (Vertex and Pixel Shader Tips and Tricks)>
at first I was going to write this in AGAL but that's pretty pesky work.. I still like bitmap data ;)
/**
* Copyright codeonwort ( http://wonderfl.net/user/codeonwort )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/bLLU
*/
// forked from codeonwort's perlin noise scroll test
package {
import flash.filters.DisplacementMapFilter
import flash.geom.Rectangle
import flash.geom.Point
import flash.events.Event
import flash.display.Sprite
import flash.display.Bitmap
import flash.display.BitmapData
public class FlashTest extends Sprite {
private var map:BitmapData // original map data
private var dmfmap:BitmapData // actually this is used for displacement mapping
private var dmf:DisplacementMapFilter
private var cloud:BitmapData
private var canvas:BitmapData // displayed on screen
private var uScroll:int = 0, uRect:Rectangle
private var vScroll:int = 465, vRect:Rectangle
public function FlashTest() {
// write as3 code here..
var temp:BitmapData = new BitmapData(465, 465, false, 0x0)
temp.perlinNoise(465, 465, 8, Math.random() * int.MAX_VALUE, true, true, 1)
var temp2:BitmapData = new BitmapData(465, 465, false, 0x0)
temp2.perlinNoise(465, 465, 8, Math.random() * int.MAX_VALUE, true, true, 2)
temp.copyChannel(temp2, temp.rect, new Point, 2, 2)
map = new BitmapData(465*2, 465, false, 0x0)
map.copyPixels(temp, temp.rect, new Point)
map.copyPixels(temp, temp.rect, new Point(465, 0))
temp.dispose()
temp2.dispose()
cloud = new BitmapData(465, 465, false, 0x0)
cloud.perlinNoise(465/4, 465/4, 8, Math.random() * int.MAX_VALUE, true, true, 7, true)
cloud.draw(cloud, null, null, "add")
dmfmap = new BitmapData(465, 465, false, 0x0)
dmf = new DisplacementMapFilter(dmfmap, new Point, 1, 2, 100, 100)
uRect = new Rectangle(0, 0, 465, 465)
vRect = new Rectangle(0, 0, 465, 465)
canvas = new BitmapData(465, 465, false, 0x0)
addChild(new Bitmap(canvas))
addEventListener("enterFrame", loop)
}
private function loop(e:Event):void {
uScroll += 3
vScroll -= 3
if(uScroll > 465) uScroll = 0
if(vScroll < 0) vScroll = 465
uRect.x = uScroll
vRect.x = vScroll
dmfmap.copyChannel(map, uRect, new Point, 1, 1)
dmfmap.copyChannel(map, vRect, new Point, 2, 2)
canvas.applyFilter(cloud, canvas.rect, new Point, dmf)
}
}
}