3d立方体贴图
package com.ddd { import flash.display.Bitmap; import flash.display.Sprite; import flash.display.StageScaleMode; import flash.events.Event; import flash.geom.Matrix3D; import flash.geom.Vector3D; public class Triangles extends Sprite { private var points:Vector.<Points3D>; private var triangle:Array=[]; private var vpX:Number=stage.stageWidth / 2; private var vpY:Number=stage.stageHeight / 2; [Embed(source="img/5.jpg")] private var img:Class private var bd:Bitmap; public function Triangles() { stage.scaleMode=StageScaleMode.NO_SCALE; init(); } private function init():void { points=new Vector.<Points3D>; points[0]=new Points3D(-100, -100, -100); points[1]=new Points3D(100, -100, -100); points[2]=new Points3D(100, 100, -100); points[3]=new Points3D(-100, 100, -100); points[4]=new Points3D(-100, -100, 100); points[5]=new Points3D(100, -100, 100); points[6]=new Points3D(100, 100, 100); points[7]=new Points3D(-100, 100, 100); for (var i:int=0; i < points.length; i++) { points[i].setVPoint(vpX, vpY); points[i].setCenter(0,0,200); } /** triangle[0]=new Triangle(points[0],points[1],points[2],0x660000); triangle[1]=new Triangle(points[0],points[2],points[3],0x660000); triangle[2]=new Triangle(points[0],points[4],points[5],0x006600); triangle[3]=new Triangle(points[0],points[5],points[1],0x006600); triangle[4]=new Triangle(points[0],points[3],points[7],0x000066); triangle[5]=new Triangle(points[0],points[7],points[4],0x000066); triangle[6]=new Triangle(points[6],points[1],points[2],0x666600); triangle[7]=new Triangle(points[6],points[5],points[1],0x666600); triangle[8]=new Triangle(points[6],points[4],points[5],0x006666); triangle[9]=new Triangle(points[6],points[7],points[4],0x006666); triangle[10]=new Triangle(points[6],points[7],points[3],0x660066); triangle[11]=new Triangle(points[6],points[3],points[2],0x660066); */ triangle[0]=new Triangle(points[0], points[1], points[2], 0x6666cc); triangle[1]=new Triangle(points[0], points[2], points[3], 0x6666cc); // top triangle[3]=new Triangle(points[0], points[5], points[1], 0x66cc66); triangle[2]=new Triangle(points[0], points[4], points[5], 0x66cc66); //back triangle[5]=new Triangle(points[4], points[6], points[5], 0xcc6666); triangle[4]=new Triangle(points[4], points[7], points[6], 0xcc6666); // bottom triangle[6]=new Triangle(points[3], points[2], points[6], 0xcc66cc); triangle[7]=new Triangle(points[3], points[6], points[7], 0xcc66cc); // right triangle[8]=new Triangle(points[1], points[5], points[6], 0x66cccc); triangle[9]=new Triangle(points[1], points[6], points[2], 0x66cccc); // left triangle[10]=new Triangle(points[4], points[0], points[3], 0xcccc66); triangle[11]=new Triangle(points[4], points[3], points[7], 0xcccc66); bd=new img; this.addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(e:Event):void { var angleX:Number=(mouseY - vpY) * .001; var angleY:Number=(mouseX - vpX) * .001; for (var i:int=0; i < points.length; i++) { var p:Points3D=points[i]; p.rotateX(angleX); p.rotateY(angleY); } triangle.sortOn("depth", Array.DESCENDING | Array.NUMERIC); graphics.clear(); for (i=0; i < triangle.length; i++) { triangle[i].draw(graphics,bd.bitmapData.clone()); } } } }
package com.ddd { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Graphics; import flash.geom.Matrix; import flash.geom.Point; public class Triangle { private var color:uint=0; private var pA:Points3D; private var pB:Points3D; private var pC:Points3D; public var bd:Bitmap; public function Triangle(_pA:Points3D, _pC:Points3D, _pB:Points3D, _color:uint=0xcccccc) { pA=_pA; pB=_pB; pC=_pC; color=_color; } public function get depth():Number { return Math.min(pA.z, pB.z, pC.z); } public function draw(g:Graphics, _bd:BitmapData):void { if (isBackFacil()) { return; } /* var matrix:Matrix=new Matrix; var x:Number=(pC.screenX-pA.screenX)*.01; var y:Number=(pC.screenY-pA.screenY)*.01; //g.beginFill(color); g.beginBitmapFill(_bd,new Matrix(1,x,y,1,pA.screenX,pA.screenY),false,true); g.lineStyle(0); g.moveTo(pA.screenX, pA.screenY); g.lineTo(pB.screenX, pB.screenY); g.lineTo(pC.screenX, pC.screenY); g.lineTo(pA.screenX, pA.screenY); */ var v:Vector.<Number>=new Vector.<Number>; v.push(pA.screenX, pA.screenY); v.push(pB.screenX, pB.screenY); v.push(pC.screenX, pC.screenY); var i:Vector.<int>=new Vector.<int>; i.push(0, 1, 2); var uv:Vector.<Number>=new Vector.<Number>; uv.push(0, 0); uv.push(1, 0); uv.push(1, 1); g.lineStyle(0); g.beginBitmapFill(_bd); g.drawTriangles(v, i,uv); g.endFill(); } public function isBackFacil():Boolean { var cax:Number=pC.screenX - pA.screenX; var cay:Number=pC.screenY - pA.screenY; var bcax:Number=pB.screenX - pC.screenX; var bcay:Number=pB.screenY - pC.screenY; return cax * bcay > cay * bcax; } } }
package com.ddd { public class Points3D { public var fl:Number=250; private var vpX:Number=0; private var vpY:Number=0; private var cx:Number=0; private var cy:Number=0; private var cz:Number=0; public var x:Number=0; public var y:Number=0; public var z:Number=0; public function Points3D(_x:Number=0, _y:Number=0, _z:Number=0) { x=_x; y=_y; z=_z; } public function setCenter(_cx:Number, _cy:Number, _cz:Number):void { cx=_cx; cy=_cy; cz=_cz; } public function setVPoint(_vpx:Number, _vpy:Number):void { vpX=_vpx; vpY=_vpy; } public function get screenX():Number { var scale:Number=fl / (fl + cz + z); return vpX + cx + x * scale; } public function get screenY():Number { var scale:Number=fl / (fl + cz + z); return vpY + cy + y * scale; } public function rotateX(_angle:Number):void { var y1:Number=Math.cos(_angle) * y - Math.sin(_angle) * z; var z1:Number=Math.cos(_angle) * z + Math.sin(_angle) * y; y=y1; z=z1; } public function rotateY(_angle:Number):void { var x1:Number=Math.cos(_angle) * x - Math.sin(_angle) * z; var z1:Number=Math.cos(_angle) * z + Math.sin(_angle) * x; x=x1; z=z1; } public function rotateZ(_angle:Number):void { var x1:Number=Math.cos(_angle) * x - Math.sin(_angle) * y; var y1:Number=Math.cos(_angle) * y + Math.sin(_angle) * x; x=x1; y=y1; } } }