генератор ландшафта
Визуализируем карту высот. Просьба к гуру не плеваться. Просто побочный эффект другой работы, может кто-нибудь заставит Бискупа нервно курить - вот и пригодится.
Генератор ландшафтов
Код:
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BlendMode; import flash.display.Graphics; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.ColorTransform; import flash.geom.Rectangle; [SWF(width=500,height=300,backgroundColor=0xF1F1F1,frameRate=25)] public class pragma extends Sprite { private const SIZEX:Number = 60; private const SIZEY:Number = SIZEX; private const CELLSIZE:Number = 4; private const MAXHEIGHT:Number = 200; private const HEIGHTSTEP:Number = CELLSIZE; private const RELIEFSMOOTH:Number = 20; private const WATERLEVEL:Number = 40; private const STEPS:int = 5; private var RELIEFNOISEFACTOR:Number = 0; private var seed:int = 777; public function pragma() { stage?onAddedToStage():addEventListener(Event.ADDED_TO_STAGE,onAddedToStage); } private function onAddedToStage(event:Event=null):void { removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage); stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; addEventListener(Event.ENTER_FRAME,iterate); } private function iterate(fakedata:*=null):void { rebuild(); RELIEFNOISEFACTOR+=1/STEPS; if (RELIEFNOISEFACTOR>=1) { seed++; RELIEFNOISEFACTOR=0; } } private var bd:BitmapData = new BitmapData(SIZEX, SIZEY);; private var bd2:BitmapData = new BitmapData(SIZEX, SIZEY);; private var bmp:Bitmap = new Bitmap(bd); private var pic:Bitmap=new Bitmap(new BitmapData(stage.stageWidth,stage.stageHeight,true)); private function rebuild(fakedata:*=null):void { bd.perlinNoise(RELIEFSMOOTH, RELIEFSMOOTH, 3, seed, true, true, 7, true, null); bd.colorTransform(new Rectangle(0,0,SIZEX,SIZEY),new ColorTransform(1-RELIEFNOISEFACTOR,1-RELIEFNOISEFACTOR,1-RELIEFNOISEFACTOR)); bd2.perlinNoise(RELIEFSMOOTH, RELIEFSMOOTH, 3, seed+1, true, true, 7, true, null); bd2.colorTransform(new Rectangle(0,0,SIZEX,SIZEY),new ColorTransform(RELIEFNOISEFACTOR,RELIEFNOISEFACTOR,RELIEFNOISEFACTOR)); bd.draw(bd2,null,null,BlendMode.ADD); bmp.bitmapData=bd; var basex:Number = stage.stageWidth/2; var basey:Number = stage.stageHeight/2-CELLSIZE*SIZEY/2+stage.stageHeight/8; var g:Graphics = this.graphics; var cl:uint; var cr:uint; var ct:uint; var cm:uint; var clr:uint; while (this.numChildren>0) removeChildAt(this.numChildren-1); pic.bitmapData.fillRect(pic.bitmapData.rect,0x00000000); pic.bitmapData.lock(); for (var k:uint=0; k<SIZEX; k++) { for (var l:uint=0; l<SIZEY; l++) { clr=bd.getPixel(k,l); var h:uint=clr&0xFF; var hg:Number=h/256; var xf:Number=2*(SIZEX-Math.abs(k-SIZEX/2))/SIZEX-1; var yf:Number=2*(SIZEY-Math.abs(l-SIZEY/2))/SIZEY-1; hg=hg*Math.sqrt(xf*yf); hg*=MAXHEIGHT; hg=hg-hg%HEIGHTSTEP; var nx:Number=basex+CELLSIZE*k-CELLSIZE*l; var ny:Number=basey+(CELLSIZE*k+CELLSIZE*l)/2; cl=(h)*0x010101; cr=(h-10)*0x010101; ct=(h+10)*0x010101; cm=(h-30)*0x010101; //left g.beginFill(cl,1); g.moveTo(nx-CELLSIZE,ny); g.lineTo(nx,ny+CELLSIZE/2); g.lineTo(nx,ny-hg+CELLSIZE/2); g.lineTo(nx-CELLSIZE,ny-hg); g.lineTo(nx-CELLSIZE,ny); g.endFill(); //right g.beginFill(cr,1); g.moveTo(nx+CELLSIZE,ny); g.lineTo(nx,ny+CELLSIZE/2); g.lineTo(nx,ny-hg+CELLSIZE/2); g.lineTo(nx+CELLSIZE,ny-hg); g.lineTo(nx+CELLSIZE,ny); g.endFill(); //top g.beginFill(ct,1); g.moveTo(nx-CELLSIZE,ny-hg); g.lineTo(nx,ny-hg+CELLSIZE/2); g.lineTo(nx+CELLSIZE,ny-hg); g.lineTo(nx,ny-hg-CELLSIZE/2); g.lineTo(nx-CELLSIZE,ny-hg); g.endFill(); //water var depth:Number=(WATERLEVEL-hg)/WATERLEVEL; if (depth>0) { g.lineStyle(0,0,0); var wcolor:int=0x0000ff+0x100*int(0xB0*(1-depth)); g.beginFill(wcolor,depth+0.3); g.moveTo(nx-CELLSIZE,ny-WATERLEVEL); g.lineTo(nx,ny-WATERLEVEL+CELLSIZE/2); g.lineTo(nx+CELLSIZE,ny-WATERLEVEL); g.lineTo(nx,ny-WATERLEVEL-CELLSIZE/2); g.lineTo(nx-CELLSIZE,ny-WATERLEVEL); g.endFill(); } //grid g.lineStyle(1,cm,0.5,true); if (k==SIZEX-1 || l==SIZEY-1) for (var n:Number=hg; n>0; n=n-1-(n-1)%8) { g.moveTo(nx-CELLSIZE,ny-n); g.lineTo(nx,ny-n+CELLSIZE/2); g.lineTo(nx+CELLSIZE,ny-n); } var rect:Rectangle=new Rectangle(nx-CELLSIZE,ny-Math.max(hg,WATERLEVEL)-CELLSIZE/2,CELLSIZE*2,Math.max(hg,WATERLEVEL)+CELLSIZE); pic.bitmapData.draw(this,null,null,null,rect); g.clear(); } } pic.bitmapData.unlock(); addChild(pic); bmp.width=100; bmp.height=100; bmp.rotation=45; var hld:Sprite=new Sprite(); addChild(hld); hld.addChild(bmp); hld.scaleY=0.5; hld.x=60; hld.y=40; } } }
Всего комментариев 6
Комментарии
![]() ![]() |
|
Интересно! Но очень тормозит.
|
![]() ![]() |
|
Отредактируйте пост. Вставьте swf в тело поста и код в тег AS3.
|
![]() ![]() |
|
![]() ![]() |
|
Цитата:
stage?onAddedToStage():addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
|
![]() ![]() |
|
Alatar, спасибо за ссылку, очень познавательно.
|
Последние записи от Lecosson
- генератор ландшафта (21.12.2013)
- Рецепт серверного Flash (29.05.2012)
- Класс настроек в ActionScript 3 (19.07.2010)