ZDRay lump loading adjustments
This commit is contained in:
parent
ac90f1099a
commit
ee7771f71f
8 changed files with 283 additions and 247 deletions
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
#include "hw_levelmesh.h"
|
||||
#include "halffloat.h"
|
||||
|
||||
LevelMesh::LevelMesh()
|
||||
{
|
||||
|
|
@ -187,3 +188,105 @@ void LevelSubmesh::BuildTileSurfaceLists()
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LevelSubmesh::SetupTileTransforms()
|
||||
{
|
||||
for (auto& tile : LightmapTiles)
|
||||
{
|
||||
tile.SetupTileTransform(LMTextureSize);
|
||||
}
|
||||
}
|
||||
|
||||
void LevelSubmesh::PackLightmapAtlas(int lightmapStartIndex)
|
||||
{
|
||||
std::vector<LightmapTile*> sortedTiles;
|
||||
sortedTiles.reserve(LightmapTiles.Size());
|
||||
|
||||
for (auto& tile : LightmapTiles)
|
||||
{
|
||||
sortedTiles.push_back(&tile);
|
||||
}
|
||||
|
||||
std::sort(sortedTiles.begin(), sortedTiles.end(), [](LightmapTile* a, LightmapTile* b) { return a->AtlasLocation.Height != b->AtlasLocation.Height ? a->AtlasLocation.Height > b->AtlasLocation.Height : a->AtlasLocation.Width > b->AtlasLocation.Width; });
|
||||
|
||||
RectPacker packer(LMTextureSize, LMTextureSize, RectPacker::Spacing(0));
|
||||
|
||||
for (LightmapTile* tile : sortedTiles)
|
||||
{
|
||||
int sampleWidth = tile->AtlasLocation.Width;
|
||||
int sampleHeight = tile->AtlasLocation.Height;
|
||||
|
||||
auto result = packer.insert(sampleWidth, sampleHeight);
|
||||
int x = result.pos.x, y = result.pos.y;
|
||||
|
||||
tile->AtlasLocation.X = x;
|
||||
tile->AtlasLocation.Y = y;
|
||||
tile->AtlasLocation.ArrayIndex = lightmapStartIndex + (int)result.pageIndex;
|
||||
}
|
||||
|
||||
LMTextureCount = (int)packer.getNumPages();
|
||||
|
||||
// Calculate final texture coordinates
|
||||
for (int i = 0, count = GetSurfaceCount(); i < count; i++)
|
||||
{
|
||||
auto surface = GetSurface(i);
|
||||
if (surface->LightmapTileIndex >= 0)
|
||||
{
|
||||
const LightmapTile& tile = LightmapTiles[surface->LightmapTileIndex];
|
||||
for (int i = 0; i < surface->MeshLocation.NumVerts; i++)
|
||||
{
|
||||
auto& vertex = Mesh.Vertices[surface->MeshLocation.StartVertIndex + i];
|
||||
FVector2 uv = tile.ToUV(vertex.fPos(), (float)LMTextureSize);
|
||||
vertex.lu = uv.X;
|
||||
vertex.lv = uv.Y;
|
||||
vertex.lindex = (float)tile.AtlasLocation.ArrayIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if 0 // Debug atlas tile locations:
|
||||
float colors[30] =
|
||||
{
|
||||
1.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f,
|
||||
1.0f, 1.0f, 0.0f,
|
||||
0.0f, 1.0f, 1.0f,
|
||||
1.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.0f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f,
|
||||
0.5f, 0.5f, 0.0f,
|
||||
0.0f, 0.5f, 0.5f,
|
||||
0.5f, 0.0f, 0.5f
|
||||
};
|
||||
LMTextureData.Resize(LMTextureSize * LMTextureSize * LMTextureCount * 3);
|
||||
uint16_t* pixels = LMTextureData.Data();
|
||||
for (LightmapTile& tile : LightmapTiles)
|
||||
{
|
||||
tile.NeedsUpdate = false;
|
||||
|
||||
int index = tile.Binding.TypeIndex;
|
||||
float* color = colors + (index % 10) * 3;
|
||||
|
||||
int x = tile.AtlasLocation.X;
|
||||
int y = tile.AtlasLocation.Y;
|
||||
int w = tile.AtlasLocation.Width;
|
||||
int h = tile.AtlasLocation.Height;
|
||||
for (int yy = y; yy < y + h; yy++)
|
||||
{
|
||||
uint16_t* line = pixels + tile.AtlasLocation.ArrayIndex * LMTextureSize * LMTextureSize + yy * LMTextureSize * 3;
|
||||
for (int xx = x; xx < x + w; xx++)
|
||||
{
|
||||
float gray = (yy - y) / (float)h;
|
||||
line[xx * 3] = floatToHalf(color[0] * gray);
|
||||
line[xx * 3 + 1] = floatToHalf(color[1] * gray);
|
||||
line[xx * 3 + 2] = floatToHalf(color[2] * gray);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0, count = GetSurfaceCount(); i < count; i++)
|
||||
{
|
||||
auto surface = GetSurface(i);
|
||||
surface->AlwaysUpdate = false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,9 @@
|
|||
#include "hw_surfaceuniforms.h"
|
||||
#include <memory>
|
||||
|
||||
#include <dp_rect_pack.h>
|
||||
typedef dp::rect_pack::RectPacker<int> RectPacker;
|
||||
|
||||
struct LevelMeshTileStats;
|
||||
|
||||
struct LevelSubmeshDrawRange
|
||||
|
|
@ -61,9 +64,8 @@ public:
|
|||
void UpdateCollision();
|
||||
void GatherTilePixelStats(LevelMeshTileStats& stats);
|
||||
void BuildTileSurfaceLists();
|
||||
|
||||
private:
|
||||
FVector2 ToUV(const FVector3& vert, const LightmapTile* tile);
|
||||
void SetupTileTransforms();
|
||||
void PackLightmapAtlas(int lightmapStartIndex);
|
||||
};
|
||||
|
||||
class LevelMesh
|
||||
|
|
|
|||
|
|
@ -69,4 +69,96 @@ struct LightmapTile
|
|||
float v = (AtlasLocation.Y + (localPos | Transform.ProjLocalToV)) / textureSize;
|
||||
return FVector2(u, v);
|
||||
}
|
||||
|
||||
enum PlaneAxis
|
||||
{
|
||||
AXIS_YZ = 0,
|
||||
AXIS_XZ,
|
||||
AXIS_XY
|
||||
};
|
||||
|
||||
static PlaneAxis BestAxis(const FVector4& p)
|
||||
{
|
||||
float na = fabs(float(p.X));
|
||||
float nb = fabs(float(p.Y));
|
||||
float nc = fabs(float(p.Z));
|
||||
|
||||
// figure out what axis the plane lies on
|
||||
if (na >= nb && na >= nc)
|
||||
{
|
||||
return AXIS_YZ;
|
||||
}
|
||||
else if (nb >= na && nb >= nc)
|
||||
{
|
||||
return AXIS_XZ;
|
||||
}
|
||||
|
||||
return AXIS_XY;
|
||||
}
|
||||
|
||||
void SetupTileTransform(int textureSize)
|
||||
{
|
||||
BBox bounds = Bounds;
|
||||
|
||||
// round off dimensions
|
||||
FVector3 roundedSize;
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
bounds.min[i] = SampleDimension * (floor(bounds.min[i] / SampleDimension) - 1);
|
||||
bounds.max[i] = SampleDimension * (ceil(bounds.max[i] / SampleDimension) + 1);
|
||||
roundedSize[i] = (bounds.max[i] - bounds.min[i]) / SampleDimension;
|
||||
}
|
||||
|
||||
FVector3 tCoords[2] = { FVector3(0.0f, 0.0f, 0.0f), FVector3(0.0f, 0.0f, 0.0f) };
|
||||
|
||||
PlaneAxis axis = BestAxis(Plane);
|
||||
|
||||
int width;
|
||||
int height;
|
||||
switch (axis)
|
||||
{
|
||||
default:
|
||||
case AXIS_YZ:
|
||||
width = (int)roundedSize.Y;
|
||||
height = (int)roundedSize.Z;
|
||||
tCoords[0].Y = 1.0f / SampleDimension;
|
||||
tCoords[1].Z = 1.0f / SampleDimension;
|
||||
break;
|
||||
|
||||
case AXIS_XZ:
|
||||
width = (int)roundedSize.X;
|
||||
height = (int)roundedSize.Z;
|
||||
tCoords[0].X = 1.0f / SampleDimension;
|
||||
tCoords[1].Z = 1.0f / SampleDimension;
|
||||
break;
|
||||
|
||||
case AXIS_XY:
|
||||
width = (int)roundedSize.X;
|
||||
height = (int)roundedSize.Y;
|
||||
tCoords[0].X = 1.0f / SampleDimension;
|
||||
tCoords[1].Y = 1.0f / SampleDimension;
|
||||
break;
|
||||
}
|
||||
|
||||
// clamp width
|
||||
if (width > textureSize - 2)
|
||||
{
|
||||
tCoords[0] *= ((float)(textureSize - 2) / (float)width);
|
||||
width = (textureSize - 2);
|
||||
}
|
||||
|
||||
// clamp height
|
||||
if (height > textureSize - 2)
|
||||
{
|
||||
tCoords[1] *= ((float)(textureSize - 2) / (float)height);
|
||||
height = (textureSize - 2);
|
||||
}
|
||||
|
||||
Transform.TranslateWorldToLocal = bounds.min;
|
||||
Transform.ProjLocalToU = tCoords[0];
|
||||
Transform.ProjLocalToV = tCoords[1];
|
||||
|
||||
AtlasLocation.Width = width;
|
||||
AtlasLocation.Height = height;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue