Skip to content

Instantly share code, notes, and snippets.

@gonchar
Last active August 29, 2015 13:56
Show Gist options
  • Save gonchar/8832905 to your computer and use it in GitHub Desktop.
Save gonchar/8832905 to your computer and use it in GitHub Desktop.
generate orientation texture
public static function generateOrientationTexture(size:int):BitmapData {
var half:uint = size / 2;
var bmd:BitmapData = new BitmapData(size, size, false, 0x000000);
for (var i:uint = 0; i < size; i++) {
for (var j:uint = 0; j < size; j++) {
var directionX:Number = i - half;
var directionY:Number = j - half;
var length:Number = Math.sqrt(directionX * directionX + directionY * directionY);
directionX /= length;
directionY /= length;
var red:uint = packFloat(directionX);
var green:uint = packFloat(directionY);
bmd.setPixel(i, j, (red << 16) + (green << 8) + 0xFF);
}
}
return bmd;
}
private static function packFloat(value:Number):uint {
var result:uint;
if (value < 0) {
result = 128 - Math.abs(value) * 128;
} else {
result = value * 128 + 128;
}
if (result > 255) result = 255;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment