地球儀4
/**
* Copyright umhr ( http://wonderfl.net/user/umhr )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/8zDT
*/
package
{
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.geom.Matrix3D;
import flash.geom.Rectangle;
import flash.geom.Vector3D;
import flash.geom.ColorTransform;
import flash.system.LoaderContext;
/**
* ...
* @author umhr
*/
public class Main extends Sprite
{
private var _mulitiLoader:MultiLoader;
private var _xyzData:Vector.<Number>;
private var _colorData:Vector.<int>;
private var _matrix3D:Matrix3D;
private var _canvas:Bitmap;
private var _bg:Bitmap;
private const FADE:ColorTransform = new ColorTransform(1, 1, 1, 1, -0xA, -0xA, -0xA);
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
_mulitiLoader = new MultiLoader();
_mulitiLoader.add("http://assets.wonderfl.net/images/related_images/c/c0/c059/c059a7be659265a9985c161c17c00c7d9e34a17a", { type:"image", id:"map" , context:new LoaderContext(true)} );
_mulitiLoader.add("http://assets.wonderfl.net/images/related_images/7/7e/7e68/7e686406c0df1a6028dac3428e58a516ef14c5f9", { type:"image", id:"bump" , context:new LoaderContext(true) } );
_mulitiLoader.addEventListener(Event.COMPLETE, atComp);
_mulitiLoader.start();
}
private function atComp(event:Event):void {
_bg = new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x00000000), "auto", false);
_bg.filters = [new BlurFilter(16, 16)];
this.addChild(_bg);
_canvas = new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000),"auto",true);
this.addChild(_canvas);
_colorData = new Vector.<int>();
_xyzData = new Vector.<Number>();
_matrix3D = new Matrix3D();
var map:BitmapData = _mulitiLoader.getBitmapData("map");
var bump:BitmapData = _mulitiLoader.getBitmapData("bump");
var h:int = map.height;
var w:int = map.width;
for (var i:int = 0; i < h; i++) {
for (var j:int = 0; j < w; j++) {
_colorData.push(map.getPixel32(j, i));
var rx:Number = Math.PI * (2 * (j - (w - 1) / 2) / w);
var ry:Number = Math.PI * ((i - (h - 1) / 2) / h);
var scale:Number = 160 + 6 * bump.getPixel(j, i) / 0xFFFFFF;
var nx:Number = Math.cos(ry) * Math.sin(rx) * scale;
var ny:Number = Math.sin(ry) * scale;
var nz:Number = Math.cos(ry) * Math.cos(rx) * scale;
_xyzData.push(nx);
_xyzData.push(ny);
_xyzData.push(nz);
}
}
this.addEventListener(Event.ENTER_FRAME, atEnter);
}
private function atEnter(event:Event):void {
var mouseVec:Vector3D = new Vector3D(-stage.mouseY + stage.stageWidth / 2, stage.mouseX - stage.stageHeight / 2);
mouseVec.normalize();
_matrix3D.appendRotation(0.5, mouseVec);
_matrix3D.appendRotation(1, Vector3D.Y_AXIS);
var xyz:Vector.<Number> = new Vector.<Number>();
_matrix3D.transformVectors(_xyzData, xyz);
_canvas.bitmapData.lock();
_bg.bitmapData.lock();
_canvas.bitmapData.fillRect(new Rectangle(0, 0, 465, 465), 0x00000000);
_bg.bitmapData.colorTransform(_canvas.bitmapData.rect, FADE);
var n:int = xyz.length / 3;
var vp:Number = 1000;
for (var i:int = 0; i < n; i++) {
if (xyz[i * 3 + 2] < 0) { continue };
var per:Number = vp / (vp + xyz[i * 3 + 2]);
_canvas.bitmapData.setPixel32(xyz[i * 3]/per + 465 / 2, xyz[i * 3 + 1]/per + 465 / 2, _colorData[i]);
}
_bg.bitmapData.draw(_canvas, null, null, "add");
_canvas.bitmapData.unlock();
_bg.bitmapData.unlock();
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.Security;
import flash.utils.Dictionary;
import flash.utils.ByteArray;
import flash.utils.getQualifiedClassName;
import flash.display.MovieClip;
class MultiLoader{
public static var IMAGE_EXTENSIONS:Array = ["swf", "jpg", "jpeg", "gif", "png"];
public static var TEXT_EXTENSIONS:Array = ["txt", "js", "xml", "php", "asp"];
public static const COMPLETE:String = "complete";
private var _listener:Function = function(event:Event):void{};
private var _loads:Dictionary;
private var _keyFromId:Dictionary;
private var _loadCount:int;
private var _itemsLoaded:int;
public var items:Array;
public function MultiLoader(name:String = ""){
_loads = new Dictionary();
_keyFromId = new Dictionary();
_itemsLoaded = 0;
items = [];
}
public function add(url:String, props:Object = null):void {
var loadingItem:LoadingItem = new LoadingItem();
loadingItem.url = url;
loadingItem.type = getType(url, props);
if(props){
if(props.context){
loadingItem.context = props.context;
}
if (props.id) {
_keyFromId[props.id] = url;
}
if (props.preventCache) {
loadingItem.preventCache = props.preventCache;
}
}
items.push(loadingItem);
}
private function getType(url:String, props:Object = null):String{
var result:String = "";
if (props && props.type) {
return props.type;
}
var i:int;
var extension:String;
var n:int = IMAGE_EXTENSIONS.length;
for (i = 0; i < n; i++) {
extension = IMAGE_EXTENSIONS[i];
if(extension == url.substr(-extension.length).toLowerCase()){
result = "image";
break;
}
}
if(result == ""){
n = TEXT_EXTENSIONS.length;
for (i = 0; i < n; i++) {
extension = TEXT_EXTENSIONS[i];
if(extension == url.substr(-extension.length).toLowerCase()){
result = "text";
break;
}
}
}
return result;
}
public function start():void{
var n:int = items.length;
for (var i:int = 0; i < n; i++) {
var type:String = items[i].type;
var url:String = items[i].url;
url += (items[i].preventCache)?"?rand=" + Math.random():"";
var uRLRequest:URLRequest = new URLRequest(url);
if(type == "image"){
_loads[items[i].url] = loadImage(uRLRequest, items[i].context);
}else if(type == "text"){
_loads[items[i].url] = loadText(uRLRequest);
}else if (type == "byteImage") {
byteImage(uRLRequest, items[i].context);
}
}
}
public function addEventListener(type:String,listener:Function):void{
_listener = listener;
}
public function getBitmap(key:String):Bitmap{
key = keyMatching(key);
var bitmap:Bitmap;
try{
if (getQualifiedClassName(_loads[key].content) == "flash.display::MovieClip") {
var mc:MovieClip = _loads[key].content;
var bitmapData:BitmapData = new BitmapData(mc.width, mc.height);
bitmapData.draw(mc);
bitmap = new Bitmap(bitmapData);
}else {
bitmap = _loads[key].content;
}
}catch (e:*) {
//bitmap = new Bitmap();
}
return bitmap;
}
public function getBitmapData(key:String):BitmapData{
key = keyMatching(key);
var bitmap:Bitmap = getBitmap(key);
var bitmapData:BitmapData = new BitmapData(bitmap.width, bitmap.height);
bitmapData.draw(bitmap);
return bitmapData;
}
private function loadImage(url:URLRequest, context:LoaderContext = null):Loader {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComp);
loader.load(url, context);
return loader;
}
private function byteImage(url:URLRequest, context:LoaderContext = null):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadBytes);
loader.load(url, context);
function loadBytes(event:Event):void {
_loads[url.url] = new Loader();
_loads[url.url].contentLoaderInfo.addEventListener(Event.COMPLETE, onComp);
_loads[url.url].loadBytes(event.target.bytes);
}
}
public function getBinary(key:String):ByteArray{
return _loads[keyMatching(key)].contentLoaderInfo.bytes;
}
public function getText(key:String):String {
key = keyMatching(key);
return key?_loads[key].data:key;
}
public function getXML(key:String):XML {
return XML(getText(key));
}
private function keyMatching(key:String):String {
return _loads[key]?key:_keyFromId[key];
}
private function loadText(url:URLRequest):URLLoader{
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComp);
loader.load(url);
return loader;
}
private function onComp(event:Event):void {
_itemsLoaded ++;
if (_itemsLoaded == items.length) {
_itemsLoaded = 0;
_listener(event);
}
}
public function get itemsTotal():int{
return items.length;
}
public function get itemsLoaded():int{
return _itemsLoaded;
}
public function get loadedRatio():Number {
return _itemsLoaded / items.length;
}
}
import flash.net.URLRequest;
import flash.system.LoaderContext;
class LoadingItem{
public var url:String;
public var preventCache:Boolean;
public var type:String;
public var status:String;
public var context:LoaderContext;
public function LoadingItem(){};
}