Saturday, 15 February 2014

Bitmap - 3D Mesh - Sprite3D --> Away3D

Depending of your scene sometimes require just of billboards or shapes without geometries known as Sprite3D in away3D <what it could be a transparency material in C4D>, do not have mouse event enabled they are considerer as low pol meshes and their use is just limitated as flat image, hence they do not create shadows by the reflection of light( e.g. directionalLight ) but the can be affected by light themselves, the other option is to add a plane geometry and dress it with the material wished, it has mouse event enable and can be handle as a 3D mesh, (see how to add 3d model with away3D), also can create shadow by the reflection of light. Otherwise just adding a bitmap to a movieclip it could be an option its use is known as a flash display object  and of course have to be treated as 2D. The idea is to know to deal with these to set up the best your scene that include a beautiful and fast render of course.
Here's the code:

package code {
   
 import away3d.cameras.Camera3D;
 import away3d.containers.View3D;
 import away3d.entities.Mesh;
 import away3d.events.MouseEvent3D;
 import away3d.events.LoaderEvent;
 import away3d.materials.TextureMaterial;
 import away3d.primitives.SphereGeometry;
 import away3d.textures.BitmapTexture;
 import away3d.textures.Texture2DBase;
 import away3d.textures.VideoTexture;
 import away3d.tools.helpers.MeshHelper;
 import away3d.utils.*;
 import away3d.primitives.PlaneGeometry;
 import away3d.entities.Sprite3D;

 import flash.display.Sprite;
 import flash.display3D.textures.Texture;
 import flash.events.Event;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.events.MouseEvent;
 import flash.display.MovieClip;
 import flash.text.TextField;
 import flash.display.BitmapData;
 import flash.display.Bitmap;
 import away3d.core.pick.*;

 [SWF(width="1000", height="600", frameRate="60", backgroundColor="#000000")]
   
    public class main extends Sprite {
        private var _view:View3D;
        [Embed(source="/embeds/tree.png")]
        private var tree:Class;
        private const WIDTH:Number = 1000;
        private const HEIGHT:Number= 600;
        private var ms:Mesh;
        private var lbls:Array= new Array("Bitmap","3D plane","Sprite3D");
       
       public function main() {
            // constructor code
            super();
           if (stage) initialize();
            else addEventListener(Event.ADDED_TO_STAGE, initialize);
        }
       
         private function initialize(evt:Event = null):void
        {
          removeEventListener( Event.ADDED_TO_STAGE, initialize );
            turnon();
        }
       
        private function turnon():void{
            initStage();
            initEngine();
            setUp();
            addEventListener( Event.ENTER_FRAME, enterframeHandler );
        }
        private function initStage():void {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;
           
        }
        private function initEngine():void
        {
            _view = new View3D();
            _view.backgroundColor = 0x333333;
            _view.width = WIDTH;
            _view.height = HEIGHT;
            _view.antiAlias = 4;
            _view.camera.lens.far = 15000;
            _view.addSourceURL( "srcview/index.html" );
            _view.forceMouseMove = true;
            _view.camera.x=-5000.76;
            _view.camera.y = -700;
            _view.camera.z =-10026.991;
            _view.camera.rotationX=4.5 ;
           
            addChild( _view );
           
        }
       
        private function enterframeHandler( event:Event ):void {
            update();
        }
        private function update():void {
            _view.render();
        }
        private function setUp():void{
            //Bitmap - MC
            var btm:Bitmap= new Bitmap(Cast.bitmapData(tree));
            btm.x=-100;
            btm.y=-50;
            var mc:MovieClip = new MovieClip();
            mc.addChild(btm);
            addChild(mc);
            mc.addEventListener(MouseEvent.CLICK,ClickMeMC);
            //Handle as movieclip
            //Mesh
             var txture:BitmapTexture = new BitmapTexture(Cast.bitmapData(tree));
             var material:TextureMaterial = new TextureMaterial(txture,true, true, true);
                  material.alphaThreshold = 0.5;
                  material.mipmap = false;
                  material.repeat = false;
                var lovelyTree:PlaneGeometry = new PlaneGeometry(10000, 10000, 1, 1, false, false);
                ms = new Mesh(lovelyTree, material);
                ms.x =  -5000;
                //ms.y = -100;
                //ms.rotationY=90;
                _view.scene.addChild(ms);
                ms.pickingCollider = PickingColliderType.AS3_FIRST_ENCOUNTERED;
                ms.mouseEnabled = true;
                ms.addEventListener(MouseEvent3D.MOUSE_DOWN,MouseDownMesh);
            //Supports Mouse Events 3D,carefull handling to get appereance wanted, you can also
            // add your own 3D mesh
            //made it in the 3D software of your preferance, see how to add 3d model with away3D
           
           
          //Sprite3D
            var TreeSprite3D:Sprite3D = new Sprite3D(material,10000,10400);
            TreeSprite3D.x =  1400;
            _view.scene.addChild(TreeSprite3D);
            TreeSprite3D.setCam(_view.camera);
            //Doesn't support mouse events
           
            for(var i:uint=0;i<lbls.length;i++){
                var MC:MovieClip = new MovieClip();
                var txt:TextField = new TextField();
                txt.htmlText="<p> <font color='#CCCCCC' face='Verdana, Geneva, sans-serif' size='16'>"+lbls[i]+"</font></p>"
                MC.addChild(txt);
                MC.x =(i*330)+120;
                MC.y = 430;
                addChild(MC);
            }
           
        }
       
        private function ClickMeMC(evt:Event):void{
            trace("you clicked on bitmap");
        }
        private function MouseDownMesh(evt:MouseEvent3D):void{
            trace("you clicked on Mesh");
        }
    }
   
}



 

Tuesday, 11 February 2014

Adding 3d model --> Away3D

Loading a 3D mesh made with the 3d software of your preference in as3 with away3d libraries using AssetLibrary (inside away3d.library package) you can use loader3d as well (inside away3d.loaders package).There's a parser class for some 3d file format as AC3DParser, AWD1Parser, DAEParser, OBJParser,between others, see parsers for more file formats availables.

code:

package code {
   
 import away3d.cameras.Camera3D;
 import away3d.containers.View3D;
 import away3d.events.LoaderEvent;
 import away3d.utils.*;
 import away3d.library.*;
 import away3d.library.assets.*;
 import away3d.events.AssetEvent;
 import away3d.loaders.misc.AssetLoaderContext;
 import away3d.loaders.parsers.*;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.net.URLRequest;
 import away3d.entities.Mesh;
 import away3d.textures.BitmapTexture;
 import away3d.materials.TextureMaterial;
 import away3d.tools.utils.Bounds;
 import away3d.tools.helpers.MeshHelper;
 import flash.display.BitmapData;

 [SWF(width="1000", height="600", frameRate="60", backgroundColor="#000000")]
   
    public class model extends Sprite {
        private var _view:View3D;
        private const WIDTH:Number = 1000;
        private const HEIGHT:Number= 600;
        private var sphere : Mesh;
        private var txture : BitmapTexture = new BitmapTexture(new BitmapData(512, 512, false, 0xCC0000));
        private var sphereMaterial : TextureMaterial;
       
       public function model() {
            // constructor code
            super();
           if (stage) initialize();
            else addEventListener(Event.ADDED_TO_STAGE, initialize);
        }
       
         private function initialize(evt:Event = null):void
        {
          removeEventListener( Event.ADDED_TO_STAGE, initialize );
            turnon();
        }
       
        private function turnon():void{
            initStage();
            initEngine();
            setUp3DModel();
            addEventListener( Event.ENTER_FRAME, enterframeHandler );
        }
        private function initStage():void {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;
           
           
        }
        private function initEngine():void
        {
            _view = new View3D();
            _view.backgroundColor = 0x333333;
            _view.width = WIDTH;
            _view.height = HEIGHT;
            _view.antiAlias = 4;
            _view.camera.lens.far = 15000;
            _view.addSourceURL( "srcview/index.html" );
            _view.forceMouseMove = true;
            _view.camera.x=-5000.76;
            _view.camera.y = -700;
            _view.camera.z =-10026.991;
            _view.camera.rotationX=4.5 ;
           
            addChild( _view );
           
        }
       
        private function enterframeHandler( event:Event ):void {
            update();
        }
        private function update():void {
            _view.render();
        }
        private function setUp3DModel():void{
          //With AssetLibrary

            AssetLibrary.enableParser(Max3DSParser);/*See for the rest of parsers http://away3d.com/livedocs/away3d/4.0/away3d/loaders/parsers/package-detail.html*/
            AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
            AssetLibrary.load(new URLRequest("embeds/Sphere.3ds"),new AssetLoaderContext(false));
           
         // With Loader3D

           var loader3d:Loader3D = new Loader3D(false);
           var context:AssetLoaderContext = new AssetLoaderContext(false);
            loader3d.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete, false, 0, true);
            loader3d.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete, false, 0, true);
            loader3d.load(new URLRequest("embeds/Sphere.3ds"),context, null, new Max3DSParser());

        }
        private function onAssetComplete(event:AssetEvent):void
        {
            if (event.asset.assetType == AssetType.MESH) {
                sphere = event.asset as Mesh;
                sphereMaterial = new TextureMaterial(txture);
                sphere.material = sphereMaterial;
                           
                _view.scene.addChild(sphere);
                sphere.x= -5000;
                sphere.y=-800;
                sphere.z=-9000
                sphere.scale(6);
               
                //To obtain bounds of the 3d file
                Bounds.getMeshBounds(sphere);//Retrieve object bounds
                var depth:Number = Bounds.depth;
                var width:Number = Bounds.width;
                var height:Number = Bounds.height;
                trace("depth "+depth +" width "+width+" height "+height);
            }
         
        }
    
    }
   
}
       

Saturday, 1 February 2014

Simple MenuBar - Down List ->Jquery

 Simple MenuBar - Down List, slides on mouseenter, hides on mouseleave.

JS.js file:
(function ($) {
    $.fn.MenuBar = function(options) {
            var options = $.extend(variables, options);
            var variables = {// default variable
                speed : 2000
              }
            var $this    = $(this);
            var opts    = options;
            $this.children("ul").addClass("menudown");
            var list    = $this.find("ul.menudown");// all div
            $this.find("ul.menudown>li:first-child").addClass("firstchild");
            var firstchild= list.find(".firstchild");// first child   
            $this.find("ul.menudown>li").not($this.find("ul.menudown>li:first-child")).addClass("restchildren");           
            var restchildren    = list.find(".restchildren");// rest children

            /* If mouse is entered on first child slide rest children, bind mouseleave event for all div*/ 

                firstchild.mouseenter(function() {
                    restchildren.animate({
                       height:restchildren.find("ul").height()+8,
                      },opts.speed, "linear");
           
                     $this.bind({
             * If mouseleave on rest children hide rest children, unbind mouseout for all div*/

                        mouseleave: function(event) {
                             restchildren.animate({
                              height:0,
                             },opts.speed, "linear");
                             $this.unbind(event)
                         }
                    });
                    event.preventDefault();
                   
                   })
                 };
})(jQuery);

CSS.css file:
@charset "utf-8";
body {font-family:"Arial Black", Gadget, sans-serif; font-size:14px;width:60%;margin:0 auto;background-color: #CCC;}
#menubar a {font-family:Verdana, Geneva, sans-serif;font-size:100%;font-weight:bold;text-decoration:none;}
#menubar {position: absolute;left:46px;top: 0px;width:150px;height:58px;overflow:visible;opacity:0.7;filter:alpha(opacity=70);}
#menubar .menudown {position:relative;list-style-type:none;display:block;margin:0;padding:0;background:transparent;color:#fff;cursor:pointer;}
#menubar .restchildren ul {width:auto;height:auto;list-style-type:none;margin:0;padding:0;background:#fff;}
#menubar .restchildren ul li {display:block;padding:0;margin:0;background: #000000; }
#menubar .restchildren ul li a:hover {background:#C0C;color:#fff; border:double}
#menubar .restchildren {width:100%;border:none;position: absolute;height:0;overflow:hidden;margin-top: 0;margin-bottom: 0;}
#menubar .firstchild{width:auto;padding:0; }
#menubar .firstchild a{padding:10px 8px;display:block;color:#fff; background-color:#F06 ; border:1px solid #a1a1a1;
border-radius:5px;}/*some radius border for first child */
#menubar .menudown ul li a {width:auto;text-decoration:none;color:#fff;display:block;margin:0;padding:10px 8px;}

HTML.html file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link type="text/css" rel="stylesheet" media="all" href="CSS.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="JS.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#menubar").MenuBar();/*Calling the function throu ID div*/
});
</script>
</head>
<body>
<div id="menubar">
    <ul>
        <li><a href="http://bellacarta.free.fr" target="_blank">Gender</a></li>
        <li>
            <ul>
                <li><a href="http://en.wikipedia.org/wiki/Male" target="_blank">Male</a></li>
                <li><a href="http://en.wikipedia.org/wiki/Female" target="_blank">Female</a></li>
            </ul>
        </li>
    </ul>
</div>
</body>
</html>

the result:

Menu bar- jquery