WPF 3D - mapping gradient brush on complex geometry

前提是你 提交于 2019-12-04 16:02:45

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>

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

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}

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)

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!