Cubo Dragon Numero Cinco
---WHAT THE--- Dragon v5
A dragon varient of creating waves and passing them down an array.
With Bitmaps!! yeah!! skin!!
---CREDIT WHERE CREDIT IS DUE---
If it weren't for the artistic skills of Bryan "Lite Duty" Moore, I don't think
there would of been the motivation to take this another step further
towards really freaking cool.
--CONTROLS--- Dragons are unweidly creatures and hard to control, but they can be influenced.
MOUSE position controls the turning speeds.
CLICKing switches between camera postions
ARROW Keys, PgDn, and End keys lend XYZ camera control to look at the beast
---Things to do:
-Dynamic path Id to maintain spread with speed.
or Relativisitic approach to setting positions.
-long cube primitive with max = w.segs, 1 = h.segs? convert geometry into indexs
-long cylinder primitve?
-rotation wouldn't matter on everything, just the settings of the first four points
-could use rotate func in Number3D? better/worse?
-pyramid with sphere primitive for head. correctly app
/**
* Copyright mfc314159 ( http://wonderfl.net/user/mfc314159 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/pfv9
*/
package {
// ---WHAT THE--- Dragon v5
// A dragon varient of creating waves and passing them down an array.
// With Bitmaps!! yeah!! skin!!
// ---CREDIT WHERE CREDIT IS DUE---
// If it weren't for the artistic skills of Bryan "Lite Duty" Moore, I don't think
// there would of been the motivation to take this another step further
// towards really freaking cool.
//
// --CONTROLS--- Dragons are unweidly creatures and hard to control, but they can be influenced.
// MOUSE position controls the turning speeds.
// CLICKing switches between camera postions
// ARROW Keys, PgDn, and End keys lend XYZ camera control to look at the beast
//
// ---Things to do:
//-Dynamic path Id to maintain spread with speed.
// or Relativisitic approach to setting positions.
//-long cube primitive with max = w.segs, 1 = h.segs? convert geometry into indexs
//-long cylinder primitve?
//-rotation wouldn't matter on everything, just the settings of the first four points
//-could use rotate func in Number3D? better/worse?
//-pyramid with sphere primitive for head. correctly applying bitMaps? how the..?
import flash.system.Security;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.geom.Point;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.objects.primitives.Sphere;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.objects.*
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.core.math.Number3D;
public class dragonFiveWFL extends Sprite {
private var cont:DisplayObject3D;
//Papervision init
private var viewport:Viewport3D;
private var scene:Scene3D;
private var renderer:BasicRenderEngine;
private var camera:Camera3D;
private var followOn:int = 1;
private var camPoint:DisplayObject3D;
private var camNum:Number3D;
private var materialsList:MaterialsList;
private var cube:Cube;
private var headR:Plane;
private var headL:Plane;
//--Everyone loves Pie
private const piE:Number = Math.PI / 180;
//--Reusuable id# to use with While loops
private var id:int = 0;
//--Number of segments
private var max:int = 75;
//--Spread of segements
private var spread:int = 1;
private var path:Array = []; //traveled path
private var _dragon:Array = []; //segment objects
private var pathID:Array = []; //segment's path ID#
//--Direction Angles
private var xAng:Number = 0.00;//= Math.random() * 180.00;
private var yAng:Number = 0.00;//= Math.random() * 180.00;
private var zAng:Number = 0.00;//= Math.random() * 180.00;
//--Turing Speeds
private var turnSpeed:Number = 1.00;
private var turnSpeedX:Number = turnSpeed / 9;
private var turnSpeedY:Number = turnSpeed / 12;
private var turnSpeedZ:Number = turnSpeed / 25;
//--Leading Path vars
private var speed:Number = 1.00;
private var course:Number3D;
private var origin:Number3D;
private var cubeSizes:Array = [];
private var pathTotal:Number = 0.00;
private var pttl:int;
private var basePath:String;
private var image1:String;
private var image2:String;
private var image3:String;
private var image4:String;
private var image5:String;
public function dragonFiveWFL():void {
//-------------------------MAIN INIT
Security.loadPolicyFile("http://assets.wonderfl.net/crossdomain.xml");
//--PAPERVISION INIT
viewport = new Viewport3D();
viewport.opaqueBackground = 0x000000;
viewport.viewportWidth = 1000;
viewport.viewportHeight = 700;
viewport.x = ( this.stage.stageWidth - viewport.viewportWidth ) / 2;
viewport.y = ( this.stage.stageHeight - viewport.viewportHeight ) / 2;
addChild(viewport);
scene = new Scene3D();
camera = new Camera3D();
camera.z = 100;
camPoint = new DisplayObject3D(null);
scene.addChild(camPoint);
renderer = new BasicRenderEngine();
basePath = "http://assets.wonderfl.net/images/related_images/";
image1 = basePath + "9/9d/9d64/9d64fbd23c06129662930ed51fe4b6d2a3efc337";
image2 = basePath + "c/c1/c10b/c10b0eeafb504156c8559d9f6881642ca8ff92ea";
image3 = basePath + "2/2f/2fce/2fcecc0bc53fce5f510b9d33c3df51c094ee3023";
image4 = basePath + "e/eb/eb49/eb49c19b46edbb20d85d1b8e9657d947e33984c8";
image5 = basePath + "9/9c/9c30/9c30645b34c30e7532181f68796289b851eece2a";
var scales:BitmapFileMaterial = new BitmapFileMaterial(image1);
var scales2:BitmapFileMaterial = new BitmapFileMaterial(image2);
var belly:BitmapFileMaterial = new BitmapFileMaterial(image5);
var headBmp:BitmapFileMaterial = new BitmapFileMaterial(image4);
var headBmp2:BitmapFileMaterial = new BitmapFileMaterial(image3);
headBmp.doubleSided = true;
headBmp2.doubleSided = true;
//--Add dragon material to list
materialsList = new MaterialsList();
materialsList.addMaterial(scales , "front");
materialsList.addMaterial(scales , "back");
materialsList.addMaterial(scales2 , "left");
materialsList.addMaterial(scales , "right");
materialsList.addMaterial(scales, "top");
materialsList.addMaterial(belly , "bottom");
//--planes color material
var colorOne:ColorMaterial = new ColorMaterial(0xFF0000, .5);
//--------------------------------------------PATH INIT
course = new Number3D();
origin = new Number3D();
//--init course
course.x = speed * Math.sin(piE * xAng);
course.y = speed * Math.cos(piE * yAng);
course.z = speed * Math.sin(piE * zAng);
//--origin position array
//Cube Size
//150
//140
//130
//120
//110
//100 dxxxxxxxxxdd
//90 d d
//80 | | | | | | | | | | |
//70 ddd d
//60 dd d
//50 xxxxxx
//40 | | | | | | | d | | |
//30 ddddddd
//20 dd
//10 ddddd
// 0 10 20 30 40 50 60 70 80 90 100
// || = 2.5% Dragon Cube Scale
// x = static sizing d = dynamic sizing
var p1:Point = new Point(0, 50);
var p2:Point = new Point(15, 50);
var p3:Point = new Point(30, 150);
var p4:Point = new Point(60, 150);
var p5:Point = new Point(70, 30);
var p6:Point = new Point(90, 20);
var p7:Point = new Point(100, 10);
id = 0;
var idF:Number;
var cs:Point = new Point();
while(id < 100) {
if((id >= 0) && (id < p2.x)) {
idF = (id - p1.x) / (p2.x - p1.x);
cs = Point.interpolate(p2, p1, idF);
}
if((id >= p2.x) && (id < p3.x)) {
idF = (id - p2.x) / (p3.x - p2.x);
cs = Point.interpolate(p3, p2, idF);
}
if((id >= p3.x) && (id < p4.x)) {
idF = (id - p3.x) / (p4.x - p3.x);
cs = Point.interpolate(p4, p3, idF);
}
if((id >= p4.x) && (id < p5.x)) {
idF = (id - p4.x) / (p5.x - p4.x);
cs = Point.interpolate(p5, p4, idF);
}
if((id >= p5.x) && (id < p6.x)) {
idF = (id - p5.x) / (p6.x - p5.x);
cs = Point.interpolate(p6, p5, idF);
}
if((id >= p6.x) && (id < p7.x)) {
idF = (id - p6.x) / (p7.x - p6.x);
cs = Point.interpolate(p7, p6, idF);
}
cubeSizes[id] = cs.y;
id++;
}
//--------------------------------------------------OBJECTS INIT
//--DRAGON
var segSize:int = 0;
//add segments to the _dragon array
//set pathIDs of segments in _dragon
id = 0;
pathTotal -= (p1.y / 2)
while(id < max) {
segSize = cubeSizes[Math.round(100 * (id / max))];
pathTotal += segSize;
//trace(id, segSize, pathTotal);
if(id != 0) {
//Add Cube for segment
_dragon[id] = new Cube(materialsList, segSize, segSize, segSize, 1, 1, 1);
_dragon[id].pitch(-90);
pathID[id] = pathTotal;//int(spread + (id * spread));
} else {
//Add Cube with head dimensions for first segment
//_dragon[0] = new Cube(materialsList, segSize, segSize * 2, segSize, 4, 4, 4);
_dragon[0] = new DisplayObject3D();
headR = new Plane(headBmp, 133, 187, 1, 1);
headL = new Plane(headBmp2, 133, 187, 1, 1);
headR.x += 48;
headL.x -= 48;//good enough for goverment work
headR.pitch(-33);
headL.pitch(-33);
headR.yaw(45);
headL.yaw(-45);
_dragon[0].addChild(headR);
_dragon[0].addChild(headL);
pathID[0] = Math.round(p1.y - (p1.y / 2));
}
scene.addChild(_dragon[id]); //add segment to scene
//set segment id# on path
id++; //next id
}
pttl = Math.round(pathTotal) + 1;
id = 0;
//Fill the path array with init values
setup();
//Camera.z
stage.addEventListener(KeyboardEvent.KEY_DOWN, cameraZ);
//--Change Camera
stage.addEventListener(MouseEvent.CLICK, changeCamera);
//--Generate path from mouse input
stage.addEventListener(MouseEvent.MOUSE_MOVE, mousePath);
//--Change Speed
stage.addEventListener(MouseEvent.MOUSE_WHEEL, changeSpeed);
//--start Main Loop
stage.addEventListener(Event.ENTER_FRAME, mainLoop);
}
//------------------------------------------MOUSE INPUT HANDLERS
private function changeCamera(evt:MouseEvent):void {
followOn *= -1;
if(followOn == 1) {
camera.x = 0;
camera.y = 0;
camera.z = 100;
}
}
private function cameraZ(evt:KeyboardEvent):void {
trace(evt.keyCode, evt.keyLocation, evt.charCode);
if(evt.keyCode == 38) { camera.z += 100; }//in
if(evt.keyCode == 40) { camera.z -= 100; }//out
if(evt.keyCode == 34) { camera.y += 100; }//up
if(evt.keyCode == 35) { camera.y -= 100; }//down
if(evt.keyCode == 39) { camera.x += 100; }//right
if(evt.keyCode == 37) { camera.x -= 100; }//left
}
private function mousePath(evt:MouseEvent):void {
turnSpeedX = .02 * ((evt.stageX - 500) * .05);
turnSpeedY = .02 * ((evt.stageY - 350) * .05);
turnSpeedZ = Math.sqrt((turnSpeedX * turnSpeedX) + (turnSpeedY * turnSpeedY));
}
private function changeSpeed(evt:MouseEvent):void {
if(evt.delta > 0) { speed += .1;
} else { speed -= .1; }
//trace(speed);
}
//----------------------------------------MAIN LOOP
private function mainLoop(evt:Event):void {
//--STAGE 1: Find Next Position
//change angles
xAng += turnSpeedX;
yAng += turnSpeedY;
zAng += turnSpeedZ;
//limit angles
if(xAng > 360) { xAng = xAng - 360; }
if(yAng > 360) { yAng = yAng - 360; }
if(zAng > 360) { zAng = zAng - 360; }
//set new course
course.x = speed * Math.sin(piE * xAng);
course.y = speed * Math.cos(piE * yAng);
course.z = speed * Math.sin(piE * zAng);
//add course coordinates to current position
origin = Number3D.add(origin, course);
camNum = Number3D.add(origin, course);
camNum = Number3D.add(course, camNum);
camPoint.x = camNum.x;
camPoint.y = camNum.y;
camPoint.z = camNum.z;
_dragon[0].lookAt(camPoint);
//--STAGE 2:--Shift and Add positions to path array
//shift path positions down
path.pos.pop();
//path.p.pop();
//path.r.pop();
//path.y.pop();
//add new positions to path
path.pos.unshift(origin);
//--STAGE 3:--Set Positions
id = 1; //reset
_dragon[0].x = path.pos[pathID[0]].x;
_dragon[0].y = path.pos[pathID[0]].y;
_dragon[0].z = path.pos[pathID[0]].z;
while(id < max) {
_dragon[id].x = path.pos[pathID[id]].x;
_dragon[id].y = path.pos[pathID[id]].y;
_dragon[id].z = path.pos[pathID[id]].z;
_dragon[id].lookAt( _dragon[id - 1] );
id++;
}
//--STAGE 4:--Render The Scene
if(followOn == 1) {
//cont.yaw(.5);
//cont.pitch(-.3);
//camera.lookAt(); //_dragon[0]);
} else {
camera.x = _dragon[0].x;
camera.y = _dragon[0].y;
camera.z = _dragon[0].z - 400;
camera.lookAt(camPoint);
}
renderer.renderScene(scene, camera, viewport);
}
private function setup():void {
//fills the path array with values to cut down on inital drawing time
//--path positions array
path["pos"] = new Array(); // 3D position
path["p"] = new Array(); // pitch
path["r"] = new Array(); // roll
path["y"] = new Array(); // yaw
while(id < pttl) {
path.pos[id] = origin.clone();
path.p[id] = Number(0.00);
path.r[id] = Number(0.00);
path.y[id] = Number(0.00);
id++; //next id
}
id = 0; //reset
while(id < pttl){
//--STAGE 1
//find next position
xAng += turnSpeedX;
yAng += turnSpeedY;
zAng += turnSpeedZ;
if(xAng > 360) { xAng = xAng - 360; }
if(yAng > 360) { yAng = yAng - 360; }
if(zAng > 360) { zAng = zAng - 360; }
course.x = speed * Math.sin(piE * xAng);
course.y = speed * Math.cos(piE * yAng);
course.z = speed * Math.sin(piE * zAng);
origin = Number3D.add(origin, course);
//--STAGE 2
//shift positions down
path.pos.pop();
path.p.pop();
path.r.pop();
path.y.pop();
//add new position to path
path.pos.unshift(origin);
path.p.unshift(Number(0.00));
path.r.unshift(Number(0.00));
path.y.unshift(Number(0.00));
id++;
}
}//func
}//class
}//package