Thursday, 4 November 2010

Week 4 Line Test and Maya

Week 4 - pushing and pulling and last week's bouncy ball with a mesh deform to vertex color based shader I coded in MEL.




code...

int $numberOfVertices[]=`polyEvaluate -vertex pSphere1`;
string $neighbouredges[];
string $neighbourverts[];
float $edge_start_original[3];
float $edge_end_original[3];
float $edge_start_deformed[3];
float $edge_end_deformed[3];
float $diff_original[3];
float $diff_deformed[3];
float $total_length_original;
float $total_length_deformed;
float $stretch_amount[];
int $edges[];
int $i=0;
for($i=0;$i<$numberOfVertices[0];$i++)
 {
 //find neighbour edges
 $neighbouredges=`polyInfo -ve pSphereShape1.vtx[$i]`;
 string $buffer[];
 int $num_tokens = `tokenize $neighbouredges[0] $buffer`;
 $num_tokens-=2;
 for ($k=0;$k<$num_tokens;$k++)
  {
  $edges[$k]=(int)$buffer[$k+2];
  }
 //now convert the edges to lengths

 $total_length_original=0;
 $total_length_deformed=0;
 for ($l=0;$l<$num_tokens;$l++)
  {
  $neighbourverts=`polyInfo -ev pSphereShape1.e[$edges[$l]]`;
  tokenize $neighbourverts[0] $buffer;
  int $verts[2];
  $verts[0]=(int)$buffer[2];
  $verts[1]=(int)$buffer[3];

  //get edge lengths
  $edge_start_original=`xform -ws -q -t pSphereShape1Orig.vtx[$verts[0]]`;
  $edge_end_original=`xform -ws -q -t pSphereShape1Orig.vtx[$verts[1]]`;
  $edge_start_deformed=`xform -ws -q -t pSphereShape1.vtx[$verts[0]]`;
  $edge_end_deformed=`xform -ws -q -t pSphereShape1.vtx[$verts[1]]`;
  $diff_original[0]=$edge_end_original[0]-$edge_start_original[0];
  $diff_original[1]=$edge_end_original[1]-$edge_start_original[1];
  $diff_original[2]=$edge_end_original[2]-$edge_start_original[2];
  $diff_deformed[0]=$edge_end_deformed[0]-$edge_start_deformed[0];
  $diff_deformed[1]=$edge_end_deformed[1]-$edge_start_deformed[1];
  $diff_deformed[2]=$edge_end_deformed[2]-$edge_start_deformed[2];
  $total_length_original+=sqrt(pow($diff_original[0],2)+pow($diff_original[1],2)+pow($diff_original[2],2));
  $total_length_deformed+=sqrt(pow($diff_deformed[0],2)+pow($diff_deformed[1],2)+pow($diff_deformed[2],2));
  }
 $stretch_amount[$i]=($total_length_deformed/$total_length_original);
 }

//find max and min
float $max_stretch=-1;
float $min_stretch=-1;
for($i=0;$i<$numberOfVertices[0];$i++)
 {
 if($max_stretch==-1 && $min_stretch==-1)
  {
  $max_stretch=$stretch_amount[$i];
  $min_stretch=$stretch_amount[$i];
  }
 if($max_stretch<$stretch_amount[$i])$max_stretch=$stretch_amount[$i];
 if($min_stretch>$stretch_amount[$i])$min_stretch=$stretch_amount[$i];
 }

for($i=0;$i<$numberOfVertices[0];$i++)
 {
 if($min_stretch!=$max_stretch)$stretch_amount[$i]=($stretch_amount[$i]-$min_stretch)*(1/($max_stretch-$min_stretch));
 else $stretch_amount[$i]=0;
 polyColorPerVertex -colorR $stretch_amount[$i] pSphereShape1.vtx[$i];
 polyColorPerVertex -colorG $stretch_amount[$i] pSphereShape1.vtx[$i];
 polyColorPerVertex -colorB $stretch_amount[$i] pSphereShape1.vtx[$i];
 }

No comments:

Post a Comment