Terrain with Ridged Perlin Noise with Isometric View

by greentec
color map from: http://libnoise.sourceforge.net/tutorials/tutorial3.html

slope algorithm from:
http://andywoodruff.com/blog/shaded-relief-in-as3/
♥0 | Line 130 | Modified 2016-12-01 19:23:19 | MIT License
play

ActionScript3 source code

/**
 * Copyright greentec ( http://wonderfl.net/user/greentec )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/6Adze
 */

package  
{
    import com.bit101.components.PushButton;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;
    /**
     * ...
     * @author ypc
     */
    [SWF(width = 465, height = 465)]
    public class Isometric extends Sprite
    {
        public var bitmapData:BitmapData;
        public var colorBitmapData:BitmapData;
        public var screenBitmapData:BitmapData;
        public var terrainColorDict:Array = [0x000080, 0x0000ff, 0x0080ff, 0xf0f040, 0x20a000, 0xe0e000, 0x808080, 0xffffff];
        public var terrainColorNum:Array = [0, 0.125, 0.5, 0.53125, 0.5625, 0.6875, 0.875, 1];
        
        public var seed:uint = 0;
        public var noiseType:int = 3;
        
        public var _width:int = 200;
        public var _height:int = 200;
        
        public function Isometric() 
        {
            bitmapData = new BitmapData(_width, _height, false, 0x0);
            colorBitmapData = new BitmapData(_width, _height, false, 0x0);
            
            screenBitmapData = new BitmapData(465, 465, false, 0);
            
            
            var bitmap:Bitmap;
            bitmap = new Bitmap(screenBitmapData);
            addChild(bitmap);
            
            var bitmap1:Bitmap;
            bitmap1 = new Bitmap(bitmapData);
            bitmap1.scaleX = bitmap1.scaleY = 0.5;
            addChild(bitmap1);
            
            var bitmap2:Bitmap;
            bitmap2 = new Bitmap(colorBitmapData);
            bitmap2.scaleX = bitmap2.scaleY = 0.5;
            bitmap2.x = _width / 2;
            addChild(bitmap2);
            
            
            var perlinButton:PushButton = new PushButton(this, 0, 440, "Perlin Noise", function(e:Event):void {
                noiseType = 1;
                ridgedPerlin();
            });
            perlinButton.width = 465 / 3;
            perlinButton.height = 25;
            var ridgedPerlinButton:PushButton = new PushButton(this, perlinButton.x + perlinButton.width, perlinButton.y, "Ridged Perlin", function(e:Event):void {
                noiseType = 2;
                ridgedPerlin();
            });
            ridgedPerlinButton.width = 465 / 3;
            ridgedPerlinButton.height = 25;
            var reverseRidgedPerlinButton:PushButton = new PushButton(this, ridgedPerlinButton.x + ridgedPerlinButton.width, ridgedPerlinButton.y, "Ridged Perlin (Reverse)", function(e:Event):void {
                noiseType = 3;
                ridgedPerlin();
            });
            reverseRidgedPerlinButton.width = 465 / 3;
            reverseRidgedPerlinButton.height = 25;
            var randomizeButton:PushButton = new PushButton(this, 465 - 465 / 3, 0, "Randomize Seed", function(e:Event):void {
                seed = Math.random() * uint.MAX_VALUE;
                ridgedPerlin();
            });
            randomizeButton.width = perlinButton.width;
            randomizeButton.height = 25;
            
            
            ridgedPerlin();
        }
        
        public function ridgedPerlin():void
        {
            screenBitmapData.fillRect(screenBitmapData.rect, 0x0);
            bitmapData.perlinNoise(64, 64, 7, seed, false, true, 7, true);
            
            var i:int;
            var j:int;
            var k:int;
            var t:Number;
            var _x:int;
            var _y:int;
            var num:Number;
            var color:uint;
            var h:int;
            
            for (i = 0; i < _width; i += 1)
            {
                for (j = 0; j < _height; j += 1)
                {
                    color = bitmapData.getPixel(i, j);
                    color = color & 0xff;
                    num = color / 255.0;
                    
                    if (noiseType > 1)
                    {
                        num *= 2;
                        num -= 1;
                        num = Math.abs(num);
                        
                        num = 1 - num;
                        num *= num * num * num;
                        
                        if (noiseType == 3)
                        {
                            num = 1 - num;
                        }
                    }
                    
                    color = num * 255;
                    
                    bitmapData.setPixel(i, j, color << 16 | color << 8 | color);
                    
                    
                    h = color;
                    
                    for (k = 0; k < terrainColorNum.length - 1; k += 1)
                    {
                        if (terrainColorNum[k] <= num && terrainColorNum[k+1] >= num)
                        {
                            t = (num - terrainColorNum[k]) / (terrainColorNum[k + 1] - terrainColorNum[k]);
                            color = interpolateColor(terrainColorDict[k], terrainColorDict[k + 1], t);
                        }
                    }
                    
                    _x = 465 / 2.0 + i - j;
                    _y = 465 / 2.0 + (i + j) / 2;
                    
                    colorBitmapData.setPixel(i, j, color);
                    screenBitmapData.fillRect(new Rectangle(_x, _y - (h / 3), 1, (h / 3)), color);
                }
            }
        }
        
        public static function interpolateColor(fromColor:uint, toColor:uint, progress:Number):uint
        {
            var q:Number = 1-progress;
            var fromR:uint = (fromColor >> 16) & 0xFF;
            var fromG:uint = (fromColor >>  8) & 0xFF;
            var fromB:uint =  fromColor        & 0xFF;
            var toR:uint = (toColor >> 16) & 0xFF;
            var toG:uint = (toColor >>  8) & 0xFF;
            var toB:uint =  toColor        & 0xFF;
            var resultR:uint = fromR*q + toR*progress;
            var resultG:uint = fromG*q + toG*progress;
            var resultB:uint = fromB*q + toB*progress;
            var resultColor:uint = resultR << 16 | resultG << 8 | resultB;
            return resultColor;
        }
        
    }

}