我正在使用以下代码合并多个网格:
import java.util.ArrayList;
import java.util.Arrays;
import org.obsgolem.crystalia.gfx.Renderer;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import java.util.*;
public class MeshBatch
{
private final static VertexAttribute[] attributeConfig = new VertexAttribute[]{
new VertexAttribute(Usage.Position, 3, "a_position"),
new VertexAttribute(Usage.ColorPacked, 4, "a_color"),
new VertexAttribute(Usage.Normal, 3, "a_normal")};
private final static int VERTEX_SIZE = 3 + 1 + 3;
private Mesh m;
private List<Float> vertices = new ArrayList<Float>();
private List<Short> indices = new ArrayList<Short>();
public void addMesh(float[] vert, short[] ind)
{
int offset = (vertices.size() / VERTEX_SIZE);
//You have to throw an exception when you get over the limit of short indices
if (offset + vert.length / VERTEX_SIZE > Short.MAX_VALUE) {
throw new RuntimeException("blablabla");
}
for (short i : addOffset(ind, offset)) {
indices.add(i);
}
for (float v : vert) {
vertices.add(v);
}
}
public short[] addOffset(short[] ind, int offset)
{
short[] indarr = new short[ind.length];
for (int i = 0; i < ind.length; ++i) {
//Do you really need this check? You are the only one using this code
//so make sure that you never provide a null value. If you really want to have a chekc throw an exception instead
short value = ind[i];//ind[i] == null ? 0 : ind[i];
indarr[i] = (short) (value + offset);
}
return indarr;
}
public void end()
{
m = new Mesh(false, vertices.size(), indices.size(), attributeConfig);
m.setVertices(Renderer.makeFloatArray(vertices));
m.setIndices(Renderer.makeShortArray(indices));
}
public void render()
{
Renderer.getInstance().render(m);
}
}
然而,当我使用这个类进行绘制时,我会得到奇怪的光照效果。使用此网格,所有其他对象的光都更亮,而使用此网格渲染的对象具有看起来平坦的照明。使用正常方式(每个对象的单独网格)我得到了很好的平滑光照。下面是我截的两张截图:
使用合并网格:
没有合并网格:
是什么导致了这个问题,它如何影响另一个网格的照明?我的顶点以正确的格式发送(3 个浮点用于顶点,1 个用于颜色,3 个用于法线)。我的指数也在工作。仅当实际渲染网格时才会出现该问题。没有它,照明工作完美。我认为这个问题与法线有关,但我无法弄清楚那个问题可能是什么。
编辑:我想我已经解决了这个问题。当我将瓷砖从使用单独的网格切换到网格时,照明会自行修复。这怎么可能发生?