2

3Dキューブのセットを描画したいのですが、各キューブには名前が表示され、キューブが選択されたときに独自のイベントハンドラーが必要です。

コードビハインドまたはxamlバインディングを使用して実装することは可能ですか?

4

1 に答える 1

6

コードビハインドから3Dキューブを描画するには、Helix3DツールキットCubeVisual3Dを使用します。ただし、標準のWPF 3D要素を使い続けたい場合は、実装がかなり簡単です。

3D環境でのテキストについて学ぶためにここから始めてくださいhttp://www.codeproject.com/Articles/33893/WPF-Creation-of-Text-Labels-for-3D-Sceneは、テキストを追加するための2つの異なるアプローチをガイドします。 3D画像と私は非常に役に立ちます。

立方体の場合RectangleVisual3Dオブジェクトを使用するだけですこのようなもの。

    RectangleVisual3D myCube = new RectangleVisual3D();
    myCube.Origin = new Point3D(0, 0, 0); //Set this value to whatever you want your Cube Origen to be.
    myCube.Width = 5; //whatever width you would like.
    myCube.Length = 5; //Set Length = Width
    myCube.Normal = new Vector3D(0, 1, 0); // if you want a cube that is not at some angle then use a vector in the direction of an axis such as this one or <1,0,0> and <0,0,1>
    myCube.LengthDirection = new Vector3D(0, 1, 0); //This will depend on the orientation of the cube however since it is equilateral just set it to the same thing as normal.
    myCube.Material = new DiffuseMaterial(Brushes.Red); // Set this with whatever you want or just set the myCube.Fill Property with a brush type.

イベントハンドラーを追加しすぎます。Viewport3Dにハンドラーを追加する必要があると思います。この性質の何かが機能するはずです。

    public Window1()
    {
    InitializeComponent();
    this.mainViewport.MouseDown += new MouseButtonEventHandler(mainViewport_MouseDown);
    this.mainViewport.MouseUp += new MouseButtonEventHandler(mainViewport_MouseUp);
    }

次に、この関数を追加します

    ModelVisual3D GetHitResult(Point location)
    {
    HitTestResult result = VisualTreeHelper.HitTest(mainViewport, location);
    if(result != null && result.VisualHit is ModelVisual3D)
    {
    ModelVisual3D visual = (ModelVisual3D)result.VisualHit;
    return visual;
    }

    return null;
    }

次に、イベントハンドラーを追加します

    void mainViewport_MouseUp(object sender, MouseButtonEventArgs e)
    {
    Point location = e.GetPosition(mainViewport);
    ModelVisual3D result = GetHitResult(location);
    if(result == null)
    {
    return;
    }
    //Do Stuff Here
    }

    void mainViewport_MouseDown(object sender, MouseButtonEventArgs e)
    {
    Point location = e.GetPosition(mainViewport);
    ModelVisual3D result = GetHitResult(location);
    if(result == null)
    {
    return;
    }
    //Do Stuff Here
    }
于 2012-09-27T08:22:19.980 に答える