flash on 2010-10-9
/**
* Copyright uwi ( http://wonderfl.net/user/uwi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/z1lE
*/
package {
import org.papervision3d.view.BasicView;
import flash.text.TextField;
import flash.events.Event;
import net.hires.debug.Stats;
import org.papervision3d.objects.special.*;
import org.papervision3d.materials.*;
import org.papervision3d.core.proto.*;
import org.papervision3d.objects.primitives.*;
import org.papervision3d.objects.*;
import org.papervision3d.core.math.*;
import org.libspark.betweenas3.*;
import org.libspark.betweenas3.tweens.*;
[SWF(backgroundColor="0x000000", frameRate="60")]
public class PV3D extends BasicView {
private var _tf : TextField;
private var _cells : Array;
private var _tos : Array;
public function PV3D() {
super(0, 0, true, false);
_tf = new TextField();
addChild(_tf);
_tf.textColor = 0xffffff;
_tf.width = 100;
_tf.height = 465;
// addChild(new Stats());
// scene.addChild(new UCS());
var R : Number = 500;
var T : Number = 2 * Math.PI / 57;
var H : Number = 2 * R * Math.sin(T / 2);
// var wfm : MaterialObject3D = new WireframeMaterial(0xffffff);
var wfm : MaterialObject3D = new ColorMaterial(0xdddddd);
var W : MaterialObject3D = new ColorMaterial(0xdddddd);
var B : MaterialObject3D = new ColorMaterial(0x333333);
W.doubleSided = true;
B.doubleSided = true;
var O : DisplayObject3D = new DisplayObject3D();
var sw : uint = 12;
var sh : uint = 12;
/*
var S : Sphere = new Sphere(wfm, 500, sw, sh);
scene.addChild(S);
tr(S.geometry.vertices.length);
for(var i : uint = 1;i < sw * (sh - 1) + 2 - 1;i++){
if((i - 1) % sw < sw / 2){
S.geometry.vertices[i].z = -S.geometry.vertices[i].z;
}
}
*/
_cells = [];
_tos = [];
var arB : Array = [];
var arW : Array = [];
var y : Number, x : Number;
for(y = -14;y <= 14;y++){
for(x = -14;x <= 14;x++){
((x + y) % 2 == 0 ? arW : arB).push({
x : R * Math.sin(x * T) * Math.cos(y * T),
y : R * Math.sin(y * T),
z : -R * Math.cos(x * T) * Math.cos(y * T) + 2000
// oy : y,
// ox : x
});
}
}
var i : uint, j : uint, o : *;
for(i = 1;i < arB.length;i++){
j = Math.random() * (i - 1);
o = arB[i]; arB[i] = arB[j]; arB[j] = o;
}
for(i = 1;i < arW.length;i++){
j = Math.random() * (i - 1);
o = arW[i]; arW[i] = arW[j]; arW[j] = o;
}
var ppb : uint = 0;
var ppw : uint = 0;
for(y = -14;y <= 14;y++){
for(x = -14;x <= 14;x++){
var p : Plane = new Plane((x + y) % 2 == 0 ? W : B, H, H, 1, 1);
p.x = R * Math.sin(x * T) * Math.cos(y * T);
p.y = R * Math.sin(y * T);
p.z = R * Math.cos(x * T) * Math.cos(y * T);
p.lookAt(O);
scene.addChild(p);
_cells.push(p);
var ev : Boolean = (x + y) % 2 == 0;
if(ev){
o = arW[ppw++];
}else{
o = arB[ppb++];
}
var it : IObjectTween = BetweenAS3.to(p, o, Math.random() * 12 + 3);
/*
var it : IObjectTween = BetweenAS3.to(p, {
x : R * Math.sin(x * T) * Math.cos(y * T),
y : R * Math.sin(y * T),
z : -R * Math.cos(x * T) * Math.cos(y * T) + 2000
}, Math.random() * 13 + 3);
*/
it.onUpdateParams = [it];
it.onUpdate = function(val : *) : void
{
// DisplayObject3D(val.target).lookAt(O);
// var tz : Number = it.position / it.duration * 2000;
var targ : DisplayObject3D = val.target;
PV3DUtils.setLookAtN(targ,
new Number3D(0 - targ.x, 0 - targ.y, 2000 - targ.z),
new Number3D(0, 1, 0)
);
};
_tos.push(it);
}
}
BetweenAS3.delay(
BetweenAS3.parallelTweens(_tos)
, 1).play();
_camera = new QCamera3D(new Number3D(0, 0, 1), new Number3D(Math.cos(0.3), 0, -Math.sin(0.3)));
_camera.x = 0;
_camera.y = 0;
_camera.z = 700;
startRendering();
}
private var targ : DisplayObject3D;
private var _t : Number = 0.6;
override protected function onRenderTick(e : Event = null) : void
{
// QCamera3D(_camera)._front = new Number3D(Math.cos(_t), 0, -Math.sin(_t));
QCamera3D(_camera)._front = new Number3D(Math.cos(0.4), 0, Math.sin(0.4));
QCamera3D(_camera).head();
// SphereCamera(_camera).move(0, 0.01);
// setLookAt(camera, new Number3D(1, 0, 0), new Number3D(0, 0, 1));
//_t -= 0.02;
/*
_t += 0.1;
camera.orbit(_t, _t * 2);
*/
super.onRenderTick(e);
}
private function tr(...o : Array) : void
{
_tf.appendText(o + "\n");
_tf.scrollV = _tf.maxScrollV;
}
}
}
import org.papervision3d.objects.*;
import org.papervision3d.core.math.*;
class PV3DUtils
{
public static function rotate(x : Number3D, axis : Number3D, angle : Number) : Number3D
{
var nCos:Number = Math.cos(angle);
var nSin:Number = Math.sin(angle);
var scos:Number = 1 - nCos;
var sxy :Number = axis.x * axis.y * scos;
var syz :Number = axis.y * axis.z * scos;
var sxz :Number = axis.x * axis.z * scos;
var sz :Number = nSin * axis.z;
var sy :Number = nSin * axis.y;
var sx :Number = nSin * axis.x;
var nx : Number = (nCos + axis.x * axis.x * scos) * x.x + (-sz + sxy) * x.y + (sy + sxz) * x.z;
var ny : Number = (sz + sxy) * x.x + (nCos + axis.y * axis.y * scos) * x.y + (-sx + syz) * x.z;
var nz : Number = (-sy + sxz) * x.x + (sx + syz) * x.y + (nCos + axis.z * axis.z * scos) * x.z;
x.x = nx; x.y = ny; x.z = nz;
return x;
}
public static function rotateMulti(xs : Array, axis : Number3D, angle : Number) : void
{
var nCos:Number = Math.cos(angle);
var nSin:Number = Math.sin(angle);
var scos:Number = 1 - nCos;
var sxy :Number = axis.x * axis.y * scos;
var syz :Number = axis.y * axis.z * scos;
var sxz :Number = axis.x * axis.z * scos;
var sz :Number = nSin * axis.z;
var sy :Number = nSin * axis.y;
var sx :Number = nSin * axis.x;
for each(var x : Number3D in xs){
var nx : Number = (nCos + axis.x * axis.x * scos) * x.x + (-sz + sxy) * x.y + (sy + sxz) * x.z;
var ny : Number = (sz + sxy) * x.x + (nCos + axis.y * axis.y * scos) * x.y + (-sx + syz) * x.z;
var nz : Number = (-sy + sxz) * x.x + (sx + syz) * x.y + (nCos + axis.z * axis.z * scos) * x.z;
x.x = nx; x.y = ny; x.z = nz;
}
}
public static function setLookAt(d : DisplayObject3D, front : Number3D, up : Number3D) : void
{
var X : Number3D = Number3D.cross(front, up);
var look : Matrix3D = d.transform;
look.n11 = X.x; look.n21 = X.y; look.n31 = X.z;
look.n12 = -up.x; look.n22 = -up.y; look.n32 = -up.z;
look.n13 = front.x; look.n23 = front.y; look.n33 = front.z;
}
public static function setLookAtN(d : DisplayObject3D, front : Number3D, up : Number3D) : void
{
var Z : Number3D = front.clone();
Z.normalize();
var X : Number3D = Number3D.cross(Z, up);
X.normalize();
var Y : Number3D = Number3D.cross(Z, X);
Y.normalize();
var look : Matrix3D = d.transform;
look.n11 = X.x; look.n21 = X.y; look.n31 = X.z;
look.n12 = -Y.x; look.n22 = -Y.y; look.n32 = -Y.z;
look.n13 = Z.x; look.n23 = Z.y; look.n33 = Z.z;
}
}
import org.papervision3d.cameras.*;
class QCamera3D extends Camera3D
{
public var _up : Number3D; // カメラの上の向きの単位ベクトル
public var _front : Number3D;
private var _prevDir : Number3D;
public function QCamera3D(up : Number3D, front : Number3D= null)
{
super();
_up = null;
init(up, front);
}
// prevDirからcurDirへ向ける回転を_upにかけるだけ。カメラ自体に操作はしない
public function rotate(curDir : Number3D) : void
{
if(_prevDir != null){
var n : Number3D = Number3D.cross(curDir, _prevDir);
// if(n.moduloSquared > 0.00000001){
if(n.moduloSquared != 0){
n.normalize();
var angle : Number = Math.acos(Number3D.dot(_prevDir, curDir));
if(_front != null){
PV3DUtils.rotate(_front, n, angle);
}
PV3DUtils.rotate(_up, n, angle);
}
}
_prevDir = curDir.clone();
}
// カメラを_frontのほうへ向ける
public function head() : void
{
if(_front != null){
var Z : Number3D = _front.clone();
Z.normalize();
var X : Number3D = Number3D.cross(Z, _up);
X.normalize();
var Y : Number3D = Number3D.cross(Z, X);
Y.normalize();
var look : Matrix3D = this.transform;
look.n11 = X.x*this.scaleX; look.n21 = X.y*this.scaleY; look.n31 = X.z*this.scaleZ;
look.n12 = -Y.x*this.scaleX; look.n22 = -Y.y*this.scaleY; look.n32 = -Y.z*this.scaleZ;
look.n13 = Z.x*this.scaleX; look.n23 = Z.y*this.scaleY; look.n33 = Z.z*this.scaleZ;
}
}
public function init(up : Number3D = null, front : Number3D = null) : void
{
if(up != null){
_up = up.clone();
_up.normalize();
}
if(front != null){
_front = front.clone();
_front.normalize();
}else{
_front = null;
}
_prevDir = null;
}
}