|
||||||||||||||||
. . . . . |
Flash 3D .obj files model importer
Beta This is a beta model importer for my 3D game engine. It may be useful to you.
What it does is parse a .obj 3d Model file and converts it to code that will render that model in
Flash. # WaveFront *.obj file g cube5 cube1 wing1_11252290 usemtl Material__3 v -30.313866 15.788016 19.982368 v -26.139158 18.622164 15.802573 v -15.488976 18.131786 26.107304 v -19.663685 15.297638 30.2871 v -21.743971 21.338137 11.389283 v -11.093789 20.847759 21.694016 f 11 10 9 f 12 11 9 f 13 11 12 f 14 13 12 f 15 13 14 f 16 15 14 f 17 15 16 f 18 17 16
The lines starting with "v" detail the location of the point in 3D space. They are the x,y,z coordinates.
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com The PHP parser
Anyway, this is how i convert the .obj code to something usable for my flash 3D engine.
<?php
$fp = fopen("model.obj", "r" );
if(!$fp)
{
echo "Couldn't open the data file. Try again later.";
exit;
}
$counter = 0;
while(!feof($fp))
{
// get a line
$line = fgets($fp, 3000);
// Split the input string into words as an array
$arrWords = explode( ' ', $line );
// Count the words in the string
$numWords = count( $arrWords );
// if first word is v - vertices
if($arrWords[0] == "v"){
// write flash code to add a vertex
echo "model.addPoint($arrWords[1] ,
$arrWords[2] , $arrWords[3] ); <br />";
}
// if first word is f - face
if($arrWords[0] == "f"){
$counter++;
// write flash code to render face
echo "model.drawFace(\"face$counter\" ,
[ $arrWords[1] , $arrWords[2],
$arrWords[3] ] ,0xff0000 , $counter);<br />" ;
}
}
fclose($fp);
?>
At the moment there are a number of flaws with this script so watch out.
if($arrWords[0] == "f"){
$counter++;
array_shift($arrWords);
// write flash code to render face
echo "model.drawFace(\"face$counter\" ,
[" . implode(", ", $arrWords) . "] ,0xff0000 , $counter);\n" ;
}
And here is a further fix from al. he writes " yop steve I'm not a php guru (i really prefer Action script dev) but I may have a solution for the parsing problem you sent me in php the function wich return the position of a character in a string is "strpos" so, it gives: $mystring = "f 1/1 2/2 3/3" $findme = "/" $pos = strpos($mystring, $findme); //$pos is the int variable of position (4 inthis case) //after, you use that number in a substring function ,in other words : $rest = substr($mystring, 2, $pos); // i've put "2" because we don't need "f " if I'm right $rest will give "1" (the 1 which is before the /) after, you have to make a boucle(? sic) to parse all the string, this code is not tested (dont have php on that computer, maybe there is some syntax or algorythmik errors ) but I hope it will be helpful for you . ps : "/" character may gives conflicts in php "
If you are a php guru please fix the above script and send it to me so that it is working much better.
Usage
model.addPoint(-30.313866 , 15.788016 , 19.982368 );
model.addPoint(-26.139158 , 18.622164 , 15.802573 );
model.addPoint(-15.488976 , 18.131786 , 26.107304 );
model.addPoint(-19.663685 , 15.297638 , 30.2871 );
model.addPoint(-21.743971 , 21.338137 , 11.389283 );
model.addPoint(-11.093789 , 20.847759 , 21.694016 );
model.addPoint(69.457222 , -9.891102 , -63.020256 );
// etc ...
// etc ...
model.drawFace("face1" , [ 1 , 2, 3 ] ,0xff0000 , 1);
model.drawFace("face2" , [ 4 , 1, 3 ] ,0xff0000 , 2);
model.drawFace("face3" , [ 2 , 5, 6 ] ,0xff0000 , 3);
model.drawFace("face4" , [ 3 , 2, 6 ] ,0xff0000 , 4);
model.drawFace("face5" , [ 7 , 8, 9 ] ,0xff0000 , 5);
model.drawFace("face6" , [ 10 , 7, 9 ] ,0xff0000 , 6);
model.drawFace("face7" , [ 11 , 10, 9 ] ,0xff0000 , 7);
model.drawFace("face8" , [ 12 , 11, 9 ] ,0xff0000 , 8);
// etc ...
// etc ...
This script works with my engine. You will need to rejig the script to work with your flash code. car.addVertex(100,200,300); car.render(faceName, faceLIst,lineColor, fillColor, depth);So you will have to adjust accordingly. The above script needs major improvement and is very early days yet. If you can improve it , please do so and let everyone know. Thank you for the improvements added my musicMan and al.. When i get back from holidays, i will test the script and fix the above mess.. LMAO.. cheers , Steve Any questions , Go to the forum - A board for Flash 3D discussion and input. |
|
||||||||||||||
|
| 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 | Forum | Games | free backgrounds | Resume | Flash Animations | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | store | advertisers | . . |
||||||||||||||||