Exporting textured objects from 3d MAX to PV3D 2.0 tutorial

f

[SEE WHAT YOU ARE GOING TO LEARN]

This tutorial explains how to make a 3d object with 3d Studio MAX , unwrap the uvw, export it to actionscript 3, import to your papervision 3d project , texture it and deploy as a flash movie :)

What you need to use this:

1. Papervision3d (latest classes included) - http://blog.papervision3d.org
2. Flash CS3 - http://www.adobe.com/flash
3. 3D Studio MAX (I’ve used the latest 2009 ver. Should work with previous ones too) - http://www.autodesk.com
4. AS3GeomClassExporter for 3DStudio MAX (included) by http://dreammania.net - extended by mr.doob.
5. Some basic modelling and texturing skills to make something. Some Max interface knowledge proofs useful here too.

You could use different software for this (Maya and so on..) but I didn’t test it so I’ll focus on MAX right now.

I’m not a real tutorial writer so I’ll make this as short as possible - explaining with screenshots where possible :)

Let’s get on with this:

Part I - MAX

1. You need to open max and install the script (AS3GeomClassExporter) - this screenshots shows where and how to do this:

2. Run the script like here (it executes and shows the panel when you select the script from the combobox):

3. Configure the script for PV3d 2.0 like I did it here:

4. Model anything you want. I’ve made a simple chamfer box here:

At this point you should remember a few important things:

- use as little polygons you can handle to improve the speed (low-poly is your friend here:)),
- you can connect (boolean, modify mesh, combine with loft or lathes and so on) everything you want. But you must keep the object as ONE mesh - closed one. If you want to combine many objects you have to do this in papervision - not in max - the exporter allows to export 1 object.

Here is a sample Boolean (with union) of the chamfer box and a loft made of text and a line (for loft depth):

AS you can see in properties this is a single object with a simple structure (low amount of faces and vertices).
5. Now a very important part! You need to place the PIVOT for the object correctly. The pivot is the point which papervision would read as 0,0,0 coordinates. So it is crucial for rotating/positioning and moving the object later. This is how you do this

6. Now it is time to prepare the UVW texture coordinates and map for the object. We’ll do this in parts:
a) You need to unwrap the Mesh using UVW Unwrap. Select the object and do EDIT/select modifier Unwrap UVW from the combo:

b) Now you need to select Face from the Unwrap modifier and select all the faces (CTRL+A) of the object - then press “EDIT..”

c) A new window will open up - the selected faces (red) will overlap each other - this is time to unwrap the mesh so select MAPPING from the main menu –> normal mapping. You can modify some parameters in the window that appears - but for this object it is just fine to CLICK OK:

d) Hurray! it is done. The mesh will unwrap like in the screen below. Of course you can modify the unwrap - play with the unwrap tool a little yourself ;). In this case the automated unwrap worked rather fine (the mesh was simple) and I don’t need to correct anything here. I have one overlap which I should correct but it’s not important for this tutorial.

e) Now it is time to render the map (you need something to paint on and texture in pv3d :)). I would suggest rendering two maps in the same resolution (don’t select them too big - a 1500×1500 map is just fine and fast for a complicated object in pv3d if you don’t use too many objects). One with the edges visible (this can be helpful when drawing in photoshop - you can  apply the edges on a different layer and use as a helper :). The second one shaded - to use as a canvas for painting.
The renders aren’t to pretty but they should do:

You are done with Unwrapping at this point :) Congratulations. Lets proceed to the next section.

7. Now the fun part. As you have the object created (it is a single closed mesh with uvw coordinates applied and ready to go!) you can now EXPORT THE AS3 CLASS with this object :) Select the object with the select tool and press EXPORT CLASS in the scripts panel/as3geomexporter. Like that:

Thanks for your attention. When you finish this tutorial you should have:

- an AS3 class with the object you’ve created (it contains the mesh definition + the UVW map definition),
- a texture which you can paint on and use with the object,
- a max file which you can use again if you don’t like the results,

Part II - AS3 in FLASH

What you need next (I’m not going to explain it in details here because the code is well commented):

- the source code - you can see it working here. You can download it here.

So you are ready to go - now you can import your object and use it in papervision WITH MAPPING! :)

I hope this tutorial helps.
Feel free to comment! You are welcome as usual! :)

Have fun,

FLAIM

Directional Blur + Tweener in AS3

Hello,

What is it?

Click here to see it… - click the background to execute the movement.

How it works?

User executes tweener for movement. Then the distance (between last update and current object position) and angle (directional vector of the movement) are calculated in each tweener update - so are the blur amounts - basing on previous object coordinates (from the previous update) - then the blur is appled to the object and current coordinates become the previous one so the whole thing can start again on next tweener update :)

I assume this is not the best method for this effect (senocular did this better using bitmaps in as2- but this method is slow and needs rewriting for simple usage. There could also be transparency issues with bitmapdata there). Anyways it looks far better than mine (the object leaves the trail and is not just blurred like in my method) but the method here works without the bitmapData and sports Tweener ;)

The source?

Sure! You can download it here :)

Quick Q&A:

Q: Can it be used with papervision in 3d?

A: Yep… you have to introduce Z to the equations and change the distance calculating function for one that would calculate it in three dimensions.

Q: Can I use it with multiple objects?

A: Yes you can! You can execute as many instances you want (and flash can handle;)) It uses the object reference so it can run many times on different objects simultanously.

Have Fun!

FLAIM

AS3 analog clock in 3D

Hi,

In the previous post I have presented how to create a simple analog 2d clock using as3 only.

Now it is time to do it in Papervision3D :)

I’ve used the same mechanism as in the 2D one - the polygon generator I’ve created serves now as a vertex creator - and basing on the vertex - cubes are added to the scene. Tweener is used for animating the cubes then. A simple mathematical mechanism is used for camera movement.

You can download the source code here. The source is well commented. All classes included.

Comments are welcome as usual ;)

Have fun with it,

FLAIM

 

Simple AS3 analog clock (2d)

clock_v1

hi,

Basing on the previous post (2d polygon generation) I’ve created a simple, ‘redrawing’ analog clock (in 2d).

Why complicate with polygons? Why not circles and lines from the drawing api?

- I wanted to have all the points coordinates (x,y) for later use with papervision3d. Drawing a circle using the Flash drawing API (graphics.*) will not give you such information.

Of course there are other methods to get the coordinates but this is probably the fastest one - additionaly we have full control of the number of vertexes we create.

How it works?

- The interval is Tweener based - it executes a function that gets the seconds/minutes/hours and then clears and redraws the polygons - once every second using a fake Sprite,

- polygons are drawn using the method from the previous post,

- entire clock is code based - no stage elements required.

The code

You can download the entire source here.

Have fun,

FLAIM

Simple Maths in AS3 part 4 - drawing polygons

polys 

Hello,

This time some simple drawing in a really short post. I’ve needed this functionality for my next post (I’m finishing a 3d-html-rotator -and creating polygons is crucial for positioning the elements in a 3d environment).

What it does?

Draws a regular convex polygon using specified parameters (radius,number of sides,x,y).

drawPoly(30,6,50,50); // draws a 30px radius hexagon positioned on x:50,y:50px

The code (AS3):

//some vars
var poly_id,ratio,top,coords;
//the main function  drawPoly(radius,segments,center x, center y);
function drawPoly(r:int,seg:int,cx:Number,cy:Number):void
{
 poly_id=0; //->0
 coords=new Array(); //->0
 ratio=360/seg; //calculated ratio/step
 top=cy-r; //top
 for(var i:int=0;i<=360;i+=ratio) //the main loop- ratio used here
 {
  var px:Number=cx+Math.sin(radians(i))*r; // point X
  var py:Number=top+(r-Math.cos(radians(i))*r); // point Y
  coords[poly_id]=new Array(px,py); //2nd level array
  if(poly_id>=1)
  {
   poly_draw(coords[poly_id-1][0],coords[poly_id-1][1],coords[poly_id][0],coords[poly_id][1]); //drawing here
  }
   poly_id++; // increment for the id
 }
 poly_id=0; //id->0
};
//degrees2radians
function radians(n:Number):Number
{
 return(Math.PI/180*n);
};
//the drawing function - you can draw anything you want here - in this case it connects the points with red lines
function poly_draw(sx:Number,sy:Number,ex:Number,ey:Number):void
{
 graphics.lineStyle(1,0xFF0000,1);
 graphics.moveTo(sx,sy);
 graphics.lineTo(ex,ey);
};
// example usage
drawPoly(30,6,50,50); //hexagon
drawPoly(30,3,150,50); //triangle
drawPoly(30,4,250,50); //square
drawPoly(30,8,350,50); //octagon
drawPoly(30,64,450,50); //almost a circle ;-)

The .fla file - here.

Have fun,

FLAIM

Real 3d in Html - updates

updates to html3d

Hello,

The update includes:

- z-sorting for html objects (see this page),

- simple mouse interactivity,

- optimized z-scaling of fonts,

- optimized speed (redesigned communication with js).

 

The complete updated source is here.

Known issues:

- Antivirus software (checked with KAV) causes major slowdown when checking Javascript (only Internet Explorer browsers) …use Mozilla if you have Kaspersky turned on - fixed that

- slow…as hell ;) - it’s better now :)

have fun,

FLAIM

***** RECENT UPDATE ******

I’ve optimized the code and changed the files in this post (both the sources and example link) - everything is 500-800% faster than in the previous post. Thanks to the optimization the issue with Kaspersky disappeared also :)

… have fun :)

Real 3d in html / javascript / pv3d

Hello everyone,

In the previous post I’ve mentioned that it could be possible to use papervision 3d and ExternalInterface to create 3d in HTML :)

..well that wasn’t too hard to imagine when I’ve found out that it is possible to get coordinates of any vertex of a pv3d object you want - using : “..geometry.vertices[index].vertex3DInstance.attribute “..so I did it..

It’s a bit “proof of concept” idea - it of course ‘doable’ rather than ‘usable’ (too slow) but that didn’t put me off ;)

You can see the test movies right here (or by clicking the large image above). Click the embed button first and then wait for the flash to load. I did it on purpose - because it is processor heavy.

You can download the complete source! here.

So how does it work?

It’s described in the sources but shortly:

- creates a whole pv3d environment with a simple cube, a camera and so on…

- dynamically creates html content then used to animate (simple divs with text),

- creates communication (see the html source) between js and as3,

- uses Tweener for animation :) yep - that’s a cool part:),

- the pv3d cube is a fake all the time - it exists but it’s not really visible - the coordinates are passed to html objects using the method described in the beginning of this post,

What could be done with it? 

- a lot! You could do a whole menu/website with it - as long as you don’t use too many items on the stage. It’s very easy to add full mouse interaction to it using the same methods I used in the demo. It’s also very easy to modify the 3d environment and steer the camera from js :) Use your imagination!

I hope you like the little demo - comments are welcome!

FLAIM

*** UPDATE

Please use the code from my newer post about the 3dHTML - it’s optimized and works significantly faster.

Using Flash and Tweener to tween html objects without external javascript!

click to see the example 

Hello ,

This time I’ve found a nice feature of ExternalInterface in AS3 (and AS2 also) which allows you to create your own Javascript code INSIDE the flash file and then execute it from INSIDE Flash and even pass parameters to this code.

And what is the use of that?

Well… I can give you some examples..

- you could (as in this example) use AS3 tweener to tween any properties of html objects on the site (you could use JSTweener instead of this method - but it’s a lot easier to do it with Actionscript if you are a flash developer!)

- you could even pass coordinates of 3d points in papervision to create a simple 3D HTML site ! :),

- you could use any other class you use with your movieclips/sprites/bitmaps and try to implement and use them in plain html,

- use your imagination ;)

But what is the difference between normal javascript (and executing it only) and building javascript inside flash?

- the js code is not easily visible for everyone and compiled inside flash,

- you edit everything in the flash environment without having to use other software thus having everything in one place,

- your html is more tidy and doesn’t need any external js classes,

- many others I didn’t think of just now,

And how to do it?

Well I’ve made a simple example. It consists of a html page with a flash movie embedded. There is a div on the site with id=”box”. Flash creates a js function inside an XML object and then executes it every Tweener update (onUpdate). Normally Tweener wouldn’t run on parametres which doesn’t exist - so I create a Sprite which is not really visible and Tween its parametres with Tweener - and then -> I pass those parameters to the js function…really simple and running smooth. Note that the motion in html is now strictly dependant on the flash framerate :)

You can see the example HERE.

The files including the flash source are HERE.

And the sample code is here:

// import
import caurina.transitions.Tweener;
import flash.external.ExternalInterface;

// XML variable with the javascript function
var myJavaScript :XML =    
<script>       
<![CDATA[           
   function(x,y,id)
   {                              
   mover = function(x,y,id)
   {
    document.getElementById(id).style.left=x+'px';
    document.getElementById(id).style.top=y+'px';
   }
   mover(x,y,id);
  }       
]]>
</script>

//this function is executed every Tweener update (onUpdate),
function move_div(div_id)
{
 //we will now use the sprites properties to animate the DIV
 var xpos=getChildByName(div_id).x;
 var ypos=getChildByName(div_id).y;
 // execution of the function - passing x,y and the ID to the javascript function
 ExternalInterface.call(myJavaScript ,xpos,ypos,div_id);
};

function create_object(div_id)
{
 //a virtual sprite which will hold the properties…
 var object:Sprite = new Sprite();
 object.name=div_id;
 object.x=500;
 addChild(object);
 //…and the tween on that sprite
 Tweener.addTween(object,{x:500,y:500,time:2,transition:”easeOutBounce”,onUpdate:move_div,onUpdateParams:[object.name]});
};

// pass the id of the div you want to move
create_object(”box”);

Feel free to comment and..

..have fun,

FLAIM

 

Simple maths in AS3 part 3 - rotating object around another

object rotation

Hi everyone,

Next part of the series.

This time I wrote a small function which uses trigonometry (sin and cos) to rotate one object around another one.

The only thing you need to do is to specify the two objects used in the animation and some parameters like that:

rotate_around_object ( object_to_rotate, center_object, radius, speed , start angle);

The function will create an event listener for the object (on_enter_frame) and move object_to_rotate around the center_object in a certain radius at a certain speed. The animation will start at the specified start_angle. If you want to change the rotation direction - specify the speed with a minus value.

The code is here (AS3) :

function rotate_around_object(object1,object2,radius,speed,angle)
{
 var xpos,ypos;
 object1.addEventListener(Event.ENTER_FRAME, object_ENTERFRAME);
 function object_ENTERFRAME(e:Event):void
 {
  xpos = object2.x + object2.width/2 + Math.cos(angle) * radius;
  ypos = object2.y + object2.height/2 + Math.sin(angle) * radius;
  angle += speed;
  object1.x=xpos;
  object1.y=ypos;
 }
};

// example usage (you need 4 movieclips on the stage in this example..
// …with instance names: point1, point2.. and so on.
rotate_around_object(point2,point1,100,.2,0);
rotate_around_object(point4,point3,200,.1,45);

You can download the fla file here.

You can see it running here.

Have fun,

FLAIM

Simple maths in AS3 part 2 - rotation

Hello,

This time a short explanation how to use trigonometry and rotation to achieve a ‘point_to’ effect. The maths is really simple here and useful for various effects and other uses. I’ve used a mousemove listener on the stage to execute the function and mouseX + mouseY to give the coordinates to the script. The movieclip (or sprite if you wish) can be rotated to face the direction of a point you specify (in this case - the mouse position on the stage).

Here is the code (as3):

var dx,dy,target_rotation:Number;

function point_to(what)
{
dx = mouseX - what.x;
dy = mouseY - what.y;
what.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
}

// example usage
stage.addEventListener(MouseEvent.MOUSE_MOVE,stage_MOUSEMOVE);
function stage_MOUSEMOVE(e:MouseEvent):void
{
 point_to(movie_mc);
}

You can download the FLA file here. The example can be seen here.

Feel free to comment and have fun with Flash!,

FLAIM

simple maths in AS3 part 1 - distance between two points

as3 distance Pythagorean Theorem

;-)

Just decided to start a small series of simple beginner tutorials about actionscript maths and usage. People send me loads of questions about ‘how to do this or that…?” - most of them are so simple to solve that it gives me a feeling that most of us nowadays stopped thinking at all…. ; (

Anyways… here is how to calculate distance between two points using the Pythagorean Theorem (a2+b2=c2 - simple as that). The small picture above shows the idea (older than the wheel i guess ;o-)) and here is the code which does the simple calculation…

// distance
function distance(p1,p2)
{
 var dist,dx,dy:Number;
 dx = p2.x-p1.x;
 dy = p2.y-p1.y;
 dist = Math.sqrt(dx*dx + dy*dy);
 trace(dist);
}
distance(point1,point2); // point1 and point2 are example objects (movieclips or sprites) located somewhere on the stage (just create them with the flash ui and give them instance names)

Hope it helps,

FLAIM

Blur + Tweener in AS3

blur

Hi again ;-)

Many people ask me how to do tweening of blurX and blurY (and other filters and properties) using Tweener - but in AS3 not AS2 (as in previous posts).

The solution is quite simple:

1. You need Tweener for AS3 (you can download it from googlecode) - import the class,

2. import the filter you want (from flash.filters.*),

3. place an example mc on the stage and name it ‘my_mc’,

4. finally here is the example code:

// Import Filters
import flash.filters.*;
// Import Tweener
import caurina.transitions.Tweener;
// Add Events
my_mc.addEventListener(MouseEvent.ROLL_OVER, my_mc_ROLLOVER);
my_mc.addEventListener(MouseEvent.ROLL_OUT, my_mc_ROLLOUT);

// And that is how it works…
function my_mc_ROLLOVER(e:MouseEvent):void
{
 Tweener.addTween(my_mc, {_blur_blurX:15,_blur_blurY:15, time:2});
};

function my_mc_ROLLOUT(e:MouseEvent):void
{
 Tweener.addTween(my_mc, {_blur_blurX:0,_blur_blurY:0, time:2});
};

HAVE FUN,

FLAIM

RGB2HSV conversion using actionscript…

rgb2hsv conversion

Another simple conversion function. This time it converts RGB values to HSV (Hue, Sat, Val).

function rgb2hsv(r,g,b)
{    
 var x, val, d1, d2, hue, sat, val;    
 r/=255;    
 g/=255;    
 b/=255;    
 x = Math.min(Math.min(r, g), b);    
 val = Math.max(Math.max(r, g), b);    
 if (x==val)
 {        
  return(”h is undefined, s: “+0+”,v: “+ val*100); //err obj
 }    
 d1 = (r == x) ? g-b : ((g == x) ? b-r : r-g);    
 d2 = (r == x) ? 3 : ((g == x) ? 5 : 1);    
 hue = Math.floor((d2-d1/(val-x))*60)%360;    
 sat = Math.floor(((val-x)/val)*100);    
 val = Math.floor(val*100);    
 return(hue+”,”+sat+”,”+val);
};

Have fun,

FLAIM

RGB2CMYK conversion using actionscript…

rgb2 cmyk conversion example

Hello :)

A small and useful function for converting RGB values to CMYK color space…

function rgb2cmyk(r,g,b)
{
 C = 1 - ( r / 255 );
 M = 1 - ( g / 255 );
 Y = 1 - ( b / 255 );
var_K = 1

if ( C < var_K )   var_K = C
if ( M < var_K )   var_K = M
if ( Y < var_K )   var_K = Y
if ( var_K == 1 ) { //Black
   C = 0
   M = 0
   Y = 0
}
else {
   C = ( C - var_K ) / ( 1 - var_K ) *100
   M = ( M - var_K ) / ( 1 - var_K ) *100
   Y = ( Y - var_K ) / ( 1 - var_K ) *100
}
 K = var_K*100
 return(”C: “+Math.floor(C)+”/ M: “+Math.floor(M)+”/ Y: “+Math.floor(Y)+”/ K: “+Math.floor(K));
};

// rgb2cmyk(255,0,0); returns –> C: 0/ M: 100/ Y: 100/ K: 0

Have fun :)

FLAIM