Voici le code source avec les explications nécessaires :
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.GraphicsPathCommand;
import flash.Vector;
import flash.geom.Vector3D;
import flash.events.Event;
class Test3D {
private var surface : Sprite;
private var commands : Vector< Int >; // tableau contenant la suite des déplacements
private var vertices : Vector< Vector3D >; // tableau contenant les sommets de notre figure
private static var perspective : Int = 300; // paramètre pour l'ajustement de la vue lors de la rotation
private var _root : MovieClip;
public function new(_parent:MovieClip):Void {
_root = _parent;
// creation du plan
surface = new Sprite();
_root.addChild(surface);
// positions initiales
surface.x = _root.stage.stageWidth / 2;
surface.y = _root.stage.stageHeight / 2;
// on stocke dans un vecteur les valeurs à utiliser pour chaque commande de déplacement
commands = new Vector< Int >();
commands.push(GraphicsPathCommand.MOVE_TO);
commands.push(GraphicsPathCommand.LINE_TO);
commands.push(GraphicsPathCommand.LINE_TO);
commands.push(GraphicsPathCommand.LINE_TO);
commands.push(GraphicsPathCommand.LINE_TO);
// on stocke dans le tableau les vecteurs
vertices = new Vector< Vector3D >();
vertices.push(new Vector3D(-100,100,0,1));
vertices.push(new Vector3D(100,100,0,1));
vertices.push(new Vector3D(100,-100,0,1));
vertices.push(new Vector3D(-100,-100,0,1));
vertices.push(new Vector3D(-100,100,0,1));
_root.addEventListener(Event.ENTER_FRAME, onRender);
}
/**
* Boucle
*/
private function onRender(e:Event):Void {
var datas:Vector< Float > = new Vector< Float >();
// Parcours tous les points
for(i in 0...vertices.length) {
// Effectue une rotation sur l'axe Y en recalculant les nouveaux points
var vertex : Vector3D = rotation_Y(5 * Math.PI / 180, vertices[i]);
vertices[i] = vertex;
// Projection
var point_x : Float = vertex.x * perspective / (perspective + vertex.z);
var point_y : Float = vertex.y * perspective / (perspective + vertex.z);
datas.push(point_x);
datas.push(point_y);
}
drawFace(datas);
}
/**
* Dessin de la face
*/
private function drawFace(vertices:Vector< Float >):Void {
surface.graphics.clear();
surface.graphics.lineStyle(2,0x84B2ED);
surface.graphics.beginFill(0x3C3C3C);
surface.graphics.drawPath(commands, vertices);
surface.graphics.endFill();
}
/**
* Rotation de la face
*/
private function rotation_Y(delta:Float, vertex:Vector3D):Vector3D {
var cosTmp:Float = Math.cos(5 * Math.PI / 180);
var sinTmp:Float = Math.sin(5 * Math.PI / 180);
// définition des nouveaux points
var point_x : Float = vertex.x * cosTmp - vertex.z * sinTmp;
var point_z : Float = vertex.x * sinTmp + vertex.z * cosTmp;
return new Vector3D(point_x,vertex.y,point_z,1);
}
public static function main():Void {
new Test3D(flash.Lib.current);
}
}
Quelques précisions :
Le tableau commands permet de déterminer la commande qui sera associée au point contenu dans le tableau vertices.- MOVE_TO : déplacement d'un point vers une position
- LINE_TO : dessin d'une ligne à partir d'un point jusqu'à un autre

Tweet This!
Write a comment