我目前正在使用以下方法将纹理应用于由TriangleList
形成的多边形public static VertexPositionColorTexture[] TextureMapping(VertexPositionColorTexture[] vertices, float xScale, float yScale)
{
bool initialized = false;
float x, y;
float lX = 0, hX = 0, lY = 0, hY = 0;
for (int i = 0; i < vertices.Length; i++)
{
x = vertices[i].Position.X;
y = vertices[i].Position.Y;
if (!initialized)
{
hX = x;
lX = x;
hX = y;
hY = y;
initialized = true;
}
else
{
if (x > hX)
{
hX = x;
}
else if (x < lX)
{
lX = x;
}
if (y > hY)
{
hY = y;
}
else if (y < lY)
{
lY = y;
}
}
}
float width = (Math.Abs(lX) + Math.Abs(hX)) / xScale;
float height = (Math.Abs(lY) + Math.Abs(hY)) / yScale;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i].TextureCoordinate.X = vertices[i].Position.X / width;
vertices[i].TextureCoordinate.Y = vertices[i].Position.Y / height;
}
return vertices;
这对于具有Z = 0的点的多边形当前工作正常(例如:(0,0,0)(0,10,0)(10,10,0)(10,0,0) )但不适用于任何沿z旋转或不平坦的情况(例如(0,0,0)(0,0,10)(0,10,10)(0,10,0))。我得到的唯一解决方案是获得多边形所在的平面(它将始终是平坦的)并以某种方式旋转或平移上述方法中的顶点以将其展平为xy线以允许正确的高度和宽度待定。有人指出我正确的方向,或建议别的吗?
答案 0 :(得分:0)
通过重新编写并将多边形旋转到z平面来解决这个问题。