ModelVisual3D无法在DockPanel中正确呈现

时间:2017-04-25 16:46:59

标签: c# .net wpf xaml modelvisual3d

我是WPF的新手,我正在尝试创建一个应用程序,我将在一个选项卡中绘制一个地球,并在第二个选项卡中显示一个平面地图。我能够在测试应用程序中绘制全局图,代码如下:

Select JTS.JobType, count(*) as count 
from (
   SELECT 1 as JT
     UNION ALL
   SELECT 2 as JT
     UNION ALL
   SELECT 3 as JT
     UNION ALL
   SELECT 4 as JT
) AS JTS
LEFT JOIN PrintJobs AS P with (nolock) ON P.JobType = JTS.JT 
      AND convert(datetime, P.TimeSubmitted) > cast(CAST(dateadd(dd, -1,GETDATE()) as date)  as datetime) + CAST('17:30:00.000' as time)
group by P.JobType

我使用此处给出的代码从this link提取的球形代码:

    <Window x:Class="SphereTutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SphereTutorial"
        xmlns:earth="clr-namespace:SphereTutorial.Globe"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <Grid.Resources>
            <earth:SphereMeshGenerator x:Key="SphereGeometrySource1"/>
            <MeshGeometry3D x:Key="SphereGeometry1" 

                            Positions="{Binding Source={StaticResource 
                                  SphereGeometrySource1}, Path=Geometry.Positions}"

                            TriangleIndices="{Binding Source={StaticResource 
                                  SphereGeometrySource1}, Path=Geometry.TriangleIndices}"

                            TextureCoordinates="{Binding Source={StaticResource SphereGeometrySource1}, Path=Geometry.TextureCoordinates}"/>

        </Grid.Resources>
        <Grid.Background>
            Black
        </Grid.Background>
        <Viewport3D x:Name="mainScene3D">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="mainCam" LookDirection="-1,0,0" Position="5,0,0" UpDirection="0,1,0"/>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <Model3DGroup>
                        <AmbientLight Color="White"/>
                        <GeometryModel3D Geometry="{StaticResource SphereGeometry1}">
                            <GeometryModel3D.Material>
                                <MaterialGroup>
                                    <DiffuseMaterial>
                                        <DiffuseMaterial.Brush>
                                            <ImageBrush ImageSource="H:\C#\SphereTutorial\SphereTutorial\Images\Earth.jpg"/>
                                        </DiffuseMaterial.Brush>
                                    </DiffuseMaterial>
                                </MaterialGroup>
                            </GeometryModel3D.Material>
                        </GeometryModel3D>
                    </Model3DGroup>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>
    </Grid>
</Window>

我能够验证它是否适用于此方法;然而,当我在我的应用程序中实现相同的逻辑时,事情就崩溃了。出于某种原因,它开始仅在边缘上渲染图像。我有一种感觉,它可能与在DockPanel中有关?虽然2D图像在其DockPanel中工作正常。以下代码不起作用:

    namespace VenProp.Sphere3D
{
    class SphereMeshGenerator
    {
        private int _slices = 100;
        private int _stacks = 50;
        private Point3D _center = new Point3D();
        private double _radius = 1;

        public int Slices
        {
            get { return _slices; }
            set { _slices = value; }
        }

        public int Stacks
        {
            get { return _stacks; }
            set { _stacks = value; }
        }

        public Point3D Center
        {
            get { return _center; }
            set { _center = value; }
        }

        public double Radius
        {
            get { return _radius; }
            set { _radius = value; }
        }

        public MeshGeometry3D Geometry
        {
            get
            {
                return CalculateMesh();
            }
        }


        private MeshGeometry3D CalculateMesh()
        {
            MeshGeometry3D mesh = new MeshGeometry3D();

            for (int stack = 0; stack <= Stacks; stack++)
            {
                double phi = Math.PI / 2 - stack * Math.PI / Stacks; 
                double y = _radius * Math.Sin(phi);
                double scale = -_radius * Math.Cos(phi);

                for (int slice = 0; slice <= Slices; slice++)
                {
                    double theta = slice * 2 * Math.PI / Slices; 
                    double x = scale * Math.Sin(theta); 
                    double z = scale * Math.Cos(theta); 

                    Vector3D normal = new Vector3D(x, y, z);  
                    mesh.Normals.Add(normal);
                    mesh.Positions.Add(normal + Center);  
                    mesh.TextureCoordinates.Add(new Point((double)slice / Slices, (double)stack / Stacks));
                }
            }

            for (int stack = 0; stack <= Stacks; stack++)
            {
                int top = (stack + 0) * (Slices + 1);
                int bot = (stack + 1) * (Slices + 1);

                for (int slice = 0; slice < Slices; slice++)
                {
                    if (stack != 0)
                    {
                        mesh.TriangleIndices.Add(top + slice);
                        mesh.TriangleIndices.Add(bot + slice);
                        mesh.TriangleIndices.Add(top + slice + 1);
                    }

                    if (stack != Stacks - 1)
                    {
                        mesh.TriangleIndices.Add(top + slice + 1);
                        mesh.TriangleIndices.Add(bot + slice);
                        mesh.TriangleIndices.Add(bot + slice + 1);
                    }
                }
            }

            return mesh;
        }
    }
}

出于测试目的,如果使用SolidColorBrush替换ImageBrush,将会发生相同的效果。

有人知道发生了什么吗?另外,我是Stack Overflow的新手,所以如果我能做些什么来让我的问题更清楚,请告诉我。感谢您提前提供任何帮助!

1 个答案:

答案 0 :(得分:1)

请务必检查输出窗口是否存在绑定错误,如下所示:

  

System.Windows.Data错误:40:BindingExpression路径错误:'TriangleIndicies'属性找不到'object'''MeshGeometry3D'(HashCode = 33785274)'。 BindingExpression:路径= Geometry.TriangleIndicies; DataItem ='SphereMeshGenerator'(HashCode = 18281552); target元素是'MeshGeometry3D'(HashCode = 34085817); target属性是'TriangleIndices'(类型'Int32Collection')

TriangleIndicies 更改为 TriangleIndices 后,它运作正常:

enter image description here

相关问题