我正试图在盒子的每个面上应用纹理,下面是代码:
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。基本上,我得到的效果是:
纹理90x90,盒子40x40
如果我将长方体的尺寸设置为90(与纹理相同(,效果就是我想要的。
纹理90x90,盒子90x90
基本上,我会在任何尺寸的盒子上拉伸整个纹理。有什么帮助吗?
您的纹理坐标需要设置为0到1的范围,而不是0到框宽