-
package
-
{
-
import flash.display.Sprite;
-
import flash.events.Event;
-
import flash.events.MouseEvent;
-
import flash.text.TextField;
-
import org.papervision3d.materials.ColorMaterial;
-
import org.papervision3d.materials.MovieMaterial;
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.objects.primitives.Cube;
-
import org.papervision3d.view.BasicView;
-
-
/**
-
* ...
-
* @author Charlie Schulze, charlie[at]woveninteractive[dot]com
-
*/
-
-
public class Main extends BasicView
-
{
-
protected var cube:Cube;
-
protected var interactiveMats:Array;
-
protected var materialsList:MaterialsList;
-
protected var targetrotationX:Number;
-
protected var targetrotationY:Number;
-
protected var targetrotationZ:Number;
-
protected var tweening:Boolean;
-
-
public function Main():void
-
{
-
super();
-
init();
-
}
-
protected function init():void
-
{
-
createChildren();
-
startRendering();
-
}
-
protected function createChildren():void
-
{
-
//Set the viewport to interactive
-
viewport.interactive = true;
-
-
//Create Materials:
-
materialsList = new MaterialsList();
-
interactiveMats = ["front", "back", "left", "right", "bottom", "top"];
-
var colorsArray:Array = [0x76b6f8, 0x4291e1, 0x1f73c8, 0xe77111, 0xe8914c, 0xfad2b2];
-
-
for (var i:int = 0; i <interactiveMats.length; i++)
-
{
-
//Create a color box so we can use our MouseEvents
-
var colorBox:Sprite = new Sprite();
-
colorBox.graphics.beginFill(colorsArray[i]);
-
colorBox.graphics.drawRect(0, 0, 100, 100);
-
colorBox.graphics.endFill();
-
colorBox.name = interactiveMats[i];
-
-
//Add a textField for reference
-
var textField:TextField = new TextField()
-
colorBox.addChild(textField)
-
textField.text = interactiveMats[i];
-
-
//Add a MouseEvent to the Sprite
-
colorBox.mouseChildren = false;
-
colorBox.addEventListener(MouseEvent.CLICK, onMovieMatClicked);
-
-
//Create the MovieMat
-
var movieMat:MovieMaterial = new MovieMaterial(colorBox, true, true);
-
movieMat.interactive = true;
-
movieMat.smooth = true;
-
materialsList.addMaterial(movieMat, interactiveMats[i]);
-
}
-
-
//Create Cube
-
cube = new Cube(materialsList, 100, 100, 100);
-
-
//Add cube to the scene
-
scene.addChild(cube);
-
}
-
-
protected function onMovieMatClicked(evt:MouseEvent):void
-
{
-
if (tweening)
-
{
-
// Let it rotate again
-
tweening = false;
-
}
-
else
-
{
-
switch(evt.target.name) {
-
case "front":
-
targetrotationX = 0;
-
targetrotationY = 180;
-
targetrotationZ = 0;
-
tweening = true;
-
break;
-
case "back":
-
targetrotationX = 0;
-
targetrotationY = 0;
-
targetrotationZ = 0;
-
tweening = true;
-
break;
-
case "left":
-
targetrotationX = 0;
-
targetrotationY = -90;
-
targetrotationZ = 0;
-
tweening = true;
-
break;
-
case "right":
-
targetrotationX = 0;
-
targetrotationY = 90;
-
targetrotationZ = 0;
-
tweening = true;
-
break;
-
case "top":
-
targetrotationX = -90;
-
targetrotationY = 0;
-
targetrotationZ = 0;
-
tweening = true;
-
break;
-
case "bottom":
-
targetrotationX = 90;
-
targetrotationY = 0;
-
targetrotationZ = 180;
-
tweening = true;
-
break;
-
}
-
}
-
}
-
-
override protected function onRenderTick(event:Event = null):void
-
{
-
super.onRenderTick(event);
-
-
if (tweening) {
-
// If a face has been clicked
-
if (camera.zoom <230) {
-
// If the camera isn't zoomed enough then zoom in a bit more:
-
camera.zoom += Math.sqrt(230-camera.zoom)/5;
-
}
-
// Test each rotation and rotate it towards the target rotation:
-
// X axis:
-
if (cube.rotationX <targetrotationX)
-
{
-
cube.rotationX += Math.sqrt(targetrotationX-cube.rotationX);
-
cube.rotationX = Math.round(cube.rotationX);
-
}
-
else if (cube.rotationX> targetrotationX)
-
{
-
cube.rotationX -= Math.sqrt(cube.rotationX-targetrotationX);
-
cube.rotationX = Math.round(cube.rotationX);
-
}
-
-
// Y axis:
-
if (cube.rotationY <targetrotationY)
-
{
-
cube.rotationY += Math.sqrt(targetrotationY-cube.rotationY);
-
cube.rotationY = Math.round(cube.rotationY);
-
}
-
else if (cube.rotationY> targetrotationY)
-
{
-
cube.rotationY -= Math.sqrt(cube.rotationY-targetrotationY);
-
cube.rotationY = Math.round(cube.rotationY);
-
}
-
-
// Z axis:
-
if (cube.rotationZ <targetrotationZ)
-
{
-
cube.rotationZ += Math.sqrt(targetrotationZ-cube.rotationZ);
-
cube.rotationZ = Math.round(cube.rotationZ);
-
}
-
else if (cube.rotationZ> targetrotationZ)
-
{
-
cube.rotationZ -= Math.sqrt(cube.rotationZ-targetrotationZ);
-
cube.rotationZ = Math.round(cube.rotationZ);
-
}
-
}
-
else
-
{
-
// If the camera is zoomed in, it shouldn't be now
-
if (camera.zoom> 200)
-
{
-
// So zoom out a bit.
-
camera.zoom -= Math.sqrt(camera.zoom-2)/5;
-
}
-
// Rotate the cube a bit:
-
cube.rotationX += 2;
-
cube.rotationY += 2;
-
// Make sure that we dont "wind up" the rotation
-
if (cube.rotationX>= 360) cube.rotationX = 0;
-
if (cube.rotationY>= 360) cube.rotationY = 0;
-
}
-
}
-
}
-
}