WPF 3D渐变画笔问题

发布于 2024-08-06 07:11:47 字数 2124 浏览 4 评论 0原文

主要问题出在 LinearGradientBrush 中。它用纯红色填充三角形。怎么了?我已经尝试过使用二维形状几乎相同的代码。它工作得很好。

<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>

The main problem is in LinearGradientBrush. It fills triangle in solid red. What's wrong? I have tried almost the same code with 2d shapes. It worked perfectly.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2024-08-13 07:11:47

您需要将纹理坐标添加到几何图形中:

<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>

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

You need to add TextureCoordinates to your 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>

Once you do this, the material will map to the texture coordinates appropriately, and you'll see your black->red gradients. Right now, the texture coordinates are all defaulting to a point that's in the "red" portion of the gradient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文