forked from: forked from: [PV3D] Field of Blur
Papervision3Dで被写界深度
* 参照: http://clockmaker.jp/blog/2008/07/pv3d_gw_blur/
// forked from arfyasu's forked from: [PV3D] Field of Blur
// forked from clockmaker's [PV3D] Field of Blur
/**
* Papervision3Dで被写界深度
* 参照: http://clockmaker.jp/blog/2008/07/pv3d_gw_blur/
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import org.papervision3d.view.*;
import org.papervision3d.materials.*;
import org.papervision3d.objects.*;
import org.papervision3d.objects.primitives.*
[SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#000000")]
public class Main extends BasicView
{
// const vars
static private const OBJ_LENGTH :int = 20;
static private const CIRCLE_RANGE :int = 500;
static private const CIRCLE_RANGE2 :int = 450;
static private const CIRCLE_RANGE3 :int = 400;
// 3d vars
private var list :Array = []
private var list2 :Array = []
private var list3 :Array = []
private var wrap :DisplayObject3D;
private var wrap2 :DisplayObject3D;
private var wrap3 :DisplayObject3D;
/**
* Constructor
*/
public function Main()
{
//camera
camera.x = 0;
camera.y = 500;
camera.z = 1000;
camera.rotationX = 5;
// wrap
wrap = new DisplayObject3D();
scene.addChild(wrap);
wrap2 = new DisplayObject3D();
scene.addChild(wrap2);
wrap3 = new DisplayObject3D();
scene.addChild(wrap3);
for (var i:int = 0; i < OBJ_LENGTH; i++)
{
var rot:Number = 360 * i / OBJ_LENGTH ;
var m:ColorMaterial = new ColorMaterial(Math.random() * 0xffffff);
m.doubleSided = true;
var o:Plane = new Plane(m, 100, 100);
o.x = CIRCLE_RANGE * Math.sin(rot * Math.PI / 180);
o.y = 0;
o.z = CIRCLE_RANGE * Math.cos(rot * Math.PI / 180);
o.rotationY = rot;
o.useOwnContainer = true; //ココ重要 これがないとblewしない
// 配列に追加 & wrapの子供にする
list.push(wrap.addChild(o));
}
for (var j:int = 0; j < OBJ_LENGTH; j++)
{
rot = 360 * j / OBJ_LENGTH ;
m = new ColorMaterial(0x0000CC);
m.doubleSided = true;
o = new Plane(m, 60, 60);
o.x = CIRCLE_RANGE2 * Math.sin(rot * Math.PI / 180);
o.y = 0;
o.z = CIRCLE_RANGE2 * Math.cos(rot * Math.PI / 180);
o.rotationY = rot;
o.useOwnContainer = true; //ココ重要 これがないとblewしない
// 配列に追加 & wrapの子供にする
list2.push(wrap2.addChild(o));
}
for (var k:int = 0; k < OBJ_LENGTH; k++)
{
rot = 360 * k / OBJ_LENGTH ;
m = new ColorMaterial(0x999999);
m.doubleSided = true;
o = new Plane(m, 40, 40);
o.x = CIRCLE_RANGE3 * Math.sin(rot * Math.PI / 180);
o.y = 0;
o.z = CIRCLE_RANGE3 * Math.cos(rot * Math.PI / 180);
o.rotationY = rot;
o.useOwnContainer = true; //ココ重要 これがないとblewしない
// 配列に追加 & wrapの子供にする
list3.push(wrap3.addChild(o));
}
addEventListener(Event.ENTER_FRAME, loop);
addEventListener(Event.ENTER_FRAME, loop2);
addEventListener(Event.ENTER_FRAME, loop3);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
startRendering();
}
// loop
private function loop(event:Event):void
{
wrap.yaw(-1);
// wrap.pitch(-1);
// wrap.roll(-1);
// 被写界深度
for (var i:int = 0; i < list.length; i++)
{
var o:DisplayObject3D = list[i] as DisplayObject3D;
// var blur:Number = Math.ceil((CIRCLE_RANGE2 - o.sceneZ) / 50);
var blur:Number = calcDistanceFromCamera(o) * .005;
o.filters = [new BlurFilter(blur, blur, 2)];
}
}
// loop
private function loop2(event:Event):void
{
wrap2.yaw(2);
wrap2.pitch(1);
// 被写界深度
for (var i:int = 0; i < list2.length; i++)
{
var o:DisplayObject3D = list2[i] as DisplayObject3D;
// var blur:Number = Math.ceil((CIRCLE_RANGE2 - o.sceneZ) / 50);
var blur:Number = calcDistanceFromCamera(o) * .005;
o.filters = [new BlurFilter(blur, blur, 2)];
}
}
// loop
private function loop3(event:Event):void
{
wrap3.yaw(-4);
wrap3.pitch(-1);
// 被写界深度
for (var i:int = 0; i < list3.length; i++)
{
var o:DisplayObject3D = list3[i] as DisplayObject3D;
// var blur:Number = Math.ceil((CIRCLE_RANGE2 - o.sceneZ) / 50);
var blur:Number = calcDistanceFromCamera(o) * .005;
o.filters = [new BlurFilter(blur, blur, 2)];
}
}
private function calcDistanceFromCamera(obj:DisplayObject3D):Number {
var dx:Number = obj.sceneX - camera.x;
var dy:Number = obj.sceneY - camera.y;
var dz:Number = obj.sceneZ - camera.z;
return Math.abs(Math.sqrt(dx * dx + dy * dy + dz * dz));
}
private function onMouseMove(e:MouseEvent):void {
// if pitch == 0, everything disappears?
var pitch : Number = (-mouseY * 0.3);
if (pitch >= 0)
pitch = -0.1;
camera.orbit(pitch, mouseX * 0.8);
}
}
}