UV adjustment feature

This commit is contained in:
KP 2024-07-31 00:58:55 -05:00
parent cc42b4ea87
commit 35ccb134b5
4 changed files with 856 additions and 354 deletions

File diff suppressed because it is too large Load diff

View file

@ -304,9 +304,9 @@ void Map::BuildQuad(Sector& sector, Wall& wall, Flat& flat_topr, Flat& flat_bott
// Fix up the UVs so they keep the right scale
float tw = texture_scale / wall.materials[mat_type]->textures[MAT_TEX_DIFFUSE].GetWidth();
float th = texture_scale / wall.materials[mat_type]->textures[MAT_TEX_DIFFUSE].GetHeight();
Vertex3D vtxa = Vertex3D(fva, Vec2(au * tw, av * th));
Vertex3D vtxb = Vertex3D(fvb, Vec2(bu * tw, bv * th));
Vertex3D vtxc = Vertex3D(fvc, Vec2(cu * tw, cv * th));
Vertex3D vtxa = Vertex3D(fva, Vec2(au * tw + uv_offset.x/128.0f, av * th + uv_offset.y/128.0f));
Vertex3D vtxb = Vertex3D(fvb, Vec2(bu * tw + uv_offset.x/128.0f, bv * th + uv_offset.y/128.0f));
Vertex3D vtxc = Vertex3D(fvc, Vec2(cu * tw + uv_offset.x/128.0f, cv * th + uv_offset.y/128.0f));
BatchSectorInfo info = {BatchSectorInfo::BATCH_WALL, (MaterialType)(mat_type + 2), &sector, nullptr, &wall};
m_mesh.AddBatch(&wall.materials[mat_type]->textures[MAT_TEX_DIFFUSE], {vtxa, vtxb, vtxc}, flip, std::make_any<BatchSectorInfo>(info), &wall.materials[mat_type]->textures[MAT_TEX_NORMAL]);
}

View file

@ -230,6 +230,39 @@ void Editor::UpdateModeNormal()
{
using namespace kp3d;
struct Target
{
Vec3 position;
float distance;
RenderBatch3D* b;
};
std::vector<Target> targets;
for (RenderBatch3D& b : sandbox->map.GetMeshRef().GetBatchesRef())
{
for (size_t i = 0; i < b.vertex_data.size(); i += 3)
{
Vec3 pos;
Triangle tri = { b.vertex_data[i].position, b.vertex_data[i + 1].position, b.vertex_data[i + 2].position };
auto ray = GetRayFromCamera(sandbox->camera);
bool raycast = RayIntersectsTriangle(ray[0], ray[1], &tri, pos);
if (raycast)
{
float dist = sqrtf(
pow(pos.x - sandbox->camera.position.x, 2) +
pow(pos.y - sandbox->camera.position.y, 2) +
pow(pos.z - sandbox->camera.position.z, 2)
);
targets.push_back({ pos, dist, &b });
}
}
}
if (!targets.empty())
{
std::sort(targets.begin(), targets.end(), [&](const Target& a, const Target& b) { return a.distance > b.distance; });
const Target& target = targets.back();
ray_pos = target.position;
}
// Wall/flat selection
if (sandbox->IsMouseButtonDown(kp3d::MOUSE_BUTTON_LEFT) && !editing_gizmo)
{
@ -238,36 +271,12 @@ void Editor::UpdateModeNormal()
{
editor_hovered_batch.clear();
}
struct Target
{
Vec3 position;
float distance;
RenderBatch3D* b;
};
std::vector<Target> targets;
for (RenderBatch3D& b : sandbox->map.GetMeshRef().GetBatchesRef())
{
for (size_t i = 0; i < b.vertex_data.size(); i += 3)
{
Vec3 pos;
Triangle tri = { b.vertex_data[i].position, b.vertex_data[i + 1].position, b.vertex_data[i + 2].position };
auto ray = GetRayFromCamera(sandbox->camera);
bool raycast = RayIntersectsTriangle(ray[0], ray[1], &tri, pos);
if (raycast)
{
float dist = sqrtf(
pow(pos.x - sandbox->camera.position.x, 2) +
pow(pos.y - sandbox->camera.position.y, 2) +
pow(pos.z - sandbox->camera.position.z, 2)
);
targets.push_back({ pos, dist, &b });
}
}
}
if (!targets.empty())
{
std::sort(targets.begin(), targets.end(), [&](const Target& a, const Target& b) { return a.distance > b.distance; });
const Target& target = targets.back();
//std::sort(targets.begin(), targets.end(), [&](const Target& a, const Target& b) { return a.distance > b.distance; });
//const Target& target = targets.back();
//ray_pos = target.position;
if (editor_hovered_batch.empty())
{
if (std::find(editor_hovered_batch.begin(), editor_hovered_batch.end(), target.b) == editor_hovered_batch.end())
@ -414,11 +423,42 @@ void Editor::UpdateModeNormal()
// UV adjust
if (sandbox->IsMouseButtonDown(kp3d::MOUSE_BUTTON_MIDDLE))
{
if (!uv_adjusting)
{
old_ray_pos = ray_pos;
uv_adjusting = true;
}
try
{
const auto& info = std::any_cast<kp3d::BatchSectorInfo>(kp3d::editor_hovered_batch[0]->userdata);
if (info.wall)
{
Vec3 delta = ray_pos - old_ray_pos;
Vec2 delta_proj = {
(delta.x - delta.z),
-delta.y
};
info.wall->uv_offset[info.mat_type - 2].x += delta_proj.x * 128.0f;
info.wall->uv_offset[info.mat_type - 2].y += delta_proj.y * 128.0f;
if (Distance(ray_pos, old_ray_pos) > 1.0f/128.0f)
RebuildMap();
old_ray_pos = ray_pos;
}
}
catch (std::bad_any_cast& e)
{
KP3D_LOG_ERROR("Bad any cast: {}", e.what());
kp3d::editor_hovered_batch.clear();
}
}
else
{
uv_adjusting = false;
}
if (editing_gizmo)

View file

@ -75,4 +75,8 @@ private:
WallUpdate wall_update;
bool can_wall_update = false;
kp3d::Vec3 old_ray_pos;
kp3d::Vec3 ray_pos;
bool uv_adjusting = false;
};