1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| package {
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.filters.*;
[SWF(width="300",height="300",backgroundColor="#FFFFFF", framerate="30")]
public class testComplexObjet extends Sprite {
//Embed your texture
[Embed(source="../rsc/your_texture.jpg")]
private var texture:Class;
//the displayed object
private var primitive:Sprite;
private var angle:int = 0;
//torus property (since its a function not an object let's put it there)
//Instantiate parameters
private var paraVec:Vector3D;
private var verts:Vector.<Number>;
private var indices:Vector.<int>;
private var projectedVerts:Vector.<Number>;
private var projectionMatrix:Matrix3D;
private var uvtData:Vector.<Number>;
private var bitmapData:BitmapData;
private var ix:Number;
private var iy:Number;
//Set perspective
private var perspective:PerspectiveProjection = new PerspectiveProjection();
public function testComplexObjet() {
primitive = createTorus(75,50);
primitive.x=stage.stageWidth/2;
primitive.y=stage.stageHeight/2;
addChild(primitive);
stage.addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void{
var a:int = angle + 3;
projectionMatrix = perspective.toMatrix3D();
projectionMatrix.prependTranslation(0,0,0.3);
projectionMatrix.prependRotation( a ,new Vector3D(1,0,0.0));
Utils3D.projectVectors(projectionMatrix, verts, projectedVerts, uvtData);
with(primitive.graphics){
clear();
lineStyle(0.5,0x000000);
bitmapData = new texture().bitmapData;
beginBitmapFill(bitmapData,null, false, false);
drawTriangles(projectedVerts, indices, uvtData,TriangleCulling.POSITIVE);
endFill();
}
angle = a;
}
private function createTorus(cols:int,rows:int):Sprite {
var torus:Sprite = new Sprite();
paraVec = new Vector3D();
verts = new Vector.<Number>();
indices = new Vector.<int>();
projectedVerts = new Vector.<Number>();
uvtData = new Vector.<Number>();
bitmapData = new texture().bitmapData;
perspective.fieldOfView = 35.0; // camera angle, in degrees
//Calculate Vertices
for (var i:int = 0 ; i!=rows; i++) {
ix= i/(rows-1)*Math.PI*2.0;
for (var j:int =0 ; j!=cols; j++) {
iy= j/(cols-1)*Math.PI*2.0;
//Torus
paraVec = new Vector3D(
Math.sin(iy) * (7 + Math.cos(iy/3 - 2 * ix) + 2 * Math.cos(iy/3 + ix)),
Math.cos(iy) * (7 + Math.cos(iy/3 - 2 * ix) + 2 * Math.cos(iy/3 + ix)),
Math.sin(iy/3 - 2 * ix) + 2 * Math.sin(iy/3 + ix)
);//*/
//Collect vetices in the verts Vector array
verts.push(0.1 * paraVec.x,0.1 * paraVec.y,0.1 * paraVec.z);
uvtData.push( i/(rows-1),j/(cols-1), 0.0);
//Initialize projected vertices
projectedVerts.push(0.0,0.0);
}
}
//Create indices
var ii:int =0;
for (var ik:int=0 ; ik!=rows-1; ik++) {
for (var jk:int=0 ; jk!=cols-1; jk++) {
indices.push( ii,ii+cols+1,ii+1,
ii+cols,ii+cols+1,ii++);
}
ii++;
}
return torus;
}
}
} |
Partager