我正在使用libGDX开发3D引擎。我正在制作一个外部工具来创建地图,并且设法使墙变了纹理。现在,我正在尝试制作天花板和地板。要创建它们的形状,我使用EarClipping创建构成形状的三角形。如您所见,它几乎可以正常工作:
形状是完美的,但是没有将材料应用到天花板上-它只是一种灰色。为此,我使用了MeshBuilder(用于墙壁和三角形的矩形构成了天花板的形状)。 这是负责创建该网格的代码:
ModelBuilder modelBuilder = new ModelBuilder();
MeshPartBuilder meshBuilder;
Vector3 corner00 = new Vector3(),
corner10 = new Vector3(),
corner11 = new Vector3(),
corner01 = new Vector3(),
normal = new Vector3();
modelBuilder.begin();
Texture wallTexture = new Texture("wall.PNG");
wallTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
wallTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
Material material = new Material(TextureAttribute.createDiffuse(wallTexture));
EarClippingTriangulator triangulator = new EarClippingTriangulator();
Array<Sector> sectors = level.getSectors();
for (Sector sector : sectors) {
meshBuilder = modelBuilder.part("room1", GL20.GL_TRIANGLES,
VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal |
VertexAttributes.Usage.TextureCoordinates, material);
Array<Wall> walls = sector.getWalls();
for (Wall wall : walls) {
Vertex v1 = wall.getV1();
Vertex v2 = wall.getV2();
meshBuilder.setUVRange(0, 0, (Vector2.dst(v1.getX(), v1.getY(), v2.getX(), v2.getY())) / wallTexture.getWidth(), 1);
meshBuilder.rect(corner00.set(v1.getX() / PROP, v1.getY() / PROP, 0), corner10.set(v2.getX() / PROP, v2.getY() / PROP, 0),
corner11.set(v2.getX() / PROP, v2.getY() / PROP, 10), corner01.set(v1.getX() / PROP, v1.getY() / PROP, 10),
normal.set(wall.getNormal(), 0));
}
meshBuilder = modelBuilder.part("ceil", GL20.GL_TRIANGLES,
VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal |
VertexAttributes.Usage.TextureCoordinates, material);
Array<Vertex> sectorVertices = sector.getVertices();
float[] sectorVerticesToTri = new float[sectorVertices.size * 2];
for (int i = 0; i < sectorVertices.size; i++) {
Vertex vertex = sectorVertices.get(i);
int currentTriIndex = i * 2;
sectorVerticesToTri[currentTriIndex] = vertex.getX();
sectorVerticesToTri[currentTriIndex + 1] = vertex.getY();
}
ShortArray triangles = triangulator.computeTriangles(sectorVerticesToTri);
for (int i = 0; i < triangles.size / 3; i++) {
int currentTriangleIndex = i * 3;
Vertex v1 = sectorVertices.get(triangles.get(currentTriangleIndex));
Vertex v2 = sectorVertices.get(triangles.get(currentTriangleIndex + 1));
Vertex v3 = sectorVertices.get(triangles.get(currentTriangleIndex + 2));
//Why these triangles don't show the texture?
meshBuilder.triangle(
corner00.set(v1.getX() / PROP, v1.getY() / PROP, 10),
corner01.set(v2.getX() / PROP, v2.getY() / PROP, 10),
corner10.set(v3.getX() / PROP, v3.getY() / PROP, 10));
}
}
Model worldModel = modelBuilder.end();
instance = new ModelInstance(worldModel);
如您所见,这段代码创建了三角形:
meshBuilder.triangle(
corner00.set(v1.getX() / PROP, v1.getY() / PROP, 10),
corner01.set(v2.getX() / PROP, v2.getY() / PROP, 10),
corner10.set(v3.getX() / PROP, v3.getY() / PROP, 10));
PROP只是一个常数,以使其适合地图工具的比例。
我不知道为什么它不能与rects一起工作。 还有一件事,即使三角形确实显示了纹理,也将每个三角形单独显示一个。有没有办法将纹理包裹在由三角形创建的整个形状上?
谢谢