我需要在 helix-toolkit 视口中显示一个圆形圆环。我找到的最接近的选项是 HelixVisual3D,但它有开放式末端。所以我尝试创建自己的类来绘制它,使用 HelixVisual3D 和两个细圆锥来闭合末端。我使用了他们的 Building Demo 中的类作为参考,所以我想出了这个:
public class TorusVisual3D : UIElement3D
{
public static readonly DependencyProperty AngleProperty = DependencyPropertyEx.Register("Angle", 90, (s, e) => s.AppearanceChanged());
public static readonly DependencyProperty RadiusProperty = DependencyPropertyEx.Register("Radius", 0.35, (s, e) => s.AppearanceChanged());
public static readonly DependencyProperty DiameterProperty = DependencyPropertyEx.Register("Diameter", 0.1, (s, e) => s.AppearanceChanged());
private GeometryModel3D torus = new GeometryModel3D();
private GeometryModel3D cap1 = new GeometryModel3D();
private GeometryModel3D cap2 = new GeometryModel3D();
public TorusVisual3D()
{
AppearanceChanged();
Model3DGroup model = new Model3DGroup();
model.Children.Add(this.torus);
model.Children.Add(this.cap1);
model.Children.Add(this.cap2);
this.Visual3DModel = model;
}
public double Angle
{
get
{
return (double)this.GetValue(AngleProperty);
}
set
{
this.SetValue(AngleProperty, value);
}
}
public double Radius
{
get
{
return (double)this.GetValue(RadiusProperty);
}
set
{
this.SetValue(RadiusProperty, value);
}
}
public double Diameter
{
get
{
return (double)this.GetValue(DiameterProperty);
}
set
{
this.SetValue(DiameterProperty, value);
}
}
private void AppearanceChanged()
{
Material mat = MaterialHelper.CreateMaterial(Utils.GetRandomColor());
HelixVisual3D h = new HelixVisual3D();
h.Origin = new Point3D(0, 0, 0);
h.Diameter = Diameter;
h.Turns = Angle / 360.0;
h.Radius = Radius;
h.Length = 0;
h.BackMaterial = mat;
h.Material = mat;
h.UpdateModel();
this.torus = h.Model;
MeshBuilder cap1Builder = new MeshBuilder(false, false);
Point3D p1 = new Point3D(0, Radius, 0);
cap1Builder.AddCone(p1, new Vector3D(0, 1, 0), h.Diameter / 2, h.Diameter / 2, 0.0001, true, true, 40);
this.cap1.Material = MaterialHelper.CreateMaterial(Colors.Yellow);
this.cap1.Geometry = cap1Builder.ToMesh();
MeshBuilder cap2Builder = new MeshBuilder(false, false);
Point3D p2 = new Point3D(-1, 0, 0);
cap2Builder.AddCone(p2, new Vector3D(1, 0, 0), h.Diameter / 2, h.Diameter / 2, 0.0001, true, true, 40);
this.cap2.Material = MaterialHelper.CreateMaterial(Colors.Red);
this.cap2.Geometry = cap2Builder.ToMesh();
}
}
要绘制它,我使用以下代码:
TorusVisual3D t = new TorusVisual3D();
t.Angle = m_angle;
t.Radius = m_radius1;
t.Diameter = m_radius2 * 2.0;
t.Transform = new TranslateTransform3D(0, 0, 0);
ModelVisual3D model = new ModelVisual3D();
model.Children.Add(t);
var container = new ContainerUIElement3D();
container.Children.Add(model);
viewport.Children.Add(container);
问题是螺旋线是使用默认值(半径=0.35,直径=0.1 和角度=90)绘制的,并且永远不会再次更新。无论我在属性上设置什么值,它都保持不变。两个圆柱体都正确更新,只是螺旋没有。
我在做什么错?