避免3D圆柱模型中的接缝

时间:2014-01-31 08:13:19

标签: wpf 3d textures texture-mapping uv-mapping

我有这个纹理代表管道的内部区域:

enter image description here

我创建了一个简单的圆柱模型(GeometryModel3D),将上面的纹理分解为点网格,然后将其包裹成圆柱体,然后将上面的纹理映射到它(网格上的每个正方形)两个三角形):

enter image description here

纹理的左右边缘在上面的黑色接缝线处相遇,这是不需要的。

我无法在那里使用单个顶点进行平滑连续,因为该点对应于0度和360度(单角度),而是2D纹理边缘上的两个不同点。

所以我在同一个位置使用了一对顶点,但每个顶点对应于源纹理中的不同点。

我可以做些什么来隐藏接缝线?

模型创建代码:

    private void SceneViewerWindowOnLoaded(object sender, RoutedEventArgs e)
    {
        var imageSource = new BitmapImage(new Uri("pack://application:,,,/Resources/out1.jpg"));

        var meshGeometry3D = new MeshGeometry3D();

        double circumference = imageSource.PixelWidth;
        double radius = (circumference / (2.0 * Math.PI));

        double angleSegment = (2.0 * Math.PI / SegmentCountTrans);

        double sizeSegmentLong = ((double)imageSource.PixelHeight / SegmentCountLong);
        double sizeSegmentTrans = ((double)imageSource.PixelWidth / SegmentCountTrans);

        for (int indexSegmentLong = 0; indexSegmentLong < SegmentCountLong; indexSegmentLong++)
        {
            double depth = (indexSegmentLong * sizeSegmentLong);

            for (int indexSegmentTrans = 0; indexSegmentTrans < SegmentCountTrans; indexSegmentTrans++)
            {
                meshGeometry3D.Positions.Add(GetVertexPosition(indexSegmentTrans, depth, radius, angleSegment));
                meshGeometry3D.TextureCoordinates.Add(new Point(indexSegmentTrans * sizeSegmentTrans, depth));
            }

            // add one extra vertex representing 360 degrees for complete wrap-around
            meshGeometry3D.Positions.Add(GetVertexPosition(SegmentCountTrans, depth, radius, angleSegment));
            meshGeometry3D.TextureCoordinates.Add(new Point(SegmentCountTrans * sizeSegmentTrans, depth));
        }

        meshGeometry3D.TriangleIndices.Clear();

        for (int indexSegmentLong = 0; indexSegmentLong < (SegmentCountLong - 1); indexSegmentLong++)
        {
            for (int indexSegmentTrans = 0; indexSegmentTrans < SegmentCountTrans; indexSegmentTrans++)
            {
                int indexCurrent = (indexSegmentLong * (SegmentCountTrans + 1) + indexSegmentTrans);

                meshGeometry3D.TriangleIndices.Add(indexCurrent);
                meshGeometry3D.TriangleIndices.Add(indexCurrent + 1);
                meshGeometry3D.TriangleIndices.Add(indexCurrent + SegmentCountTrans + 1);

                meshGeometry3D.TriangleIndices.Add(indexCurrent + SegmentCountTrans + 1);
                meshGeometry3D.TriangleIndices.Add(indexCurrent + 1);
                meshGeometry3D.TriangleIndices.Add(indexCurrent + SegmentCountTrans + 2);
            }
        }

        var geometryModel3D = new GeometryModel3D
                            {
                                Geometry = meshGeometry3D,
                                Material = new DiffuseMaterial
                                           {
                                               Brush = new ImageBrush(imageSource)
                                           }
                            };

        this.SceneViewer.Model = geometryModel3D;
    }

    private static Point3D GetVertexPosition(int indexSegmentAngle, double depth, double radius, double angleSegment)
    {
        double angle = (StartAngle + indexSegmentAngle * angleSegment);

        return new Point3D(
            radius * Math.Cos(angle),
            radius * Math.Sin(angle),
            -depth);
    }

包含模型的Viewport3D元素的XAML代码:

<Viewport3D x:Name="Viewport3D">
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="Camera" Position="0.0,0.0,0.0" LookDirection="0.0,0.0,-1.0" UpDirection="0.0,1.0,0.0" FieldOfView="90"/>
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <AmbientLight Color="White"/>
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D x:Name="ModelVisualModel"/>
    </Viewport3D>

2 个答案:

答案 0 :(得分:3)

创建图像画笔时,尝试将TileMode设置为TileMode.Tile,将ViewportUnits设置为BrushMappingMode.Absolute。如果仍然无效,请在此处发布后续评论,我会尝试在我的最终重现。

答案 1 :(得分:2)

我使用DrawingBrush而不是Imagebrush。这修复了接缝,我在设置缓存时也获得了fps提升。

Brush brush = new DrawingBrush(new ImageDrawing(_texture, new Rect(0,0,1,1)));
RenderOptions.SetCachingHint(brush , CachingHint.Cache);
RenderOptions.SetCacheInvalidationThresholdMinimum(brush , 0.5);
RenderOptions.SetCacheInvalidationThresholdMaximum(brush , 2.0);