70002193 Calculating TexCoords of a vdPolyface created by Generate3dPathSection

Article 70002193
Type HowTo
Product Engine
Version 8
Date Added 4/12/2022 12:00:00 AM
Fixed 10.1001.0.1 (4/12/2022 12:00:00 AM)
Submitted by kerry francis

Summary

Calculating TexCoords of a vdPolyface created by Generate3dPathSection in order to keep the path direction for the selected Material

Solution

 vdPolyface pf;//the selected vdPolyface object
 //The calculation will be done according to the selected Material Matrix
 Matrix matmat = pf.PenColor.MaterilaMatrix;
 //matmat.RotateZMatrix(Globals.HALF_PI); //just a test to see the result if we change the material matrix by rotating 90 degrees
 Matrix _w2ecs = new Matrix();
 Vector normal = new Vector();
 gPoint uv0 = new gPoint();
 //hold the 4 points of each face in facelist
 gPoint[] pts = new gPoint[4];
 //A texture coord array (with x, y) values for each vertex in facelist
 DoubleArray textcoords = new DoubleArray();
 for (int i = 0; i < pf.FaceList.Count; i = i + 5)
 {
     //get the 4 points of the face
     for (int k = 0; k < 4; k++)
     {
          pts[k] = pf.VertexList[Math.Abs(pf.FaceList[i + k]) - 1];
     }
     //calculate the normal of the face perpendicular to the face plane
     Vector.NormalVector(pts[0], pts[1], pts[2], ref normal);
     //calculate the texture coords for each vertex in the face
     //by applying the direction of the face which in case of creating a polyface using the Generate3dpath is always
     //from first point of the face to the forth point --> new Vector(pts[0], pts[3])
     _w2ecs.IdentityMatrix();//a matrix used to transform the points from word to to new coord system, defined by calculated  normal and direction
     _w2ecs.ApplyECS2WCS(normal, new Vector(pts[0], pts[3])); _w2ecs.Invert();
     for (int k = 0; k < 4; k++)
     {
         _w2ecs.TransformPt(pts[k], uv0);
         uv0.z = 0;
         //translate with selected material matrix to get the x,y texture coords
         matmat.TransformRefPt(ref uv0);
         if (!Globals.AreEqual(uv0.z, 0, Globals.DefaultVectorEquality)) { uv0.x /= uv0.z; uv0.y /= uv0.z; }
         //add the finally calculated texture coords
         textcoords.Add(uv0.x);
         textcoords.Add(uv0.y);
     }
 }
 //select the calculate texture coords to the polyface and update it
 pf.TexCoords = textcoords;
 pf.Update();
 doc.Redraw(false);

Before


After

Send comments on this topic.