将ImageBrush添加到ModelVisual3D [英] Adding ImageBrush to ModelVisual3D
问题描述
大家好!
使用以下代码创建我的ModelVisual3D模型.
Hello guys!
With the following code I create my ModelVisual3D model.
private ImageBrush texture = new ImageBrush(new BitmapImage(new Uri("Images//texture.png", UriKind.RelativeOrAbsolute))) { ViewboxUnits = BrushMappingMode.Absolute };
model = new ModelVisual3D();
Point3D pA = new Point3D(pos.X + 0.86602 * size, pos.Y, pos.Z + 0.50000 * size);
Point3D pB = new Point3D(pos.X + 0.86602 * size, pos.Y, pos.Z + -0.50000 * size);
Point3D pC = new Point3D(pos.X, pos.Y, pos.Z + size);
Point3D pD = new Point3D(pos.X + -0.86602 * size, pos.Y, pos.Z + -0.50000 * size);
Point3D pE = new Point3D(pos.X + -0.86602 * size, pos.Y, pos.Z + 0.50000 * size);
Point3D pF = new Point3D(pos.X, pos.Y, pos.Z - size);
Model3DGroup grp = new Model3DGroup();
grp.Children.Add(CreateTriangleModel(new DiffuseMaterial(texture), pB, pF, pD, new Point(0, 0), new Point(0, 100), new Point(100, 100)));
grp.Children.Add(CreateTriangleModel(new DiffuseMaterial(texture), pD, pE, pA, new Point(0, 0), new Point(0, 0.5), new Point(0.5, 0.5)));
grp.Children.Add(CreateTriangleModel(new DiffuseMaterial(texture), pB, pD, pA, new Point(0, 0), new Point(100, 0), new Point(100, 100)));
grp.Children.Add(CreateTriangleModel(new DiffuseMaterial(texture), pA, pE, pC, new Point(0, 0), new Point(0, 100), new Point(100, 100)));
我使用这种方法来创建我的三角形模型:
I use this method to create my triangle models:
public static Model3DGroup CreateTriangleModel(Material material, Point3D p0, Point3D p1, Point3D p2, Point tc0, Point tc1, Point tc2)
{
MeshGeometry3D mesh = new MeshGeometry3D();
Vector3D normal = CalculateNormal(p0, p1, p2);
mesh.Positions.Add(p0);
mesh.Positions.Add(p1);
mesh.Positions.Add(p2);
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(2);
mesh.TextureCoordinates.Add(tc0);
mesh.TextureCoordinates.Add(tc1);
mesh.TextureCoordinates.Add(tc2);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
GeometryModel3D model = new GeometryModel3D(mesh, material);
Model3DGroup group = new Model3DGroup();
group.Children.Add(model);
return group;
}
很好的问题是,图像无法正确显示.无论我为TextureCoordinates使用什么点,它始终显示完整图像.而且我想在每个三角形模型的图像上选择一个特殊的三角形...这就是为什么我使用"ViewboxUnits = BrushMappingMode.Absolute"的原因.如果我将ViewboxUNits设置为BasedToBoundingBox并使用诸如(0,0.5)之类的点,则它也不起作用.它总是在我的triangleModel上显示完整图像,而不仅仅是它的一部分...
问候
well the problem is, the image is not displayed correctly. it always shows the full image, no matter what points I use for TextureCoordinates. And I want to select a special triangle on my image for every triangleModel... thats why i use "ViewboxUnits = BrushMappingMode.Absolute"... but somehow this doesnt work. And if i set ViewboxUNits to AccordingToBoundingBox and use Points like (0, 0.5) it doesnt work either. It always shows the full image on my triangleModel and not only a part of it...
regards
推荐答案
我遇到了不规则的问题,并通过以下方法解决了这个问题:
http://geekswithblogs.net/TimH/archive/2010/07/24/wpf-3d --- programmatically-adding-an-image-brush.aspx [ http://blogs.msdn.com/b/danlehen/archive/2005/11/06/489627.aspx [ ^ ]
I had asimular problem and solved it by this:
http://geekswithblogs.net/TimH/archive/2010/07/24/wpf-3d---programmatically-adding-an-image-brush.aspx[^]
Do this then:
http://blogs.msdn.com/b/danlehen/archive/2005/11/06/489627.aspx[^]
好,我设法找到了解决方法:
Ok, I managed to find a solution:
model = new ModelVisual3D();
Point3D pD = new Point3D(pos.X + 0.86602 * size, pos.Y, pos.Z + 0.50000 * size);
Point3D pE = new Point3D(pos.X + 0.86602 * size, pos.Y, pos.Z + -0.50000 * size);
Point3D pF = new Point3D(pos.X, pos.Y, pos.Z - size);
Point3D pA = new Point3D(pos.X + -0.86602 * size, pos.Y, pos.Z + -0.50000 * size);
Point3D pB = new Point3D(pos.X + -0.86602 * size, pos.Y, pos.Z + 0.50000 * size);
Point3D pC = new Point3D(pos.X, pos.Y, pos.Z + size);
MeshGeometry3D mesh = new MeshGeometry3D();
Vector3D normal = Model3DHelper.CalculateNormal(pA, pB, pC);
mesh.Positions.Add(pA);
mesh.Positions.Add(pB);
mesh.Positions.Add(pC);
mesh.Positions.Add(pD);
mesh.Positions.Add(pE);
mesh.Positions.Add(pF);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.Normals.Add(normal);
mesh.TextureCoordinates.Add(new Point(0.25, 1));
mesh.TextureCoordinates.Add(new Point(0.75, 1));
mesh.TextureCoordinates.Add(new Point(1, 0.5));
mesh.TextureCoordinates.Add(new Point(0.75, 0));
mesh.TextureCoordinates.Add(new Point(0.25, 0));
mesh.TextureCoordinates.Add(new Point(0, 0.5));
mesh.TriangleIndices.Add(0); mesh.TriangleIndices.Add(4); mesh.TriangleIndices.Add(5);
mesh.TriangleIndices.Add(0); mesh.TriangleIndices.Add(1); mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(3); mesh.TriangleIndices.Add(4); mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1); mesh.TriangleIndices.Add(2); mesh.TriangleIndices.Add(3);
这篇关于将ImageBrush添加到ModelVisual3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!