home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Templates / Flash / flashmo_101_3d_carousel / org / papervision3d / materials / BitmapMaterial.as < prev    next >
Text File  |  2007-07-19  |  10KB  |  362 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 (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 MATERIAL
  37.  
  38. package org.papervision3d.materials
  39. {
  40. import flash.display.BitmapData;
  41. import flash.geom.Rectangle;
  42. import flash.geom.Point;
  43. import flash.geom.Matrix;
  44.  
  45. import org.papervision3d.core.proto.MaterialObject3D;
  46. import org.papervision3d.Papervision3D;
  47. import org.papervision3d.core.draw.IFaceDrawer;
  48. import org.papervision3d.core.geom.Face3D;
  49. import flash.display.Graphics;
  50. import org.papervision3d.core.geom.Vertex2D;
  51. import flash.utils.Dictionary;
  52. import org.papervision3d.objects.DisplayObject3D;
  53.  
  54. /**
  55. * The BitmapMaterial class creates a texture from a BitmapData object.
  56. *
  57. * Materials collect data about how objects appear when rendered.
  58. *
  59. */
  60. public class BitmapMaterial extends MaterialObject3D implements IFaceDrawer
  61. {
  62.     /**
  63.      * Indicates if mip mapping is forced.
  64.      */
  65.     static public var AUTO_MIP_MAPPING :Boolean = true;
  66.  
  67.     /**
  68.      * Levels of mip mapping to force.
  69.      */
  70.     static public var MIP_MAP_DEPTH :Number = 8;
  71.  
  72.     // ______________________________________________________________________ TEXTURE
  73.  
  74.     /**
  75.     * A texture object.
  76.     */
  77.     public function get texture():*
  78.     {
  79.         return this._texture;
  80.     }
  81.  
  82.     public function set texture( asset:* ):void
  83.     {
  84.         this.bitmap   = createBitmap( asset );
  85.         this._texture = asset;
  86.     }
  87.  
  88.     public var uvMatrices:Dictionary;
  89.     
  90.     
  91.     // ______________________________________________________________________ NEW
  92.  
  93.     /**
  94.     * The BitmapMaterial class creates a texture from a BitmapData object.
  95.     *
  96.     * @param    asset                A BitmapData object.
  97.     * @param    initObject            [optional] - An object that contains additional properties with which to populate the newly created material.
  98.     */
  99.     public function BitmapMaterial( asset :*, initObject :Object=null )
  100.     {
  101.         super( initObject );
  102.  
  103.         texture = asset;
  104.         init();
  105.     }
  106.     
  107.     private function init():void
  108.     {
  109.         uvMatrices = new Dictionary();
  110.     }
  111.     
  112.     /**
  113.      *  drawFace3D
  114.      */
  115.     override public function drawFace3D(instance:DisplayObject3D, face3D:Face3D, graphics:Graphics, v0:Vertex2D, v1:Vertex2D, v2:Vertex2D):int
  116.     {
  117.         if(bitmap){
  118.             var map:Matrix = (uvMatrices[face3D] || transformUV(face3D, instance));
  119.             
  120.             var x0:Number = v0.x;
  121.             var y0:Number = v0.y;
  122.             var x1:Number = v1.x;
  123.             var y1:Number = v1.y;
  124.             var x2:Number = v2.x;
  125.             var y2:Number = v2.y;
  126.             
  127.             _triMatrix.a = x1 - x0;
  128.             _triMatrix.b = y1 - y0;
  129.             _triMatrix.c = x2 - x0;
  130.             _triMatrix.d = y2 - y0;
  131.             _triMatrix.tx = x0;
  132.             _triMatrix.ty = y0;
  133.                 
  134.             _localMatrix.a = map.a;
  135.             _localMatrix.b = map.b;
  136.             _localMatrix.c = map.c;
  137.             _localMatrix.d = map.d;
  138.             _localMatrix.tx = map.tx;
  139.             _localMatrix.ty = map.ty;
  140.             _localMatrix.concat(_triMatrix);
  141.             
  142.             graphics.beginBitmapFill( bitmap, _localMatrix, true, smooth);
  143.             graphics.moveTo( x0, y0 );
  144.             graphics.lineTo( x1, y1 );
  145.             graphics.lineTo( x2, y2 );
  146.             graphics.lineTo( x0, y0 );
  147.             graphics.endFill();
  148.         }
  149.         return 1;
  150.     }
  151.     
  152.     /**
  153.     * Applies the updated UV texture mapping values to the triangle. This is required to speed up rendering.
  154.     *
  155.     */
  156.     public function transformUV(face3D:Face3D, instance:DisplayObject3D=null):Matrix
  157.     {
  158.         if( ! face3D.uv )
  159.         {
  160.             Papervision3D.log( "MaterialObject3D: transformUV() uv not found!" );
  161.         }
  162.         else if( bitmap )
  163.         {
  164.             var uv :Array  = face3D.uv;
  165.  
  166.             var w  :Number = bitmap.width * maxU;
  167.             var h  :Number = bitmap.height * maxV;
  168.  
  169.             var u0 :Number = w * uv[0].u;
  170.             var v0 :Number = h * ( 1 - uv[0].v );
  171.             var u1 :Number = w * uv[1].u;
  172.             var v1 :Number = h * ( 1 - uv[1].v );
  173.             var u2 :Number = w * uv[2].u;
  174.             var v2 :Number = h * ( 1 - uv[2].v );
  175.  
  176.             // Fix perpendicular projections
  177.             if( (u0 == u1 && v0 == v1) || (u0 == u2 && v0 == v2) )
  178.             {
  179.                 u0 -= (u0 > 0.05)? 0.05 : -0.05;
  180.                 v0 -= (v0 > 0.07)? 0.07 : -0.07;
  181.             }
  182.  
  183.             if( u2 == u1 && v2 == v1 )
  184.             {
  185.                 u2 -= (u2 > 0.05)? 0.04 : -0.04;
  186.                 v2 -= (v2 > 0.06)? 0.06 : -0.06;
  187.             }
  188.  
  189.             // Precalculate matrix & correct for mip mapping
  190.             var at :Number = ( u1 - u0 );
  191.             var bt :Number = ( v1 - v0 );
  192.             var ct :Number = ( u2 - u0 );
  193.             var dt :Number = ( v2 - v0 );
  194.  
  195.             var m :Matrix = new Matrix( at, bt, ct, dt, u0, v0 );
  196.             m.invert();
  197.  
  198.             var mapping:Matrix = uvMatrices[face3D] || (uvMatrices[face3D] = m.clone() );
  199.             mapping.a  = m.a;
  200.             mapping.b  = m.b;
  201.             mapping.c  = m.c;
  202.             mapping.d  = m.d;
  203.             mapping.tx = m.tx;
  204.             mapping.ty = m.ty;
  205.         }
  206.         else Papervision3D.log( "MaterialObject3D: transformUV() material.bitmap not found!" );
  207.  
  208.         return mapping;
  209.     }
  210.     
  211.     // ______________________________________________________________________ TO STRING
  212.  
  213.     /**
  214.     * Returns a string value representing the material properties in the specified BitmapMaterial object.
  215.     *
  216.     * @return    A string.
  217.     */
  218.     public override function toString(): String
  219.     {
  220.         return 'Texture:' + this.texture + ' lineColor:' + this.lineColor + ' lineAlpha:' + this.lineAlpha;
  221.     }
  222.  
  223.  
  224.     // ______________________________________________________________________ CREATE BITMAP
  225.  
  226.     protected function createBitmap( asset:* ):BitmapData
  227.     {
  228.         return correctBitmap( asset, false );
  229.     }
  230.  
  231.  
  232.     // ______________________________________________________________________ CORRECT BITMAP FOR MIP MAPPING
  233.  
  234.     public function correctBitmap( bitmap :BitmapData, dispose :Boolean ):BitmapData
  235.     {
  236.         var okBitmap :BitmapData;
  237.  
  238.         var levels :Number = 1 << MIP_MAP_DEPTH;
  239.         var width  :Number = levels * Math.ceil( bitmap.width  / levels );
  240.         var height :Number = levels * Math.ceil( bitmap.height / levels );
  241.  
  242.         // Check for BitmapData maximum size
  243.         var ok:Boolean = true;
  244.  
  245.         if( width  > 2880 )
  246.         {
  247.             width  = bitmap.width;
  248.             ok = false;
  249.         }
  250.  
  251.         if( height > 2880 )
  252.         {
  253.             height = bitmap.height;
  254.             ok = false;
  255.         }
  256.         
  257.         if( ! ok ) Papervision3D.log( "Material " + this.name + ": Texture too big for mip mapping. Resizing recommended for better performance and quality." );
  258.  
  259.         // Create new bitmap?
  260.         if( AUTO_MIP_MAPPING && bitmap && ( bitmap.width % levels !=0  ||  bitmap.height % levels != 0 ) )
  261.         {
  262.  
  263.             this.maxU = bitmap.width / width;
  264.             this.maxV = bitmap.height / height;
  265.  
  266.             okBitmap = new BitmapData( width, height, bitmap.transparent, 0x00000000 );
  267.  
  268.             okBitmap.draw( bitmap );
  269.             
  270.             extendBitmapEdges( okBitmap, bitmap.width, bitmap.height );
  271.             
  272.             // Dispose bitmap if needed
  273.             if( dispose ) bitmap.dispose();
  274.         }
  275.         else
  276.         {
  277.             this.maxU = this.maxV = 1;
  278.  
  279.             okBitmap = bitmap;
  280.         }
  281.  
  282.         return okBitmap;
  283.     }
  284.  
  285.     protected function extendBitmapEdges( bmp:BitmapData, originalWidth:Number, originalHeight:Number ):void
  286.     {
  287.         var srcRect  :Rectangle = new Rectangle();
  288.         var dstPoint :Point = new Point();
  289.         var i        :int;
  290.  
  291.         // Check width
  292.         if( bmp.width > originalWidth )
  293.         {
  294.             // Extend width
  295.             srcRect.x      = originalWidth-1;
  296.             srcRect.y      = 0;
  297.             srcRect.width  = 1;
  298.             srcRect.height = originalHeight;
  299.             dstPoint.y     = 0;
  300.             
  301.             for( i = originalWidth; i < bmp.width; i++ )
  302.             {
  303.                 dstPoint.x = i;
  304.                 bmp.copyPixels( bmp, srcRect, dstPoint );
  305.             }
  306.         }
  307.  
  308.         // Check height
  309.         if( bmp.height > originalHeight )
  310.         {
  311.             // Extend height
  312.             srcRect.x      = 0;
  313.             srcRect.y      = originalHeight-1;
  314.             srcRect.width  = bmp.width;
  315.             srcRect.height = 1;
  316.             dstPoint.x     = 0;
  317.  
  318.             for( i = originalHeight; i < bmp.height; i++ )
  319.             {
  320.                 dstPoint.y = i;
  321.                 bmp.copyPixels( bmp, srcRect, dstPoint );
  322.             }
  323.         }
  324.     }
  325.  
  326.     // ______________________________________________________________________
  327.  
  328.     /**
  329.     * Copies the properties of a material.
  330.     *
  331.     * @param    material    Material to copy from.
  332.     */
  333.     override public function copy( material :MaterialObject3D ):void
  334.     {
  335.         super.copy( material );
  336.  
  337.         this.maxU = material.maxU;
  338.         this.maxV = material.maxV;
  339.     }
  340.  
  341.     /**
  342.     * Creates a copy of the material.
  343.     *
  344.     * @return    A newly created material that contains the same properties.
  345.     */
  346.     override public function clone():MaterialObject3D
  347.     {
  348.         var cloned:MaterialObject3D = super.clone();
  349.  
  350.         cloned.maxU = this.maxU;
  351.         cloned.maxV = this.maxV;
  352.  
  353.         return cloned;
  354.     }
  355.  
  356.     // ______________________________________________________________________ PRIVATE VAR
  357.  
  358.     protected var _texture :*;
  359.     protected static var _triMatrix:Matrix = new Matrix()
  360.     protected static var _localMatrix:Matrix = new Matrix();
  361. }
  362. }