当前位置: 首页 > 工具软件 > Away3D > 使用案例 >

ActionScript 3.0 学习(十七) away3D学习2 away3d的贴图原理

东郭臻
2023-12-01
  

我正在做这样一个东西,在球体的表面,贴上多张图片,如四张图片,其实很简单,如果你明白它的贴图原理。

away3d空间中的最小单位是Face(surface的简称),也就是一个三角形Triangle(但是Face和Triangle分别是两个不同的类),每一个模型(sphere,cube等)都是由很多

个Face组成的。Face中最重要的内容是:

(1)三个x,y,z坐标的点a(x1,y1,z1),b(x2,y2,z2),c(x3,y3,z3),这三个点决定了三角形在三维空间中的位置;

(2)三个U,V坐标的点(u1,v1),(u2,v2),(u3,v3),这三个坐标是二维平面上的坐标,范围是0~1,决定了三角形的贴图信息。如果要给三角形贴上一个长为w,宽为h的图

片,那么,空间中的点(x1,y1,z1)就对应着图片上(u1*w,v1*h)这个点,同理其他两个点也是如此,所以贴图时,只是把图片上(u1*w,v1*h),(u2*w,v2*h),

(u3*w,v3*h) 这三个点围起来的三角形的颜色值完全拷贝出来绘制到三维空间的那个目标三角形上。


package  
{  
    import away3d.cameras.Camera3D;  
    import away3d.containers.ObjectContainer3D;  
    import away3d.containers.Scene3D;  
    import away3d.containers.View3D;  
    import away3d.entities.Mesh;  
    import away3d.materials.TextureMaterial;  
    import away3d.primitives.PlaneGeometry;  
    import away3d.primitives.SphereGeometry;  
    import away3d.textures.BitmapTexture;  
      
    import flash.display.BitmapData;  
    import flash.display.Sprite;  
    import flash.display.Stage3D;  
    import flash.events.Event;  
    import flash.events.MouseEvent;  
    import flash.geom.Vector3D;  
      
    [SWF(width="1024", height="768", frameRate="30", backgroundColor="#00AAAA")]  
    public class Triaxe extends Sprite  
    {  
        private var view: View3D;  
        private var camera: Camera3D;  
        private var scene: Scene3D;  
        private var group: ObjectContainer3D;  
                  
        //嵌入外部图片资料  
        [Embed( "assets/earth.jpg" )]  
        private var EarthImage: Class;  
          
        [Embed( "assets/checker.jpg" )]  
        private var CheckerImage:Class;  
          
        [Embed( "assets/away3d.jpg" )]  
        private var Away3DImage: Class;  
      
        public function Triaxe()  
        {  
            //TODO: implement function  
            super();  
              
            this.init();  
        }  
          
        private function init(): void  
        {     
            trace( "init" );  
            this.init3D();  
            this.createScene();   
              
            this.addEventListener(Event.ENTER_FRAME, onEnterFrame);  
            this.addEventListener(Event.RESIZE, onResize );  
        }  
          
        private function init3D(): void  
        {  
            trace( "createScene" );  
            camera = new Camera3D();  
            scene = new Scene3D();  
              
            view = new View3D( scene, camera );  
            view.antiAlias = 4;  
              
            addChild( view );  
        }  
          
          
        private function createScene(): void  
        {  
          
            group = new ObjectContainer3D();  
            scene.addChild( group );  
              
            //创建一个地球模型  
            this.createEarth();  
            this.createAway3dNote();  
        }  
          
        private function createEarth(): void  
        {  
            var earthBitmap: BitmapTexture = new BitmapTexture( new EarthImage().bitmapData );  
            var texture:TextureMaterial = new TextureMaterial( earthBitmap );  
              
            var sphGeo: SphereGeometry = new SphereGeometry();  
            var sphere: Mesh = new Mesh( sphGeo, texture );  
              
            sphere.position = new Vector3D( 200, 0, 0 );  
              
            //设置名字为 earth, 以后可以通过这个名字来访问地球  
            sphere.name = "earth";  
              
            trace( "sphere'name: " + sphere.name + "Position: " + sphere.position );              
            group.addChild( sphere );  
              
        }  
          
        private function createAway3dNote(): void  
        {  
            var away3dBitmap: BitmapTexture = new BitmapTexture( new Away3DImage().bitmapData );  
            var texture: TextureMaterial = new TextureMaterial( away3dBitmap );  
              
            var planeGeo: PlaneGeometry= new PlaneGeometry(200, 200, 10, 10, true );  
            var away3dNote: Mesh = new Mesh( planeGeo, texture );  
              
            away3dNote.pitch( -90 );  
            away3dNote.name = "away3dnote";  
              
            trace( "plane'name: " + away3dNote.name );            
            group.addChild( away3dNote );  
        }  
          
          
        private function onEnterFrame( evt: Event ):void  
        {     
            group.roll( 2 );  
//          group.pitch( 1 );  
//          group.yaw( 1 );  
              
            view.render();  
        }  
          
          
        private function onResize( evt: Event ): void  
        {     
              
        }  
          
    }  
} 


 类似资料: