在盒子上应用纹理

Apply a texture on a box

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

        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

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

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