我目前正在使用以下内容将纹理应用于由 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 线以允许正确的高度和宽度待确定。有人指出我正确的方向,还是提出其他建议?