Sign in to follow this  
Followers 0
akaitora

Not sure what is causing this Visual Artifact

4 posts in this topic

I am currently learning MelScript and am writing an obj exporter.  I able able to export all of the geometry however when I import my model into Unity, I see stange black shading all over my model.  The only thing I can think of is that I am exporting the normals incorrectly.  What does it look like to you guys?

 

Robot.png

 

The following is my MelScript code


<pre>
global proc int GetFaceCountInObject( string $obj )
{ 
   int $iFaceCount[] = `polyEvaluate -f $obj`; return $iFaceCount[0];
} 

global proc int GetVertexCountInObject( string $obj )
{ 
   int $iVertexCount[] = `polyEvaluate -v $obj`; 
   return $iVertexCount[0];
} 

global proc vector[] GetVerticiesInObject( string $obj )
{ 
   select -r $obj; 
   changeSelectMode -object; 
   Triangulate; 
   int $iVertCount = GetVertexCountInObject( $obj ); 
   vector $v[]; 
   int $vi = 0; 
   for( $i=0; $i < $iVertCount; ++$i ) 
   { 
          float $vert[] = `getAttr ($obj+".vt["+$i+"]")`; 
          $v[$vi] = <<$vert[0], $vert[1], $vert[2]>>; 
          ++$vi; 
   } 

   return $v;
} 

global proc vector[] GetUVSInObjects( string $obj )
{
   select -r $obj;
   changeSelectMode - object;
   Triangulate;
   int $iNumUVs = GetVertexCountInObject( $obj );
   vector $uv[];
   int $uvi = 0;
   for( $i = 0; $i < $iNumUVs; ++$i )
   {
      float $vert[] = `getAttr ($obj+".uv["+$i+"]")`; 
      $uv[$uvi] = <<$vert[0], $vert[1], 0>>; 
      ++$uvi; 
   }

   return $uv;
}

global proc vector[] GetNormalsInObject( string $obj )
{ 
   select -r $obj; 
   changeSelectMode -object; 
   Triangulate; 
   int $iNumFaces = GetFaceCountInObject( $obj ); 
   vector $n[]; 
   int $ni = 0; 
   for( $i = 0; $i < $iNumFaces; ++$i ) 
   { 
      select -r ($obj+".f["+$i+"]"); 
      string $normalInfo[] = `polyInfo -fn`; 
      string $split[]; tokenize $normalInfo[0] $split; 
      vector $normal = << (float)$split[2], (float)$split[3], (float)$split[4] >>; 
      $normal = unit($normal); 
      $n[$ni] = $normal; 
      ++$ni; 
   } 

   return $n;
}

// Each vector contains the indicies into its verticies.
// IE. <<1,3,2>> means vert 1, vert 3, vert 2.
global proc vector[] GetFacesInObject( string $obj )
{ 
   select -r $obj; 
   changeSelectMode -object; 
   Triangulate; 
   int $iNumFaces = GetFaceCountInObject( $obj ); 
   vector $f[]; int $fi = 0; 
   select -r $obj; 
   for( $i = 0; $i < $iNumFaces; ++$i ) 
   { 
      select -r ($obj+".f["+$i+"]"); 
      string $vinds[] = `polyInfo -fv`; 
      string $split[]; tokenize $vinds[0] $split; 
      int $v1 = ((int)$split[2])+1; 
      int $v2 = ((int)$split[3])+1; 
      int $v3 = ((int)$split[4])+1; 

      $f[$fi] = << $v1, $v2, $v3 >>; ++$fi; 
   }
   return $f;
}  

global proc matrix GetWorldMatrixOfObject( string $obj )
{  
   string $data[] = `listRelatives -p $obj`;
   string $transform = $data[0]; 
   float $f[] = `xform -q -ws -m $transform`;
   matrix $m[4][4] = << $f[0], $f[1],   $f[2],    $f[3]; 
                        $f[4], $f[5],   $f[6],    $f[7]; 
                        $f[8], $f[9],   $f[10],   $f[11]; 
                        $f[12], $f[13], $f[14],   $f[15] >>;
 
   return $m; 
} 

global proc vector TransformPoint( vector $p, matrix $m )
{ 
   float $f[]; 
   $f[0] = $p.x * $m[0][0] + $p.y * $m[1][0] + $p.z * $m[2][0] + $m[3][0]; 
   $f[1] = $p.x * $m[0][1] + $p.y * $m[1][1] + $p.z * $m[2][1] + $m[3][1]; 
   $f[2] = $p.x * $m[0][2] + $p.y * $m[1][2] + $p.z * $m[2][2] + $m[3][2]; 

   vector $v = << $f[0], $f[1], $f[2] >>; 

   return $v;
} 

global proc vector GetWorldPositionOfObject( string $obj )
{ 
   float $f[] = `getAttr ($obj+".parentMatrix")`; 
   vector $vPos = << $f[12], $f[13], $f[14] >>; 
   return $vPos;
} 

proc ExportScene(string $fileName)
{ 
   string $nodes[] = `ls -type "mesh"`; 
   $f = `fopen $fileName "w"`; 
   int $iTotalVerts = 0; 
   int $iTotalFaces = 0; 

   for($n in $nodes) 
   { 
      $iTotalVerts += GetVertexCountInObject( $n ); 
      $iTotalFaces += GetFaceCountInObject( $n ); 
   } 

   fprint $f ("# Vertex Count: " + $iTotalVerts + "\n"); 
   fprint $f ("# Face Count: " + $iTotalFaces + "\n\n"); 
   vector $verts[]; 
   vector $normals[];
   vector $faces[]; 
   $vi = 0; 
   $ni = 0;
   $uvi = 0; 

   // Export all vertex information. 
   fprint $f ("# Vertex Information\n"); 
   for ( $obj in $nodes ) 
   { 
      matrix $matWorld[4][4] = GetWorldMatrixOfObject( $obj ); 
      vector $localVerts[] = GetVerticiesInObject( $obj ); 

      for( $i = 0; $i < size($localVerts); ++$i ) 
      { 
        vector $v = TransformPoint( $localVerts[$i], $matWorld );
        $verts[$i] = $v; 
        fprint $f ("v " + $v.x + " " + $v.y + " " + $v.z + "\n"); 
      } 
   } 

   fprint $f ("\n"); fprint $f ("# Normal Information\n"); 
   for ( $obj in $nodes ) 
   {
      matrix $matWorld[4][4] = GetWorldMatrixOfObject( $obj );  
      vector $localNormals[] = GetNormalsInObject( $obj );
      for( $i = 0; $i < size($localNormals); ++$i ) 
      { 
         //vector $normal = $localNormals[$i];
         vector $normal = TransformPoint( $localNormals[$i], $matWorld ); 
         $normals[$i] = $normal; 
         fprint $f ("n " + $normal.x + " " + $normal.y + " " + $normal.z + "\n"); 
      } 
   } 

   fprint $f ("\n"); fprint $f ("# UV Information\n"); 
   for ( $obj in $nodes ) 
   { 
      vector $localUVs[] = GetUVSInObjects( $obj ); 
      for( $i = 0; $i < size($localUVs); ++$i ) 
      { 
         vector $uv = $localUVs[$i]; 
         $uvs[$i] = $uv; 
         fprint $f ("vt " + $uv.x + " " + $uv.y + " " + $uv.z + "\n"); 
      } 
   }

   fprint $f ("\n"); 
   fprint $f ("# Face Information\n"); 
   int $iVC = 0; 
   int $iNC = 0;
   int $nbase = 0; 
   int $vbase = 0; 
   for ( $obj in $nodes ) 
   { 
      vector $localVerts[] = GetVerticiesInObject( $obj ); 
      vector $localNormals[] = GetNormalsInObject( $obj ); 
      vector $faces[] = GetFacesInObject( $obj ); 
      for( $i = 0; $i < size($faces); ++$i ) 
      { 
         vector $fv = $faces[$i]; 
         vector $fvf = << $fv.x + $vbase, $fv.y + $vbase, $fv.z + $vbase >>; 
         vector $nvf = << $fv.x + $nbase, $fv.y + $nbase, $fv.z + $nbase >>;
         int $uv = 1;
         fprint $f ("f " + $fvf.x + "\/" +$uv+ "\/" + $nvf.x + " " + $fvf.y + "\/" +$uv+ "\/" + $nvf.y + " " + $fvf.z + "\/"+$uv+"\/" + $nvf.z + "\n"); 
         ++$iVC; 
         ++$iNC;
      } 

      $nbase += size($localNormals); 
      $vbase += size($localVerts); 
   } 

   fclose $f; 

   select -cl;
} 

proc int onSave( string $filename, string $type )
{ 
   ExportScene($filename); 
   return true;
} 

string $result[] = `fileDialog2 -fileFilter "*.OBJ (*.obj)" -dialogStyle 2 -fileMode 0 -okc "Export"`;

if ( size($result) > 0 )
{
   onSave( $result[0], "");
}

</pre>

0

Share this post


Link to post
Share on other sites

Is it possible that you're doubling the geometry or loading two of them in the same spot?

0

Share this post


Link to post
Share on other sites

 

I am currently learning MelScript

Yikes...good luck with that. MEL is a pretty terrible language. At some point you may want to consider switching to Python so that you can use PyMEL.

0

Share this post


Link to post
Share on other sites

Good catch!

 

So I solved the problem with your help.  The issue was indeed a Z-Fighting issue.  I was importing an animated/skinned mesh into Maya.  Maya creates a duplicate base mesh for skinned meshes and I was exported all meshes in the scene.  As soon as I unbind the skinned mesh from the original mesh and delete the skinned mesh, the exporter works fine.  Thanks guys!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0