3D Face Extrusion

Page 2 of 3
C. Put it All Together And Turn Yourself About
So lets make us a good ole triangle!
- First we need some sort mass, a place to put all the stuff in.
createEmptyMovieClip("mass", _root.depth++); - Now we need the 3 points of a triangle.
mass.pts = new Array();
// only do this once, makes pts an array
mass.pts[0] = new Object;
//makes a point where we can add properties
mass.pts[0].x = 0; mass.pts[0].y = 0; mass.pts[0].z = 0;
mass.pts[1] = new Object;
//makes another point
mass.pts[1].x = 40; mass.pts[0].y =40; mass.pts[0].z = 0;
mass.pts[2] = new Object;
//makes another point
mass.pts[2].x = 0; mass.pts[0].y =40; mass.pts[0].z = 0; - Great, but they’re still just points in space. Lets make a shape out of them.
First, we need to add something to our mass to hold the different shapes:
Now we have some space for shapes, we need to make space in a shape for points:mass.shapes = new Array ();
// only write this once in the script
And now lets finally make a shape out of points:mass.shapes[0] = new Array();
// gives room in the first shape for points
The order didn’t really matter but illustrate how the external point list. For more advanced shapes, it could become a headache – different orders means the lines are connected in the different orders.mass.shapes[0][0] = 2;
// first point in shape will be the point with ID = 0
mass.shapes[0][1] = 0;
// 2nd point in shape will be the point with ID = 1
mass.shapes[0][2] = 1;
// first point in shape will be the point with ID = 2
II. Extrude - Lets make it 3D
A. Point check
// A very useful function for optimization would be to check if a point exists, and if so, whats its ID
// if it doesn?t find one, the function will return ?1 (an impossible ID)
function checkPts(mc, x, y, z)
{
f = -1;
for (b = 0; b < mc.pts.length; b++)
{
if (mc.pts[b].x == x)
if (mc.pts[b].y == y & mc.pts[b].z == z)
f = b;
if (f > -1) break;
}
return f;
}
B. Actual extrude:
function extrude (mc, from, amount) // which mass, shape, and desired depth
{
// this will go through each point in our shape
for (i = 0; i < mc.shapes[from].length; i++)
{
// let?s check if our point with a change depth (z) already exists
k = checkPts(mc, mc.pts[mc.shapes[from][i]].x, mc.pts[mc.shapes[from][i]].y,
mc.pts[mc.shapes[from][i]].z + amount);
if ( k == -1)
{
// if it doesn?t, lets make a new one with the extruded z
k = mc.pts.length;
mc.pts[k] = new Object;
mc.pts[k].x = mc.pts[mc.shapes[from][i]].x;
mc.pts[k].y = mc.pts[mc.shapes[from][i]].y;
mc.pts[k].z = mc.pts[mc.shapes[from][i]].z + amount;
// the only change from the original coordinate is z, depth
}
// lets make a face out of this coordinate, the previous, and their extruded copy
// that means it?ll be on of the sides of the new shape (not top or bottom)
// need to make sure we start on the second coordinate though:
if (i > 0)
{
// make a new face
mc.shapes[mc.shapes.length] = new Array();
mc.shapes[mc.shapes.length-1][0] = checkPts(mc, mc.pts[mc.shapes[from][i]].x,
mc.pts[mc.shapes[from][i]].y, mc.pts[mc.shapes[from][i]].z + amount);
mc.shapes[mc.shapes.length-1][1] = checkPts(mc, mc.pts[mc.shapes[from][i - 1]].x,
mc.pts[mc.shapes[from][i - 1]].y, mc.pts[mc.shapes[from][i - 1]].z + amount);
mc.shapes[mc.shapes.length-1][2] = mc.shapes[from][i - 1];
mc.shapes[mc.shapes.length-1][3] = mc.shapes[from][i];
}
}
// we still need to make the last face, connecting from the first coordinate, last, and their extruded copies
mc.shapes[mc.shapes.length] = new Array();
mc.shapes[mc.shapes.length-1][0] = checkPts(mc, mc.pts[mc.shapes[from].length-1].x,
mc.pts[mc.shapes[from].length-1].y, mc.pts[mc.shapes[from].length-1].z + amount );
mc.shapes[mc.shapes.length-1][1] = mc.shapes[from][mc.shapes[from].length - 1];
mc.shapes[mc.shapes.length-1][2] = mc.shapes[from][0];
mc.shapes[mc.shapes.length-1][3] = checkPts(mc, mc.pts[ mc.shapes[from][0] ].x,
mc.pts[ mc.shapes[from][0]].y, mc.pts[ mc.shapes[from][0]].z + amount);
// and lets make the top of the shape
mc.shapes[mc.shapes.length] = new Array();
for (i = 0; i < mc.shapes[from].length; i++)
mc.shapes[mc.shapes.length - 1][i] = checkPts(mc, mc.pts[mc.shapes[from][i]].x,
mc.pts[mc.shapes[from][i]].y, mc.pts[mc.shapes[from][i]].z + amount);
}
C. Great.. how do extrude the thing again?
extrude(mass, 0, 20);
// takes the first shape in our mass and extrudes it 20 pixels


