Browse Source

Build a single giant mesh for every texture-lightmap group of surfaces. Greatly improves rendering performance.

console
Nico de Poel 5 years ago
parent
commit
32897138b5
  1. 50
      Assets/Scripts/Modules/BrushModel.cs

50
Assets/Scripts/Modules/BrushModel.cs

@ -47,10 +47,7 @@ public class BrushModel
var groupGO = new GameObject($"T{key.Item1}_L{key.Item2}"); var groupGO = new GameObject($"T{key.Item1}_L{key.Item2}");
groupGO.transform.SetParent(modelGO.transform); groupGO.transform.SetParent(modelGO.transform);
for (int i = 0; i < group.Value.Count; ++i)
{
CreateSurfaceMeshes(group.Value[i], $"{i}", groupGO);
}
CreateMeshFromSurfaces(group.Value, groupGO.name, groupGO);
} }
} }
} }
@ -138,6 +135,51 @@ public class BrushModel
} }
} }
private void CreateMeshFromSurfaces(List<QSurface> surfaces, string key, GameObject parentGO)
{
tempVertices.Clear();
tempTextureUVs.Clear();
tempLightmapUVs.Clear();
tempIndices.Clear();
int vertOffset = 0;
for (int surfIdx = 0; surfIdx < surfaces.Count; ++surfIdx)
{
foreach (var polyVerts in surfaces[surfIdx].GetPolygons())
{
for (int vertIdx = 0; vertIdx < polyVerts.Length; ++vertIdx)
{
tempVertices.Add(polyVerts[vertIdx].position.ToVector3().ToUnity());
tempTextureUVs.Add(polyVerts[vertIdx].textureUV.ToVector2());
tempLightmapUVs.Add(polyVerts[vertIdx].lightmapUV.ToVector2());
}
// Reconstruct triangle fan
for (ushort index = 2; index < polyVerts.Length; ++index)
{
tempIndices.Add((ushort)vertOffset);
tempIndices.Add((ushort)(vertOffset + index - 1));
tempIndices.Add((ushort)(vertOffset + index));
}
vertOffset += polyVerts.Length;
}
}
Mesh mesh = new Mesh();
mesh.name = $"Surfaces_{key}";
mesh.SetVertices(tempVertices);
mesh.SetUVs(0, tempTextureUVs);
mesh.SetUVs(1, tempLightmapUVs);
mesh.SetIndices(tempIndices, MeshTopology.Triangles, 0);
mesh.RecalculateNormals();
mesh.UploadMeshData(true);
meshes.Add(mesh);
CreateMeshObject(mesh, parentGO);
}
private void CreateMeshObject(Mesh mesh, GameObject parentGO) private void CreateMeshObject(Mesh mesh, GameObject parentGO)
{ {
var meshGO = new GameObject(mesh.name); var meshGO = new GameObject(mesh.name);

Loading…
Cancel
Save