Papervision3D - マウスに反応して回転
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.objects.primitives.*;
import org.papervision3d.materials.*;
import org.papervision3d.materials.special.CompositeMaterial;
import org.papervision3d.core.math.Matrix3D;
import org.papervision3d.materials.utils.MaterialsList;
public class Papervision3DSample extends Sprite {
private var scene:Scene3D;
private var viewport:Viewport3D;
private var camera:Camera3D;
private var renderer:BasicRenderEngine;
private var cube:Cube;
public function Papervision3DSample() {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.frameRate = 30;
init();
}
private function init():void {
// viewport
viewport = new Viewport3D(0,0,true);
viewport.opaqueBackground = 0x000000;
addChild(viewport);
// renderer
renderer = new BasicRenderEngine();
// camera
camera = new Camera3D();
camera.focus = 500;
camera.zoom = 1;
// scene
scene = new Scene3D();
// object
cube = new Cube(getMaterialsList(), 350, 350, 350, 1, 1, 1);
scene.addChild(cube);
// event
addEventListener(Event.ENTER_FRAME, onEnerFrameHandler);
}
private function getMaterialsList():MaterialsList {
// CompositeMaterial
var colorMat:ColorMaterial = new ColorMaterial( 0x006699, 1 );
var wireMat:WireframeMaterial = new WireframeMaterial( 0x0099cc );
var compoMat:CompositeMaterial = new CompositeMaterial();
compoMat.addMaterial(colorMat);
compoMat.addMaterial(wireMat);
compoMat.doubleSided = true;
return new MaterialsList({
front :compoMat
,back :compoMat
,right :compoMat
,left :compoMat
,top :compoMat
,bottom:compoMat
});
}
private var rotX:Number = 0;
private var rotY:Number = 0;
private function onEnerFrameHandler(evt:Event):void{
// マウス座標でオブジェクト全体を回転
rotY += this.mouseY / 50;
rotX += this.mouseX / 50;
cube.rotationY = rotY;
cube.rotationX = rotX;
renderer.renderScene(scene,camera,viewport);
}
}
}