更新GeometryModel3D材质的单点颜色,而不是整个点系

时间:2016-06-06 22:14:47

标签: c# wpf colors

我无法理解C#WPF项目的颜色/材质系统,目前我正在更新模型每次更新时整个点系统的颜色,而我更愿意更新单点的颜色(添加时)。

AggregateSystem类

public class AggregateSystem {
    // stack to store each particle in aggregate
    private readonly Stack<AggregateParticle> particle_stack;
    private readonly GeometryModel3D particle_model;
    // positions, indices and texture co-ordinates for particles
    private readonly Point3DCollection particle_positions;
    private readonly Int32Collection triangle_indices;
    private readonly PointCollection text_coords;
    // brush to apply to particle_model.Material
    private RadialGradientBrush rad_brush;
    // ellipse for rendering
    private Ellipse ellipse;
    private RenderTargetBitmap render_bitmap;

    public AggregateSystem() {
        particle_stack = new Stack<AggregateParticle>();
        particle_model = new GeometryModel3D { Geometry = new MeshGeometry3D() };
        ellipse = new Ellipse {
            Width = 32.0,
            Height = 32.0
        };
        rad_brush = new RadialGradientBrush();
        // fill ellipse interior using rad_brush
        ellipse.Fill = rad_brush;
        ellipse.Measure(new Size(32,32));
        ellipse.Arrange(new Rect(0,0,32,32));
        render_bitmap = new RenderTargetBitmap(32,32,96,96,PixelFormats.Pbgra32));
        ImageBrush img_brush = new ImageBrush(render_bitmap);
        DiffuseMaterial diff_mat = new DiffuseMaterial(img_brush);
        particle_model.Material = diff_mat;
        particle_positions = new Point3DCollection();
        triangle_indices = new Int32Collection();
        tex_coords = new PointCollection();
    }

    public Model3D AggregateModel => particle_model;

    public void Update() {
        // get the most recently added particle
        AggregateParticle p = particle_stack.Peek();
        // compute position index for triangle index generation
        int position_index = particle_stack.Count * 4;
        // create points associated with particle for circle generation
        Point3D p1 = new Point3D(p.position.X, p.position.Y, p.position.Z);
        Point3D p2 = new Point3D(p.position.X, p.position.Y + p.size, p.position.Z);
        Point3D p3 = new Point3D(p.position.X + p.size, p.position.Y + p.size, p.position.Z);
        Point3D p4 = new Point3D(p.position.X + p.size, p.position.Y, p.position.Z);
        // add points to particle positions collection
        particle_positions.Add(p1);
        particle_positions.Add(p2);
        particle_positions.Add(p3);
        particle_positions.Add(p4);
        // create points for texture co-ords
        Point t1 = new Point(0.0, 0.0);
        Point t2 = new Point(0.0, 1.0);
        Point t3 = new Point(1.0, 1.0);
        Point t4 = new Point(1.0, 0.0);
        // add texture co-ords points to texcoords collection
        tex_coords.Add(t1);
        tex_coords.Add(t2);
        tex_coords.Add(t3);
        tex_coords.Add(t4);
        // add position indices to indices collection
        triangle_indices.Add(position_index);
        triangle_indices.Add(position_index + 2);
        triangle_indices.Add(position_index + 1);
        triangle_indices.Add(position_index);
        triangle_indices.Add(position_index + 3);
        triangle_indices.Add(position_index + 2);
        // update colour of points - **NOTE: UPDATES ENTIRE POINT SYSTEM** 
        // -> want to just apply colour to single particles added
        rad_brush.GradientStops.Add(new GradientStop(p.colour, 0.0));
        render_bitmap.Render(ellipse);
        // set particle_model Geometry model properties
        ((MeshGeometry3D)particle_model.Geometry).Positions = particle_positions;
        ((MeshGeometry3D)particle_model.Geometry).TriangleIndices = triangle_indices;
        ((MeshGeometry3D)particle_model.Geometry).TextureCoordinates = tex_coords;
    }

    public void SpawnParticle(Point3D _pos, Color _col, double _size) {
        AggregateParticle agg_particle = new AggregateParticle {
            position = _pos, colour = _col, size = _size;
        }
        // push most-recently-added particle to stack
        particle_stack.Push(agg_particle);
    }

}

其中AggregateParticle是由Point3D positionColor colordouble size字段组成的POD类,这些字段不言自明。

是否有任何简单有效的方法来更新单个粒子的颜色,因为它是在Update方法而不是整个粒子系统中添加的?或者我是否需要为系统中的每个粒子创建List(或类似的数据结构)DiffuseMaterial个实例,并为每个粒子应用必要颜色的画笔?

[后者是我想不惜一切代价避免的,部分原因是它需要对我的代码进行大量的结构更改,而且我确信有更好的方法来解决这个问题 - 即 必须是一种将颜色应用于一组纹理坐标的简单方法,当然?!。]

更多详情

  • AggregateModel是与Model3D字段对应的单个particle_model个实例,该字段已添加到Model3DGroup的{​​{1}}。

  • 我应该注意到我想要实现的目标,具体来说,这是一个&#34;渐变&#34;聚合结构中每个粒子的颜色,其中粒子在温度梯度中具有MainWindow&#34; (在程序中的其他地方计算),这取决于它产生的顺序 - 即如果先前产生则颗粒具有较冷的颜色,如果稍后产生则具有较暖的颜色。此颜色列表已预先计算并传递到Color方法中的每个粒子,如上所示。

  • 我尝试过的一个解决方案是为每个粒子创建一个单独的Update实例,其中每个对象都有一个关联的AggregateComponent,因此也有相应的画笔。然后创建了一个Model3D类,其中包含每个AggregateComponentManager的{​​{1}}。这个解决方案很有效,但是每次添加一个粒子时每个组件都需要更新,因此内存使用量会爆炸 - 这是一种方法来调整我可以缓存已经渲染的List而无需调用每次添加粒子时都会使用AggregateComponent方法吗?

完整的源代码(AggregateComponent目录中的C#代码)可以在GitHub上找到:https://github.com/SJR276/DLAProject

1 个答案:

答案 0 :(得分:8)

我们为小点云(+/- 100 k点)创建WPF 3D模型,其中每个点作为八面体(8个三角形)添加到MeshGeometry3D。

为了在这样的点云中允许不同点的不同颜色(我们用它来选择一个点或一个子集),我们从一个小的位图中指定纹理坐标。

在高级别,我们有一些像这样的代码:

BitmapSource bm = GetColorsBitmap(new List<Color> { BaseColor, SelectedColor });
ImageBrush ib = new ImageBrush(bm) 
{ 
    ViewportUnits = BrushMappingMode.Absolute, 
    Viewport = new Rect(0, 0, 1, 1) // Matches the pixels in the bitmap.
};  
GeometryModel3D model = new GeometryModel3D { Material = new DiffuseMaterial(ib) };

现在纹理坐标只是

new Point(0, 0);
new Point(1, 0);

......等等。

Bitmap的颜色来自:

// Creates a bitmap that has a single row containing single pixels with the given colors.
// At most 256 colors.
public static BitmapSource GetColorsBitmap(IList<Color> colors)
{
    if (colors == null) throw new ArgumentNullException("colors");
    if (colors.Count > 256) throw new ArgumentOutOfRangeException("colors", "More than 256 colors");

    int size = colors.Count;
    for (int j = colors.Count; j < 256; j++)
    {
        colors.Add(Colors.White);
    }

    var palette = new BitmapPalette(colors);
    byte[] pixels = new byte[size];
    for (int i = 0; i < size; i++)
    {
        pixels[i] = (byte)i;
    }

    var bm = BitmapSource.Create(size, 1, 96, 96, PixelFormats.Indexed8, palette, pixels, 1 * size);
    bm.Freeze();
    return bm;
}

在更新点云时,我们还会努力缓存和重用内部几何结构。

最后,我们用令人敬畏的Helix Toolkit显示这个。