PaperVision3D Test
...
@author 9re
package
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.utils.Timer;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.view.BasicView;
import gs.easing.Quad;
import gs.TweenLite;
/**
* ...
* @author 9re
*/
[SWF(frameRate="31")]
public class PaperVision3D_Test_0 extends BasicView
{
private const NUM_SLICES:int = 5;
private const WIDTH:int = 465;
private const HEIGHT:int = 300;
private const K:Number = 0.1;
private var cubes:Array = [];
private var _targetRotation:int;
private var _animated:Boolean = true;
private var _timer:Timer;
private var _vx:Number = 0;
private var _vy:Number = 0;
private var _fx:Number = 0.0;
private var _fy:Number = 0.0;
private var _fz:Number = 1.0;
public function PaperVision3D_Test_0()
{
var sliceWidth:Number = WIDTH / NUM_SLICES;
super(WIDTH, HEIGHT);
opaqueBackground = 0x000000;
camera.focus = 100;
camera.zoom = 8;
camera.ortho = false;
var bd:BitmapData;
var wordList:Array = ["Hello, wonderfl!!", "PaperVision3D Test", "_____9__R__E_____", "Tweening 3D Object"];
var tfm:TextFormat = new TextFormat("_sans", 40, 0xffffff, null, null, null, null, null, TextFormatAlign.CENTER);
var sp:Sprite = new Sprite();
sp.graphics.beginFill(0);
sp.graphics.lineStyle(1, 0xffffff);
sp.graphics.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
sp.graphics.endFill();
var tf:TextField;
var cube:Cube;
var imageList:Array = [];
for (var i:int = 0; i < wordList.length; i++) {
if (tf != null)
sp.removeChild(tf);
tf = new TextField();
tf.defaultTextFormat = tfm;
tf.width = WIDTH;
tf.text = wordList[i];
tf.y = 130;
sp.addChild(tf);
bd = new BitmapData(WIDTH, HEIGHT);
bd.draw(sp);
imageList.push(bd);
}
var materialList:MaterialsList = new MaterialsList();
var xPos:Number;
for (i = 0; i < NUM_SLICES; i++) {
materialList = new MaterialsList();
xPos = sliceWidth * i;
materialList.addMaterial(sliceBitmapData(imageList[0] as BitmapData, xPos, sliceWidth, true), "front");
materialList.addMaterial(sliceBitmapData(imageList[1] as BitmapData, xPos, sliceWidth), "back");
materialList.addMaterial(sliceBitmapData(imageList[2] as BitmapData, xPos, sliceWidth), "top");
materialList.addMaterial(sliceBitmapData(imageList[3] as BitmapData, xPos, sliceWidth, true), "bottom");
materialList.addMaterial(new ColorMaterial(0xffffff), "right");
materialList.addMaterial(new ColorMaterial(0xffffff), "left");
cube = new Cube(materialList, sliceWidth, HEIGHT, HEIGHT);
cube.x = xPos - WIDTH / 2 + sliceWidth / 2;
cube.z = HEIGHT / 2;
scene.addChild(cube);
cubes.push(cube);
}
startRendering();
_timer = new Timer(3000);
_timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);
_timer.start();
_fz = _camera.z;
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.CLICK, mouseClickHandler);
}
private function enterFrameHandler(e:Event):void
{
_fy += (mouseX - WIDTH / 2) / WIDTH * K;
_fx += (mouseY - HEIGHT / 2) / HEIGHT * K;
_camera.y = 1000 * Math.sin(_fx);
_camera.x = 1000 * Math.cos(_fx) * Math.sin(_fy);
_camera.z = 1000 * Math.cos(_fx) * Math.cos(_fy);
}
private function timerHandler(e:TimerEvent):void
{
if (!_animated) {
_timer.stop();
_timer = null;
}
mouseClickHandler(null);
}
private function mouseClickHandler(e:Event):void
{
if (e != null) {
_animated = false;
}
_targetRotation += 90;
_targetRotation %= 360;
cubes.forEach(cubes_forEachCallBack);
}
private function cubes_forEachCallBack(cube:Cube, index:int, array:Array):void
{
TweenLite.to(cube, ++index * 0.2, { rotationX:_targetRotation, ease:Quad.easeInOut } );
}
private function sliceBitmapData(bitmapData:BitmapData, sliceX:Number, sliceWidth:Number, isFlipped:Boolean = false):BitmapMaterial {
var bd:BitmapData = new BitmapData(sliceWidth, HEIGHT);
if (isFlipped) {
var matrix:Matrix = new Matrix();
matrix.translate( -WIDTH / NUM_SLICES, -HEIGHT / 2);
matrix.rotate(Math.PI);
matrix.translate(sliceX, HEIGHT / 2);
bd.draw(bitmapData, matrix);
} else {
bd.copyPixels(bitmapData, new Rectangle(sliceX, 0, sliceWidth, HEIGHT), new Point());
}
return new BitmapMaterial(bd, true);
}
}
}