下面看来来相册的示意图。
上图最后一行为i = 0,第一列为j = 0。
cx、cy的公式如下:
cx = columns* planeSpacing * 0.5 - planeSpacing * 0.5;
cy = rows* planeSpacing * 0.5 - planeSpacing * 0.5;
只要求出cx、cy的坐标,就可以使用下面的公式算出所有平面的位置。
plane.x =–cx + j * planeSpacing;
plane.y =–cy + i * planeSpacing;
公式x由j控制,因为其对应横坐标。y由i控制,因为其对应纵坐标。
当i = 0, j = 0时,
plane.x =–cx;
plane.y =–cy;
当i = 0, j = 1时,
plane.x =–cx;
plane.y =–cy + planeSpacing;
当i = 1, j = 1时,
plane.x =–cx + planeSpacing;
plane.y =–cy + planeSpacing;
下面是相册的完整代码:
package
{
import flash.events.Event;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.view.BasicView;
import org.papervision3d.events.InteractiveScene3DEvent;
/**
* ...
* @author hellopv3d
*/
public class InteractionExample extends BasicView
{
private var container:DisplayObject3D;//定义一个容器,存放所有的平面
public function InteractionExample()
{
viewport.interactive = true;
//创建容器并加入场景
container = new DisplayObject3D();
scene.addChild(container);
//定义行数和列数
var row:int = 2;
var col:int = 2;
//定义平面大小
var planeSize:int = 300;
//两个平面的距离
var planeSpacing:int = 306;
//求出基点坐标
var cx:Number = planeSpacing * col * 0.5 - planeSpacing * 0.5;
var cy:Number = planeSpacing * row * 0.5 - planeSpacing * 0.5;
for (var i:int = 0; i < row; i++) {
for (var j:int = 0; j < col; j++ ) {
//var m:ColorMaterial = new ColorMaterial(0xFFFFFF * Math.random());
var s:String = String(i) + String(j) + ".jpg";
var m:BitmapFileMaterial = new BitmapFileMaterial("../assets/" + s);
m.interactive = true;
var plane:Plane = new Plane(m, planeSize, planeSize, 10, 10);
plane.x = -cx + j * planeSpacing;
plane.y = -cy + i * planeSpacing;
//添加事件处理,当鼠标移动到plane上时,调用onOver
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, onOver);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, onOut);
container.addChild(plane);
}
}
startRendering();
}
private function onOver(e:InteractiveScene3DEvent):void {
e.target.scaleX = 2;
e.target.scaleY = 2;
e.target.z -= 20;
}
private function onOut(e:InteractiveScene3DEvent):void {
e.target.scaleX = 1;
e.target.scaleY = 1;
e.target.z += 20;
}
override protected function onRenderTick(e:Event = null):void {
super.onRenderTick();
var xDist:Number = mouseX - stage.stageWidth / 2;
var yDist:Number = mouseY - stage.stageHeight / 2;
camera.x += (xDist - camera.x * 0.5) * 0.3;
camera.y += (yDist - camera.y * 0.5) * 0.3;
camera.z += (-mouseY * 2 - camera.z) * 0.05;
}
}
}