home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Templates / Flash / flashmo_097_3d_sphere / org / papervision3d / materials / BitmapFileMaterial.as < prev    next >
Text File  |  2007-07-19  |  8KB  |  290 lines

  1. /*
  2.  *  PAPER    ON   ERVIS  NPAPER ISION  PE  IS ON  PERVI IO  APER  SI  PA
  3.  *  AP  VI  ONPA  RV  IO PA     SI  PA ER  SI NP PE     ON AP  VI ION AP
  4.  *  PERVI  ON  PE VISIO  APER   IONPA  RV  IO PA  RVIS  NP PE  IS ONPAPE
  5.  *  ER     NPAPER IS     PE     ON  PE  ISIO  AP     IO PA ER  SI NP PER
  6.  *  RV     PA  RV SI     ERVISI NP  ER   IO   PE VISIO  AP  VISI  PA  RV3D
  7.  *  ______________________________________________________________________
  8.  *  papervision3d.org ∩┐╜ blog.papervision3d.org ∩┐╜ osflash.org/papervision3d
  9.  */
  10.  
  11. /*
  12.  * Copyright 2006-2007 (c) Carlos Ulloa Matesanz, noventaynueve.com.
  13.  *
  14.  * Permission is hereby granted, free of charge, to any person
  15.  * obtaining a copy of this software and associated documentation
  16.  * files (the "Software"), to deal in the Software without
  17.  * restriction, including without limitation the rights to use,
  18.  * copy, modify, merge, publish, distribute, sublicense, and/or sell
  19.  * copies of the Software, and to permit persons to whom the
  20.  * Software is furnished to do so, subject to the following
  21.  * conditions:
  22.  *
  23.  * The above copyright notice and this permission notice shall be
  24.  * included in all copies or substantial portions of the Software.
  25.  *
  26.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  28.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  30.  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  31.  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  32.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  33.  * OTHER DEALINGS IN THE SOFTWARE.
  34.  */
  35.  
  36. //  ______________________________________________________________________ BITMAP FILE MATERIAL
  37.  
  38. package org.papervision3d.materials
  39. {
  40. import flash.display.Bitmap;
  41. import flash.display.BitmapData;
  42. import flash.display.Graphics;
  43. import flash.display.Loader;
  44. import flash.events.*;
  45. import flash.net.URLRequest;
  46. import flash.utils.Dictionary;
  47.  
  48. import org.papervision3d.Papervision3D;
  49. import org.papervision3d.core.geom.Face3D;
  50. import org.papervision3d.core.geom.Vertex2D;
  51. import org.papervision3d.core.proto.MaterialObject3D;
  52. import org.papervision3d.events.FileLoadEvent;
  53. import org.papervision3d.objects.DisplayObject3D;
  54.  
  55. /**
  56. * The BitmapFileMaterial class creates a texture by loading a bitmap from an external file.
  57. *
  58. * Materials collect data about how objects appear when rendered.
  59. */
  60. public class BitmapFileMaterial extends BitmapMaterial
  61. {
  62.     // ___________________________________________________________________ PUBLIC
  63.  
  64.     /**
  65.     * The URL that has been requested.
  66.     */
  67.     public var url :String = "";
  68.  
  69.     /**
  70.     * Whether or not the texture has been loaded.
  71.     */
  72.     public var loaded :Boolean;
  73.  
  74.     /**
  75.     * Function to call when the last image has loaded.
  76.     */
  77.     static public var callback :Function;
  78.  
  79.     /**
  80.     * The color to use in materials before loading has finished.
  81.     */
  82.     static public var LOADING_COLOR :int = MaterialObject3D.DEFAULT_COLOR;
  83.  
  84.     // ___________________________________________________________________ NEW
  85.  
  86.     /**
  87.     * The BitmapFileMaterial class creates a texture by loading a bitmap from an external file.
  88.     *
  89.     * @param    url                    The URL of the requested bitmap file.
  90.     * @param    initObject            [optional] - An object that contains additional properties with which to populate the newly created material.
  91.     */
  92.     public function BitmapFileMaterial( url :String, initObject :Object=null )
  93.     {
  94.         super( url, initObject );
  95.  
  96.         this.url = url;
  97.  
  98.         this.loaded = false;
  99.  
  100.         // Loading color
  101.         this.fillAlpha = 1;
  102.         this.fillColor = LOADING_COLOR;
  103.     }
  104.  
  105.     // ___________________________________________________________________ CREATE BITMAP
  106.  
  107.     /**
  108.     * [internal-use]
  109.     *
  110.     * @param    asset
  111.     * @return
  112.     */
  113.     protected override function createBitmap( asset:* ):BitmapData
  114.     {
  115.         // Empy string?
  116.         if( asset == "" )
  117.         {
  118.             return null;
  119.         }
  120.         // Already loaded?
  121.         else if( _loadedBitmaps[ asset ] )
  122.         {
  123.             var bitmap:BitmapData = _loadedBitmaps[ asset ];
  124.  
  125.             this.loadComplete();
  126.  
  127.             return bitmap;
  128.         }
  129.         else
  130.         {
  131.             queueBitmap( asset );
  132.         }
  133.     
  134.         return null;
  135.     }
  136.  
  137.     // ___________________________________________________________________ QUEUE BITMAP
  138.  
  139.     private function queueBitmap( file:String ):void
  140.     {
  141.         // New filename?
  142.         if( ! _subscribedMaterials[ file ] )
  143.         {
  144.             // Queue file
  145.             _waitingBitmaps.push( file );
  146.  
  147.             // Init subscription
  148.             _subscribedMaterials[ file ] = new Array();
  149.         }
  150.  
  151.         // Subscribe material
  152.         _subscribedMaterials[ file ].push( this );
  153.         
  154.         // Launch loading if needed
  155.         if( _loadingIdle )
  156.             loadNextBitmap();
  157.     }
  158.  
  159.     // ___________________________________________________________________ LOAD NEXT BITMAP
  160.  
  161.     private function loadNextBitmap():void
  162.     {
  163.         // Retrieve next filename in queue
  164.         var file:String = _waitingBitmaps[0];
  165.  
  166.         var request:URLRequest = new URLRequest( file );
  167.         var bitmapLoader:Loader = new Loader();
  168.         
  169.         bitmapLoader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, loadBitmapProgressHandler );
  170.         bitmapLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, loadBitmapCompleteHandler );
  171.  
  172.         try
  173.         {
  174.             // Load bitmap
  175.             bitmapLoader.load( request );
  176.  
  177.             // Save original url
  178.             _loaderUrls[ bitmapLoader ] = file;
  179.  
  180.             // Busy loading
  181.             _loadingIdle = false;
  182.  
  183.             Papervision3D.log( "BitmapFileMaterial: Loading bitmap from " + file );
  184.         }
  185.         catch( error:Error )
  186.         {
  187.             // Remove from queue
  188.             _waitingBitmaps.shift();
  189.  
  190.             // Loading finished
  191.             _loadingIdle = true;
  192.  
  193.             Papervision3D.log( "[ERROR] BitmapFileMaterial: Unable to load file " + error.message );
  194.         }
  195.     }
  196.     
  197.     // ___________________________________________________________________ LOAD BITMAP PROGRESS HANDLER
  198.  
  199.     private function loadBitmapProgressHandler( e:ProgressEvent ):void
  200.     {
  201.         var progressEvent:FileLoadEvent = new FileLoadEvent( FileLoadEvent.LOAD_PROGRESS, url, e.bytesLoaded, e.bytesTotal);
  202.         dispatchEvent( progressEvent );
  203.     }
  204.  
  205.     // ___________________________________________________________________ LOAD BITMAP COMPLETE HANDLER
  206.  
  207.     private function loadBitmapCompleteHandler( e:Event ):void
  208.     {
  209.         var loader:Loader = Loader( e.target.loader );
  210.         var loadedBitmap:Bitmap = Bitmap( loader.content );
  211.  
  212.         // Retrieve original url
  213.         var url:String = _loaderUrls[ loader ];
  214.  
  215.         // Retrieve loaded bitmapdata
  216.         var bitmap:BitmapData = correctBitmap( loadedBitmap.bitmapData, true );
  217.  
  218.         // Update subscribed materials
  219.         for each( var material:BitmapFileMaterial in _subscribedMaterials[ url ] )
  220.         {
  221.             material.bitmap = bitmap;
  222.             material.maxU = this.maxU;
  223.             material.maxV = this.maxV;
  224.             material.loadComplete();
  225.         }
  226.  
  227.         // Include in library
  228.         _loadedBitmaps[ url ] = bitmap;
  229.  
  230.         // Remove from queue
  231.         _waitingBitmaps.shift();
  232.  
  233.         // Queue finished?
  234.         if( _waitingBitmaps.length > 0 )
  235.         {
  236.             // Continue loading
  237.             loadNextBitmap();
  238.         }
  239.         else
  240.         {
  241.             // Loading finished
  242.             _loadingIdle = true;
  243.             
  244.             if( Boolean( callback ) ) callback();
  245.         }
  246.     }
  247.  
  248.     // ___________________________________________________________________ LOAD COMPLETE
  249.  
  250.     private function loadComplete():void
  251.     {
  252.         this.fillAlpha = 0;
  253.         this.fillColor = 0;
  254.         this.loaded = true;
  255.  
  256.         // Dispatch event
  257.         var fileEvent:FileLoadEvent = new FileLoadEvent( FileLoadEvent.LOAD_COMPLETE, this.url );
  258.         this.dispatchEvent( fileEvent );
  259.     }
  260.     
  261.     /**
  262.      *  drawFace3D
  263.      */
  264.     override public function drawFace3D(instance:DisplayObject3D, face3D:Face3D, graphics:Graphics, v0:Vertex2D, v1:Vertex2D, v2:Vertex2D):int
  265.     {
  266.         if(bitmap == null) return 1;
  267.         bitmap.lock();
  268.         var i:int = super.drawFace3D(instance, face3D, graphics, v0, v1, v2);
  269.         bitmap.unlock();
  270.         return i;        
  271.     }
  272.  
  273.     // ___________________________________________________________________ PRIVATE
  274.  
  275.     // Filenames in the queue
  276.     static private var _waitingBitmaps :Array = new Array();
  277.  
  278.     // URLs per loader
  279.     static private var _loaderUrls :Dictionary = new Dictionary();
  280.  
  281.     // Loaded bitmap library
  282.     static private var _loadedBitmaps :Object = new Object();
  283.  
  284.     // Materials subscribed  to the loading queue
  285.     static private var _subscribedMaterials :Object = new Object();
  286.  
  287.     // Loading status
  288.     static private var _loadingIdle :Boolean = true;
  289. }
  290. }