个人学习笔记
1 import fl.events.SliderEvent;
2 import fl.controls.CheckBox;
3 imgselect.setStyle("textFormat",new TextFormat("宋体",14,0xffffff));
4 var sp:Sprite =addChild(new Sprite()) as Sprite;
5 sp.x=stage.stageWidth/2;
6 sp.y=stage.stageHeight/2;
7 sp.z=0;
8 var mypic:Pic=new Pic(0,0);
9 var mypic1:Pic1=new Pic1(0,0);
10 var bitmapData:BitmapData =new BitmapData(a.width,a.height)
11 bitmapData.draw (a)
12 var bitmapData1:BitmapData =new BitmapData(b.width,b.height)
13 bitmapData1.draw (b)
14 var i_num:int=30;//环切片分割数
15 var j_num:int=20;//切片内扇形数
16 var R:Number=30;//环的半径
17 var r:Number=60;//切片的半径
18 var i:int;
19 var j:int;
20 var arr3D:Vector.<Number>;
21 var new_arr3D:Vector.<Number>;//
22 var arr2D:Vector.<Number>;
23 var uvt:Vector.<Number>;
24 var vindicies:Vector.<int>;
25 var mtxworld:Matrix3D=new Matrix3D();//改变点坐标的矩阵
26 var pm:Matrix3D=sp.transform.matrix3D;//实现投影转换的投影 Matrix3D 对象
27 pm.prependTranslation(-137.5,-100,0);
28
29 var Rotation:Boolean;
30 var f:Number=1000;
31 var _oldMousePoint:Point=new Point ;
32 init(R,r,i_num,j_num);
33 function init(R:Number=120,r:Number=30,i_num:int=30,j_num:int=20) {
34 arr3D=new Vector.<Number>();
35 arr2D=new Vector.<Number>();
36 new_arr3D=Vector.<Number>([]);
37 uvt=Vector.<Number>([]);
38 vindicies=Vector.<int>([]);
39 for (i=0; i<=i_num; i++) {
40 var temp_Rr:Number=R*Math.cos((i/(i_num-10))*(Math.PI /2))+r
41 if(i>i_num-10)temp_Rr=r-(i-(i_num-10))*(r/(i_num-(i_num-10)))
42 for (j=0; j<=j_num; j++) {
43 //var temp_x:Number =-(R+r*Math.cos(j*(2*Math.PI/j_num)))*Math.cos(i*(2*Math.PI/i_num));
44 var temp_x:Number =-temp_Rr*Math.cos(j*(2*Math.PI/j_num))
45 //var temp_z:Number=-r*Math.sin(j*(2*Math.PI/j_num));//trace("j="+j+",y="+temp_y)
46 var temp_z:Number=-temp_Rr*Math.sin(j*(2*Math.PI/j_num))
47 //var temp_y:Number=-(R+r*Math.cos(j*(2*Math.PI/j_num)))*Math.sin(i*(2*Math.PI/i_num));
48 var temp_y:Number=100*(i-i_num)/i_num
49 if(i>i_num-10)temp_y=-100*10/i_num
50 arr3D.push(temp_x,temp_y,temp_z);
51 uvt.push(j/j_num,i/i_num,1);//f/(f+100+temp_z)
52 }
53 }
54 for (i=0; i<i_num; i++) {
55 for (j=0; j<j_num; j++) {
56 vindicies.push(i*(j_num+1)+j,i*(j_num+1)+(j+1),(i+1)*(j_num+1)+(j+1),(i+1)*(j_num+1)+(j+1),(i+1)*(j_num+1)+j,i*(j_num+1)+j);
57 }
58 }
59 render(imgselect.selected);
60 auto_btn.addEventListener(MouseEvent.CLICK, autoHandler);
61 hand_btn.addEventListener(MouseEvent.CLICK, handHandler);
62 mov_btn.addEventListener(MouseEvent.CLICK, movHandler);
63 }
64
65 function render(isFill:Boolean=true,degreX:Number=0,degreY:Number=0,degreZ:Number=0,TranslationX:Number=0,TranslationY:Number=0,TranslationZ:Number=0) {
66 mtxworld.appendRotation(degreX/1,Vector3D.X_AXIS);//后置增量旋转
67 mtxworld.appendRotation(degreY/1,Vector3D.Y_AXIS);
68 mtxworld.appendRotation(degreZ/1,Vector3D.Z_AXIS);
69 mtxworld.appendTranslation(TranslationX,TranslationY,TranslationZ);
70 mtxworld.transformVectors(arr3D,new_arr3D);//new_arr3D获取旋转后的坐标以便得到Z值设置层深
71 Utils3D.projectVectors(pm, new_arr3D, arr2D, uvt);
72 var Z:Array=[];
73 var tempInd:Vector.<int>=new Vector.<int>();//放置重新排列后的画三角形的顺序
74 for (var k=0; k<vindicies.length; k+=3) {
75 var ind:Vector.<int>=vindicies.slice(k,k+3);
76 //vindicies数组中每三个元素一组,绘制成为一个三角形ind
77 //该三角形ind得Z值为三个顶点的平均值;
78 var d:Number=new_arr3D[ind[0]*3+2]+new_arr3D[ind[1]*3+2]+new_arr3D[ind[2]*3+2];
79 Z.push({d:d,ind:ind});
80 }
81 Z.sortOn("d",Array.NUMERIC |Array.DESCENDING );//按数值降序排列,大的先绘制在里,小的后绘制在外
82 for (i=0; i<Z.length; i++) {
83 for (j=0; j<3; j++) {
84 tempInd.push(Z[i].ind[j]);//重新排列所有三角形的顺序
85 }
86 }
87 sp.graphics.clear();
88 //sp.graphics.lineStyle(1,0xffffff);
89 isFill?sp.graphics.beginBitmapFill(bitmapData,null,false):sp.graphics.lineStyle(1,0xffffff);
90 sp.graphics.drawTriangles(arr2D,tempInd,uvt,TriangleCulling.POSITIVE);//
91 isFill?sp.graphics.beginBitmapFill(bitmapData1,null,false):sp.graphics.lineStyle(1,0xffffff);
92 sp.graphics.drawTriangles(arr2D,tempInd,uvt,TriangleCulling.NEGATIVE);//
93 }
94
95 function autoHandler(e:Event ):void {
96 auto_btn.enabled=false;
97 hand_btn.enabled=true;
98 mov_btn.enabled=true;
99 stage.addEventListener(Event.ENTER_FRAME, autoloopHandler);
100 sp.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
101
102 }
103 function handHandler(e:Event ):void {
104 Rotation=true;
105 auto_btn.enabled=true;
106 hand_btn.enabled=false;
107 mov_btn.enabled=true;
108 stage.removeEventListener(Event.ENTER_FRAME, autoloopHandler);
109 sp.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
110
111 }
112 function movHandler(e:Event ):void {
113 Rotation=false;
114 auto_btn.enabled=true;
115 hand_btn.enabled=true;
116 mov_btn.enabled=false;
117 stage.removeEventListener(Event.ENTER_FRAME, autoloopHandler);
118 sp.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
119
120 }
121
122 function autoloopHandler(e:Event ):void {
123 render(imgselect.selected,Math.random ()*10,Math.random ()*10,Math.random ()*10);
124 }
125 function downHandler(e:Event ):void {
126 Mouse.cursor=MouseCursor.HAND;
127 _oldMousePoint.x=mouseX;
128 _oldMousePoint.y=mouseY;
129 stage.addEventListener(Event.ENTER_FRAME, loopHandler);
130 stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
131 }
132
133 function loopHandler(e:Event ):void {
134 if (Rotation) {
135 render(imgselect.selected,mouseY-_oldMousePoint.y,-(mouseX-_oldMousePoint.x));
136 } else {
137 render(imgselect.selected,0,0,0,mouseX-_oldMousePoint.x,mouseY-_oldMousePoint.y,0);
138 }
139 _oldMousePoint.x=mouseX;
140 _oldMousePoint.y=mouseY;
141 }
142
143 function upHandler(e:Event ):void {
144 Mouse.cursor=MouseCursor.AUTO;
145 stage.removeEventListener(Event.ENTER_FRAME, loopHandler);
146
147 }
148 imgselect.addEventListener(MouseEvent.CLICK, sliderDrag);
149 i_slider.addEventListener(SliderEvent.THUMB_DRAG, sliderDrag);
150 j_slider.addEventListener(SliderEvent.THUMB_DRAG, sliderDrag);
151 r_slider.addEventListener(SliderEvent.THUMB_DRAG, sliderDrag);
152 R_slider.addEventListener(SliderEvent.THUMB_DRAG, sliderDrag);
153 function sliderDrag(e):void {//:SliderEvent
154 init(R_slider.value,r_slider.value,i_slider.value,j_slider.value);
155 i_txt.text=String(i_slider.value)
156 j_txt.text=String(j_slider.value)
157
158 }
2 import fl.controls.CheckBox;
3 imgselect.setStyle("textFormat",new TextFormat("宋体",14,0xffffff));
4 var sp:Sprite =addChild(new Sprite()) as Sprite;
5 sp.x=stage.stageWidth/2;
6 sp.y=stage.stageHeight/2;
7 sp.z=0;
8 var mypic:Pic=new Pic(0,0);
9 var mypic1:Pic1=new Pic1(0,0);
10 var bitmapData:BitmapData =new BitmapData(a.width,a.height)
11 bitmapData.draw (a)
12 var bitmapData1:BitmapData =new BitmapData(b.width,b.height)
13 bitmapData1.draw (b)
14 var i_num:int=30;//环切片分割数
15 var j_num:int=20;//切片内扇形数
16 var R:Number=30;//环的半径
17 var r:Number=60;//切片的半径
18 var i:int;
19 var j:int;
20 var arr3D:Vector.<Number>;
21 var new_arr3D:Vector.<Number>;//
22 var arr2D:Vector.<Number>;
23 var uvt:Vector.<Number>;
24 var vindicies:Vector.<int>;
25 var mtxworld:Matrix3D=new Matrix3D();//改变点坐标的矩阵
26 var pm:Matrix3D=sp.transform.matrix3D;//实现投影转换的投影 Matrix3D 对象
27 pm.prependTranslation(-137.5,-100,0);
28
29 var Rotation:Boolean;
30 var f:Number=1000;
31 var _oldMousePoint:Point=new Point ;
32 init(R,r,i_num,j_num);
33 function init(R:Number=120,r:Number=30,i_num:int=30,j_num:int=20) {
34 arr3D=new Vector.<Number>();
35 arr2D=new Vector.<Number>();
36 new_arr3D=Vector.<Number>([]);
37 uvt=Vector.<Number>([]);
38 vindicies=Vector.<int>([]);
39 for (i=0; i<=i_num; i++) {
40 var temp_Rr:Number=R*Math.cos((i/(i_num-10))*(Math.PI /2))+r
41 if(i>i_num-10)temp_Rr=r-(i-(i_num-10))*(r/(i_num-(i_num-10)))
42 for (j=0; j<=j_num; j++) {
43 //var temp_x:Number =-(R+r*Math.cos(j*(2*Math.PI/j_num)))*Math.cos(i*(2*Math.PI/i_num));
44 var temp_x:Number =-temp_Rr*Math.cos(j*(2*Math.PI/j_num))
45 //var temp_z:Number=-r*Math.sin(j*(2*Math.PI/j_num));//trace("j="+j+",y="+temp_y)
46 var temp_z:Number=-temp_Rr*Math.sin(j*(2*Math.PI/j_num))
47 //var temp_y:Number=-(R+r*Math.cos(j*(2*Math.PI/j_num)))*Math.sin(i*(2*Math.PI/i_num));
48 var temp_y:Number=100*(i-i_num)/i_num
49 if(i>i_num-10)temp_y=-100*10/i_num
50 arr3D.push(temp_x,temp_y,temp_z);
51 uvt.push(j/j_num,i/i_num,1);//f/(f+100+temp_z)
52 }
53 }
54 for (i=0; i<i_num; i++) {
55 for (j=0; j<j_num; j++) {
56 vindicies.push(i*(j_num+1)+j,i*(j_num+1)+(j+1),(i+1)*(j_num+1)+(j+1),(i+1)*(j_num+1)+(j+1),(i+1)*(j_num+1)+j,i*(j_num+1)+j);
57 }
58 }
59 render(imgselect.selected);
60 auto_btn.addEventListener(MouseEvent.CLICK, autoHandler);
61 hand_btn.addEventListener(MouseEvent.CLICK, handHandler);
62 mov_btn.addEventListener(MouseEvent.CLICK, movHandler);
63 }
64
65 function render(isFill:Boolean=true,degreX:Number=0,degreY:Number=0,degreZ:Number=0,TranslationX:Number=0,TranslationY:Number=0,TranslationZ:Number=0) {
66 mtxworld.appendRotation(degreX/1,Vector3D.X_AXIS);//后置增量旋转
67 mtxworld.appendRotation(degreY/1,Vector3D.Y_AXIS);
68 mtxworld.appendRotation(degreZ/1,Vector3D.Z_AXIS);
69 mtxworld.appendTranslation(TranslationX,TranslationY,TranslationZ);
70 mtxworld.transformVectors(arr3D,new_arr3D);//new_arr3D获取旋转后的坐标以便得到Z值设置层深
71 Utils3D.projectVectors(pm, new_arr3D, arr2D, uvt);
72 var Z:Array=[];
73 var tempInd:Vector.<int>=new Vector.<int>();//放置重新排列后的画三角形的顺序
74 for (var k=0; k<vindicies.length; k+=3) {
75 var ind:Vector.<int>=vindicies.slice(k,k+3);
76 //vindicies数组中每三个元素一组,绘制成为一个三角形ind
77 //该三角形ind得Z值为三个顶点的平均值;
78 var d:Number=new_arr3D[ind[0]*3+2]+new_arr3D[ind[1]*3+2]+new_arr3D[ind[2]*3+2];
79 Z.push({d:d,ind:ind});
80 }
81 Z.sortOn("d",Array.NUMERIC |Array.DESCENDING );//按数值降序排列,大的先绘制在里,小的后绘制在外
82 for (i=0; i<Z.length; i++) {
83 for (j=0; j<3; j++) {
84 tempInd.push(Z[i].ind[j]);//重新排列所有三角形的顺序
85 }
86 }
87 sp.graphics.clear();
88 //sp.graphics.lineStyle(1,0xffffff);
89 isFill?sp.graphics.beginBitmapFill(bitmapData,null,false):sp.graphics.lineStyle(1,0xffffff);
90 sp.graphics.drawTriangles(arr2D,tempInd,uvt,TriangleCulling.POSITIVE);//
91 isFill?sp.graphics.beginBitmapFill(bitmapData1,null,false):sp.graphics.lineStyle(1,0xffffff);
92 sp.graphics.drawTriangles(arr2D,tempInd,uvt,TriangleCulling.NEGATIVE);//
93 }
94
95 function autoHandler(e:Event ):void {
96 auto_btn.enabled=false;
97 hand_btn.enabled=true;
98 mov_btn.enabled=true;
99 stage.addEventListener(Event.ENTER_FRAME, autoloopHandler);
100 sp.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
101
102 }
103 function handHandler(e:Event ):void {
104 Rotation=true;
105 auto_btn.enabled=true;
106 hand_btn.enabled=false;
107 mov_btn.enabled=true;
108 stage.removeEventListener(Event.ENTER_FRAME, autoloopHandler);
109 sp.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
110
111 }
112 function movHandler(e:Event ):void {
113 Rotation=false;
114 auto_btn.enabled=true;
115 hand_btn.enabled=true;
116 mov_btn.enabled=false;
117 stage.removeEventListener(Event.ENTER_FRAME, autoloopHandler);
118 sp.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
119
120 }
121
122 function autoloopHandler(e:Event ):void {
123 render(imgselect.selected,Math.random ()*10,Math.random ()*10,Math.random ()*10);
124 }
125 function downHandler(e:Event ):void {
126 Mouse.cursor=MouseCursor.HAND;
127 _oldMousePoint.x=mouseX;
128 _oldMousePoint.y=mouseY;
129 stage.addEventListener(Event.ENTER_FRAME, loopHandler);
130 stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
131 }
132
133 function loopHandler(e:Event ):void {
134 if (Rotation) {
135 render(imgselect.selected,mouseY-_oldMousePoint.y,-(mouseX-_oldMousePoint.x));
136 } else {
137 render(imgselect.selected,0,0,0,mouseX-_oldMousePoint.x,mouseY-_oldMousePoint.y,0);
138 }
139 _oldMousePoint.x=mouseX;
140 _oldMousePoint.y=mouseY;
141 }
142
143 function upHandler(e:Event ):void {
144 Mouse.cursor=MouseCursor.AUTO;
145 stage.removeEventListener(Event.ENTER_FRAME, loopHandler);
146
147 }
148 imgselect.addEventListener(MouseEvent.CLICK, sliderDrag);
149 i_slider.addEventListener(SliderEvent.THUMB_DRAG, sliderDrag);
150 j_slider.addEventListener(SliderEvent.THUMB_DRAG, sliderDrag);
151 r_slider.addEventListener(SliderEvent.THUMB_DRAG, sliderDrag);
152 R_slider.addEventListener(SliderEvent.THUMB_DRAG, sliderDrag);
153 function sliderDrag(e):void {//:SliderEvent
154 init(R_slider.value,r_slider.value,i_slider.value,j_slider.value);
155 i_txt.text=String(i_slider.value)
156 j_txt.text=String(j_slider.value)
157
158 }
效果如下: