Browse Source

simplify erosion calcs

AzaezelX 7 months ago
parent
commit
f9eeaf8869

+ 17 - 48
Engine/source/gui/worldEditor/terrainActions.cpp

@@ -758,22 +758,8 @@ void ThermalErosionAction::process(Selection * sel, const Gui3DMouseEvent &, boo
    if (!tblock)
    if (!tblock)
       return;
       return;
 
 
-   Vector<F32> scratch = mNoiseData;
-   if (type == Begin)
-   {
-      mNoise.setSeed(Sim::getCurrentTime());
-      mNoise.fBm(&mNoiseData, mNoiseSize, 12, 1.0f, 5.0f);
-      scratch = mNoiseData;
-      mNoise.rigidMultiFractal(&mNoiseData, &scratch, mNoiseSize, 12, 1.0f, 5.0f);
-      mNoise.getMinMax(&mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize);
-   }
-   scratch = mNoiseData;
-   //erodeThermal(Vector<F32> *src, Vector<F32> *dst, F32 slope, F32 materialLoss, U32 iterations, U32 size, U32 squareSize, F32 maxHeight );
-   mNoise.erodeThermal(&scratch, &mNoiseData, 30.0f, 5.0f, 5, mNoiseSize, 1, tblock->getObjBox().len_z());
-   mNoise.getMinMax(&mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize);
-   mScale = 1.5f / (mMinMaxNoise.x - mMinMaxNoise.y + 0.0001);
-
    F32 selRange = sel->getMaxHeight()-sel->getMinHeight();
    F32 selRange = sel->getMaxHeight()-sel->getMinHeight();
+   F32 avg = sel->getAvgHeight();
    if (selChanged)
    if (selChanged)
    {
    {
       F32 heightDiff = 0;
       F32 heightDiff = 0;
@@ -785,23 +771,11 @@ void ThermalErosionAction::process(Selection * sel, const Gui3DMouseEvent &, boo
 
 
          mTerrainEditor->getUndoSel()->add((*sel)[i]);
          mTerrainEditor->getUndoSel()->add((*sel)[i]);
 
 
-         const Point2I& gridPos = (*sel)[i].mGridPoint.gridPos;
-
-         const F32 noiseVal = mNoiseData[(gridPos.x % mNoiseSize) +
-            ((gridPos.y % mNoiseSize) * mNoiseSize)]/(mMinMaxNoise.y-mMinMaxNoise.x) * selRange + mMinMaxNoise.y;
-
-         heightDiff = (noiseVal * mTerrainEditor->mNoiseFactor - (*sel)[i].mHeight) / tblock->getObjBox().len_z() * 2.0;
-
-         const F32 squareSize = tblock->getSquareSize();
-         Point2F p;
-         Point3F norm;
-
-         p.x = (*sel)[i].mGridPoint.gridPos.x * squareSize;
-         p.y = (*sel)[i].mGridPoint.gridPos.y * squareSize;
-         tblock->getNormal(p, &norm, true);
-
-         F32 bias = 0.75f-norm.z;
-         (*sel)[i].mHeight += heightDiff * (*sel)[i].mWeight * bias;
+         F32 bias = ((*sel)[i].mHeight - avg) / selRange;
+         F32 nudge = mRandF(-mTerrainEditor->getBrushPressure(), mTerrainEditor->getBrushPressure());
+         F32 heightTarg = mRoundF((*sel)[i].mHeight - bias * nudge, mTerrainEditor->getBrushPressure() * 2.0f) ;
+         heightDiff = heightTarg - (*sel)[i].mHeight;
+         (*sel)[i].mHeight += heightDiff * (*sel)[i].mWeight;
 
 
          if ((*sel)[i].mHeight > mTerrainEditor->mTileMaxHeight)
          if ((*sel)[i].mHeight > mTerrainEditor->mTileMaxHeight)
             (*sel)[i].mHeight = mTerrainEditor->mTileMaxHeight;
             (*sel)[i].mHeight = mTerrainEditor->mTileMaxHeight;
@@ -824,22 +798,13 @@ void HydraulicErosionAction::process(Selection* sel, const Gui3DMouseEvent&, boo
    TerrainBlock* tblock = mTerrainEditor->getActiveTerrain();
    TerrainBlock* tblock = mTerrainEditor->getActiveTerrain();
    if (!tblock)
    if (!tblock)
       return;
       return;
-   Vector<F32> scratch = mNoiseData;
-   if (type == Begin)
-   {
-      mNoise.setSeed(Sim::getCurrentTime());
-      mNoise.fBm(&mNoiseData, mNoiseSize, 12, 1.0f, 5.0f);
-      scratch = mNoiseData;
-   }
-   mNoise.erodeHydraulic(&scratch, &mNoiseData, 1, mNoiseSize);
-   mNoise.getMinMax(&mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize);
-   mScale = 1.5f / (mMinMaxNoise.x - mMinMaxNoise.y + 0.0001);
 
 
    F32 selRange = sel->getMaxHeight() - sel->getMinHeight();
    F32 selRange = sel->getMaxHeight() - sel->getMinHeight();
    F32 avg = sel->getAvgHeight();
    F32 avg = sel->getAvgHeight();
    if (selChanged)
    if (selChanged)
    {
    {
       F32 heightDiff = 0;
       F32 heightDiff = 0;
+      const F32 squareSize = tblock->getSquareSize();
 
 
       for (U32 i = 0; i < sel->size(); i++)
       for (U32 i = 0; i < sel->size(); i++)
       {
       {
@@ -848,15 +813,19 @@ void HydraulicErosionAction::process(Selection* sel, const Gui3DMouseEvent&, boo
 
 
          mTerrainEditor->getUndoSel()->add((*sel)[i]);
          mTerrainEditor->getUndoSel()->add((*sel)[i]);
 
 
-         const Point2I& gridPos = (*sel)[i].mGridPoint.gridPos;
+         Point2F p;
+         Point3F norm;
+
+         p.x = (*sel)[i].mGridPoint.gridPos.x * squareSize;
+         p.y = (*sel)[i].mGridPoint.gridPos.y * squareSize;
+         tblock->getNormal(p, &norm, true);
 
 
-         const F32 noiseVal = mNoiseData[(gridPos.x % mNoiseSize) +
-            ((gridPos.y % mNoiseSize) * mNoiseSize)] / (mMinMaxNoise.y - mMinMaxNoise.x) * selRange + mMinMaxNoise.y;
+         F32 bias = mPow(norm.z,3.0f) * ((*sel)[i].mHeight - avg) / selRange;
+         F32 nudge = mRandF(-mTerrainEditor->getBrushPressure(), mTerrainEditor->getBrushPressure());
 
 
-         heightDiff = (noiseVal * mTerrainEditor->mNoiseFactor - (*sel)[i].mHeight) / tblock->getObjBox().len_z() * 2.0;
+         heightDiff = bias * (-(*sel)[i].mHeight + bias * nudge) / tblock->getObjBox().len_z() * 2.0;
 
 
-         F32 bias = ((*sel)[i].mHeight - avg)/ selRange;
-         (*sel)[i].mHeight += heightDiff * (*sel)[i].mWeight * bias;
+         (*sel)[i].mHeight += heightDiff * (*sel)[i].mWeight;
 
 
          if ((*sel)[i].mHeight > mTerrainEditor->mTileMaxHeight)
          if ((*sel)[i].mHeight > mTerrainEditor->mTileMaxHeight)
             (*sel)[i].mHeight = mTerrainEditor->mTileMaxHeight;
             (*sel)[i].mHeight = mTerrainEditor->mTileMaxHeight;

+ 3 - 28
Engine/source/gui/worldEditor/terrainActions.h

@@ -307,48 +307,23 @@ class ThermalErosionAction : public TerrainAction
 {
 {
    public:
    public:
       ThermalErosionAction(TerrainEditor * editor) 
       ThermalErosionAction(TerrainEditor * editor) 
-      : TerrainAction(editor),
-         mNoiseSize(256)
+      : TerrainAction(editor)
       {
       {
-         mNoise.setSeed( 1 );//Sim::getCurrentTime() );
-         mNoiseData.setSize(mNoiseSize * mNoiseSize);
-         mNoise.fBm(&mNoiseData, mNoiseSize, 12, 1.0f, 5.0f);
-         mNoise.getMinMax(&mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize);
-         mScale = 1.5f / (mMinMaxNoise.x - mMinMaxNoise.y);
-      }
-      
+      }      
       StringTableEntry getName(){return("thermalErode");}
       StringTableEntry getName(){return("thermalErode");}
-
       void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
       void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
-
-      const U32 mNoiseSize;
-      Noise2D mNoise;
-      Vector<F32> mNoiseData;
-      Point2F mMinMaxNoise;
-      F32 mScale;
 };
 };
 
 
 class HydraulicErosionAction : public TerrainAction
 class HydraulicErosionAction : public TerrainAction
 {
 {
 public:
 public:
    HydraulicErosionAction(TerrainEditor* editor)
    HydraulicErosionAction(TerrainEditor* editor)
-      : TerrainAction(editor),
-      mNoiseSize(256)
+      : TerrainAction(editor)
    {
    {
-      mNoise.setSeed(1);//Sim::getCurrentTime() );
-      mNoiseData.setSize(mNoiseSize * mNoiseSize);
-      mNoise.fBm(&mNoiseData, mNoiseSize, 12, 1.0f, 5.0f);
-      mNoise.getMinMax(&mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize);
-      mScale = 1.5f / (mMinMaxNoise.x - mMinMaxNoise.y);
    }
    }
 
 
    StringTableEntry getName() { return("hydraulicErode"); }
    StringTableEntry getName() { return("hydraulicErode"); }
    void process(Selection* sel, const Gui3DMouseEvent& event, bool selChanged, Type type);
    void process(Selection* sel, const Gui3DMouseEvent& event, bool selChanged, Type type);
-   const U32 mNoiseSize;
-   Noise2D mNoise;
-   Vector<F32> mNoiseData;
-   Point2F mMinMaxNoise;
-   F32 mScale;
 };
 };
 
 
 class TerrainScratchPad
 class TerrainScratchPad