码迷,mamicode.com
首页 > 其他好文 > 详细

[ActionScript 3.0] AS3 3D双圆环贴图

时间:2015-09-15 12:55:57      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

  1 package
  2 {
  3     import flash.display.Bitmap;
  4     import flash.display.BitmapData;
  5     import flash.display.MovieClip;
  6     import flash.display.Sprite;
  7     import flash.display.TriangleCulling;
  8     import flash.events.Event;
  9     import flash.geom.ColorTransform;
 10     import flash.geom.Vector3D;
 11     
 12     /**
 13      * @author Frost.Yen
 14      * @E-mail 871979853@qq.com
 15      * @create 2015-9-11 下午2:59:18
 16      *
 17      */
 18     [SWF(width="1024",height="768")]
 19     public class RingTexture extends Sprite
 20     {
 21         [Embed(source="test.jpg")]
 22         private var _img:Class;
 23         private var _bmpd:BitmapData;
 24         private var _pointContainer:Sprite;
 25         private var _graphicContainer:Sprite;
 26         private var _ringContainer:Sprite;
 27         private var _pointArr:Array = [];//放所有点,用于计算每个点的2D投影坐标
 28         private var _arr1:Array = [];//_arr1(二维数数)放所有点,用于设置点的绘制顺序
 29         private var _arr2:Array = [];//_arr2存每个方块,含它们的V,I,U,Z
 30         private var _circle:int=40;//环上的圆圈个数
 31         private var _num:int = 10;
 32         private var _ang:Number = 360/_circle;
 33         private var _focus:Number = 500;
 34         private var _h:int;
 35         private var _w:int; 
 36         public function RingTexture()
 37         {
 38             initViews();
 39             initEventListeners();
 40         }
 41         private function initViews():void
 42         {
 43             _pointContainer = new Sprite();
 44             _graphicContainer = new Sprite();
 45             _ringContainer = new Sprite();
 46             _ringContainer.x = _graphicContainer.x = this.stage.stageWidth*0.5;
 47             _ringContainer.y = _graphicContainer.y = this.stage.stageHeight*0.5;    
 48             _ringContainer.z = 0;//必须写,不要认为0是它的默认值
 49             _bmpd = (new _img() as Bitmap).bitmapData;
 50             _graphicContainer.transform.colorTransform=new ColorTransform(1,1,1,1,50,50,50);//提高一点填充的亮度,图是画在_graphicContainer中的
 51             this.addChild(_ringContainer);
 52             this.addChild(_graphicContainer);
 53             createRing();
 54         }
 55         private function initEventListeners():void
 56         {
 57             this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
 58         }
 59         private function onEnterFrame(e:Event):void{
 60             _graphicContainer.graphics.clear();
 61             _arr2 = [];
 62             //总容器绕自身Y,X轴随鼠标动
 63             _ringContainer.transform.matrix3D.prependRotation((_graphicContainer.x-mouseX)/50,Vector3D.Y_AXIS);
 64             _ringContainer.transform.matrix3D.prependRotation((mouseY-_graphicContainer.y)/50,Vector3D.X_AXIS);
 65             for(var i:int = 0;i<_pointArr.length;i++){
 66                 _pointArr[i].Z=(_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_graphicContainer).position.z;
 67                 _pointArr[i].X=(_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_graphicContainer).position.x*(_focus/(_focus+_pointArr[i].Z));
 68                 _pointArr[i].Y=(_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_graphicContainer).position.y*(_focus/(_focus+_pointArr[i].Z));//加入了焦距控制,符合透视原理,求出3D点的2D投影坐标
 69             }
 70             for(i=0;i<_h;i++){
 71                 for(var k:int = 0;k<_w;k++){
 72                     var vertices:Vector.<Number>=Vector.<Number>([_arr1[i][k].X,_arr1[i][k].Y,_arr1[i+1][k].X,_arr1[i+1][k].Y,_arr1[i+1][k+1].X,_arr1[i+1][k+1].Y,_arr1[i][k+1].X,_arr1[i][k+1].Y]);//坐标
 73                     var indices:Vector.<int>=Vector.<int>([0,1,3,1,2,3]);//画三角形的顶点顺序
 74                     var uvtData:Vector.<Number>=Vector.<Number>([i/_h*2,k/_w*2,(i+1)/_h*2,k/_w*2,(i+1)/_h*2,(k+1)/_w*2,i/_h*2,(k+1)/_w*2]);//分割位图的比例
 75                     i!=40?_arr2.push({V:vertices,I:indices,U:uvtData,Z:_arr1[i][k].Z+_arr1[i+1][k].Z+_arr1[i+1][k+1].Z+_arr1[i][k+1].Z}):0;//当h==40时,如果也压入UVTZ,会把两个环的绘制起点首尾相连,,,Z是四个点z坐标总和,用于后面的排序
 76                     
 77                 }
 78             }
 79             _arr2.sortOn("Z",18);
 80             for(i = 0;i<_arr2.length;i++){
 81                 //_graphicContainer.graphics.lineStyle(1,0xff0000);
 82                 _graphicContainer.graphics.beginBitmapFill(_bmpd);
 83                 _graphicContainer.graphics.drawTriangles(_arr2[i].V,_arr2[i].I,_arr2[i].U,TriangleCulling.NONE);//画三角形,背面不剔除
 84                 _graphicContainer.graphics.endFill();
 85             }
 86         }
 87         private function createRing():void
 88         {
 89             for(var m:int = 0;m<2;m++){
 90                 var sp:Sprite = new Sprite();//环容器,用于存放mc点
 91                 _ringContainer.addChild(sp);
 92                 sp.z = m*160-80;
 93                 for(var i:int=0;i<=_circle;i++){
 94                     _arr1[i+m*41] = [];//两个环共82个小圈(0-40,41-81),每个小圈中有11份,这个二维数组共存902个点
 95                     for(var j:int = 0;j<=_num;j++){
 96                         var mc:MovieClip = new MovieClip();
 97                         sp.addChild(mc);
 98                         mc.x=(m==0)?90+15*Math.cos(j*2*Math.PI/_num):15*Math.cos(j*2*Math.PI/_num);//90改为50,变成连体,现在是嵌套
 99                         mc.y=(m==0)?15*Math.sin(j*2*Math.PI/_num):100+15*Math.sin(j*2*Math.PI/_num);
100                         mc.z = 0;//当m为0时,圆平放,为1时竖放
101                         if(m==0){
102                             //让坐标点在大圆周上分布
103                             mc.transform.matrix3D.appendRotation(i*_ang,Vector3D.Y_AXIS);
104                         }else{
105                             mc.transform.matrix3D.appendRotation(i*_ang,Vector3D.X_AXIS)
106                         }
107                         //放所有点,用于获取每个3d点的二维投影坐标,存入动态属性X,Y
108                         _pointArr.push(mc);
109                         //放所有点于二维数组中,用于设置drawTriangles的三角形顶点
110                         _arr1[i+m*41].push(mc);
111                     }
112                 }
113                 
114             }
115             _h = _arr1.length-1;
116             _w = _arr1[0].length - 1;//_h纬线数最大索引81,_w经线数最大索引10
117         }
118     }
119 }

 

[ActionScript 3.0] AS3 3D双圆环贴图

标签:

原文地址:http://www.cnblogs.com/frost-yen/p/4809854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!