Skip to content

Commit

Permalink
Traktor: Remap heights in heightfield asset editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed Jan 23, 2024
1 parent a915d6a commit 5b47459
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions code/Heightfield/Editor/HeightfieldAssetEditor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -156,6 +156,7 @@ bool HeightfieldAssetEditor::create(ui::Widget* parent, db::Instance* instance,
toolBar->addItem(new ui::ToolBarButton(L"Clear...", ui::Command(L"HeightfieldAssetEditor.Clear")));
toolBar->addItem(new ui::ToolBarButton(L"Resize...", ui::Command(L"HeightfieldAssetEditor.Resize")));
toolBar->addItem(new ui::ToolBarButton(L"Crop...", ui::Command(L"HeightfieldAssetEditor.Crop")));
toolBar->addItem(new ui::ToolBarButton(L"Remap height...", ui::Command(L"HeightfieldAssetEditor.RemapHeight")));
toolBar->addEventHandler< ui::ToolBarButtonClickEvent >(this, &HeightfieldAssetEditor::eventToolBar);

m_imagePreview = new ui::Image();
Expand Down Expand Up @@ -375,10 +376,39 @@ bool HeightfieldAssetEditor::handleCommand(const ui::Command& command)
}

m_heightfield = cropped;

m_editSize->setText(toString(m_heightfield->getSize()));

updatePreviewImage();
return true;
}
else if (command == L"HeightfieldAssetEditor.RemapHeight")
{
const Vector4 oldWorldExtent = m_heightfield->getWorldExtent();
const Vector4 newWorldExtent = Vector4(oldWorldExtent.x(), 128.0f, oldWorldExtent.z());

const float scale = oldWorldExtent.y() / newWorldExtent.y();
const int32_t size = m_heightfield->getSize();

Ref< Heightfield > cropped = new Heightfield(
size,
newWorldExtent
);

for (int32_t iy = 0; iy < size; ++iy)
{
for (int32_t ix = 0; ix < size; ++ix)
{
const float h = m_heightfield->unitToWorld(m_heightfield->getGridHeightNearest(ix, iy));
const bool c = m_heightfield->getGridCut(ix, iy);

cropped->setGridHeight(ix, iy, cropped->worldToUnit(h));
cropped->setGridCut(ix, iy, c);
}
}

m_heightfield = cropped;

updatePreviewImage();
return true;
}
else
Expand Down
2 changes: 1 addition & 1 deletion code/Terrain/Editor/TerrainEditModifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ bool TerrainEditModifier::activate()
{
for (int32_t u = 0; u < size; ++u)
{
float height = m_heightfield->getGridHeightNearest(u, v); // * asset->m_scale;
const float height = m_heightfield->getGridHeightNearest(u, v); // * asset->m_scale;
*ptr++ = height;
}
}
Expand Down

0 comments on commit 5b47459

Please sign in to comment.