Hi:
I'm trying to:
1) draw a gradient with code
2) use it as a mask on a bitmap
the masked bitmap is embedded and instantiated: new EmbeddedClass()
the mask is created via a new Shape()
that Shape uses the graphics object methods to draw it: beginGradientFill, draw Rect, etc.
the gradient has an alphas array like this: [0, 255], so it goes from visible to invisible (I've checked this)
the actual masking: maskedBitmap.mask=maskShape;
the result is a solid mask, not a gradient mask...how can I apply a dynamic gradient mask?
the code is not pretty but here it goes:
Code:
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.DisplayObject;
import flash.display.InterpolationMethod;
import flash.display.Shape;
import flash.display.SpreadMethod;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.DisplacementMapFilter;
import flash.geom.Matrix;
import flash.geom.Point;
public class TestIt extends Sprite
{
[Embed(source="images/desert.jpg")] internal var Desert:Class;
internal var picture:Bitmap;
internal var rippleMap:BitmapData
internal var rippleFilter:DisplacementMapFilter;
internal var offset:Point;
internal var bandMask:Shape;
internal var bgPicture:Bitmap;
public function TestIt()
{
//put embedded picture in a bitmap
picture= new Desert();
//clone a background for when the foreground picture is partly masked
bgPicture=new Bitmap(picture.bitmapData.clone());
addChild(bgPicture);
addChild(picture);
//create a displacement map based on blue, with a lot of noise along y
rippleMap=new BitmapData(picture.width, picture.height/2);
rippleFilter=new DisplacementMapFilter(rippleMap, new Point(0,50), 100, BitmapDataChannel.BLUE, 0, 55, "clamp", 0, 0);
//listen to enterframe and animate the filter by continously offsetting the filter's displacement map's perlin noise
addEventListener(Event.ENTER_FRAME, makeRipple)
offset=new Point(0,0);
//create a gradient to use as a mask
bandMask=new Shape();
var colors:Array=[0xffffff, 0x0000FF];
var alphas:Array=[100,0];
var ratios:Array=[0, 255];
var spread:String=SpreadMethod.PAD;
var inter:String=InterpolationMethod.LINEAR_RGB;
var focal:int=0
var mat:Matrix=new Matrix();
mat.createGradientBox(640, 200, -Math.PI/2);
bandMask.graphics.beginGradientFill("linear",colors,alphas,ratios,mat, spread, inter, focal);
bandMask.graphics.drawRect(0, 50, 640,200)
bandMask.graphics.endFill();
bandMask.blendMode="layer"
//put the mask on the display list and mask the picture
addChild (bandMask);
picture.mask=bandMask;
}
internal function makeRipple(evt:Event):void {
//move the first octave of the perlin noise
offset.x+=.2;
offset.y+=.6;
rippleMap.perlinNoise(150,20, 5, 89, true, true, BitmapDataChannel.BLUE, false, [offset]);
picture.filters=[rippleFilter];
}
}
}
basically, I'm using the DisplacementMapFilter to make heat rise from the horizon in the desert, but I'd like the ripples to fade out gradually into the sky by using a gradient mask
I'm considering alternatives, like merging the gradient right onto the displacement map (to fade the perlin noise gradually wherever I want)
that would probably be easier on the processer, less alphaing, but still, I'd like to know how to get a dynamic gradient mask working