最近在做3DWall,需要倒影的效果..以前是用Mask来做的..效果很低..
网上参考了一下别人的代码..整理了成一个函数..
调用的时候直接使用getReflection(target)返回一个bitmapData对象
也可以使用getReflection(target,100)来指定倒影高度...
private function getReflection(target:DisplayObject,height:Number = -1):BitmapData
{
if(height<0)height = target.height
var bit:BitmapData = new BitmapData(target.width,height,true,0);
bit.draw(target,new Matrix(1,0,0,-1,0,target.height));
//渐变拷贝
var mtx:Matrix = new Matrix();
mtx.createGradientBox(target.width,height,0.5 * Math.PI);
var shape:Shape = new Shape();
shape.graphics.beginGradientFill(GradientType.LINEAR,[0,0],[0.5,0],[0,255],mtx)
shape.graphics.drawRect(0,0,target.width,height);
shape.graphics.endFill();
var alpha:BitmapData = new BitmapData(target.width,height,true,0);
alpha.draw(shape);
bit.copyPixels(bit,bit.rect,new Point(0,0),alpha,new Point(0,0),false);
return bit.clone()
}
应用一例
var reflection:Bitmap = new Bitmap(getReflection(target,100));
reflection.x = target.x;
reflection.y = target.y + target.height;
addChild(reflection);