piątek, 27 marca 2009

Napisy przestrzenne (WPF 3D) (przykłady w języku C#)

Aby zobaczyć jak tworzyć etykiety 3D, zobaczymy jak działa funkcja, która to realizuje: CreateTextLabel3D. Ta funkcja jest odpowiedzialna za wytworzenie obiektu typu ModelVisual3D , zawierającego etykiety 3D. Funkcja ta będzie miała następującą deklarację:

public static ModelVisual3D CreateTextLabel3D( string text, Brush textColor, bool isDoubleSided, double height, Point3D basePoint, bool isBasePointCenterPoint, Vector3D vectorOver, Vector3D vectorUp);

Parametry wywołania:

  • text - tekst który chcemy narysować
  • textColor - kolor tekstu
  • isDoubleSided - czy ma być widoczny z dwóch stron?
  • height - wysokość znaków
  • basePoint - punkt bazowy etykiety
  • isBasePointCenterPoint - jeśli ustawione na true punkt bazowy jest punktem centralnym etykiety
  • vectorOver - wektor poziomy napisu
  • vectorUp - wektor pionowy napisu

Funkcja zwraca: element dodawany do Viewport3D
Uwaga: Dwa wektory: vectorOver i vectorUp tworzą powierzchnię, na której rysowany jest tekst. obydwa wektory używane są do wyliczeń wielkości etykiety więc najlepiej, by każda ich współrzędna była 0 lub 1. np. [1,1,0] lub [1,0,1], etc...

Jak ona działa?

  1. W pierwszym kroku tworzymy TextBlock , który zawiera naszą etykietę.

    TextBlock textblock = new TextBlock(new Run(text));

    textblock.Foreground = textColor; // ustawianie koloru

    textblock.FontFamily = new FontFamily("Arial"); // ustawianie czcionki

  2. W drugim kroku, tworzymy pędzel i materiał TextBlock , który zawiera stworzony w poprzednim kroku.

    DiffuseMaterial mataterialWithLabel = new DiffuseMaterial();

    // Allows the application of a 2-D brush,

    // like a SolidColorBrush or TileBrush, to a diffusely-lit 3-D model.

    // we are creating the brush from the TextBlock

    mataterialWithLabel.Brush = new VisualBrush(textblock);

  3. Teraz czas jest na przygotowanie objektu 3D, który zostanie pokryty tekstem

    //calculation of text width (assumming that characters are square):

    double width = text.Length * height;

    // we need to find the four corners

    // p0: the lower left corner; p1: the upper left

    // p2: the lower right; p3: the upper right

    Point3D p0 = basePoint;

    // when the base point is the center point we have to set it up in different way

    if(isBasePointCenterPoint)

    p0 = basePoint - width / 2 * vectorOver - height / 2 * vectorUp;

    Point3D p1 = p0 + vectorUp * 1 * height;

    Point3D p2 = p0 + vectorOver * width;

    Point3D p3 = p0 + vectorUp * 1 * height + vectorOver * width;

    // we are going to create object in 3D now:

    // this object will be painted using the (text) brush created before

    // the object is rectangle made of two triangles (on each side).

    MeshGeometry3D my3D_Restangle = new MeshGeometry3D();

    my3D_Restangle.Positions = new Point3DCollection();

    my3D_Restangle.Positions.Add(p0); // 0

    my3D_Restangle.Positions.Add(p1); // 1

    my3D_Restangle.Positions.Add(p2); // 2

    my3D_Restangle.Positions.Add(p3); // 3

    // when we want to see the text on both sides:

    if (isDoubleSided)

    {

    my3D_Restangle.Positions.Add(p0); // 4

    my3D_Restangle.Positions.Add(p1); // 5

    my3D_Restangle.Positions.Add(p2); // 6

    my3D_Restangle.Positions.Add(p3); // 7

    }

    my3D_Restangle.TriangleIndices.Add(0);

    my3D_Restangle.TriangleIndices.Add(3);

    my3D_Restangle.TriangleIndices.Add(1);

    my3D_Restangle.TriangleIndices.Add(0);

    my3D_Restangle.TriangleIndices.Add(2);

    my3D_Restangle.TriangleIndices.Add(3);

    // when we want to see the text on both sides:

    if (isDoubleSided)

    {

    my3D_Restangle.TriangleIndices.Add(4);

    my3D_Restangle.TriangleIndices.Add(5);

    my3D_Restangle.TriangleIndices.Add(7);

    my3D_Restangle.TriangleIndices.Add(4);

    my3D_Restangle.TriangleIndices.Add(7);

    my3D_Restangle.TriangleIndices.Add(6);

    }

  4. W kolejnym kroku pokrywamy stworzony przed chwilą obiekt 3D używając materiału stworzonego na początku. // texture coordinates must be set to // stretch the TextBox brush to cover // the full side of the 3D label. my3D_Restangle.TextureCoordinates.Add(new Point(0, 1)); my3D_Restangle.TextureCoordinates.Add(new Point(0, 0)); my3D_Restangle.TextureCoordinates.Add(new Point(1, 1)); my3D_Restangle.TextureCoordinates.Add(new Point(1, 0)); // when the label is double sided: if (isDoubleSided) { my3D_Restangle.TextureCoordinates.Add(new Point(1, 1)); my3D_Restangle.TextureCoordinates.Add(new Point(1, 0)); my3D_Restangle.TextureCoordinates.Add(new Point(0, 1)); my3D_Restangle.TextureCoordinates.Add(new Point(0, 0)); }
  5. Teraz jest czas na stworznie obiektu ModelVisual3D z napisem

    ModelVisual3D result = new ModelVisual3D();

    // we are setting the content:

    // our 3D rectangle object covered with materila that is made of label

    // (TextBox with text)

    result.Content = new GeometryModel3D(my3D_Restangle, mataterialWithLabel);

    return result;

(To tylko jedna z części artykułu, przeczytaj pozostałe: 1, 2, 3, 4, 5, 6, 7)

Brak komentarzy:

Prześlij komentarz

Posty powiązane / Related posts