Skip to content

Instantly share code, notes, and snippets.

@leeprobert
Created March 22, 2012 17:48
Show Gist options
  • Save leeprobert/2160886 to your computer and use it in GitHub Desktop.
Save leeprobert/2160886 to your computer and use it in GitHub Desktop.
Away3D 4 Flex application example
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:loaders="away3d.loaders.*"
xmlns:a3d="*"
backgroundAlpha="0"
creationComplete="init()"
viewSourceURL="srcview/index.html"
>
<fx:Script>
<![CDATA[
import away3d.events.LoaderEvent;
import away3d.loaders.parsers.Parsers;
[Embed(source="./fx_appicon.jpg")]
private var flexLogo:Class;
[Embed(source="./away3d_logo_white.jpg")]
private var a3dLogo:Class;
//------------------------------------------------------
public function init():void
{
/*
Parsers can also be enabled individually to save on file size
*/
Parsers.enableAllBundled();
/*
The Loader3D class is initiated in the declarations block.
The .awd files is an Away3D file format.
*/
_loader.load(new URLRequest('vase.awd'));
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
//------------------------------------------------------
private function onResourceComplete(ev : LoaderEvent) : void
{
_loader.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete);
_loader.removeEventListener(LoaderEvent.LOAD_ERROR, onLoadError);
/*
Add the loaded mesh to the View3D display3D list
*/
awayview.view.scene.addChild(_loader);
}
//------------------------------------------------------
private function onLoadError(ev : LoaderEvent) : void
{
trace('Could not find', ev.url);
_loader.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete);
_loader.removeEventListener(LoaderEvent.LOAD_ERROR, onLoadError);
_loader = null;
}
//------------------------------------------------------
private function onEnterFrame(ev : Event) : void
{
/*
Rotate the loaded mesh based on the mouse position
*/
_loader.rotationY = stage.mouseX - stage.stageWidth/2;
/*
Move the camera up and down based on the mouse position
*/
awayview.camera.y = 5 * (stage.mouseY - stage.stageHeight/2);
awayview.camera.lookAt(_loader.position);
}
]]>
</fx:Script>
<s:HGroup
width="100%"
height="100%"
>
<a3d:AwayView
id="awayview"
width="100%"
height="100%"
/>
<s:VGroup
id="flexGroup"
width="100%"
height="100%"
horizontalAlign="center"
paddingTop="10"
>
<s:Image
source="{new flexLogo()}"
width="50%"
height="50%"
/>
<s:Image
source="{new a3dLogo()}"
width="100%"
height="100%"
/>
</s:VGroup>
</s:HGroup>
<fx:Declarations>
<loaders:Loader3D
id="_loader"
resourceComplete="onResourceComplete(event)"
loadError="onLoadError(event)"
/>
</fx:Declarations>
</s:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:UIComponent
xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%"
height="100%"
>
<mx:Script>
<![CDATA[
import away3d.cameras.Camera3D;
import away3d.containers.View3D;
protected var _view:View3D;
public function get view():View3D { return _view; };
public function get camera():Camera3D { return view.camera; };
protected var _autoRender:Boolean = true;
public function get autoRender():Boolean { return _autoRender; };
public function set autoRender(v:Boolean):void
{
_autoRender = v;
if(view.stage)
{
view.removeEventListener(Event.ENTER_FRAME, view_enterFrameHandler);
if(_autoRender)
view.addEventListener(Event.ENTER_FRAME, view_enterFrameHandler);
}
};
override protected function createChildren():void
{
super.createChildren();
_view = new View3D();
view.antiAlias = 4;
view.backgroundColor = 0xe5e5e5;
this.addChild(view);
view.addEventListener(Event.ADDED_TO_STAGE, update);
if(autoRender)
view.addEventListener(Event.ENTER_FRAME, view_enterFrameHandler);
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
update();
}
private function update(e:* = null):void
{
if(view.stage)
{
view.width = unscaledWidth;
view.height = unscaledHeight;
}
}
protected function view_enterFrameHandler(event:Event):void
{
if ( (view.width > 0 && view.height > 0) && autoRender )
{
view.render();
}
}
]]>
</mx:Script>
</mx:UIComponent>
@xnpeng
Copy link

xnpeng commented Aug 11, 2012

cannot run in flex 4.6 + away3d 4.
Error: Error #2030: 遇到文件尾。
at flash.utils::ByteArray/readBytes()
at away3d.loaders.parsers::AWD2Parser/parseTexture()[C:\workspace\flexspaces\Away3D\src\away3d\loaders\parsers\AWD2Parser.as:458]
at away3d.loaders.parsers::AWD2Parser/parseNextBlock()[C:\workspace\flexspaces\Away3D\src\away3d\loaders\parsers\AWD2Parser.as:276]
at away3d.loaders.parsers::AWD2Parser/proceedParsing()[C:\workspace\flexspaces\Away3D\src\away3d\loaders\parsers\AWD2Parser.as:215]
at away3d.loaders.parsers::ParserBase/onInterval()[C:\workspace\flexspaces\Away3D\src\away3d\loaders\parsers\ParserBase.as:420]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment