AS3 Landscape generator (1)

June 6th, 2009 by terrypaton, under Library.

A top down terrain / landscape generator using perlinNoise and paletteMap to create the effect. The gradient that applies the colours can easily be adjusted.
CLICK TO GENERATE NEW IMAGES.

Download zip

Here’s the code …

package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.GradientType;
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.events.MouseEvent
public class AS3LandscapeGenerator extends Sprite {
public function AS3LandscapeGenerator() {
generatePaletteMap();
bdColor = new BitmapData(320,240,false);
bdNoise = new BitmapData(320,240,false);
var _bmp:Bitmap = new Bitmap(bdColor);
addChild(_bmp);
stage.addEventListener(MouseEvent.CLICK, generate)
generate(null)
}
private var paletteMap:Array;
private var bdColor:BitmapData
private var bdNoise:BitmapData
private function generate(event:MouseEvent):void {
trace(“generate”)
generatePaletteMap();
var seed:int = Math.random() * 30000;
bdColor.perlinNoise(70,70,3,seed,true,true,7,true);
bdColor.paletteMap(bdColor, bdColor.rect, new Point(), paletteMap, [], []);
bdNoise.perlinNoise(2,2,1,seed,true,true,7,true);
bdColor.draw(bdNoise,null,new ColorTransform(1,1,1,.2),BlendMode.OVERLAY);
}
private function generatePaletteMap():void {
paletteMap = [];
var myShape:Shape = new Shape();
var gradientBoxMatrix:Matrix = new Matrix();
gradientBoxMatrix.createGradientBox(256,40,0,0,0);
myShape.graphics.beginGradientFill(GradientType.LINEAR,[0x023175,0x0250C1,0xC7BC96,0x115525,0x725C5A,0x666666,0xFFffff],[1,1,1,1,1,1,1],[0,128,148,157,182,193,218],gradientBoxMatrix);
myShape.graphics.drawRect(0,0,256,10);
myShape.graphics.endFill();
var _bmpData:BitmapData = new BitmapData(256,10,true,0);
var _bit:Bitmap = new Bitmap(_bmpData);
_bmpData.draw(myShape);
_bit.y = 269
for (var n:int = 0;n < 256;n++) {
paletteMap.push(_bmpData.getPixel32(n,0));
}
}

}

}

One Response to “AS3 Landscape generator”

  1. Steve says:

    Hiya, How do I add an alpha transparancy value to this, i want to combine multiple maps say one like your one above and another showing clouds on top where you can see between the clouds to the sea and land below?

Leave a Reply