

. . . . Elena Tresh Foundation Florida . 
Flash 3D Backface Culling
This particular example uses Flash MX2004 Actionscript 2.0 but can be adapted to Actionscript 1.0. Go to the page describing the Vector3D(v3d) class before attempting this tutorial. I will be using functions from the Vector class that will save heaps of time and typing. This tutorial was adapted from a C++ tutorial by Simon Brown .
Bacface Culling entails not rendering faces on 2D models that will not be visible. They are behind other faces.
It saves time and processing power.
// create a movieclip this.createEmptyMovieClip("mc1",1); // make the points of a triangle // let z = 0 for convenience var p1 = new v3d(100,100,0); var p2 = new v3d(150, 250,0); var p3 = new v3d(50,350,0); // declare a variable for later use. var v1 = new v3d(0,0,0);First up we need to get the vector that goes from point 1 to point 2. I did this one the old fashioned slow way //  STEP 1 // get the vector going from point 1 to point 2 // ie pt1  pt2 v1.x = p1.x  p2.x; v1.y = p1.y  p2.y; v1.z = p1.z  p2.z;Then i will get the vector going from point 2 to point 3 by using my Vector class, minVecNew, that subtracts one vector from another and returns a new vector //  STEP 2 // get the vector going from pt 2 to pt 3 // ie pt2  pt3 var v2 = p2.minVecNew(p3); var testv = p1.minVecNew(p2); // v2.toString(); testv.toString(); trace(v1.x +" , "+v1.y + " , "+v1.z);Now we need to get the normals of the 2 new vectors we have created. A normal is the length or magnitude of a vector. //  STEP 3 // GET normals of vectors from 1 2 and 23 // get normal = magnitude var mag1 = v1.getNormal(); var mag2 = v2.getNormal();Step 3 is not necessary because it is enclosed in step 4 .
//  STEP 4 // normalise both vectors // this step actually encompasses step 3 // so step 3 is not necessary v1.normalize(); v2.normalize(); trace(v1.toString());Now get the cross product of the normalised vectors //  STEP 5 // get the cross products of normlised vectors var n1 = v1.cross(v2); n1.toString() ;Now assume that our Point Of View (POV) is (0,0,0). We could calculate it from the position of our camera if we had one. This POV vector goes to our model , so we choose point 1 arbitrarilly. //  STEP 6 //compute a vector from the camera(OR pov(0,0,0))to our triangle // i.e point 1 of our triangle // if we had a camera it would be ( point 1  camPoint ) var camVector = p1; trace(camVector.toString()+" = camvector");The dot product can now be calculated to give us the cosine of the angle between the two vectors. Therefore if the angle is either less than 90 degrees or more than 270 degrees, then the triangle is facing away and the cosine of the angle will be positive. So all we need to do is compute the dot product and cull triangles where the result is positive. // get the dot product of normal and vector to model var result; result = n1.dot(camVector); //result = n1.x*camVector.x + n1.y*camVector.y + n1.z*camVector.z; trace(result + " result"); // if result less than 0 , draw // if result greater than 0 , dont draw.. if(result <= 0){ // draw it with(mc1){ beginFill(0x00ff00); lineStyle(2,0x000000); moveTo(p1.x,p1.y); lineTo(p2.x,p2.y); lineTo(p3.x, p3.y); lineTo(p1.x, p1.y); endFill(); } Interested in obtaining a job in web programming? There are many online college degrees in Information Technology, so you can live your dreams. You can find university degrees online as well as education grants to enable your success. Method 2This method is a lot simpler and seems to work just fine. function isVisible(p1,p2,p3){ return ((p2.xp1.x)*(p3.yp1.y)<(p3.xp1.x)*(p2.yp1.y)); }Just use it the same as the other backface culling method. 


.  Home  Flash MX  Actionscript 2.0  Flash 3D  Flash 8  Flash Database  Flash Mobile  Flash CS3  Java For Kids  Video Course  General Video  Photoshop  Web Design  Digital Photography  Games  free backgrounds  Resume  Streaming Video  Students Work  Links  Contact me  sitemap  reviews  . . 