在盒子上应用纹理

问题描述 投票:0回答:1

我正在尝试在盒子的每个面上应用纹理,这是代码:

        int boxWidth = 40;

        Point3D location = new Point3D(0, 0, 0);
        var meshBuilder = new MeshBuilder();
        meshBuilder.AddBox(new Point3D(location.X, location.Y, location.Z), boxWidth, boxWidth, boxWidth);

        PointCollection pntCol = new PointCollection();
        pntCol.Add(new Point(boxWidth, boxWidth));
        pntCol.Add(new Point(0, boxWidth));
        pntCol.Add(new Point(0, 0));
        pntCol.Add(new Point(boxWidth, 0));

        pntCol.Add(new Point(boxWidth, boxWidth));
        pntCol.Add(new Point(0, boxWidth));
        pntCol.Add(new Point(0, 0));
        pntCol.Add(new Point(boxWidth, 0));

        pntCol.Add(new Point(boxWidth, boxWidth));
        pntCol.Add(new Point(0, boxWidth));
        pntCol.Add(new Point(0, 0));
        pntCol.Add(new Point(boxWidth, 0));

        pntCol.Add(new Point(boxWidth, boxWidth));
        pntCol.Add(new Point(0, boxWidth));
        pntCol.Add(new Point(0, 0));
        pntCol.Add(new Point(boxWidth, 0));

        pntCol.Add(new Point(boxWidth, boxWidth));
        pntCol.Add(new Point(0, boxWidth));
        pntCol.Add(new Point(0, 0));
        pntCol.Add(new Point(boxWidth, 0));

        pntCol.Add(new Point(boxWidth, boxWidth));
        pntCol.Add(new Point(0, boxWidth));
        pntCol.Add(new Point(0, 0));
        pntCol.Add(new Point(boxWidth, 0));
        meshBuilder.TextureCoordinates = pntCol;

        ImageBrush brush = new ImageBrush();
        brush.ImageSource = new BitmapImage(new Uri(@"mv-pallet-cubi.jpg", UriKind.Relative));
        brush.TileMode = TileMode.Tile;
        brush.ViewportUnits = BrushMappingMode.RelativeToBoundingBox;
        brush.ViewboxUnits = BrushMappingMode.Absolute;
        brush.Stretch = Stretch.None;
        brush.AlignmentX = AlignmentX.Left;
        brush.AlignmentY = AlignmentY.Top;
        brush.Viewbox = new Rect(0, 0, brush.ImageSource.Width, brush.ImageSource.Height);
        brush.Viewport = new Rect(0, 0, 1, 1);

        DiffuseMaterial mat = new DiffuseMaterial(brush);
        GeometryModel3D gModel3D = new GeometryModel3D { Geometry = meshBuilder.ToMesh(), Material = mat };
        ModelVisual3D mvt = new ModelVisual3D();
        mvt.Content = gModel3D;
        viewport.Children.Add(mvt);

我的盒子的宽度和高度是40,纹理的大小是90x90。基本上我得到的效果是:

Texture 90x90, Box 40x40

如果我将盒子的尺寸设置为90(与纹理相同),则效果就是我想要的。

Texture 90x90, Box 90x90

基本上,我会在任何尺寸的盒子上拉伸整个纹理。有帮助吗?

wpf texture-mapping helix-3d-toolkit
1个回答
0
投票

您的纹理坐标需要设置为0到1的范围,而不是0到框的宽度

© www.soinside.com 2019 - 2024. All rights reserved.