WPF 3D - 在复杂几何图形上映射渐变画笔 [英] WPF 3D - mapping gradient brush on complex geometry

查看:21
本文介绍了WPF 3D - 在复杂几何图形上映射渐变画笔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下是否有人知道如何在 WPF 3D 中将渐变画笔映射到复杂对象上.结果应该类似于 matlab 中的 3D 图像(例如 3D 函数).假设您有一些想要可视化的 3 维数据,并且想要通过颜色区分某些级别的值.

I wanted to ask if anyone knows how to map gradient brush on complex objects in WPF 3D. The result should look similar to 3D images in matlab (3D function for example). Lets say you have some 3-dimensional data you wish to visualize and you want to diferentiate certain levels of values by color.

推荐答案

给定一个 GradientBrush 定义如下:

Given a GradientBrush defined something like this:

        <LinearGradientBrush x:Name="RedYellowGradient">
            <GradientStop Color="Blue" Offset="0.01" />
            <GradientStop Color="Purple" Offset="0.25"/>
            <GradientStop Color="Red" Offset="0.5"/>
            <GradientStop Color="Orange" Offset="0.75"/>
            <GradientStop Color="Yellow" Offset="1.0"/>
        </LinearGradientBrush>

从根本上说,您将 GradientBrush 分配给 MeshGeometry3DDiffuseMaterial.定义画笔时,将其 ViewportUnits 属性设置为绝对".

Fundamentally, you assign the GradientBrush to the DiffuseMaterial of your MeshGeometry3D. When defining the brush, set its ViewportUnits property to "Absolute".

这样的事情可以直接在 XAML 表单的代码隐藏中工作(否则,使用相应的属性在代码中(在您的 ViewModel 中)创建画笔,或从您的资源字典中调用它):

Something like this would work directly in the code-behind of a XAML form (otherwise, create the brush in code (in your ViewModel) using the corresponding properties, or call it from your resource dictionary):

MyMaterial = New DiffuseMaterial(RedYellowGradient) With {.ViewportUnits = BrushMappingMode.Absolute}

然后,对于几何体中的每个 Point3D,为相应的纹理坐标分配一个介于 0.0 和 1.0 之间的值.通常,对于预先调整大小的 Point 数组,它可能如下所示:

Then, for each Point3D in your geometry, assign a value between 0.0 and 1.0 to the corresponding texture coordinate. Generically, for a pre-sized Point array it could look like this:

    Parallel.For(0, positions.Count - 3, Sub(i)
                                             Dim p = positions(i)
                                             Dim plotValue = GetYourValue(p.X, p.Y, p.Z)
                                             Dim t = (plotValue - minPlot) / (maxPlot - minPlot)
                                             If t < 0 Then t = 0
                                             If t > 1 Then t = 1                                                
                                             arr(i) = New Point(t, 0.5)
                                         End Sub)

如果您的面很长或顶点之间的值相距很远,您的绘图会看起来很奇怪.但考虑到 WPF 3D 的局限性,它可能是您无需大量 UV 贴图就能做到的最佳选择.

If your facets are very long or the values between vertices very far apart, your plotting will look odd. But given the strictures of WPF 3D it's probably the best you can do without a lot of UV Mapping.

(如果您需要 C#,Roslyn CTP 有一个 VS Add-on,它将剪贴板上的 VB 代码转换为 C#...)

(If you need C#, the Roslyn CTP has a VS Add-on which will convert VB code on the clipboard to C#...)

这篇关于WPF 3D - 在复杂几何图形上映射渐变画笔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆