マウスでぐりぐりズームと回転3D
メモ:
InteractiveScene3DEvent.OBJECT_CLICKなどのマウスのイベントは、
マテリアルのinteractiveと
BasicViewのコンストラクタの第四引数のinteractiveを
trueにしないと取得できない。
/**
* Copyright lla ( http://wonderfl.net/user/lla )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/e4Miu
*/
package {
import flash.display.Sprite;
import caurina.transitions.Tweener;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.view.BasicView;
import org.papervision3d.objects.DisplayObject3D;
public class FlashTest extends Sprite {
public static var tf: TextField ;
public function FlashTest() {
stage.quality = flash.display.StageQuality.LOW;
stage.frameRate = 10;
//2d
tf = new TextField();
tf.border = true;
tf.x = tf.y = 20;
tf.width = 400;
tf.selectable = false;
addChild(tf);
var view : Test = new Test();
addChild(view);
/*
var lock2 : Array = [false];
view.cube.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, function(e:InteractiveScene3DEvent): void{
Scale.expand3D(view.cube, Math.random()*2, 0.25, lock2);
});
*/
/////////////
var drag: Drag = new Drag(stage);
view.addEventListener(Drag.MOUSE_DRAG, function(e: MouseEvent): void{
view.rootNode.rotationY -= drag.diffX;
view.rootNode.rotationX -= drag.diffY;
//tf.text = String(drag.diffX);
});
/*
var cameraDefaultZ = view.camera.z;
var posY = 0;
var posX = 0;
view.addEventListener(Drag.MOUSE_DRAG, function(e: MouseEvent): void{
posY += drag.diffY;
var wtY = Math.PI*2 / 180.0 * posY;
view.camera.y = cameraDefaultZ * Math.sin(wtY);
//view.camera.z = cameraDefaultZ * Math.cos(wtY);
posX += drag.diffX;
var wtX = Math.PI*2 / 180.0 * posX;
view.camera.x = cameraDefaultZ * Math.sin(wtX);
view.camera.z = cameraDefaultZ * Math.cos(wtX);
//tf.text = String([int(view.camera.z)]);
});
*/
////////////////
var zoomButton = new TextField();
zoomButton.border = true;
zoomButton.x = 20;
zoomButton.y = 120;
zoomButton.selectable = false;
zoomButton.text = "Zoom";
zoomButton.autoSize = flash.text.TextFieldAutoSize.LEFT;
addChild(zoomButton);
zoomButton.addEventListener(Drag.MOUSE_DRAG, function(e: MouseEvent): void{
view.camera.zoom -= drag.diffY;
//tf.text = String(drag.diffX);
});
}
}
}
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.view.*
import org.papervision3d.materials.*
import org.papervision3d.materials.utils.*
import org.papervision3d.materials.shadematerials.*
import org.papervision3d.objects.primitives.*
import org.papervision3d.lights.*
import org.papervision3d.cameras.CameraType;
import flash.events.Event;
class Test extends BasicView{
public var sphere: Sphere;
public var cube: Cube;
public var arrow: Arrow;
public var rootNode : DisplayObject3D;
public function Test(){
super(0, 0, true, true);
startRendering();
rootNode = new DisplayObject3D();
scene.addChild(rootNode);
var material: WireframeMaterial = new WireframeMaterial();
//material.doubleSided = true;
var light: PointLight3D = new PointLight3D();
var shadeMaterial: FlatShadeMaterial = new FlatShadeMaterial(light, 0x3399FF);
material.interactive = true;
shadeMaterial.interactive = true;
shadeMaterial.doubleSided = true;
//shadeMaterial.smooth = true;
//sphere = new Sphere(material, 400, 15, 15);
//scene.addChild(sphere);
//var matsList : MaterialsList = new MaterialsList( { all: new MovieAssetMaterial( "Front", true )});
var matsList : MaterialsList = new MaterialsList( { all: material, back: shadeMaterial, right: shadeMaterial, top: shadeMaterial});
cube = new Cube(matsList, 200,200,200);
rootNode.addChild(cube);
//camera.zoom = 100;
arrow = new Arrow(shadeMaterial);
arrow.rotationX = -90;
arrow.scale = 0.25;
arrow.y = 200;
rootNode.addChild(arrow);
}
}
import caurina.transitions.Tweener;
import flash.display.DisplayObject;
import org.papervision3d.objects.DisplayObject3D;
//import flash.text.TextField;
class Scale {
public static function expand3D(obj: DisplayObject3D, scale: Number, time: Number, doing: Array): void{
if(doing[0]){ return; }
doing[0] = true;
var tweenParams1: Object = {
scale: scale,
time: time,
transition:'easeOutQuad',
onComplete:function():void{ }
};
var tweenParams2: Object = {
scale: 1,
time:time,
delay:time,
transition:'easeOutQuad',
onComplete:function(): void{ doing[0] = false; }
};
Tweener.addTween(obj, tweenParams1);
Tweener.addTween(obj, tweenParams2);
}
}
import flash.display.DisplayObject;
//import flash.geom.Point;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import flash.display.DisplayObjectContainer;
class Drag {
public var offsetY : Number;
public var offsetX : Number;
public var diffX: Number;
public var diffY: Number;
public var obj: DisplayObjectContainer;
public var updatePriority: int;
public var objectUnderMouse: Object;
public var bubbles: Boolean;
public static var MOUSE_DRAG: String = "mouseDrag";
//public var callback: Function;
//public var objectsUnderMouse: Array;
//public var topOnly : Boolean;
private static function clamp(val: Number, min: Number, max: Number): Number{
return Math.max(min, Math.min(max, val));
}
public static function cram(rect: Rectangle, obj: DisplayObject): void {
obj.x = clamp(obj.x, rect.x, rect.width - obj.width);
obj.y = clamp(obj.y, rect.y, rect.height - obj.height);
}
public function Drag(obj: DisplayObjectContainer, bubbles: Boolean = true){
obj.addEventListener(MouseEvent.MOUSE_DOWN, start);
obj.addEventListener(MouseEvent.MOUSE_UP, stop);
obj.addEventListener(Event.MOUSE_LEAVE, stop);
this.obj = obj;
diffX = diffY = 0;
updatePriority = 2;
this.bubbles = bubbles;
//this.topOnly = true;
}
public function start(e: Event): void{
offsetX = obj.mouseX;
offsetY = obj.mouseY;
//FlashTest.tf.text = String([int(offsetX), int(offsetY)]);
//objectsUnderMouse = obj.getObjectsUnderPoint(new Point(obj.mouseX, obj.mouseY));
objectUnderMouse = e.target;
obj.addEventListener(MouseEvent.MOUSE_MOVE, drag, false, updatePriority);
}
public function stop(e: Event): void{
obj.removeEventListener(MouseEvent.MOUSE_MOVE, drag);
}
//e.stageX,Y を使うとpapervisionの座標が混じってしまう。
public function drag(e: Event): void{
//FlashTest.tf.text = String([int(e.stageX), int(e.stageY)]);
diffX = (obj.mouseX - offsetX);
diffY = (obj.mouseY - offsetY);
offsetX = obj.mouseX;
offsetY = obj.mouseY;
//callback(e);
//FlashTest.tf.text = String(e.target);
//ここで取得するとオブジェクトをマウスで押すような動作ができる。
//objectsUnderMouse = obj.getObjectsUnderPoint(new Point(obj.mouseX, obj.mouseY));
//objectUnderMouse = e.target;
var event: MouseEvent = new MouseEvent(MOUSE_DRAG, this.bubbles);
objectUnderMouse.dispatchEvent(event);
/*
if(objectsUnderMouse.length == 0) {
obj.dispatchEvent(event);
} else if(topOnly) {
//前面のオブジェクトにだけイベントを伝える場合
objectsUnderMouse[objectsUnderMouse.length-1].dispatchEvent(event);
} else {
for(var i:uint = 0; i<objectsUnderMouse.length; i++) {
objectsUnderMouse[i].dispatchEvent(event);
}
}
*/
//e.updateAfterEvent();
}
}