package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.TriangleCulling;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Vector3D; /**
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @create 2015-9-11 下午2:59:18
*
*/
[SWF(width="1024",height="768")]
public class RingTexture extends Sprite
{
[Embed(source="test.jpg")]
private var _img:Class;
private var _bmpd:BitmapData;
private var _pointContainer:Sprite;
private var _graphicContainer:Sprite;
private var _ringContainer:Sprite;
private var _pointArr:Array = [];//放所有点,用于计算每个点的2D投影坐标
private var _arr1:Array = [];//_arr1(二维数数)放所有点,用于设置点的绘制顺序
private var _arr2:Array = [];//_arr2存每个方块,含它们的V,I,U,Z
private var _circle:int=40;//环上的圆圈个数
private var _num:int = 10;
private var _ang:Number = 360/_circle;
private var _focus:Number = 500;
private var _h:int;
private var _w:int;
public function RingTexture()
{
initViews();
initEventListeners();
}
private function initViews():void
{
_pointContainer = new Sprite();
_graphicContainer = new Sprite();
_ringContainer = new Sprite();
_ringContainer.x = _graphicContainer.x = this.stage.stageWidth*0.5;
_ringContainer.y = _graphicContainer.y = this.stage.stageHeight*0.5;
_ringContainer.z = 0;//必须写,不要认为0是它的默认值
_bmpd = (new _img() as Bitmap).bitmapData;
_graphicContainer.transform.colorTransform=new ColorTransform(1,1,1,1,50,50,50);//提高一点填充的亮度,图是画在_graphicContainer中的
this.addChild(_ringContainer);
this.addChild(_graphicContainer);
createRing();
}
private function initEventListeners():void
{
this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(e:Event):void{
_graphicContainer.graphics.clear();
_arr2 = [];
//总容器绕自身Y,X轴随鼠标动
_ringContainer.transform.matrix3D.prependRotation((_graphicContainer.x-mouseX)/50,Vector3D.Y_AXIS);
_ringContainer.transform.matrix3D.prependRotation((mouseY-_graphicContainer.y)/50,Vector3D.X_AXIS);
for(var i:int = 0;i<_pointArr.length;i++){
_pointArr[i].Z=(_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_graphicContainer).position.z;
_pointArr[i].X=(_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_graphicContainer).position.x*(_focus/(_focus+_pointArr[i].Z));
_pointArr[i].Y=(_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_graphicContainer).position.y*(_focus/(_focus+_pointArr[i].Z));//加入了焦距控制,符合透视原理,求出3D点的2D投影坐标
}
for(i=0;i<_h;i++){
for(var k:int = 0;k<_w;k++){
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]);//坐标
var indices:Vector.<int>=Vector.<int>([0,1,3,1,2,3]);//画三角形的顶点顺序
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]);//分割位图的比例
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坐标总和,用于后面的排序 }
}
_arr2.sortOn("Z",18);
for(i = 0;i<_arr2.length;i++){
//_graphicContainer.graphics.lineStyle(1,0xff0000);
_graphicContainer.graphics.beginBitmapFill(_bmpd);
_graphicContainer.graphics.drawTriangles(_arr2[i].V,_arr2[i].I,_arr2[i].U,TriangleCulling.NONE);//画三角形,背面不剔除
_graphicContainer.graphics.endFill();
}
}
private function createRing():void
{
for(var m:int = 0;m<2;m++){
var sp:Sprite = new Sprite();//环容器,用于存放mc点
_ringContainer.addChild(sp);
sp.z = m*160-80;
for(var i:int=0;i<=_circle;i++){
_arr1[i+m*41] = [];//两个环共82个小圈(0-40,41-81),每个小圈中有11份,这个二维数组共存902个点
for(var j:int = 0;j<=_num;j++){
var mc:MovieClip = new MovieClip();
sp.addChild(mc);
mc.x=(m==0)?90+15*Math.cos(j*2*Math.PI/_num):15*Math.cos(j*2*Math.PI/_num);//90改为50,变成连体,现在是嵌套
mc.y=(m==0)?15*Math.sin(j*2*Math.PI/_num):100+15*Math.sin(j*2*Math.PI/_num);
mc.z = 0;//当m为0时,圆平放,为1时竖放
if(m==0){
//让坐标点在大圆周上分布
mc.transform.matrix3D.appendRotation(i*_ang,Vector3D.Y_AXIS);
}else{
mc.transform.matrix3D.appendRotation(i*_ang,Vector3D.X_AXIS)
}
//放所有点,用于获取每个3d点的二维投影坐标,存入动态属性X,Y
_pointArr.push(mc);
//放所有点于二维数组中,用于设置drawTriangles的三角形顶点
_arr1[i+m*41].push(mc);
}
} }
_h = _arr1.length-1;
_w = _arr1[0].length - 1;//_h纬线数最大索引81,_w经线数最大索引10
}
}
}