Changeset 14858

Show
Ignore:
Timestamp:
06/29/08 22:09:50 (5 years ago)
Author:
gyuque
Message:

texture related...

Location:
lang/actionscript/gmapkmz/exl/render/collada
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • lang/actionscript/gmapkmz/exl/render/collada/DAEFace.as

    r14593 r14858  
    11package exl.render.collada 
    22{ 
     3        import flash.display.BitmapData; 
     4 
    35        public class DAEFace 
    46        { 
     
    911                } 
    1012 
     13                public var shadingFactor:Number = 1; 
     14                public var textureIndex:int = -1; 
    1115                public var fid:uint; 
    1216                public var p:Array = [null, null, null]; 
     17                public var textureBitmap:BitmapData = null; 
    1318 
    1419                private var mHasN:uint = 0; 
  • lang/actionscript/gmapkmz/exl/render/collada/DAEMaterial.as

    r14723 r14858  
    55                public var hasDiffuseColor:Boolean = false; 
    66                public var hasTexture:Boolean      = false; 
    7                 public var dR:int = 255; 
    8                 public var dG:int = 255; 
    9                 public var dB:int = 255; 
     7                public var textureIndex:int        = -1; 
     8                public var dR:int; 
     9                public var dG:int; 
     10                public var dB:int; 
    1011 
    11                 public function setDiffuseRGB(r:int, g:int, b:int):void 
     12                public function setDiffuseRGB(r:int = 255, g:int = 255, b:int = 255):void 
    1213                { 
    1314                        dR = r; 
  • lang/actionscript/gmapkmz/exl/render/collada/DAEModel.as

    r14723 r14858  
    1616 
    1717                protected var mNameMap:Dictionary = new Dictionary(true); 
     18 
     19                protected var mTextureNameMap:Dictionary = new Dictionary(); 
     20                protected var mTextureIndexMap:Dictionary = new Dictionary(); 
     21                protected var mNextTexIndex:int = 1; 
     22 
    1823                protected var mCompleteHandler:Function; 
    1924                protected var mRoot:DAESceneObject = null; 
     
    3136                        if (path) 
    3237                                mReader.read(path); 
     38                } 
     39 
     40                public function get textureIndexMap():Dictionary 
     41                { 
     42                        return mTextureIndexMap; 
     43                } 
     44 
     45                private function toTextureIndex(name:String):uint 
     46                { 
     47                        if (mTextureNameMap[name]) 
     48                                return mTextureNameMap[name]; 
     49 
     50                        mTextureNameMap[name] = mNextTexIndex; 
     51                        mTextureIndexMap[mNextTexIndex] = name; 
     52                        return mNextTexIndex++; 
    3353                } 
    3454 
     
    179199                                        else if (c.type == DaeColorOrTexture.TYPE_TEXTURE) 
    180200                                        { 
    181                                                 // STDOUT.puts( texture_filename(c, effect) ); 
     201                                                material = new DAEMaterial(); 
     202                                                material.textureIndex = toTextureIndex( texture_filename(c, effect) ); 
     203                                                material.hasTexture = true; 
    182204                                        } 
    183205                                } 
     
    212234                        var m:Array = geometry.materials; 
    213235                        var n:Array = geometry.normals; 
     236                        var t:Array = geometry.texcoords; 
    214237 
    215238                        var geom:DAEGeometry = new DAEGeometry(); 
     
    228251                                var tri:Array = f[i]; 
    229252                                var eff:DaeEffect; 
     253                                var texindex:int = -1; 
    230254 
    231255                                var lib_mate:DAEMaterial = mMaterialList.getMaterialByName(m[i]); 
     
    238262                                                b = lib_mate.dB; 
    239263                                        } 
     264                                        else if (lib_mate.hasTexture) 
     265                                                texindex = lib_mate.textureIndex; 
    240266                                } 
    241267                                else 
     
    256282                                                        else if (c.type == DaeColorOrTexture.TYPE_TEXTURE) 
    257283                                                        { 
    258                                                                 // STDOUT.puts( texture_filename(c) ); 
     284                                                                texindex = toTextureIndex( texture_filename(c, eff) ); 
    259285                                                        } 
    260286                                                } 
     
    266292                                face.p[1] = new DAEVertex(geom.vertices[ tri[1] ], n[i][1], r, g, b); 
    267293                                face.p[2] = new DAEVertex(geom.vertices[ tri[2] ], n[i][2], r, g, b); 
    268  
     294                                face.textureIndex = texindex; 
     295 
     296                                if (t[i] && t[i].length > 2) 
     297                                { 
     298                                        var ti:Array = t[i]; 
     299                                        face.p[0].tu = ti[0][0]; 
     300                                        face.p[0].tv = 1-ti[0][1]; 
     301 
     302                                        face.p[1].tu = ti[1][0]; 
     303                                        face.p[1].tv = 1-ti[1][1]; 
     304 
     305                                        face.p[2].tu = ti[2][0]; 
     306                                        face.p[2].tv = 1-ti[2][1]; 
     307                                } 
    269308                                geom.faces[i] = face; 
    270309                        } 
     
    273312                } 
    274313 
    275                 private static const rxFN:RegExp = /\/([-a-zA-Z0-9_.]+)$/; 
     314                private static const rxFN:RegExp = /([-a-zA-Z0-9_.]+)$/; 
    276315                protected function texture_filename(ct:DaeColorOrTexture, ef:DaeEffect):String 
    277316                { 
  • lang/actionscript/gmapkmz/exl/render/collada/DAEModelRenderer.as

    r14723 r14858  
    22{ 
    33        import flash.display.*; 
     4        import flash.geom.*; 
    45        import exl.trans3d.*; 
     6        import exl.advanced.*; 
    57 
    68        public class DAEModelRenderer 
     
    1921                private var mExtentY:int = 0; 
    2022 
    21                 function DAEModelRenderer(scn:DAESceneObject) 
    22                 { 
     23                private var mTexturePool:TexturePool = null; 
     24 
     25                function DAEModelRenderer(scn:DAESceneObject, texpool:TexturePool = null) 
     26                { 
     27                        mTexturePool = texpool; 
    2328                        mSceneRoot = scn; 
    2429                        invalidateTraverse(); 
     
    9196                                        dp = N.x * vL.x + N.y * vL.y + N.z * vL.z; 
    9297                                        dp = dp*0.25 + 0.749; 
    93  
     98                                        f.shadingFactor = dp; 
    9499                                        for (i = 0;i < 3;i++) 
    95100                                        { 
     
    111116                } 
    112117 
    113                 public function render(g:Graphics):void 
    114                 { 
    115                         if (mTraverseDirty) 
    116                         { 
    117                                 mMatrixStack = []; 
    118                                 if (mTopTransform) 
    119                                         mMatrixStack.push(mTopTransform); 
    120  
    121                                 mFacesCount = 0; 
    122                                 traverse(mSceneRoot, g); 
    123                                 mTraverseDirty = false; 
    124                         } 
    125                         else 
    126                                 invalidateAll(); 
    127  
    128                         finalTransform(); 
    129                         if (mExtentY > 4) 
    130                                 sortPrimitives(); 
     118                public function render(g:Graphics, redrawOnly:Boolean = false):void 
     119                { 
     120                        if (mTraverseDirty || !redrawOnly) 
     121                        { 
     122                                if (mTraverseDirty) 
     123                                { 
     124                                        mMatrixStack = []; 
     125                                        if (mTopTransform) 
     126                                                mMatrixStack.push(mTopTransform); 
     127         
     128                                        mFacesCount = 0; 
     129                                        traverse(mSceneRoot, g); 
     130                                        mTraverseDirty = false; 
     131                                } 
     132                                else 
     133                                        invalidateAll(); 
     134         
     135                                finalTransform(); 
     136                                if (mExtentY > 4) 
     137                                        sortPrimitives(); 
     138                        } 
    131139 
    132140                        if (mExtentY > 2) 
     
    226234                                        tlv.pos.y = aout[1]; 
    227235                                        tlv.pos.z = aout[2]; 
     236                                        tlv.tu = f.p[k].tu; 
     237                                        tlv.tv = f.p[k].tv; 
    228238 
    229239                                        if (screen_y_min > tlv.pos.y) screen_y_min = tlv.pos.y; 
     
    289299                } 
    290300 
     301                protected function calcTextureTransform(vs:Array, m:Matrix, t:BitmapData):void 
     302                { 
     303                        var tw:Number = t.width; 
     304                        var th:Number = t.height; 
     305 
     306                        var M:M22 = new M22(); 
     307                        var vA_:Vec2 = new Vec2(); 
     308                        var vB_:Vec2 = new Vec2(); 
     309                        var ox:Number = vs[0].tu; 
     310                        var oy:Number = vs[0].tv; 
     311 
     312                        m.tx = vs[0].pos.x; 
     313                        m.ty = vs[0].pos.y; 
     314 
     315                        M._11 = (vs[1].tu - ox) * tw; 
     316                        M._12 = (vs[1].tv - oy) * th; 
     317                        M._21 = (vs[2].tu - ox) * tw; 
     318                        M._22 = (vs[2].tv - oy) * th; 
     319                        M = M.getInvert(); 
     320                        if (!M) return; 
     321 
     322                        vA_.x = vs[1].pos.x - m.tx; 
     323                        vA_.y = vs[1].pos.y - m.ty; 
     324                        vB_.x = vs[2].pos.x - m.tx; 
     325                        vB_.y = vs[2].pos.y - m.ty; 
     326 
     327                        m.a = vA_.x * M._11 + vB_.x * M._12; 
     328                        m.c = vA_.x * M._21 + vB_.x * M._22; 
     329 
     330                        m.b = vA_.y * M._11 + vB_.y * M._12; 
     331                        m.d = vA_.y * M._21 + vB_.y * M._22; 
     332 
     333                        ox *= tw; 
     334                        oy *= th; 
     335                        m.tx -= m.a*ox + m.c*oy; 
     336                        m.ty -= m.b*ox + m.d*oy; 
     337                } 
     338 
    291339                protected function drawPrimitives(g:Graphics):void 
    292340                { 
    293                         var i:uint; 
     341                        var i:uint, ti:int; 
    294342                        var p:TrianglePrimitive; 
     343                        var f:DAEFace; 
     344                        var t:BitmapData; 
     345 
     346                        var bM:Matrix = new Matrix(); 
    295347 
    296348                        var tlvertices:Array; 
     349                        var drawShade:Boolean = false; 
    297350                        for (i = 0;i < mVisibleFacesCount;i++) 
    298351                        { 
    299352                                p = TrianglePrimitive(mVisibleFacePool[i]); 
    300353                                tlvertices = p.tlvertices; 
     354                                f = p.f; 
     355 
     356                                ti = f.textureIndex; 
     357                                if (ti >= 0 && !f.textureBitmap && mTexturePool) 
     358                                { 
     359                                        if (( f.textureBitmap = mTexturePool.getByIndex(ti) )) 
     360                                                f.textureIndex = -1; 
     361                                } 
    301362 
    302363                                g.lineStyle(); 
    303                                 g.beginFill(p.f.p[2].computed_color, p.f.p[2].computed_color); 
     364                                if ((t = f.textureBitmap)) 
     365                                { 
     366                                        calcTextureTransform(tlvertices, bM, t); 
     367                                        g.beginBitmapFill(t, bM); 
     368                                        drawShade = f.shadingFactor < 0.99; 
     369                                } 
     370                                else 
     371                                        g.beginFill(f.p[2].computed_color, f.p[2].computed_color); 
    304372                                g.moveTo(tlvertices[0].pos.x, tlvertices[0].pos.y); 
    305373                                g.lineTo(tlvertices[1].pos.x, tlvertices[1].pos.y); 
    306374                                g.lineTo(tlvertices[2].pos.x, tlvertices[2].pos.y); 
    307375                                g.endFill(); 
     376 
     377                                if (drawShade) { 
     378                                        g.beginFill(0, 1-f.shadingFactor); 
     379                                        g.moveTo(tlvertices[0].pos.x, tlvertices[0].pos.y); 
     380                                        g.lineTo(tlvertices[1].pos.x, tlvertices[1].pos.y); 
     381                                        g.lineTo(tlvertices[2].pos.x, tlvertices[2].pos.y); 
     382                                        g.endFill(); 
     383                                } 
    308384                        } 
    309385                } 
     
    348424                protected function addFace(f:DAEFace):void 
    349425                { 
    350  
    351  
    352426                        if (!mFacePool) mFacePool = [new TrianglePrimitive()]; 
    353427                        if (mFacesCount >= mFacePool.length) 
  • lang/actionscript/gmapkmz/exl/render/collada/DAEVertex.as

    r14593 r14858  
    2222                public var pos:Vec3; 
    2323                public var N:Vec3; 
     24 
     25                public var tu:Number = 0; 
     26                public var tv:Number = 0; 
    2427        } 
    2528}