WPF 3D Gradientbrush问题

时间:2009-09-24 17:11:27

标签: wpf xaml texture-mapping viewport3d

主要问题在于LinearGradientBrush。它以实心红色填充三角形。怎么了?我用2d形状尝试了几乎相同的代码。它运作得很好。

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="215" Width="336">
<Grid Height="146" Width="232">
    <Viewport3D Name="myViewPort" ClipToBounds="False">
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="myCamera"
                               Position="10,10,10" 
                               UpDirection="0,1,0" 
                               LookDirection="-10,-10,-10"
                               FieldOfView="10"/>

        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <GeometryModel3D>
                    <GeometryModel3D.Geometry>
                        <MeshGeometry3D Positions="-1,0,0 0,1,0 1,0,0" TriangleIndices="0,2,1" />
                    </GeometryModel3D.Geometry>
                    <GeometryModel3D.Material>
                        <DiffuseMaterial>
                            <DiffuseMaterial.Brush>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                    <GradientStop Color="Black" Offset="0"></GradientStop>
                                    <GradientStop Color="Red" Offset="0.6"></GradientStop>
                                </LinearGradientBrush>
                            </DiffuseMaterial.Brush>
                        </DiffuseMaterial>
                    </GeometryModel3D.Material>
                </GeometryModel3D>

            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <AmbientLight></AmbientLight>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>

</Grid>

1 个答案:

答案 0 :(得分:3)

您需要将TextureCoordinates添加到Geometry:

<GeometryModel3D.Geometry>
    <MeshGeometry3D 
        Positions="-1,0,0 0,1,0 1,0,0" 
        TextureCoordinates="0,0 1,0 1,1"
        TriangleIndices="0,2,1" 
    />
</GeometryModel3D.Geometry>

完成此操作后,材质将适当地映射到纹理坐标,您将看到黑色 - >红色渐变。现在,纹理坐标都默认为渐变的“红色”部分中的一个点。