In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Away3D basic setup

the most simple setup for away3d
Get Adobe Flash player
by Loth2012 20 Oct 2012
    Embed
/**
 * Copyright Loth2012 ( http://wonderfl.net/user/Loth2012 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dm2K
 */

package {
    import away3d.containers.View3D;
    import away3d.entities.Mesh;
    import away3d.materials.ColorMaterial;
    import away3d.primitives.PlaneGeometry;
    import away3d.primitives.CubeGeometry;
    import away3d.primitives.SphereGeometry;
    import away3d.lights.DirectionalLight;
    import away3d.materials.lightpickers.StaticLightPicker;
    import away3d.lights.shadowmaps.NearDirectionalShadowMapper;
    import away3d.materials.methods.FilteredShadowMapMethod;
    import away3d.materials.methods.NearShadowMapMethod;
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Vector3D;
    
    public class away3dRoots extends Sprite {
        //engine variables
        private var _view:View3D;
        private var _lightPicker:StaticLightPicker;
        private var _shadowMethod:NearShadowMapMethod;
        //scene objects
        private var _plane:Mesh;
        private var _cube:Mesh;
        private var _sphere:Mesh;
        //garbage 
        private var _mesh:Vector.<Mesh>;
        
        public function away3dRoots() {
            
            stage.align = 'TL';
            stage.scaleMode = 'noScale';
            stage.quality = 'low';

            //setup the view
            _view = new View3D();
            addChild(_view);

            //setup the camera
            _view.camera.z = -1000;
            _view.camera.y = 500;
            _view.camera.lookAt(new Vector3D(0,200,0));
            
            //setup the light
            var sun:DirectionalLight = new DirectionalLight( -1, -1, 1);
            sun.castsShadows = true;
            sun.shadowMapper = new NearDirectionalShadowMapper(0.5);
            sun.ambient = 0.2 ;
            _lightPicker = new StaticLightPicker([sun]);
            _view.scene.addChild(sun);
            //setup the effect 
            _shadowMethod = new NearShadowMapMethod(new FilteredShadowMapMethod(sun), 0.05);
            _shadowMethod.epsilon = .0007;

            //setup the scene object
            _plane = new Mesh(new PlaneGeometry(1000, 1000), new ColorMaterial(0x505050));
            _cube = new Mesh(new CubeGeometry(100,100,100), new ColorMaterial(0x00FF00));
            _sphere = new Mesh(new SphereGeometry(50), new ColorMaterial(0x0000FF));
            
            _cube.position = new Vector3D(-200,50,0);
            _sphere.position = new Vector3D(200,50,0);
            
            //add to reference vector
            _mesh = new Vector.<Mesh>();
            _mesh.push(_plane, _cube, _sphere);
            
            //apply light and shadow to object and add to scene
            for (var i:int; i< _mesh.length; i++){
                _mesh[i].material.lightPicker = _lightPicker;
                ColorMaterial(_mesh[i].material).shadowMethod = _shadowMethod;
                //ShowTime
                _view.scene.addChild( _mesh[i]);
            }

            //setup the render loop
            addEventListener(Event.ENTER_FRAME, _onEnterFrame);
            stage.addEventListener(Event.RESIZE, onResize);
            onResize();
            
            //you can easely clone object without impact the gpu
            for ( i=0; i<100; i++){
                var nCube:Mesh = Mesh(_cube.clone());
                var nSphere:Mesh = Mesh(_sphere.clone());
                nCube.y = nSphere.y = 120*i;
                _view.scene.addChild( nCube);
                _view.scene.addChild( nSphere);
                }
        }
        
        /** render loop */
        private function _onEnterFrame(e:Event):void
        {
            _plane.rotationY += 1;
            _view.render();
        }

        /** stage listener for resize events */
        private function onResize(event:Event = null):void
        {
            _view.width = stage.stageWidth;
            _view.height = stage.stageHeight;
        }
        
        // that all :)
    }
}