Thursday, January 1, 2009

CODE

package {


import away3d.events.VideoEvent;

import com.transmote.flar.FLARManager;
import com.transmote.flar.marker.FLARMarkerEvent;
import com.transmote.utils.time.FramerateDisplay;
import com.transmote.flar.marker.FLARMarker;
import com.transmote.flar.utils.geom.FLARPVGeomUtils;

// Basic
import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.parsers.DAE;
import org.papervision3d.render.LazyRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;


import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.events.InteractiveScene3DEvent;

import flash.display.Sprite;
import flash.events.Event;

//For Video
//import fl.video.*;

// http://code.google.com/p/tweener/
import caurina.transitions.Tweener;

[SWF(width="640", height="480", frameRate="40", backgroundColor="#FFFFFF")]

public class Story3DAnimationMyths extends Sprite {

private var fm:FLARManager;
private var scene:Scene3D;
private var camera:FLARCamera3D;
private var view:Viewport3D;
private var lre:LazyRenderEngine;

private var marker0:FLARMarker;
private var marker1:FLARMarker;
private var marker2:FLARMarker;
private var marker3:FLARMarker;
//private var marker4:FLARMarker;
//private var marker5:FLARMarker;
//private var marker6:FLARMarker;
private var marker7:FLARMarker;
private var marker8:FLARMarker;
private var marker9:FLARMarker;
private var marker10:FLARMarker;
private var marker11:FLARMarker;
private var marker12:FLARMarker;

private var container0:DisplayObject3D;
private var container1:DisplayObject3D;
private var container2:DisplayObject3D;
private var container3:DisplayObject3D;
//private var container4:DisplayObject3D;
//private var container5:DisplayObject3D;
//private var container6:DisplayObject3D;
private var container7:DisplayObject3D;
private var container8:DisplayObject3D;
private var container9:DisplayObject3D;
private var container10:DisplayObject3D;
private var container11:DisplayObject3D;
private var container12:DisplayObject3D;

private var pointLight:PointLight3D;

private var p:Plane;
private var v:Vid;

private var p2:Plane;
private var v2:Vid;

private var planeWidth:uint = 320;
private var planeHeight:uint = 240;
private var quality:uint = 8;

//[Embed (source = "../src/media/Butterfly.swf")]
//[Embed (source = '../src/media/Butterfly.swf')]


public function Story3DAnimationMyths() {

initFLAR();

v = new Vid();
v.vid.source = "../src/kramer.m4v";
v.vid.stop();

v2 = new Vid();
v2.vid.source = "../src/media/ODYSSEY.mov";
v2.vid.stop();

}

private function initFLAR():void
{
fm = new FLARManager("flarConfig.xml");

fm.addEventListener(FLARMarkerEvent.MARKER_ADDED, onAdded);
fm.addEventListener(FLARMarkerEvent.MARKER_REMOVED, onRemoved);

fm.addEventListener(Event.INIT, init3D);

addChild(Sprite(fm.flarSource));

}

private function onAdded(e:FLARMarkerEvent):void
{
trace("Added");
trace(e.marker.patternId.toString());

if ( e.marker.patternId == 0 ) {
marker0 = e.marker;
container0.visible = true;

} else if ( e.marker.patternId == 1 ){
marker1 = e.marker;
container1.visible = true;

} else if ( e.marker.patternId == 2 ){
marker2 = e.marker;
container2.visible = true;

} else if ( e.marker.patternId == 3 ){
marker3 = e.marker;
container3.visible = true;

} /* else if ( e.marker.patternId == 4 ){
marker4 = e.marker;
container4.visible = true;

} else if ( e.marker.patternId == 5 ){
marker5 = e.marker;
container5.visible = true;

} else if ( e.marker.patternId == 6 ){
marker6 = e.marker;
container6.visible = true;

} */ else if ( e.marker.patternId == 7 ){
marker7 = e.marker;
container7.visible = true;

} else if ( e.marker.patternId == 8 ){
marker8 = e.marker;
container8.visible = true;

} else if ( e.marker.patternId == 9 ){
marker9 = e.marker;
container9.visible = true;

} else if ( e.marker.patternId == 10 ){
marker10 = e.marker;
container10.visible = true;

} else if ( e.marker.patternId == 11 ){
marker11 = e.marker;
p.visible = true;
v.vid.play();
container11.visible = true;

} else if ( e.marker.patternId == 12 ){
marker12 = e.marker;
container12.visible = true;

} else {
//nomarker
}

}

private function onRemoved(e:FLARMarkerEvent):void
{
trace("Removed");
trace(e.marker.patternId.toString());

if ( e.marker.patternId == 0 ) {
marker0 = null;
container0.visible = false;

} else if ( e.marker.patternId == 1 ){
marker1 = null;
container1.visible = false;

} else if ( e.marker.patternId == 2 ){
marker2 = null;
container2.visible = false;

} else if ( e.marker.patternId == 3 ){
marker3 = null;
container3.visible = false;

} /*else if ( e.marker.patternId == 4 ){
marker4 = null;
container4.visible = false;

} else if ( e.marker.patternId == 5 ){
marker5 = null;
container5.visible = false;

} else if ( e.marker.patternId == 6 ){
marker6 = null;
container6.visible = false;

} */ else if ( e.marker.patternId == 7 ){
marker7 = null;
container7.visible = false;

} else if ( e.marker.patternId == 8 ){
marker8 = null;
container8.visible = false;

} else if ( e.marker.patternId == 9 ){
marker9 = null;
container9.visible = false;

} else if ( e.marker.patternId == 10 ){
marker10 = null;
container10.visible = false;

} else if ( e.marker.patternId == 11 ){
marker11 = null;
p.visible = false;
v.vid.stop();
container11.visible = false;

} else if ( e.marker.patternId == 12 ){
marker12 = null;
container12.visible = false;

} else {
//nomarker
}

}

private function init3D(e:Event):void
{
scene = new Scene3D();
camera = new FLARCamera3D(fm.cameraParams);
camera.z = -30;

view = new Viewport3D(640, 480, true);
lre = new LazyRenderEngine(scene, camera, view);

var mat:MovieMaterial = new MovieMaterial(v, false, true);
p = new Plane(mat, 320, 240, 2, 2);
p.scaleY = -1;
p.rotationZ = 90;
p.visible = false;


var mat2:MovieMaterial = new MovieMaterial(v2, false, true);
p2 = new Plane(mat2, 320, 240, 2, 2);
p2.scaleY = -1;
p2.rotationZ = 90;
p2.visible = false;

pointLight = new PointLight3D();
pointLight.x = 1000;
pointLight.y = 1000;
pointLight.z = -1000;

// load the model.
// Model may have to be scaled and rotated to fit the marker; every model is different.

var model0:DAE = new DAE(true, "model", true);
var model1:DAE = new DAE(true, "model", true);
var model2:DAE = new DAE(true, "model", true);
var model3:DAE = new DAE(true, "model", true);
//var model4:DAE = new DAE(true, "model", true);
//var model5:DAE = new DAE(true, "model", true);
//var model6:DAE = new DAE(true, "model", true);
var model7:DAE = new DAE(true, "model", true);
var model8:DAE = new DAE(true, "model", true);
var model9:DAE = new DAE(true, "model", true);
var model10:DAE = new DAE(true, "model", true);
//var model11:DAE = new DAE(true, "model", true);
//var model12:DAE = new DAE(true, "model", true);

model0.load("../src/Models/Blender2Banana.dae");
model0.scale = 8.0;
model0.rotationX = 30;
model0.rotationY = 45;
model0.rotationZ = 45;


model1.load("../src/Models/corn.dae");
model1.rotationZ = 270;
model1.scale = 4.0;


model2.load("../src/Models/BlenderCroc.dae");
model2.scale = 14.0;
model2.rotationZ = 270;


model3.load("../src/Models/BlenderPinkDolphin.dae");
model3.scale = 12.0;
model3.rotationX = 180;
model3.rotationY = 90;
model3.rotationZ = 90;


//model4.load("../src/Models/EarCorn.dae"); // For now EarCorn.pat is attached
//model5.load("../src/Models/Fire.dae");
//model6.load("../src/Models/Fish.dae");

model7.load("../src/Models/hut.dae");
model7.rotationZ = 270;
model7.rotationX = 90;

model8.load("../src/Models/BlenderPalmTree.dae");
model8.scale = 12.0;
model8.rotationZ = 270;

model9.load("../src/Models/BlenderPlantain.dae");
model9.scale = 12.0;
//model9.rotationY = 180;

model10.load("../src/Models/scout.dae");

//model11.load("../src/Models/movie.dae");

//model12.load("../src/Models/Snake.dae");

//To Test Video commenting the following

container0 = new DisplayObject3D();
container0.addChild(model0);
container0.visible = false;
scene.addChild(container0);

container1 = new DisplayObject3D();
container1.addChild(model1);
container1.visible = false;
scene.addChild(container1);

container2 = new DisplayObject3D();
container2.addChild(model2);
container2.visible = false;
scene.addChild(container2);

container3 = new DisplayObject3D();
container3.addChild(model3);
container3.visible = false;
scene.addChild(container3);

//container4 = new DisplayObject3D();
//container4.addChild(model4);
//container4.visible = false;
//scene.addChild(container4);


container7 = new DisplayObject3D();
container7.addChild(model7);
container7.visible = false;
scene.addChild(container7);

container8 = new DisplayObject3D();
container8.addChild(model8);
container8.visible = false;
scene.addChild(container8);

container9 = new DisplayObject3D();
container9.addChild(model9);
container9.visible = false;
scene.addChild(container9);

container10 = new DisplayObject3D();
container10.addChild(model10);
container10.visible = false;
scene.addChild(container10);

container11 = new DisplayObject3D();
container11.addChild(p);
scene.addChild(container11);

container12 = new DisplayObject3D();
container12.addChild(p2);
scene.addChild(container12);


addChild(view);

//addChild(new FramerateDisplay());

addEventListener(Event.ENTER_FRAME, loop);

}

private function loop(e:Event):void
{
if (marker0 != null)
{
container0.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker0.transformMatrix);

}
if (marker1 != null)
{
container1.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker1.transformMatrix);

}
if (marker2 != null)
{
container2.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker2.transformMatrix);

}
if (marker3 != null)
{
container3.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker3.transformMatrix);

}
/*if (marker4 != null)
{
container4.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker4.transformMatrix);

}*/

if (marker7 != null)
{
container7.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker7.transformMatrix);

}
if (marker8 != null)
{
container8.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker8.transformMatrix);

}
if (marker9 != null)
{
container9.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker9.transformMatrix);

}
if (marker10 != null)
{
container10.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker10.transformMatrix);

}
if (marker11 != null)
{
container11.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker11.transformMatrix);

}
if (marker12 != null)
{
container12.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(marker12.transformMatrix);

}

lre.render();
}
}
}