8.11.创建布林噪波

问题
我要创建随机的类似天然的效果如云,烟或水
解决办法
使用BitmapData类的perlinNoise( )方法
讨论
和noise( )方法一样,perlinNoise( )方法也创建随机图案,但是布林噪波的算法能产生类似自然图案,该算法由肯布林发明,能产生如爆炸,烟雾,水等自然效果,因为它是基于算法的,其运算速度比创建同等位图快且占用内存少等优点,方法原型如下:
+展开
-ActionScript
bitmap.perlinNoise(baseX, baseY, octaves, seed, stitch, fractal,
channels, grayscale, offsets);

前6个参数是必须的,后3个可选,现在我们创建一个简单的例子,下面的代码创建一个位图,应用布林噪波并显示:
+展开
-ActionScript
bitmap = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xff000000);
bitmap.perlinNoise(100, 100, 1, 1000, falsefalse, 1, truenull);
var image:Bitmap = new Bitmap(bitmap);
addChild(image);

效果如下:

试着修改下参数值看看效果如何。

baseX和baseY 决定图案的大小,这里设置成100,如果改成200和50,则会在水平上进行拉伸,效果如下:


octaves 参数是个整数,决定噪波的迭代次数,数值越大,产生越细的噪波,花费的时间也长一些。

seed参数和noise( )方法中的一样意思,如果用同样的随机种子,产生的是同一个图案。

stitch参数为true时,图案四周相互协调,能使位图很小能平铺,看下面的代码:

+展开
-ActionScript
bitmap = new BitmapData(100, 100, false, 0xff000000);
bitmap.perlinNoise(100, 100, 2, 1000, truefalse, 1, true);
graphics.beginBitmapFill(bitmap);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
graphics.endFill( );

ractal 参数为true时,图案被光滑且模糊化:
+展开
-ActionScript
bitmap = new BitmapData(stage.stageWidth, stage.stageHeight,
false, 0xff000000);
bitmap.perlinNoise(200, 100, 5, 1000, falsefalse, 1, truenull);
var image:Bitmap = new Bitmap(bitmap);
addChild(image);

设置fractal为TRue:
+展开
-ActionScript
bitmap.perlinNoise(200, 100, 5, 1000, falsetrue, 1, truenull);


这样的效果看起来像云

布林噪波还可建立在alpha通道上,这样可以创建透明的云彩或雾效果。


最后的参数表示偏移量,是个Point 对象数组,每个点指定单个分形的x,y坐标偏移量,如果布林噪波有多个分形(第三个参数代表分形),那么数组的长度就等于分形的数量,下面的例子创建了两个分形布林噪波图案在x轴上拉伸:
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
public class Clouds extends Sprite {
private var _bitmap:BitmapData;
private var _xoffset:int = 0;
public function Clouds( ) {
_bitmap = new BitmapData(stage.stageWidth, stage.stageHeight,
true, 0xffffffff);
var image:Bitmap = new Bitmap(_bitmap);
addChild(image);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
_xoffset++;
var point:Point = new Point(_xoffset, 0);
// use the same point in both elements
// of the offsets array
_bitmap.perlinNoise(200, 100, 2, 1000, false, true,
1, true, [point, point]);
}
}
}

加支付宝好友偷能量挖...


评论(0)网络
阅读(47)喜欢(0)flash/flex/fcs/AIR