3DS Model Viewer (Away3D)
Code taken from:
http://lessonsforall.ru/away3d/5-sozdanie-prilozheniya-dlya-zagruzki-3ds-modeley-s-pomoschyu-away3d.html
Original:
http://active.tutsplus.com/tutorials/actionscript/view-3ds-models-with-away3d-4-0-beta/
/**
* Copyright WLAD ( http://wonderfl.net/user/WLAD )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/bnxu
*/
package
{
//Классы Away3D
import away3d.cameras.lenses.PerspectiveLens;
import away3d.containers.ObjectContainer3D;
import away3d.containers.View3D;
import away3d.entities.Mesh;
import away3d.events.LoaderEvent;
import away3d.loaders.Loader3D;
import away3d.loaders.parsers.Max3DSParser;
import away3d.materials.TextureMaterial;
import away3d.textures.BitmapTexture;
import away3d.debug.AwayStats;
//Главные классы Flash для отображения и интерактивности
import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.text.TextField;
//Классы для загрузки 3ds файла с локального диска
import flash.display.SimpleButton;
import flash.events.IOErrorEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.system.Security;
import flash.events.SecurityErrorEvent;
import flash.events.ProgressEvent;
import com.bit101.components.PushButton;
public class Main extends Sprite
{
private var file:FileReference;
private var view3d:View3D;
private var loader:Loader3D;
private var labelDebug:TextField;
private var object3d:Mesh;
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
inittrace(stage);
//graphics.beginFill(0);
//graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
//graphics.endFill();
//разрешаем загрузку файлов с локального хранилища
Security.allowDomain("*");
removeEventListener(Event.ADDED_TO_STAGE, init);
// инициализируем 3D движок
view3d = new View3D();
view3d.backgroundColor = 0x666666;
view3d.camera.lens = new PerspectiveLens();
view3d.camera.z = 100;
addChild(view3d);
var stats:AwayStats = new AwayStats(view3d);
addChild(stats);
stats.x = stage.stageWidth - stats.width;
//3D загрузчик
initLoader();
//Кнопка для открытия окна обзора файлов
var but:PushButton = new PushButton(this,5,5,"Browse",onclick_mcBrowse);
//файл
file = new FileReference();
trace("Click and browse 3DS Models");
}
private function initLoader():void
{
//Очистка
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
while (view3d.scene.numChildren > 0)
{
view3d.scene.removeChild(view3d.scene.getChildAt(0));
}
//Инициализируем загрузчик 3d модели
Loader3D.enableParser(Max3DSParser);
loader = new Loader3D();
loader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onComplete_loader);
loader.addEventListener(LoaderEvent.LOAD_ERROR, onerror_loader);
view3d.scene.addChild(loader);
//конфигурируем камеру
view3d.camera.lookAt(loader.position);
}
private function onclick_mcBrowse(e:MouseEvent):void
{
file.browse([new FileFilter("3DS", "*.3ds")]);
file.addEventListener(Event.SELECT, onFileSelected);
file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
file.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
}
//
private function onSecurityError(e:Event):void
{
trace(".Security Error!");
}
private function onIOError(e:IOErrorEvent):void
{
trace(".File not found Error!");
}
private function onFileSelected(e:Event):void
{
trace("File :" + file.name);
file.removeEventListener(Event.SELECT, onFileSelected);
file.addEventListener(Event.COMPLETE, onFileLoaded);
file.addEventListener(ProgressEvent.PROGRESS, onProgress);
file.load();
}
private function onProgress(e:ProgressEvent):void
{
trace("Loading... " + (((e.bytesLoaded/e.bytesTotal) * 100)).toFixed(1) + "%");
}
private function onFileLoaded(e:Event):void
{
trace("File loading [" + file.name + "] complete");
file.removeEventListener(Event.COMPLETE, onFileLoaded);
file.removeEventListener(ProgressEvent.PROGRESS, onProgress);
initLoader();
loader.loadData(e.target.data);
trace("Begin model render...");
}
private function onerror_loader(e:LoaderEvent):void
{
trace("Loading Error" + e.toString());
}
private function onComplete_loader(e:LoaderEvent):void
{
trace("Rendering Complete!");
loader.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, onComplete_loader);
loader.removeEventListener(LoaderEvent.LOAD_ERROR, onerror_loader);
object3d = Mesh(loader.getChildAt(0));
view3d.scene.addChild(object3d);
loader.dispose();
loader = null;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
object3d.yaw(1); //вращение на 1 единицу
view3d.render();
}
}
}
import flash.display.Stage;
/*
__/\\\\\\\\\\\\\\\____/\\\\\\\\\_________/\\\\\\\\\___________/\\\\\\\\\__/\\\\\\\\\\\\\\\_
_\///////\\\/////___/\\\///////\\\_____/\\\\\\\\\\\\\______/\\\////////__\/\\\///////////__
_______\/\\\_______\/\\\_____\/\\\____/\\\/////////\\\___/\\\/___________\/\\\_____________
_______\/\\\_______\/\\\\\\\\\\\/____\/\\\_______\/\\\__/\\\_____________\/\\\\\\\\\\\_____
_______\/\\\_______\/\\\//////\\\____\/\\\\\\\\\\\\\\\_\/\\\_____________\/\\\///////______
_______\/\\\_______\/\\\____\//\\\___\/\\\/////////\\\_\//\\\____________\/\\\_____________
_______\/\\\_______\/\\\_____\//\\\__\/\\\_______\/\\\__\///\\\__________\/\\\_____________
_______\/\\\_______\/\\\______\//\\\_\/\\\_______\/\\\____\////\\\\\\\\\_\/\\\\\\\\\\\\\\\_
_______\///________\///________\///__\///________\///________\/////////__\///////////////__
*/
import flash.text.TextField;
import flash.text.TextFormat;
///// WONDERFL TRACE /////
function inittrace(s:Stage):void{WTrace.initTrace(s);}var trace:Function;class WTrace{
private static var FONT:String = "_sans";
private static var SIZE:Number = 12;
private static var SPACE:String = " ";
private static var COLOR:uint = 0xFF0000;
private static var LINES:uint = 6;
private static var TextFields:Array = [];private static var trace_stage:Stage;
public static function initTrace(stg:Stage):void{trace_stage = stg;trace = wtrace;}
private static function scrollup():void{if (TextFields.length > LINES){
var removeme:TextField = TextFields.shift();trace_stage.removeChild(removeme);removeme = null;
}for(var x:Number=0;x<TextFields.length;x++){(TextFields[x] as TextField).y -= SIZE*1.2;}}
public static function wtrace(... args):void{var s:String="";var tracefield:TextField;
for (var i:int;i < args.length;i++){if (i != 0) s+=SPACE;s+=args[i].toString();}
tracefield= new TextField();tracefield.autoSize = "left";tracefield.textColor = COLOR;
tracefield.text = s;tracefield.y = trace_stage.stageHeight - 20;
var tf:TextFormat = new TextFormat(FONT, SIZE);tracefield.setTextFormat(tf);
trace_stage.addChild(tracefield);scrollup();TextFields.push(tracefield);}}