Flex 3 SDKとで三次元を遊ぶ[11]
/**
* Copyright Akiyah ( http://wonderfl.net/user/Akiyah )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/8N9o
*/
package {
import flash.events.Event;
import flash.events.MouseEvent;
import org.papervision3d.core.effects.view.ReflectionView;
import org.papervision3d.core.math.Number3D;
import org.papervision3d.core.proto.MaterialObject3D;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.scenes.Scene3D;
public class DoubutsuShogi extends ReflectionView {
public static var plane_for_drug:Plane;
public static var select_koma:Koma;
public var mouse_points:Array = [];
public function DoubutsuShogi() {
viewport.interactive = true;
createKomas([[Hiyoko, 2, 3], [Zou, 1, 4], [Tora, 2, 4], [Kirin, 3, 4]], scene);
scene.addChild(createBoard());
plane_for_drug = createPlane();
scene.addChild(plane_for_drug);
addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_UP, released);
}
public function createBoard():Cube {
var boardTopMaterial:BitmapFileMaterial = new BitmapFileMaterial(Koma.IMAGE_URL + Koma.BOARD_TOP_URL);
var boardMaterial:BitmapFileMaterial = new BitmapFileMaterial(Koma.IMAGE_URL + Koma.BOARD_URL);
boardTopMaterial.interactive = true;
var mlist:MaterialsList = new MaterialsList( { all:boardMaterial, top:boardTopMaterial } );
var board:Cube = new Cube(mlist, Koma.UNIT_SIZE / 2 * 3 + 10, Koma.UNIT_SIZE / 2 * 4 + 10, 10, 3, 2, 4);
board.y = 5;
return board;
}
public function createKomas(komas:Array, scene:Scene3D):void {
var komaData:Array;
var koma:Koma;
for each(komaData in komas) {
koma = new komaData[0]();
koma.move(komaData[1], komaData[2]);
scene.addChild(koma.cube);
}
for each(komaData in komas) {
koma = new komaData[0]();
koma.move(4 - komaData[1], 5 - komaData[2]);
koma.turn();
scene.addChild(koma.cube);
}
}
public function createPlane():Plane {
var material:MaterialObject3D = new MaterialObject3D();
//var material:MaterialObject3D = new WireframeMaterial();
material.doubleSided = true;
material.interactive = true;
var plane:Plane = new Plane(material, 400, 400, 10, 10);
plane.pitch(90);
plane.y = 80;
plane.visible = false;
plane.addEventListener(InteractiveScene3DEvent.OBJECT_MOVE, moved);
return plane;
}
public function released(event:MouseEvent):void {
plane_for_drug.visible = false;
mouse_points = [];
if (select_koma) {
select_koma.released(event);
select_koma = null;
}
}
public function moved(event:InteractiveScene3DEvent):void {
if (!select_koma) {
return;
}
var x:Number = event.renderHitData.x;
var y:Number = event.renderHitData.y;
var z:Number = event.renderHitData.z;
mouse_points.unshift(new Number3D(x, y, z));
if (mouse_points.length > 1) {
select_koma.cube.x += diff(mouse_points, 'x');
select_koma.cube.z += diff(mouse_points, 'z');
}
if (mouse_points.length > 2) {
if (go_and_back(mouse_points, 'x')) {
select_koma.reverse();
}
if (go_and_back(mouse_points, 'z')) {
select_koma.turn();
}
}
}
public function go_and_back(points:Array, attribute:String):Boolean {
var d0:Number = diff(points, attribute);
var d1:Number = diff(points, attribute, 1);
return d0 * d1 < 0 && Math.abs(d0) > 10 && Math.abs(d1) > 10;
}
public function diff(points:Array, attribute:String, index:Number = 0):Number {
return points[index][attribute] - points[index + 1][attribute];
}
public function update(e:Event):void {
var c:Number = (1 - ((mouseX / stage.width) - 0.5) * 0.5) * (Math.PI / 4);
var d:Number = (1 - ((mouseY / stage.height) - 0.5) * 0.5) * (Math.PI / 4);
camera.x = Math.sin(c) * Math.cos(d) * 200;
camera.z = Math.cos(c) * Math.cos(d) * 200;
camera.y = Math.sin(d) * 200;
singleRender();
}
public static function select(koma:Koma):void {
plane_for_drug.visible = true;
select_koma = koma;
}
}
}
import caurina.transitions.Tweener;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.system.Security;
import flash.geom.Rectangle;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.materials.special.CompositeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Cube;
class Koma {
public var cube:Cube;
public static const UNIT_SIZE:Number = 100;
public static const KOMA_SIZE:Number = UNIT_SIZE * 0.8;
Security.loadPolicyFile("http://assets.wonderfl.net/crossdomain.xml");
public static const IMAGE_URL:String = "http://assets.wonderfl.net/images/related_images/";
public static const HIYOKO_URL:String = "7/73/7362/7362fe45e00bbd8b73cc67acad5ab2af451b52e2"
public static const KIRIN_URL:String = "8/8a/8a66/8a66a59b06d675822b75b08ec8a8fcfa392b236a"
public static const ZOU_URL:String = "e/e6/e6e6/e6e6a6096b7ccd7e4dfedb5e508fa10dd0b9a9fa"
public static const TORA_URL:String = "d/d5/d5d7/d5d7101a782a3f9b48a1fa722f5b5547edfb7a79"
public static const NIWATORI_URL:String = "4/41/41ad/41ad48895eacaf9249f91dd8ed7f2bd0f76a1562"
public static const BOARD_URL:String = "d/d3/d308/d308eea2f48ce3922aace07079a2b1c443897a3b"
public static const BOARD_TOP_URL:String = "8/89/89e5/89e5f619ecaece6a7c294fe9d85a6af305f2742c"
public static const KI_URL:String = "b/b8/b850/b850141ea815c9ec0c5225deb8e0d12d3805c72cm"
public function Koma(filenameTop:String, directionTop:Array, filenameBottom:String = null, directionBottom:Array = null) {
cube = createCube(filenameTop, directionTop, filenameBottom, directionBottom);
cube.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, pressed);
}
public function pressed(event:InteractiveScene3DEvent):void {
DoubutsuShogi.select(this);
Tweener.addTween(cube, {
y: UNIT_SIZE / 4 + 20,
time:1,
transition: 'easeInOutCubic'
});
}
public function released(event:MouseEvent):void {
Tweener.addTween(cube, {
y: UNIT_SIZE / 4,
time:1,
transition: 'easeInOutCubic'
});
}
public function move(x:Number, y:Number):void {
cube.x = (x - 1) * UNIT_SIZE / 2 - UNIT_SIZE / 2;
cube.y = UNIT_SIZE / 4;
cube.z = - (y - 1.5) * UNIT_SIZE / 2 + UNIT_SIZE / 2;
}
public function turn():void {
if (Tweener.isTweening(cube)) {
return;
}
Tweener.addTween(cube, {
rotationY: cube.rotationY + 180,
time:1,
transition: 'easeInOutCubic'
});
}
public function reverse():void {}
private function createCube(filenameTop:String, directionTop:Array, filenameBottom:String, directionBottom:Array):Cube {
var materialsList:MaterialsList = new MaterialsList( { all:materialKi} );
var materialKi:BitmapFileMaterial = new BitmapFileMaterial(Koma.IMAGE_URL + Koma.KI_URL);
materialKi.interactive = true
materialsList.addMaterial(materialKi, "all")
materialsList.addMaterial(createCompositeMaterial(filenameTop, directionTop), "top")
if (filenameBottom != null) {
materialsList.addMaterial(createCompositeMaterial(filenameBottom, directionBottom), "bottom")
}
return new Cube(materialsList, KOMA_SIZE / 2, KOMA_SIZE / 2, KOMA_SIZE / 4, 2, 2, 2);
}
private function createCompositeMaterial(filename:String, direction:Array):CompositeMaterial {
var material:CompositeMaterial = new CompositeMaterial();
material.addMaterial(new BitmapFileMaterial(Koma.IMAGE_URL + filename));
material.addMaterial(createDirectionMarkMaterial(direction));
material.interactive = true;
return material;
}
private function createDirectionMarkMaterial(direction:Array):BitmapMaterial {
var bd:BitmapData = new BitmapData(100, 100, true, 0x00000000);
for (var j:Number = 0; j < 3; j++) {
for (var i:Number = 0; i < 3; i++) {
if (direction[j][i] == 1) {
bd.fillRect(new Rectangle(5 + i*40, 5 + j*40, 10, 10), 0xFFFF0000);
}
}
}
return new BitmapMaterial(bd);
}
}
class Hiyoko extends Koma {
public function Hiyoko() {
super(Koma.HIYOKO_URL, [[0,1,0],[0,0,0],[0,0,0]],
Koma.NIWATORI_URL, [[1,1,1],[1,0,1],[0,1,0]]);
}
public override function reverse():void {
if (Tweener.isTweening(cube)) {
return;
}
Tweener.addTween(cube, {
rotationZ: cube.rotationZ + 180,
time:1,
transition: 'easeInOutCubic'
});
}
}
class Kirin extends Koma {
public function Kirin() {
super(Koma.KIRIN_URL, [[0,1,0],[1,0,1],[0,1,0]]);
}
}
class Tora extends Koma {
public function Tora() {
super(Koma.TORA_URL, [[1,1,1],[1,0,1],[1,1,1]]);
}
}
class Zou extends Koma {
public function Zou() {
super(Koma.ZOU_URL, [[1,0,1],[0,0,0],[1,0,1]]);
}
}