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

[ActionScript 3.0] AS3 3D星形贴图

时间:2015-09-11 15:51:02      阅读:224      评论: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.Vector3D;
10     
11     /**
12      * @author Frost.Yen
13      * @E-mail 871979853@qq.com
14      * @create 2015-9-11 上午10:48:09
15      *
16      */
17     [SWF(width="1024",height="768")]
18     public class StarTexture extends Sprite
19     {
20         [Embed(source="test.jpg")]
21         private var _img:Class;
22         private var _bmpd:BitmapData;
23         private var _pointContainer:Sprite;
24         private var _graphicContainer:Sprite;
25         private var _pointArr:Array = [];
26         private var _objArr:Array = [];
27         private var _num:int = 5;
28         private var _boo:Boolean;//是否是离五角星中心点较近的顶点
29         private var _radius1:Number = 100;//五角星中心点离较远顶点的距离(可看作五角星外接圆的半径)
30         private var _radius2:Number = 40;//五角星中心点离较近顶点的距离(可看作五角星内接圆的半径)
31         private var _focus:Number = 400;
32         public function StarTexture()
33         {
34             initViews();
35             initEventListeners();
36         }
37         private function initViews():void
38         {
39             _pointContainer = new Sprite();
40             _graphicContainer = new Sprite();
41             _pointContainer.x = _graphicContainer.x = this.stage.stageWidth*0.5;
42             _pointContainer.y = _graphicContainer.y = this.stage.stageHeight*0.5;
43             _bmpd = (new _img() as Bitmap).bitmapData;
44             this.addChild(_pointContainer);
45             this.addChild(_graphicContainer);
46             createStar();
47         }
48         private function initEventListeners():void
49         {
50             this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
51         }
52         private function onEnterFrame(e:Event):void{
53             for(var i:int=0;i<_pointArr.length;i++){
54                 _pointArr[i].Z = (_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_pointContainer).position.z;
55                 _pointArr[i].X = (_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_pointContainer).position.x*(_focus/(_focus+_pointArr[i].Z));
56                 _pointArr[i].Y = (_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_pointContainer).position.y*(_focus/(_focus+_pointArr[i].Z));
57                 (_pointArr[i] as MovieClip).transform.matrix3D.appendRotation((mouseX-_pointContainer.x)/50,Vector3D.Y_AXIS);
58                 (_pointArr[i] as MovieClip).transform.matrix3D.appendRotation((_pointContainer.y-mouseY)/50,Vector3D.X_AXIS);
59             }
60             _graphicContainer.graphics.clear();
61             _objArr = [];
62             for(i=0;i<_num*2;i++){
63                 var vertices:Vector.<Number> = new Vector.<Number>();
64                 vertices.push(_pointArr[_num*2].X,_pointArr[_num*2].Y,_pointArr[i].X,_pointArr[i].Y,_pointArr[(i+1)%(_num*2)].X,_pointArr[(i+1)%(_num*2)].Y,_pointArr[(i+2)%(_num*2)].X,_pointArr[(i+2)%(_num*2)].Y);
65                 var indices:Vector.<int> = new Vector.<int>([0,1,2,0,2,3]);
66                 var uvtData:Vector.<Number> = Vector.<Number>([0,0,1,0,1,1,0,1]);//不能new,如果new会导致运行flash.display.Graphics.drawTriangles()运行错误:ArgumentError: Error #2004: 某个参数无效。tel me why?
67                 var Z:Number = _pointArr[i].Z + _pointArr[(i+1)%(_num*2)].Z;
68                 _objArr.push({V:vertices,I:indices,U:uvtData,Z:Z});
69             }
70             _objArr.sortOn("Z",18);
71             for(i =0;i<_objArr.length;i++){
72                 _graphicContainer.graphics.beginBitmapFill(_bmpd);
73                 _graphicContainer.graphics.drawTriangles(_objArr[i].V,_objArr[i].I,_objArr[i].U,TriangleCulling.NONE);
74                 _graphicContainer.graphics.endFill();
75             }
76         }
77         private function createStar():void
78         {
79             for(var i:int = 0;i<_num*2;i++){
80                 var mc:MovieClip = new MovieClip();
81                 _pointContainer.addChild(mc);
82                 if(_boo){
83                     mc.x=_radius2*Math.cos(2*Math.PI/(_num*2)*i);
84                     mc.y=_radius2*Math.sin(2*Math.PI/(_num*2)*i);
85                 }else{
86                     mc.x=_radius1*Math.cos(2*Math.PI/(_num*2)*i);
87                     mc.y=_radius1*Math.sin(2*Math.PI/(_num*2)*i);
88                 }
89                 mc.z = 0;
90                 _pointArr.push(mc);
91                 _boo = !_boo;
92             }
93             var centerMC:MovieClip = new MovieClip();
94             _pointContainer.addChild(centerMC);
95             centerMC.x = 0;centerMC.y = 0;centerMC.z = -60;
96             _pointArr.push(centerMC);
97         }
98     }
99 }

 

[ActionScript 3.0] AS3 3D星形贴图

标签:

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

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