在WPF中使用Helix 3d创建旋转球体

时间:2014-04-21 07:38:17

标签: c# wpf xaml helix-3d-toolkit

我刚刚开始使用Helix 3d。我想要做的是创建一个球体并在它的轴上旋转它,在一些用户交互上,比如说(在滑动时)或使用鼠标旋转它。

以下代码有效,但我无法看到任何使用的颜色。也有人可以解释为什么会这样吗?

我刚刚复制并粘贴了我发现here的代码!这是我得到的输出 Ouput Image

这是C#MainWindow.XAML.cs

中的代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Media.Media3D;

    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using HelixToolkit;
    using HelixToolkit.Wpf;

namespace helixdemo1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        protected RotateManipulator r1Manip { get; set; }
        protected RotateManipulator r2Manip { get; set; }
        protected RotateManipulator r3Manip { get; set; }


        public MainWindow()
        {
            InitializeComponent();
            Create3DView();
        }

        private void Create3DView()
        {
            var builder = new MeshBuilder(true, true);
            var position = new Point3D(0, 0, 0);
            builder.AddSphere(position, 1,15,15);

            var geom = new GeometryModel3D(builder.ToMesh(), Materials.Brown);
            var visual = new ModelVisual3D();
            visual.Content = geom;

            r1Manip = new RotateManipulator();
            r1Manip.Color = Colors.Orange;
            r1Manip.Axis = new Vector3D(0, 0, 1);
            r1Manip.Diameter = 1;
            r1Manip.Bind(visual);

            r2Manip = new RotateManipulator();
            r2Manip.Color = Colors.Blue;
            r2Manip.Axis = new Vector3D(0, 1, 0);
            r2Manip.Diameter = 1;
            r2Manip.Bind(visual);

            r3Manip = new RotateManipulator();
            r3Manip.Color = Colors.Green;
            r3Manip.Axis = new Vector3D(1, 0, 0);
            r3Manip.Diameter = 1;
            r3Manip.Bind(visual);

            ModelArea.Children.Add(r1Manip);
            ModelArea.Children.Add(r2Manip);
            ModelArea.Children.Add(r3Manip);

            ModelArea.Children.Add(visual);
        }

    }
}

这是XAML代码

<Window x:Class="helixdemo1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"        
        Title="MainWindow" Height="605" Width="597">

    <Grid>
        <Grid Width="300" Height="300" Margin="0,25,0,0" VerticalAlignment="Top" HorizontalAlignment="Center" >
            <HelixToolkit:HelixViewport3D x:Name="viewport"  ZoomExtentsWhenLoaded="True">
                <ModelVisual3D x:Name="ModelArea"></ModelVisual3D>
            </HelixToolkit:HelixViewport3D>
        </Grid>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:4)

就颜色而言,您忘记为场景设置灯光:
没有灯光 - &gt;黑色物体!
试试这个:

    <Grid>
        <Grid Width="300" Height="300" Margin="0,25,0,0" VerticalAlignment="Top" HorizontalAlignment="Center" >
        <HelixToolkit:HelixViewport3D x:Name="viewport"  ZoomExtentsWhenLoaded="True">
            <HelixToolkit:DefaultLights/>
            <ModelVisual3D x:Name="ModelArea"></ModelVisual3D>
        </HelixToolkit:HelixViewport3D>
        </Grid>
    </Grid>

或者您可以在.cs文件中添加此行(例如,在Create3DView()的开头):

viewport.Children.Add(new DefaultLights());