3D星形贴图:
/** * * *---------------------* * | *** 3D星形贴图 *** | * *---------------------* * * 编辑修改收录:fengzi(疯子、wu341、wgq341) * * 不会写代码,我是代码搬运工。 * * 联系方式:QQ(493712833)。 * * 随 笔: https://www.cnblogs.com/fengziwu/ * * 版权协议:请自觉遵守LGPL协议,欢迎修改、复制、转载、传播给更多需要的人。 * 免责声明:任何因使用此软件导致的纠纷与软件/程序开发者无关。 * 日 期: 2019.05.08 * */ package fengzi.bmd { 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.Vector3D; public class StarTexture extends Sprite { private var _bmpd:BitmapData; private var _pointContainer:Sprite; private var _graphicContainer:Sprite; private var _pointArr:Array = []; private var _objArr:Array = []; private var _num:int = 5; private var _boo:Boolean;//是否是离五角星中心点较近的顶点 private var _radius1:Number;//五角星中心点离较远顶点的距离(可看作五角星外接圆的半径) private var _radius2:Number;//五角星中心点离较近顶点的距离(可看作五角星内接圆的半径) private var _focus:Number = 400; /*** * 3D星形贴图 * @param img 图片对象 * @param _radius1 五角星中心点离较远顶点的距离(可看作五角星外接圆的半径) * @param _radius2 五角星中心点离较近顶点的距离(可看作五角星内接圆的半径) ***/ public function StarTexture(img:*,_radius1:Number = 100,_radius2:Number = 40) { this._radius1=_radius1; this._radius2=_radius2; initViews(img); initEventListeners(); } private function initViews(img:*):void { _pointContainer = new Sprite(); _graphicContainer = new Sprite(); _pointContainer.x = _graphicContainer.x =img.width*0.5; _pointContainer.y = _graphicContainer.y = img.height*0.5; _bmpd = new BitmapData(img.width,img.height,false,0) _bmpd.draw(img) this.addChild(_pointContainer); this.addChild(_graphicContainer); createStar(); } private function initEventListeners():void { this.addEventListener(Event.ENTER_FRAME,onEnterFrame); } private function onEnterFrame(e:Event):void{ for(var i:int=0;i<_pointArr.length;i++){ _pointArr[i].Z = (_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_pointContainer).position.z; _pointArr[i].X = (_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_pointContainer).position.x*(_focus/(_focus+_pointArr[i].Z)); _pointArr[i].Y = (_pointArr[i] as MovieClip).transform.getRelativeMatrix3D(_pointContainer).position.y*(_focus/(_focus+_pointArr[i].Z)); (_pointArr[i] as MovieClip).transform.matrix3D.appendRotation((mouseX-_pointContainer.x)/50,Vector3D.Y_AXIS); (_pointArr[i] as MovieClip).transform.matrix3D.appendRotation((_pointContainer.y-mouseY)/50,Vector3D.X_AXIS); } _graphicContainer.graphics.clear(); _objArr = []; for(i=0;i<_num*2;i++){ var vertices:Vector.<Number> = new Vector.<Number>(); 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); var indices:Vector.<int> = new Vector.<int>([0,1,2,0,2,3]); var uvtData:Vector.<Number> = Vector.<Number>([0,0,1,0,1,1,0,1]);//不能new,如果new会导致运行flash.display.Graphics.drawTriangles()运行错误:ArgumentError: Error #2004: 某个参数无效。tell me why? var Z:Number = _pointArr[i].Z + _pointArr[(i+1)%(_num*2)].Z; _objArr.push({V:vertices,I:indices,U:uvtData,Z:Z}); } _objArr.sortOn("Z",18); for(i =0;i<_objArr.length;i++){ _graphicContainer.graphics.beginBitmapFill(_bmpd); _graphicContainer.graphics.drawTriangles(_objArr[i].V,_objArr[i].I,_objArr[i].U,TriangleCulling.NONE); _graphicContainer.graphics.endFill(); } } private function createStar():void { for(var i:int = 0;i<_num*2;i++){ var mc:MovieClip = new MovieClip(); _pointContainer.addChild(mc); if(_boo){ mc.x=_radius2*Math.cos(2*Math.PI/(_num*2)*i); mc.y=_radius2*Math.sin(2*Math.PI/(_num*2)*i); }else{ mc.x=_radius1*Math.cos(2*Math.PI/(_num*2)*i); mc.y=_radius1*Math.sin(2*Math.PI/(_num*2)*i); } mc.z = 0; _pointArr.push(mc); _boo = !_boo; } var centerMC:MovieClip = new MovieClip(); _pointContainer.addChild(centerMC); centerMC.x = 0;centerMC.y = 0;centerMC.z = -60; _pointArr.push(centerMC); } } }
原文地址:https://www.cnblogs.com/fengziwu/p/10908855.html
时间: 2024-11-19 22:31:02