matrix3d做转换
这是基类:
package cn.ndl.test { import flash.display.Bitmap; import flash.display.Sprite; import flash.display.TriangleCulling; import flash.geom.Matrix3D; import flash.geom.Point; import flash.geom.Vector3D; public class SphereBasic extends Sprite { [Embed(source="http://www.cnblogs.com/../Dot.png")] private var imgClass:Class; private var bd:Bitmap; private var vec:Vector.<Number>; private var ind:Vector.<int>; private var uvt:Vector.<Number>; private var row:int=60; private var col:int=60; private var rad:Number=200; private var centerZ:Number=400; private var fl:Number=1000; private var spr:Sprite; private var sprimg:Sprite; private var vertsVec:Array=[]; private var titlenum:int=0; public function SphereBasic() { super(); init(); } private function init():void { spr=new Sprite; spr.rotationX=0; spr.rotationY=0; spr.rotationZ=0; sprimg=new Sprite; this.addChild(sprimg); bd=new imgClass; titlenum=row * col; rad=Math.floor(bd.width / (Math.PI * 2)); serVec(); rotoXYZ(0, 0, 0); } private function serVec():void { var _x:Number=Math.PI * 2 / col; var _y:Number=Math.PI / row; for (var i:int=0; i <= row; i++) { vertsVec[i]=[]; for (var j:int=0; j <= col; j++) {
//注意这里的坐标值算法 vertsVec[i][j]=new Vector3D(rad * Math.sin(_x * i) * Math.sin(_y * j), rad * Math.cos(_y * j), rad * Math.cos(_x * i) * Math.sin(_y * j)); } } } public function rotoXYZ(_rx:Number, _ry:Number, _rz:Number):void { spr.transform.matrix3D.appendRotation(_rx, Vector3D.X_AXIS); spr.transform.matrix3D.appendRotation(_ry, Vector3D.Y_AXIS); spr.transform.matrix3D.appendRotation(_rz, Vector3D.Z_AXIS); transformSphere(spr.transform.matrix3D.clone()); } private function transformSphere(_m:Matrix3D):void { vec=new Vector.<Number>; ind=new Vector.<int>; uvt=new Vector.<Number>; var curnum:int=0; var newvertsVec:Array=[]; var distArr:Array=[]; var disPoint:Array=[]; sprimg.graphics.clear(); var v3d:Vector3D; for (var i:int=0; i <= row; i++) { newvertsVec[i]=[]; disPoint[i]=[]; for (var j:int=0; j <= col; j++) { v3d=_m.deltaTransformVector(vertsVec[i][j]); newvertsVec[i][j]=v3d; disPoint[i][j]=new Point(v3d.x, v3d.y); } } var zAv:Number=0; for (i=0; i < row; i++) { for (j=0; j < col; j++) { zAv=(newvertsVec[i][j].z + newvertsVec[i + 1][j].z + newvertsVec[i][j + 1].z + newvertsVec[i + 1][j + 1].z) / 4; distArr.push([zAv, i, j]); } } distArr.sort(distsort); var cur0:Point; var cur1:Point; var cur2:Point; var cur3:Point; for (var n:int=0; n < titlenum; n++) { i=distArr[n][1]; j=distArr[n][2]; cur0=disPoint[i][j].clone(); cur1=disPoint[i + 1][j].clone(); cur2=disPoint[i + 1][j + 1].clone(); cur3=disPoint[i][j + 1].clone(); vec.push(cur0.x, cur0.y, cur1.x, cur1.y, cur2.x, cur2.y, cur3.x, cur3.y); ind.push(curnum, curnum + 1, curnum + 3, curnum + 1, curnum + 2, curnum + 3); uvt.push(i / row, j / col, (i + 1) / row, j / col, (i + 1) / row, (j + 1) / col, i / row, (j + 1) / col); curnum+=4; } sprimg.graphics.beginBitmapFill(bd.bitmapData); sprimg.graphics.drawTriangles(vec, ind, uvt, TriangleCulling.POSITIVE); sprimg.graphics.endFill(); } private function distsort(v:Array, w:Array):int { if (v[0] > w[0]) { return -1; } else if (v[0] < w[0]) { return 1; } else { return 0; } } } }
这是调用类:
package cn.ndl.test { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; public class CirSprite extends Sprite { private var sp:SphereBasic; private var xh:Number=stage.stageWidth/2; private var yh:Number=stage.stageHeight/2; public function CirSprite() { super(); stage.scaleMode=StageScaleMode.NO_SCALE; //stage.align=StageAlign.TOP_LEFT; init(); } private function init():void { sp=new SphereBasic; this.addChild(sp); sp.x=xh; sp.y=yh; this.addEventListener(Event.ENTER_FRAME,onEnterFrame); } private function onEnterFrame(e:Event):void { var ax:Number=(mouseX-xh)*.01; var ay:Number=(mouseY-yh)*.01; //注意这里的﹣值转换对应的基类的坐标 sp.rotoXYZ(-ay,ax,0); } } }
主要用的matrix3d做转换
用的是逆三角图形,实际是顺三角算法的贴图