Skip to content

Commit

Permalink
👼Script ProceduralManager - exposed API to regen mesh in place.
Browse files Browse the repository at this point in the history
There are 2 new methods in AngelScript `ProceduralManagerClass`:
* `void rebuildObjectMesh(ProceduralObjectClassPtr@)` - previously private `updateObject()`
* `void deleteObjectMesh(ProceduralObjectClassPtr@)` - previously private `deleteObject()`

script 'road_editor.as' updated to use this.
  • Loading branch information
ohlidalp committed Dec 12, 2024
1 parent e684f43 commit d5aeb16
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
20 changes: 14 additions & 6 deletions resources/scripts/road_editor.as
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,19 @@ void drawMeshRebuildPanel()
ImGui::InputFloat("Common extra point elevation (meters)", global_extra_point_elevation);

ImGui::SetNextItemWidth(120.f);
ImGui::InputInt("Num smoothing splits (0=off)", obj.smoothing_num_splits);

// create temporary varible because `smoothing_num_splits` is a property {get;set;}
int objSmoothingNumSplits = obj.smoothing_num_splits;
if (ImGui::InputInt("Num smoothing splits (0=off)", objSmoothingNumSplits))
{
obj.smoothing_num_splits = objSmoothingNumSplits;
}
// create temporary varible because `collision_enabled` is a property {get;set;}
bool objCollisionEnabled = obj.collision_enabled;
if(ImGui::Checkbox("Collision enabled", objCollisionEnabled))
{
obj.collision_enabled = objCollisionEnabled;
}

// NOTE: hotkey is processed in `updateInputEvents()` to be independent of the UI
if (ImGui::Button("Rebuild road mesh (hotkey: '" + inputs.getEventCommandTrimmed(EV_ROAD_EDITOR_REBUILD_MESH) + "')"))
Expand All @@ -383,13 +395,9 @@ void rebuildMesh(ProceduralObjectClass@ obj)
{
ProceduralManagerClass@ roads = game.getTerrain().getHandle().getProceduralManager();

roads.removeObject(obj); // Clears the existing mesh
recalculatePointElevations(obj);
recalculatePointRotations(obj);
roads.addObject(obj); // Generates new mesh
// Because we removed and re-added the ProceduralObject from/to the manager,
// it got new index, so our selection is invalid. Update it.
setSelectedRoad(roads.getNumObjects() - 1);
roads.rebuildObjectMesh(obj);
}

void recalculatePointElevations(ProceduralObjectClass@ obj)
Expand Down
2 changes: 2 additions & 0 deletions source/main/scripting/bindings/ProceduralRoadAngelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,6 @@ void RoR::RegisterProceduralRoad(asIScriptEngine* engine)
result = engine->RegisterObjectMethod("ProceduralManagerClass", "void removeObject(ProceduralObjectClassPtr@)", asMETHOD(ProceduralManager, removeObject), asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("ProceduralManagerClass", "int getNumObjects()", asMETHOD(RoR::ProceduralManager, getNumObjects), asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("ProceduralManagerClass", "ProceduralObjectClassPtr @getObject(int pos)", asMETHOD(ProceduralManager, getObject), asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("ProceduralManagerClass", "void rebuildObjectMesh(ProceduralObjectClassPtr@)", asMETHOD(ProceduralManager, rebuildObjectMesh), asCALL_THISCALL); ROR_ASSERT(result >= 0);
result = engine->RegisterObjectMethod("ProceduralManagerClass", "void deleteObjectMesh(ProceduralObjectClassPtr@)", asMETHOD(ProceduralManager, deleteObjectMesh), asCALL_THISCALL); ROR_ASSERT(result >= 0);
}
10 changes: 5 additions & 5 deletions source/main/terrain/ProceduralManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ void ProceduralManager::removeAllObjects()
{
for (ProceduralObjectPtr obj : pObjects)
{
this->deleteObject(obj);
this->deleteObjectMesh(obj);
}
pObjects.clear(); // delete (unreference) all objects.
}

void ProceduralManager::deleteObject(ProceduralObjectPtr po)
void ProceduralManager::deleteObjectMesh(ProceduralObjectPtr po)
{
if (po->road)
{
Expand All @@ -112,10 +112,10 @@ void ProceduralManager::removeObject(ProceduralObjectPtr po)
}
}

void ProceduralManager::updateObject(ProceduralObjectPtr po)
void ProceduralManager::rebuildObjectMesh(ProceduralObjectPtr po)
{
if (po->road)
this->deleteObject(po);
this->deleteObjectMesh(po);

po->road = new ProceduralRoad();
po->road->setCollisionEnabled(po->collision_enabled);
Expand Down Expand Up @@ -172,7 +172,7 @@ void ProceduralManager::updateObject(ProceduralObjectPtr po)

void ProceduralManager::addObject(ProceduralObjectPtr po)
{
updateObject(po);
rebuildObjectMesh(po);
pObjects.push_back(po);
}

Expand Down
8 changes: 5 additions & 3 deletions source/main/terrain/ProceduralManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ class ProceduralManager: public RefCountingObject<ProceduralManager>

void removeAllObjects();

private:
/// Rebuilds the road mesh
void updateObject(ProceduralObjectPtr po);
void rebuildObjectMesh(ProceduralObjectPtr po);

/// Deletes the road mesh
void deleteObject(ProceduralObjectPtr po);
void deleteObjectMesh(ProceduralObjectPtr po);

private:

std::vector<ProceduralObjectPtr> pObjects;
Ogre::SceneNode* pGroupingSceneNode = nullptr;
Expand Down

0 comments on commit d5aeb16

Please sign in to comment.