using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.Media3D; namespace HelixToolkit { /// /// Abstract base class for PointsVisual3D and LinesVisual3D. /// public abstract class ScreenSpaceVisual3D : ModelVisual3D { /// /// Gets or sets the points collection. /// /// /// The points collection. /// public Point3DCollection Points { get { return (Point3DCollection)GetValue(PointsProperty); } set { SetValue(PointsProperty, value); } } public static readonly DependencyProperty PointsProperty = DependencyProperty.Register("Points", typeof(Point3DCollection), typeof(ScreenSpaceVisual3D), new UIPropertyMetadata(new Point3DCollection(), GeometryChanged)); /// /// Gets or sets the depth offset. /// A small positive number (0.0001) will move the visual slightly in front of other objects. /// /// /// The depth offset. /// public double DepthOffset { get { return (double)GetValue(DepthOffsetProperty); } set { SetValue(DepthOffsetProperty, value); } } public static readonly DependencyProperty DepthOffsetProperty = DependencyProperty.Register("DepthOffset", typeof(double), typeof(ScreenSpaceVisual3D), new UIPropertyMetadata(0.0, GeometryChanged)); protected static void GeometryChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((ScreenSpaceVisual3D)d).OnGeometryChanged(); } private void OnGeometryChanged() { UpdateGeometry(); } /// /// Gets or sets the color. /// /// /// The color. /// public Color Color { get { return (Color)GetValue(ColorProperty); } set { SetValue(ColorProperty, value); } } public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ScreenSpaceVisual3D), new UIPropertyMetadata(Colors.Black, ColorChanged)); private static void ColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((ScreenSpaceVisual3D)d).OnColorChanged(); } private void OnColorChanged() { model.Material = MaterialHelper.CreateMaterial(Brushes.Black, new SolidColorBrush(Color)); } protected GeometryModel3D model; protected MeshGeometry3D mesh; /// /// Initializes a new instance of the class. /// public ScreenSpaceVisual3D() { mesh = new MeshGeometry3D(); model = new GeometryModel3D(); model.Geometry = mesh; Content = model; OnColorChanged(); IsRendering = true; } private bool isRendering; /// /// Gets or sets a value indicating whether this instance is being rendered. /// When the visual is removed from the Viewport3D, this property should be set to false. /// This will unsubcribe the CompositionTarget.Rendering event. /// public bool IsRendering { get { return isRendering; } set { if (value != isRendering) { isRendering = value; if (isRendering) CompositionTarget.Rendering += CompositionTarget_Rendering; else CompositionTarget.Rendering -= CompositionTarget_Rendering; } } } void CompositionTarget_Rendering(object sender, EventArgs e) { // if (Visual3DHelper.IsAttachedToViewport3D(this)) if (isRendering) { if (UpdateTransforms()) UpdateGeometry(); } } /// /// Updates the transforms. /// /// /// True if the transform is updated. /// protected abstract bool UpdateTransforms(); /// /// Updates the geometry. /// protected abstract void UpdateGeometry(); } }