package {
/**
*
* Auteur: Vincent Helwig
* Date: 14.10.2008
* Website: http://www.tsoin.com
* Description: Démo Sandy3D Box / Sphere / Plane / Cone
* Gestion Filaire / Texture
* Smooth
*
**/
import AsyncEngine.Scheduler;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getTimer;
import net.hires.utils.Stats;
import sandy.core.Scene3D;
import sandy.core.scenegraph.Camera3D;
import sandy.core.scenegraph.Group;
import sandy.core.scenegraph.Shape3D;
import sandy.materials.Appearance;
import sandy.materials.BitmapMaterial;
import sandy.materials.ColorMaterial;
import sandy.materials.attributes.LineAttributes;
import sandy.materials.attributes.MaterialAttributes;
import sandy.primitive.Box;
import sandy.primitive.Cone;
import sandy.primitive.Plane3D;
import sandy.primitive.Sphere;
[SWF(backgroundColor="#000000", frameRate="100", width="600", height="400")]
public class Sandy3D_004 extends Sprite {
private var scene:Scene3D;
private var camera:Camera3D;
private var group:Group;
private var box:Box;
private var sphere:Sphere;
private var plane:Plane3D;
private var cone:Cone;
private var currentPrimitive:Shape3D;
private var _textFaces:TextField;
private var _tempsCreation:uint;
private var _texture:Boolean = false;
private var _smoothTexture:Boolean = false;
private var _numSegments:uint = 3;
private var currentPrimitiveName:String = "box";
private var _appereance:Appearance;
private var _appereanceBitmap:BitmapMaterial;
private static const MIN_FACES_BOX:uint = 3;
private static const MIN_FACES_SPHERE:uint = 8;
private static const MIN_FACES_PLANE:uint = 3;
private static const MIN_FACES_CONE:uint = 6;
private static const MAX_Z:int = -200;
private static const NB_BOUCLE_TEST:uint = 10000;
private static const TEXT_EXPLICATION:String = "" +
"Utiliser la molette pour modifier la coordonée z de la camera<br>" +
"Action du clavier : <br>" +
" + / - : Augmente / Diminue le nombre de segments<br>" +
" T / F : Mode texture / Mode filaire<br>" +
" L : Active/Désactive le lissage de la texture<br>" +
" C / S / P / B : Forme Cone / Sphere / Plane / Box<br><br>";
[Embed(source="AdobeFlash9.png")]
private var _textureImage:Class;
public function Sandy3D_004() {
addEventListener(Event.ADDED_TO_STAGE, init);
Scheduler.init( this );
var __stats:Stats = new Stats();
__stats.width = 75;
__stats.x = 525;
addChild( __stats );
var __textFormat:TextFormat = new TextFormat();
__textFormat.font = "Arial";
__textFormat.size = 9;
_textFaces = new TextField();
_textFaces.selectable = false;
_textFaces.multiline = true
_textFaces.width = stage.stageWidth;
_textFaces.height = stage.stageHeight;
_textFaces.defaultTextFormat = __textFormat;
_textFaces.textColor = 0x999999;
addChild( _textFaces );
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
setupScene();
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
}
private function setupScene():void {
camera = new Camera3D(600, 400);
camera.z = -300;
group = new Group("Group");
var __skin:MaterialAttributes = new MaterialAttributes(new LineAttributes(1, 0x789789) );
_appereance = new Appearance( new ColorMaterial( 0x000000, 1, __skin) );
_appereanceBitmap = new BitmapMaterial( new _textureImage().bitmapData);
scene = new Scene3D("scene", this, camera, group);
createPrimitive();
}
/**
*
* Création de la primivite suivant la valeur de la variable currentPrimitiveName
*
**/
private function createPrimitive():void {
if ( currentPrimitive != null && currentPrimitive.parent != null ) { currentPrimitive.parent.removeChildByName(currentPrimitive.name); }
var __t:Number = getTimer();
switch ( currentPrimitiveName ) {
default :
case "box" : currentPrimitive = createCube(); break;
case "sphere" : currentPrimitive = createSphere(); break;
case "plane" : currentPrimitive = createPlane(); break;
case "cone" : currentPrimitive = createCone(); break;
}
if ( _texture ) {
_appereanceBitmap.smooth = _smoothTexture;
currentPrimitive.appearance = new Appearance( _appereanceBitmap );
} else { currentPrimitive.appearance = _appereance; }
group.addChild(currentPrimitive);
onMouseMove();
_tempsCreation = getTimer() - __t;
numSegments = _numSegments;
}
private function createCube():Box {
box = new Box("box", 100, 100, 100);
return box;
}
private function createSphere():Sphere {
sphere = new Sphere("sphere", 100, _numSegments, _numSegments);
return sphere;
}
private function createPlane():Plane3D {
plane = new Plane3D("plane", 100, 100);
return plane;
}
private function createCone():Cone {
cone = new Cone("cone", 100, 100, _numSegments, _numSegments);
return cone;
}
private function onEnterFrame( event :Event ):void {
scene.render();
}
private function onMouseMove(event:Event = null):void {
currentPrimitive.rotateX = ( stage.mouseY * 180 ) / stage.stageHeight - 90;
currentPrimitive.rotateY = ( stage.mouseX * 180 ) / stage.stageWidth - 90;
}
private function onKeyDown(event:KeyboardEvent):void {
var __change:Boolean = true;
switch ( event.keyCode ) {
case 107 : case 187 : numSegments = _numSegments+1;
break;
case 109 : case 54 : if ( _numSegments != 3 ) { numSegments = _numSegments-1; }
break;
case 83 : currentPrimitiveName = "sphere";
if ( _numSegments < MIN_FACES_SPHERE ) numSegments = MIN_FACES_SPHERE;
break;
case 66 : currentPrimitiveName = "box";
if ( _numSegments < MIN_FACES_BOX ) numSegments = MIN_FACES_BOX;
break;
case 80 : currentPrimitiveName = "plane";
if ( _numSegments < MIN_FACES_PLANE ) numSegments = MIN_FACES_PLANE;
break;
case 67 : currentPrimitiveName = "cone";
if ( _numSegments < MIN_FACES_CONE ) numSegments = MIN_FACES_CONE;
break;
case 70 : _texture = false;
break;
case 84 : _texture = true;
break;
case 76 : _smoothTexture = !_smoothTexture;
break;
default : __change = false;
trace(event.keyCode);
}
if (__change) { createPrimitive(); }
}
/**
*
* Action de la molette de la souris
* On modifie la coordonnée z de la camera
*
**/
private function onMouseWheel(event:MouseEvent):void {
camera.z += event.delta * 10;
if ( camera.z > MAX_Z) camera.z = MAX_Z;
numSegments = _numSegments;
}
/**
*
* Setter du nombre de segments
* Mise à jour le texte
*
**/
private function set numSegments(nb:uint):void {
_numSegments = nb;
_textFaces.htmlText = TEXT_EXPLICATION + "<b>SegmentsH && segmentsW = </b>" + _numSegments.toString();
if ( camera != null) { _textFaces.htmlText += "<b>Zoom camera : </b>" + camera.z; }
if ( currentPrimitive != null) {
_textFaces.htmlText += "<b>Nb vertices : </b>" + currentPrimitive.aPolygons.length;
}
_textFaces.htmlText += "<b>Temps création object3D : </b>" + _tempsCreation + " ms";
_textFaces.htmlText += "<b>Lissage texture : </b>" + _smoothTexture;
}
}
}