Putting Video on a CS4 (Flash 10) Primitive

Intro

Get ready to eliminate 1000’s of lines of Papervision3D code. That’s right! By using CS4 you no longer need the Movie Material class, the DisplayObject3D class, the Video Stream Material class, and a plethora of Triangle/UV parsing and math classes. It’s just amazing…in CS4 it’s so simple!

Video on a CS4 Primitive

Video on a CS4 Primitive

Demo

Source

YouTube


Discussion

Note: This discussion uses the code and results of two previous posts:

Adding Video to a Papervision3D Curved Plane

CS4 Super Primitive using Matrix3D

Placing video on the CS4 Super Prim developed in Chapter 3 of the book is very easy and only requires four steps.

  • Step 1: Import the net and media classes in to the Super Prim Class (see the post on CS4 Super Primitive using Matrix3D – this is your starter code)
  • Step 2: Declare the video, net Connection, and net Stream variables.

//net stream assets
private var myConnection : NetConnection;
private var myStream : NetStream;
private var myVideo : Video;

  • Step 3: Incorporate the loadMyVideo() method discussed in the previous post on Adding Video to a Papervision3D Curved Plane
  • Step 4: Create a bitmapData object and in the animation loop draw your video to that bitmapData object and attach the net stream to your video, as shown below:

//Draw Video into Bitmap
bitmapData.draw( this.myVideo);
this.myVideo.attachNetStream ( this.myStream );

The results yield a video being played among the various prims contained in the super prim. At this point you change the video by changing the file name inside your net Stream method (myStream.play(“assets/eternity.flv”)), but the constructor function could be easily changed to receive a video string locater.

A Little Extra

Also in Chapter 4 of the book, we show you how to put your webcam video on a prim. Below is a shot of my Guinea Pig Don Pedro who jumped into the screen to get his web cam video on a sphere.

Pig on a WebCam

Pig on a Web Cam

The code for this is included in the download above. But the explanation is in the book. To see the entire code download the source from the link above, or click the more button below:

Video on a Prim Code

package {

import flash.display.*;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.*;
import flash.utils.Timer;

import flash.media.*;
import flash.net.*;

public class CS4VideoPrim extends Sprite {
//Embed your texture
//Primitive Declarations
private var primitive:Sprite;
private var myTimer:Timer;
private var myCount:int=0;
private var paraVec:Vector3D;
private var bitmapData:BitmapData;
private var rows:int=32;
private var cols:int=16;
private var ix:Number;
private var iy:Number;
//Elliptical Parameters
private var radx:Number=.8;
private var rady:Number=.8;
private var radz:Number=.8;
//Vertice Data
private var verts:Vector.<Number>;
private var indices:Vector.<int>;
private var uvtData:Vector.<Number>;
private var uvtShadingData:Vector.<Number>;
private var projectedVerts:Vector.<Number>;

private var perspective:PerspectiveProjection;
private var projectionMatrix:Matrix3D;
private var myAngle:Number = 0;

//net stream assets
private var myConnection : NetConnection;
private var myStream : NetStream;
private var myVideo : Video;

public function CS4VideoPrim():void {

var myTimer:Timer = new Timer(1800);
myTimer.addEventListener(“timer”, timerHandler);
myTimer.start();

loadMyVideo();

}

//Handle Timer Event
private function timerHandler(event:TimerEvent):void {

myCount=myCount%5;
init(myCount);
myCount++;

}

private function init(number:Number):void {
//Instantiate parameters
paraVec = new Vector3D();
verts = new Vector.<Number>();
indices= new Vector.<int>();
projectedVerts = new Vector.<Number>();
uvtData = new Vector.<Number>();
uvtShadingData = new Vector.<Number>();
bitmapData = new BitmapData(320, 240, false);

//remove primitive from stage before adding the next one
if(primitive){
removeChild(primitive);}
addChild(primitive= new Sprite());
//Set position on stage
primitive.x=stage.stageWidth/2;
primitive.y=stage.stageHeight/2;
//Set perspective
perspective= new PerspectiveProjection();
perspective.fieldOfView = 35.0; // camera angle, in degrees

//Calculate Vertices
for (var i:int = 0 ; i!=rows; i++) {
ix= i/(rows-1)*Math.PI*2.0;

for (var j:int =0 ; j!=cols; j++) {
iy= (j/(cols-1)-0.5)*Math.PI;

// Primitive Selection Switch Case
switch (number) {
case 0:
//Plane
paraVec = new Vector3D(-.5*(2-ix), .5*(iy),0);
break;
case 1:
//Cylinder
paraVec = new Vector3D(
Math.cos(ix), iy, Math.sin(ix));
break;
case 2:
//Cone
paraVec = new Vector3D(
(iy)*Math.cos(ix), Math.sin(iy), (iy)*Math.sin(ix));
break;
case 3:
//Sphere
paraVec = new Vector3D(
Math.cos(iy)*Math.cos(ix), Math.sin(iy), Math.cos(iy)*Math.sin(ix));
break;
case 4:
//Torus
paraVec = new Vector3D(
(1+.4*Math.cos(iy))*Math.cos(ix), .4*Math.sin(iy), (1+.4*Math.cos(iy))*Math.sin(ix));
break;
default:
trace ( ” no case tested true ” )
}
//Collect vetices in the verts Vector array
verts.push(radx*paraVec.x,rady*paraVec.y,radz*paraVec.z);

//Load uvt data
uvtData.push( i/(rows-1),j/(cols-1), 0.0);
//Initialize projected vertices
projectedVerts.push(0.0,0.0);
}
}

//Create indices
var ii:int =0;
for (var ik:int=0 ; ik!=rows-1; ik++) {
for (var jk:int=0 ; jk!=cols-1; jk++) {
indices.push( ii,ii+cols+1,ii+1,
ii+cols,ii+cols+1,ii++);
}
ii++;
}

stage.addEventListener(Event.ENTER_FRAME, loop);
}

private function loadMyVideo():void
{
//Set up Net Connection
myConnection = new NetConnection();
//Not on the media server
myConnection.connect(null);
myStream = new NetStream(myConnection);
//Set buffer time – at lease 2 seconds are required for some machines.
myStream.bufferTime = 2;
//Set up My Stream for client
myStream.client = new Object();
//Instantiate the video
myVideo = new Video(320, 240);
myStream.play(“assets/narnia.flv”);
//Attach to local client side video
myVideo.attachNetStream(myStream);

}

private function loop(event:Event =null):void {

//Draw Video into Bitmap
bitmapData.draw( this.myVideo);
this.myVideo.attachNetStream ( this.myStream );

// Set up a viewpoint:
projectionMatrix = perspective.toMatrix3D();
projectionMatrix.prependTranslation(0,0,0.35);
myAngle+=2;
//Rotate Primtive
projectionMatrix.prependRotation( myAngle ,new Vector3D(0,1,0.0));
//Update projection vectors
Utils3D.projectVectors(projectionMatrix, verts,
projectedVerts, uvtData);
//Draw primtive
with (primitive.graphics) {
clear();
beginBitmapFill(bitmapData,null, false, false);
drawTriangles(projectedVerts, indices, uvtData,TriangleCulling.NEGATIVE);
endFill();
}
}

}
}

//Come unto me, all ye that labour and are heavy laden, and I will give you rest. Take my yoke upon you, and learn of me; for I am meek and lowly in heart: and ye shall find rest unto your souls. For my yoke is easy, and my burden is light.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: