/* export_vrml_plugin.a8s */ /* * VRML export plug-in by Paulo. * 2010 */ #plugin("object", "export", "VRML", ".wrl"); #file($output, "text"); #return($result); file $output, $alert; int $result; object $curObject; $curObject = project.curObject; shape $shape, $shapes[1], $shapes2[1], $childShapes[1], $cutShape; meshdata $mdata; tridata $tdata; int $numPoints, $numTexCoords, $numView, $numAttribute, $numTriangles, $numMat, $numFaces; int $ii, $jj; point3 $point; point2 $uv; float4x4 $transformMat; string $filename, $ext, $s, $sceneName; material $material; texture $texture; attribute $attribute; $sceneName=$curObject.name; $numAttribute=$curObject.GetNumAttributes(); for $ii=0 to $numAttribute-1 do { $attribute=$curObject.GetAttribute($ii); if ($attribute.GetName() == "vrml_name") { $sceneName=$attribute.GetStringValue(); } } $output.print("#VRML V2.0 utf8\n\n"); $output.print("WorldInfo {\n title \"%s\"\n info [ \"File exported from Anim8or\" ]\n}\n",$sceneName); $output.print("NavigationInfo {"); $output.print(" type [ \"EXAMINE\", \"ANY\" ]\n}\n"); $output.print("DEF VP1 Viewpoint {\n position 0 3.6 0\n orientation 0 0 1 0\n fieldOfView 0.8\n description \"Start\"\n}\n"); $curObject.GetShapes($shapes2); $numView=2; $shapes.size=0; while ($shapes2.size > 0) { $shape = $shapes2[$shapes2.size-1]; $s=$shape.name; if ($s.GetChar(0) == 118 && $s.GetChar(1) == 105 && $s.GetChar(2) == 101 && $s.GetChar(3) == 119 && $s.GetChar(4) == 58) { $s=$s.SubString(5,$s.length()); $output.print("DEF VP%d Viewpoint {\n", $numView); $output.print("position %.4g %.4g %.4g \n",$shape.loc); $output.print("orientation %.4g %.4g %.4g %.4g \n", $shape.orientation.x, $shape.orientation.y, $shape.orientation.z, $shape.orientation.w); $output.print("fieldOfView 0.785398\ndescription \"%s\"\n}\n\n",$s); $numView=$numView+1;} else{ $shapes.push($shape); } $shapes2.pop(); } $output.print("DirectionalLight {\n direction -0.5 -0.5 0\n}\n\n"); $output.print("Transform {\n children [\n"); while ($shapes.size > 0) { $shape = $shapes.pop(); $mdata = $shape.GetMeshData(); /* If $shape is a group push child shapes onto stack: */ if ($shape.GetKind() == SHAPE_KIND_GROUP) { $shape.GetShapes($childShapes); while ($childShapes.size > 0) { $shapes.push($childShapes.pop()); } } else if ($shape.GetKind() == SHAPE_KIND_PATH || $shape.GetKind() == SHAPE_KIND_MODIFIER || $shape.GetKind() == SHAPE_KIND_TEXT) { /* No 3D mesh to output. */ } else { $s=$shape.name; for $ii = 0 to ($s.length() - 1) do { if ($s.GetChar($ii) == 32) { $s=$s.SetChar(95, $ii); } } $output.print("\n DEF %s Shape {\n", $s); $transformMat = $shape.GetGlobalTransform(); $tdata= $shape.GetTriangleData(); $output.print(" appearance Appearance {\n material Material {\n"); $material = $tdata.GetMaterial(0); $output.print(" ambientIntensity %.4g\n",max(max($material.diffuse.x,$material.diffuse.y),$material.diffuse.z)); $output.print(" diffuseColor %.4g %.4g %.4g\n",$material.diffuse); $output.print(" specularColor %.4g %.4g %.4g\n",$material.specular); $output.print(" emissiveColor %.4g %.4g %.4g\n",$material.emissive); $output.print(" shininess %.4g\n",$material.brilliance); $output.print(" transparency %.4g\n",1-$material.alpha); $output.print(" }\n"); $texture = $material.GetTexture(TEXTURE_DIFFUSE); if ($texture.name!="") { $output.print(" texture ImageTexture {\n"); $filename = $texture.GetFileName(); $ext = $filename.GetExt(); if ($ext=="bmp"){ $alert.open("$console", "w"); $alert.print("\n\nWARNING :\n---------\nSome bitmap textures are used in : %s \nVRML doesn't support this file format for textures.\n\n",$s); $alert.close(); } $output.print(" url \"%s.%s\"\n }\n",$texture.name, $ext); $output.print(" textureTransform TextureTransform {\n center 0 0\n rotation 0\n scale 1 1\n translation 0 0\n }\n"); } else { $output.print(" texture NULL\n"); $output.print(" textureTransform NULL\n"); } $output.print(" }\n"); $numPoints = $tdata.GetNumPoints(); $output.print(" geometry IndexedFaceSet {\n color NULL\n coord Coordinate {\n point [\n"); for $ii = 0 to $numPoints - 1 do {/*Points*/ $point = $tdata.GetPoint($ii); $point = $transformMat.Project($point); $output.print(" %.4g %.4g %.4g,\n", $point); } $output.print(" ]\n }\n"); $numTexCoords = $mdata.GetNumTexCoords(); if ($numTexCoords > 0) {/*Tex coord*/ $output.print(" texCoord TextureCoordinate {\n point [\n"); for $ii = 0 to $numPoints - 1 do { $uv = $tdata.GetTexCoord($ii); $output.print(" %.4g, %.4g,\n", $uv); } $output.print(" ]\n }\n"); } $output.print(" colorIndex [ ]\n coordIndex [\n"); $numTriangles = $tdata.GetNumTriangles(); for $ii = 0 to $numTriangles - 1 do {/*Faces*/ $output.print(" %d, %d, %d, -1\n", $tdata.GetIndex($ii*3), $tdata.GetIndex($ii*3 + 1), $tdata.GetIndex($ii*3 + 2)); } $output.print(" ]\n"); $output.print(" normal NULL\n creaseAngle 0\n solid TRUE\n"); $output.print(" }\n }"); } $output.print("\n"); } $output.print(" ]\n}"); $result = 1; /* Assume export will succeed. */