terrainActions.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/engineAPI.h"
  23. #include "platform/platform.h"
  24. #include "gui/worldEditor/terrainActions.h"
  25. #include "gui/core/guiCanvas.h"
  26. TerrainScratchPad gTerrainScratchPad;
  27. //------------------------------------------------------------------------------
  28. bool TerrainAction::isValid(GridInfo tile)
  29. {
  30. const bool slopeLimit = mTerrainEditor->mSlopeMinAngle > 0.0f || mTerrainEditor->mSlopeMaxAngle < 90.0f;
  31. const F32 minSlope = mSin(mDegToRad(90.0f - mTerrainEditor->mSlopeMinAngle));
  32. const F32 maxSlope = mSin(mDegToRad(90.0f - mTerrainEditor->mSlopeMaxAngle));
  33. const TerrainBlock* terrain = mTerrainEditor->getActiveTerrain();
  34. const F32 squareSize = terrain->getSquareSize();
  35. Point2F p;
  36. Point3F norm;
  37. if (slopeLimit)
  38. {
  39. p.x = tile.mGridPoint.gridPos.x * squareSize;
  40. p.y = tile.mGridPoint.gridPos.y * squareSize;
  41. if (!terrain->getNormal(p, &norm, true))
  42. return false;
  43. if (norm.z > minSlope ||
  44. norm.z < maxSlope)
  45. return false;
  46. }
  47. if (tile.mHeight < mTerrainEditor->mTileMinHeight || tile.mHeight > mTerrainEditor->mTileMaxHeight)
  48. return false;
  49. return true;
  50. }
  51. void SelectAction::process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type)
  52. {
  53. if(sel == mTerrainEditor->getCurrentSel())
  54. return;
  55. if(type == Process)
  56. return;
  57. if(selChanged)
  58. {
  59. if(event.modifier & SI_MULTISELECT)
  60. {
  61. for(U32 i = 0; i < sel->size(); i++)
  62. mTerrainEditor->getCurrentSel()->remove((*sel)[i]);
  63. }
  64. else
  65. {
  66. for(U32 i = 0; i < sel->size(); i++)
  67. {
  68. GridInfo gInfo;
  69. if(mTerrainEditor->getCurrentSel()->getInfo((*sel)[i].mGridPoint.gridPos, gInfo))
  70. {
  71. if(!gInfo.mPrimarySelect)
  72. gInfo.mPrimarySelect = (*sel)[i].mPrimarySelect;
  73. if(gInfo.mWeight < (*sel)[i].mWeight)
  74. gInfo.mWeight = (*sel)[i].mWeight;
  75. mTerrainEditor->getCurrentSel()->setInfo(gInfo);
  76. }
  77. else
  78. mTerrainEditor->getCurrentSel()->add((*sel)[i]);
  79. }
  80. }
  81. }
  82. }
  83. void DeselectAction::process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type)
  84. {
  85. if(sel == mTerrainEditor->getCurrentSel())
  86. return;
  87. if(type == Process)
  88. return;
  89. if(selChanged)
  90. {
  91. for(U32 i = 0; i < sel->size(); i++)
  92. mTerrainEditor->getCurrentSel()->remove((*sel)[i]);
  93. }
  94. }
  95. //------------------------------------------------------------------------------
  96. void SoftSelectAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type type)
  97. {
  98. TerrainBlock *terrBlock = mTerrainEditor->getActiveTerrain();
  99. if ( !terrBlock )
  100. return;
  101. // allow process of current selection
  102. Selection tmpSel;
  103. if(sel == mTerrainEditor->getCurrentSel())
  104. {
  105. tmpSel = *sel;
  106. sel = &tmpSel;
  107. }
  108. if(type == Begin || type == Process)
  109. mFilter.set(1, &mTerrainEditor->mSoftSelectFilter);
  110. //
  111. if(selChanged)
  112. {
  113. F32 radius = mTerrainEditor->mSoftSelectRadius;
  114. if(radius == 0.f)
  115. return;
  116. S32 squareSize = terrBlock->getSquareSize();
  117. U32 offset = U32(radius / F32(squareSize)) + 1;
  118. for(U32 i = 0; i < sel->size(); i++)
  119. {
  120. GridInfo & info = (*sel)[i];
  121. info.mPrimarySelect = true;
  122. info.mWeight = mFilter.getValue(0);
  123. if(!mTerrainEditor->getCurrentSel()->add(info))
  124. mTerrainEditor->getCurrentSel()->setInfo(info);
  125. Point2F infoPos((F32)info.mGridPoint.gridPos.x, (F32)info.mGridPoint.gridPos.y);
  126. //
  127. for(S32 x = info.mGridPoint.gridPos.x - offset; x < info.mGridPoint.gridPos.x + (offset << 1); x++)
  128. for(S32 y = info.mGridPoint.gridPos.y - offset; y < info.mGridPoint.gridPos.y + (offset << 1); y++)
  129. {
  130. //
  131. Point2F pos((F32)x, (F32)y);
  132. F32 dist = Point2F(pos - infoPos).len() * F32(squareSize);
  133. if(dist > radius)
  134. continue;
  135. F32 weight = mFilter.getValue(dist / radius);
  136. //
  137. GridInfo gInfo;
  138. GridPoint gridPoint = info.mGridPoint;
  139. gridPoint.gridPos.set(x, y);
  140. if(mTerrainEditor->getCurrentSel()->getInfo(Point2I(x, y), gInfo))
  141. {
  142. if(gInfo.mPrimarySelect)
  143. continue;
  144. if(gInfo.mWeight < weight)
  145. {
  146. gInfo.mWeight = weight;
  147. mTerrainEditor->getCurrentSel()->setInfo(gInfo);
  148. }
  149. }
  150. else
  151. {
  152. Vector<GridInfo> gInfos;
  153. mTerrainEditor->getGridInfos(gridPoint, gInfos);
  154. for (U32 z = 0; z < gInfos.size(); z++)
  155. {
  156. gInfos[z].mWeight = weight;
  157. gInfos[z].mPrimarySelect = false;
  158. mTerrainEditor->getCurrentSel()->add(gInfos[z]);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. //------------------------------------------------------------------------------
  166. void OutlineSelectAction::process(Selection * sel, const Gui3DMouseEvent & event, bool, Type type)
  167. {
  168. TORQUE_UNUSED(sel); TORQUE_UNUSED(event); TORQUE_UNUSED(type);
  169. switch(type)
  170. {
  171. case Begin:
  172. if(event.modifier & SI_SHIFT)
  173. break;
  174. mTerrainEditor->getCurrentSel()->reset();
  175. break;
  176. case End:
  177. case Update:
  178. default:
  179. return;
  180. }
  181. mLastEvent = event;
  182. }
  183. //------------------------------------------------------------------------------
  184. void PaintMaterialAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type)
  185. {
  186. S32 mat = mTerrainEditor->getPaintMaterialIndex();
  187. if ( !selChanged || mat < 0 )
  188. return;
  189. for( U32 i = 0; i < sel->size(); i++ )
  190. {
  191. GridInfo &inf = (*sel)[i];
  192. if (!isValid(inf))
  193. continue;
  194. // If grid is already set to our material, or it is an
  195. // empty grid spot, then skip painting.
  196. if ( inf.mMaterial == mat || inf.mMaterial == U8_MAX )
  197. continue;
  198. if ( mRandF() > mTerrainEditor->getBrushPressure() )
  199. continue;
  200. inf.mMaterialChanged = true;
  201. mTerrainEditor->getUndoSel()->add(inf);
  202. // Painting is really simple now... set the one mat index.
  203. inf.mMaterial = mat;
  204. mTerrainEditor->setGridInfo(inf, true);
  205. }
  206. mTerrainEditor->scheduleMaterialUpdate();
  207. }
  208. //------------------------------------------------------------------------------
  209. void ClearMaterialsAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type)
  210. {
  211. if(selChanged)
  212. {
  213. for(U32 i = 0; i < sel->size(); i++)
  214. {
  215. GridInfo &inf = (*sel)[i];
  216. mTerrainEditor->getUndoSel()->add(inf);
  217. inf.mMaterialChanged = true;
  218. // Reset to the first texture layer.
  219. inf.mMaterial = 0;
  220. mTerrainEditor->setGridInfo(inf);
  221. }
  222. mTerrainEditor->scheduleMaterialUpdate();
  223. }
  224. }
  225. //------------------------------------------------------------------------------
  226. void RaiseHeightAction::process( Selection *sel, const Gui3DMouseEvent &evt, bool selChanged, Type type )
  227. {
  228. // ok the raise height action is our "dirt pour" action
  229. // only works on brushes...
  230. Brush *brush = dynamic_cast<Brush*>(sel);
  231. if ( !brush )
  232. return;
  233. if ( type == End )
  234. return;
  235. Point2I brushPos = brush->getPosition();
  236. GridPoint brushGridPoint = brush->getGridPoint();
  237. Vector<GridInfo> cur; // the height at the brush position
  238. mTerrainEditor->getGridInfos(brushGridPoint, cur);
  239. if ( cur.size() == 0 )
  240. return;
  241. // we get 30 process actions per second (at least)
  242. F32 heightAdjust = mTerrainEditor->mAdjustHeightVal / 30;
  243. // nothing can get higher than the current brush pos adjusted height
  244. F32 maxHeight = cur[0].mHeight + heightAdjust;
  245. for ( U32 i = 0; i < sel->size(); i++ )
  246. {
  247. if (!isValid((*sel)[i]))
  248. continue;
  249. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  250. if ( (*sel)[i].mHeight < maxHeight )
  251. {
  252. (*sel)[i].mHeight += heightAdjust * (*sel)[i].mWeight;
  253. if ( (*sel)[i].mHeight > maxHeight )
  254. (*sel)[i].mHeight = maxHeight;
  255. }
  256. mTerrainEditor->setGridInfo((*sel)[i]);
  257. }
  258. mTerrainEditor->scheduleGridUpdate();
  259. }
  260. //------------------------------------------------------------------------------
  261. void LowerHeightAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type type)
  262. {
  263. // ok the lower height action is our "dirt dig" action
  264. // only works on brushes...
  265. Brush *brush = dynamic_cast<Brush *>(sel);
  266. if(!brush)
  267. return;
  268. if ( type == End )
  269. return;
  270. Point2I brushPos = brush->getPosition();
  271. GridPoint brushGridPoint = brush->getGridPoint();
  272. Vector<GridInfo> cur; // the height at the brush position
  273. mTerrainEditor->getGridInfos(brushGridPoint, cur);
  274. if (cur.size() == 0)
  275. return;
  276. // we get 30 process actions per second (at least)
  277. F32 heightAdjust = -mTerrainEditor->mAdjustHeightVal / 30;
  278. // nothing can get higher than the current brush pos adjusted height
  279. F32 maxHeight = cur[0].mHeight + heightAdjust;
  280. if(maxHeight < 0)
  281. maxHeight = 0;
  282. for(U32 i = 0; i < sel->size(); i++)
  283. {
  284. if (!isValid((*sel)[i]))
  285. continue;
  286. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  287. if((*sel)[i].mHeight > maxHeight)
  288. {
  289. (*sel)[i].mHeight += heightAdjust * (*sel)[i].mWeight;
  290. if((*sel)[i].mHeight < maxHeight)
  291. (*sel)[i].mHeight = maxHeight;
  292. }
  293. mTerrainEditor->setGridInfo((*sel)[i]);
  294. }
  295. mTerrainEditor->scheduleGridUpdate();
  296. }
  297. //------------------------------------------------------------------------------
  298. void SetHeightAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type)
  299. {
  300. if(selChanged)
  301. {
  302. for(U32 i = 0; i < sel->size(); i++)
  303. {
  304. if (!isValid((*sel)[i]))
  305. continue;
  306. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  307. (*sel)[i].mHeight = mTerrainEditor->mSetHeightVal;
  308. mTerrainEditor->setGridInfo((*sel)[i]);
  309. }
  310. mTerrainEditor->scheduleGridUpdate();
  311. }
  312. }
  313. //------------------------------------------------------------------------------
  314. void SetEmptyAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type)
  315. {
  316. if ( !selChanged )
  317. return;
  318. mTerrainEditor->setMissionDirty();
  319. for ( U32 i = 0; i < sel->size(); i++ )
  320. {
  321. GridInfo &inf = (*sel)[i];
  322. // Skip already empty blocks.
  323. if ( inf.mMaterial == U8_MAX )
  324. continue;
  325. if (!isValid(inf))
  326. continue;
  327. // The change flag needs to be set on the undo
  328. // so that it knows to restore materials.
  329. inf.mMaterialChanged = true;
  330. mTerrainEditor->getUndoSel()->add( inf );
  331. // Set the material to empty.
  332. inf.mMaterial = -1;
  333. mTerrainEditor->setGridInfo( inf );
  334. }
  335. mTerrainEditor->scheduleGridUpdate();
  336. }
  337. //------------------------------------------------------------------------------
  338. void ClearEmptyAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type)
  339. {
  340. if ( !selChanged )
  341. return;
  342. mTerrainEditor->setMissionDirty();
  343. for ( U32 i = 0; i < sel->size(); i++ )
  344. {
  345. GridInfo &inf = (*sel)[i];
  346. // Skip if not empty.
  347. if ( inf.mMaterial != U8_MAX )
  348. continue;
  349. // The change flag needs to be set on the undo
  350. // so that it knows to restore materials.
  351. inf.mMaterialChanged = true;
  352. mTerrainEditor->getUndoSel()->add( inf );
  353. // Set the material
  354. inf.mMaterial = 0;
  355. mTerrainEditor->setGridInfo( inf );
  356. }
  357. mTerrainEditor->scheduleGridUpdate();
  358. }
  359. //------------------------------------------------------------------------------
  360. void ScaleHeightAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type)
  361. {
  362. if(selChanged)
  363. {
  364. for(U32 i = 0; i < sel->size(); i++)
  365. {
  366. if (!isValid((*sel)[i]))
  367. continue;
  368. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  369. (*sel)[i].mHeight *= mTerrainEditor->mScaleVal;
  370. mTerrainEditor->setGridInfo((*sel)[i]);
  371. }
  372. mTerrainEditor->scheduleGridUpdate();
  373. }
  374. }
  375. void BrushAdjustHeightAction::process(Selection * sel, const Gui3DMouseEvent & event, bool, Type type)
  376. {
  377. if(type == Process)
  378. return;
  379. TerrainBlock *terrBlock = mTerrainEditor->getActiveTerrain();
  380. if ( !terrBlock )
  381. return;
  382. if(type == Begin)
  383. {
  384. mTerrainEditor->lockSelection(true);
  385. mTerrainEditor->getRoot()->mouseLock(mTerrainEditor);
  386. // the way this works is:
  387. // construct a plane that goes through the collision point
  388. // with one axis up the terrain Z, and horizontally parallel to the
  389. // plane of projection
  390. // the cross of the camera ffdv and the terrain up vector produces
  391. // the cross plane vector.
  392. // all subsequent mouse actions are collided against the plane and the deltaZ
  393. // from the previous position is used to delta the selection up and down.
  394. Point3F cameraDir;
  395. EditTSCtrl::smCamMatrix.getColumn(1, &cameraDir);
  396. terrBlock->getTransform().getColumn(2, &mTerrainUpVector);
  397. // ok, get the cross vector for the plane:
  398. Point3F planeCross;
  399. mCross(cameraDir, mTerrainUpVector, &planeCross);
  400. planeCross.normalize();
  401. Point3F planeNormal;
  402. Point3F intersectPoint;
  403. mTerrainEditor->collide(event, intersectPoint);
  404. mCross(mTerrainUpVector, planeCross, &planeNormal);
  405. mIntersectionPlane.set(intersectPoint, planeNormal);
  406. // ok, we have the intersection point...
  407. // project the collision point onto the up vector of the terrain
  408. mPreviousZ = mDot(mTerrainUpVector, intersectPoint);
  409. // add to undo
  410. // and record the starting heights
  411. for(U32 i = 0; i < sel->size(); i++)
  412. {
  413. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  414. (*sel)[i].mStartHeight = (*sel)[i].mHeight;
  415. }
  416. }
  417. else if(type == Update)
  418. {
  419. // ok, collide the ray from the event with the intersection plane:
  420. Point3F intersectPoint;
  421. Point3F start = event.pos;
  422. Point3F end = start + event.vec * 1000;
  423. F32 t = mIntersectionPlane.intersect(start, end);
  424. m_point3F_interpolate( start, end, t, intersectPoint);
  425. F32 currentZ = mDot(mTerrainUpVector, intersectPoint);
  426. F32 diff = currentZ - mPreviousZ;
  427. for(U32 i = 0; i < sel->size(); i++)
  428. {
  429. (*sel)[i].mHeight = (*sel)[i].mStartHeight + diff * (*sel)[i].mWeight;
  430. // clamp it
  431. if((*sel)[i].mHeight < 0.f)
  432. (*sel)[i].mHeight = 0.f;
  433. if((*sel)[i].mHeight > 2047.f)
  434. (*sel)[i].mHeight = 2047.f;
  435. mTerrainEditor->setGridInfoHeight((*sel)[i]);
  436. }
  437. mTerrainEditor->scheduleGridUpdate();
  438. }
  439. else if(type == End)
  440. {
  441. mTerrainEditor->getRoot()->mouseUnlock(mTerrainEditor);
  442. }
  443. }
  444. //------------------------------------------------------------------------------
  445. AdjustHeightAction::AdjustHeightAction(TerrainEditor * editor) :
  446. BrushAdjustHeightAction(editor)
  447. {
  448. mCursor = 0;
  449. }
  450. void AdjustHeightAction::process(Selection *sel, const Gui3DMouseEvent & event, bool b, Type type)
  451. {
  452. Selection * curSel = mTerrainEditor->getCurrentSel();
  453. BrushAdjustHeightAction::process(curSel, event, b, type);
  454. }
  455. //------------------------------------------------------------------------------
  456. // flatten the primary selection then blend in the rest...
  457. void FlattenHeightAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type)
  458. {
  459. if(!sel->size())
  460. return;
  461. if(selChanged)
  462. {
  463. F32 average = 0.f;
  464. // get the average height
  465. U32 cPrimary = 0;
  466. for(U32 k = 0; k < sel->size(); k++)
  467. if((*sel)[k].mPrimarySelect)
  468. {
  469. cPrimary++;
  470. average += (*sel)[k].mHeight;
  471. }
  472. average /= cPrimary;
  473. // set it
  474. for(U32 i = 0; i < sel->size(); i++)
  475. {
  476. if (!isValid((*sel)[i]))
  477. continue;
  478. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  479. //
  480. if((*sel)[i].mPrimarySelect)
  481. (*sel)[i].mHeight = average;
  482. else
  483. {
  484. F32 h = average - (*sel)[i].mHeight;
  485. (*sel)[i].mHeight += (h * (*sel)[i].mWeight);
  486. }
  487. mTerrainEditor->setGridInfo((*sel)[i]);
  488. }
  489. mTerrainEditor->scheduleGridUpdate();
  490. }
  491. }
  492. //------------------------------------------------------------------------------
  493. void SmoothHeightAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type)
  494. {
  495. if(!sel->size())
  496. return;
  497. if(selChanged)
  498. {
  499. F32 avgHeight = 0.f;
  500. for(U32 k = 0; k < sel->size(); k++)
  501. {
  502. mTerrainEditor->getUndoSel()->add((*sel)[k]);
  503. avgHeight += (*sel)[k].mHeight;
  504. }
  505. avgHeight /= sel->size();
  506. // clamp the terrain smooth factor...
  507. if(mTerrainEditor->mSmoothFactor < 0.f)
  508. mTerrainEditor->mSmoothFactor = 0.f;
  509. if(mTerrainEditor->mSmoothFactor > 1.f)
  510. mTerrainEditor->mSmoothFactor = 1.f;
  511. // linear
  512. for(U32 i = 0; i < sel->size(); i++)
  513. {
  514. (*sel)[i].mHeight += (avgHeight - (*sel)[i].mHeight) * mTerrainEditor->mSmoothFactor * (*sel)[i].mWeight;
  515. mTerrainEditor->setGridInfo((*sel)[i]);
  516. }
  517. mTerrainEditor->scheduleGridUpdate();
  518. }
  519. }
  520. void SmoothSlopeAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type)
  521. {
  522. if(!sel->size())
  523. return;
  524. if(selChanged)
  525. {
  526. // Perform simple 2d linear regression on x&z and y&z:
  527. // b = (Avg(xz) - Avg(x)Avg(z))/(Avg(x^2) - Avg(x)^2)
  528. Point2F prod(0.f, 0.f); // mean of product for covar
  529. Point2F avgSqr(0.f, 0.f); // mean sqr of x, y for var
  530. Point2F avgPos(0.f, 0.f);
  531. F32 avgHeight = 0.f;
  532. F32 z;
  533. Point2F pos;
  534. for(U32 k = 0; k < sel->size(); k++)
  535. {
  536. mTerrainEditor->getUndoSel()->add((*sel)[k]);
  537. pos = Point2F((*sel)[k].mGridPoint.gridPos.x, (*sel)[k].mGridPoint.gridPos.y);
  538. z = (*sel)[k].mHeight;
  539. prod += pos * z;
  540. avgSqr += pos * pos;
  541. avgPos += pos;
  542. avgHeight += z;
  543. }
  544. prod /= sel->size();
  545. avgSqr /= sel->size();
  546. avgPos /= sel->size();
  547. avgHeight /= sel->size();
  548. Point2F avgSlope = (prod - avgPos*avgHeight)/(avgSqr - avgPos*avgPos);
  549. F32 goalHeight;
  550. for(U32 i = 0; i < sel->size(); i++)
  551. {
  552. goalHeight = avgHeight + ((*sel)[i].mGridPoint.gridPos.x - avgPos.x)*avgSlope.x +
  553. ((*sel)[i].mGridPoint.gridPos.y - avgPos.y)*avgSlope.y;
  554. (*sel)[i].mHeight += (goalHeight - (*sel)[i].mHeight) * (*sel)[i].mWeight;
  555. mTerrainEditor->setGridInfo((*sel)[i]);
  556. }
  557. mTerrainEditor->scheduleGridUpdate();
  558. }
  559. }
  560. void PaintNoiseAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type type)
  561. {
  562. // If this is the ending
  563. // mouse down event, then
  564. // update the noise values.
  565. if ( type == Begin )
  566. {
  567. mNoise.setSeed( Sim::getCurrentTime() );
  568. mNoise.fBm( &mNoiseData, mNoiseSize, 12, 1.0f, 5.0f );
  569. mNoise.getMinMax( &mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize );
  570. mScale = 1.5f / ( mMinMaxNoise.x - mMinMaxNoise.y);
  571. }
  572. if( selChanged )
  573. {
  574. for( U32 i = 0; i < sel->size(); i++ )
  575. {
  576. if (!isValid((*sel)[i]))
  577. continue;
  578. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  579. const Point2I &gridPos = (*sel)[i].mGridPoint.gridPos;
  580. const F32 noiseVal = mNoiseData[ ( gridPos.x % mNoiseSize ) +
  581. ( ( gridPos.y % mNoiseSize ) * mNoiseSize ) ];
  582. (*sel)[i].mHeight += (noiseVal - mMinMaxNoise.y * mScale) * (*sel)[i].mWeight * mTerrainEditor->mNoiseFactor;
  583. if ((*sel)[i].mHeight > mTerrainEditor->mTileMaxHeight)
  584. (*sel)[i].mHeight = mTerrainEditor->mTileMaxHeight;
  585. if ((*sel)[i].mHeight < mTerrainEditor->mTileMinHeight)
  586. (*sel)[i].mHeight = mTerrainEditor->mTileMinHeight;
  587. mTerrainEditor->setGridInfo((*sel)[i]);
  588. }
  589. mTerrainEditor->scheduleGridUpdate();
  590. }
  591. }
  592. void ThermalErosionAction::process(Selection * sel, const Gui3DMouseEvent &, bool selChanged, Type type)
  593. {
  594. // If this is the ending
  595. // mouse down event, then
  596. // update the noise values.
  597. TerrainBlock* tblock = mTerrainEditor->getActiveTerrain();
  598. if (!tblock)
  599. return;
  600. Vector<F32> scratch = mNoiseData;
  601. if (type == Begin)
  602. {
  603. mNoise.setSeed(Sim::getCurrentTime());
  604. mNoise.fBm(&mNoiseData, mNoiseSize, 12, 1.0f, 5.0f);
  605. scratch = mNoiseData;
  606. mNoise.rigidMultiFractal(&mNoiseData, &scratch, mNoiseSize, 12, 1.0f, 5.0f);
  607. mNoise.getMinMax(&mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize);
  608. }
  609. scratch = mNoiseData;
  610. //erodeThermal(Vector<F32> *src, Vector<F32> *dst, F32 slope, F32 materialLoss, U32 iterations, U32 size, U32 squareSize, F32 maxHeight );
  611. mNoise.erodeThermal(&scratch, &mNoiseData, 30.0f, 5.0f, 5, mNoiseSize, 1, tblock->getObjBox().len_z());
  612. mNoise.getMinMax(&mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize);
  613. mScale = 1.5f / (mMinMaxNoise.x - mMinMaxNoise.y + 0.0001);
  614. if (selChanged)
  615. {
  616. F32 heightDiff = 0;
  617. for (U32 i = 0; i < sel->size(); i++)
  618. {
  619. if (!isValid((*sel)[i]))
  620. continue;
  621. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  622. const Point2I& gridPos = (*sel)[i].mGridPoint.gridPos;
  623. const F32 noiseVal = mNoiseData[(gridPos.x % mNoiseSize) +
  624. ((gridPos.y % mNoiseSize) * mNoiseSize)] + mMinMaxNoise.y;
  625. heightDiff = (noiseVal * mTerrainEditor->mNoiseFactor - (*sel)[i].mHeight) / tblock->getObjBox().len_z();
  626. (*sel)[i].mHeight += heightDiff * (*sel)[i].mWeight;
  627. if ((*sel)[i].mHeight > mTerrainEditor->mTileMaxHeight)
  628. (*sel)[i].mHeight = mTerrainEditor->mTileMaxHeight;
  629. if ((*sel)[i].mHeight < mTerrainEditor->mTileMinHeight)
  630. (*sel)[i].mHeight = mTerrainEditor->mTileMinHeight;
  631. mTerrainEditor->setGridInfo((*sel)[i]);
  632. }
  633. mTerrainEditor->scheduleGridUpdate();
  634. }
  635. }
  636. void HydraulicErosionAction::process(Selection* sel, const Gui3DMouseEvent&, bool selChanged, Type type)
  637. {
  638. // If this is the ending
  639. // mouse down event, then
  640. // update the noise values.
  641. TerrainBlock* tblock = mTerrainEditor->getActiveTerrain();
  642. if (!tblock)
  643. return;
  644. Vector<F32> scratch = mNoiseData;
  645. if (type == Begin)
  646. {
  647. mNoise.setSeed(Sim::getCurrentTime());
  648. mNoise.fBm(&mNoiseData, mNoiseSize, 12, 1.0f, 5.0f);
  649. scratch = mNoiseData;
  650. }
  651. mNoise.erodeHydraulic(&scratch, &mNoiseData, 1, mNoiseSize);
  652. mNoise.getMinMax(&mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize);
  653. mScale = 1.5f / (mMinMaxNoise.x - mMinMaxNoise.y + 0.0001);
  654. if (selChanged)
  655. {
  656. F32 heightDiff = 0;
  657. for (U32 i = 0; i < sel->size(); i++)
  658. {
  659. if (!isValid((*sel)[i]))
  660. continue;
  661. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  662. const Point2I& gridPos = (*sel)[i].mGridPoint.gridPos;
  663. const F32 noiseVal = mNoiseData[(gridPos.x % mNoiseSize) +
  664. ((gridPos.y % mNoiseSize) * mNoiseSize)] + mMinMaxNoise.y;
  665. heightDiff = (noiseVal * mTerrainEditor->mNoiseFactor - (*sel)[i].mHeight) / tblock->getObjBox().len_z();
  666. (*sel)[i].mHeight += heightDiff * (*sel)[i].mWeight;
  667. if ((*sel)[i].mHeight > mTerrainEditor->mTileMaxHeight)
  668. (*sel)[i].mHeight = mTerrainEditor->mTileMaxHeight;
  669. if ((*sel)[i].mHeight < mTerrainEditor->mTileMinHeight)
  670. (*sel)[i].mHeight = mTerrainEditor->mTileMinHeight;
  671. mTerrainEditor->setGridInfo((*sel)[i]);
  672. }
  673. mTerrainEditor->scheduleGridUpdate();
  674. }
  675. }
  676. void copyAction::process(Selection* sel, const Gui3DMouseEvent&, bool selChanged, Type type)
  677. {
  678. gTerrainScratchPad.clear();
  679. for (U32 i=0;i<sel->size();i++)
  680. {
  681. if (isValid((*sel)[i]))
  682. gTerrainScratchPad.addTile((*sel)[i].mHeight, (*sel)[i].mMaterial);
  683. else
  684. gTerrainScratchPad.addTile(0, 0);
  685. }
  686. }
  687. void pasteAction::process(Selection* sel, const Gui3DMouseEvent&, bool selChanged, Type type)
  688. {
  689. for (U32 i = 0; i < sel->size(); i++)
  690. {
  691. if (isValid((*sel)[i]))
  692. {
  693. mTerrainEditor->getUndoSel()->add((*sel)[i]);
  694. (*sel)[i].mHeight = gTerrainScratchPad[i]->mHeight;
  695. (*sel)[i].mMaterial = gTerrainScratchPad[i]->mMaterial;
  696. mTerrainEditor->setGridInfo((*sel)[i]);
  697. }
  698. }
  699. mTerrainEditor->scheduleGridUpdate();
  700. mTerrainEditor->scheduleMaterialUpdate();
  701. }
  702. IMPLEMENT_CONOBJECT( TerrainSmoothAction );
  703. ConsoleDocClass( TerrainSmoothAction,
  704. "@brief Terrain action used for leveling varying terrain heights smoothly.\n\n"
  705. "Editor use only.\n\n"
  706. "@internal"
  707. );
  708. TerrainSmoothAction::TerrainSmoothAction()
  709. : UndoAction("Terrain Smoothing"), mFactor(1.0), mSteps(1), mTerrainId(0)
  710. {
  711. }
  712. void TerrainSmoothAction::initPersistFields()
  713. {
  714. docsURL;
  715. Parent::initPersistFields();
  716. }
  717. void TerrainSmoothAction::smooth( TerrainBlock *terrain, F32 factor, U32 steps )
  718. {
  719. AssertFatal( terrain, "TerrainSmoothAction::smooth() - Got null object!" );
  720. // Store our input parameters.
  721. mTerrainId = terrain->getId();
  722. mSteps = steps;
  723. mFactor = factor;
  724. // The redo can do the rest.
  725. redo();
  726. }
  727. DefineEngineMethod( TerrainSmoothAction, smooth, void, ( TerrainBlock *terrain, F32 factor, U32 steps ), , "( TerrainBlock obj, F32 factor, U32 steps )")
  728. {
  729. if (terrain)
  730. object->smooth( terrain, factor, mClamp( steps, 1, 13 ) );
  731. }
  732. void TerrainSmoothAction::undo()
  733. {
  734. // First find the terrain from the id.
  735. TerrainBlock *terrain;
  736. if ( !Sim::findObject( mTerrainId, terrain ) || !terrain )
  737. return;
  738. // Get the terrain file.
  739. TerrainFile *terrFile = terrain->getFile();
  740. // Copy our stored heightmap to the file.
  741. terrFile->setHeightMap( mUnsmoothedHeights, false );
  742. // Tell the terrain to update itself.
  743. terrain->updateGrid( Point2I::Zero, Point2I::Max, true );
  744. }
  745. void TerrainSmoothAction::redo()
  746. {
  747. // First find the terrain from the id.
  748. TerrainBlock *terrain;
  749. if ( !Sim::findObject( mTerrainId, terrain ) || !terrain )
  750. return;
  751. // Get the terrain file.
  752. TerrainFile *terrFile = terrain->getFile();
  753. // First copy the heightmap state.
  754. mUnsmoothedHeights = terrFile->getHeightMap();
  755. // Do the smooth.
  756. terrFile->smooth( mFactor, mSteps, false );
  757. // Tell the terrain to update itself.
  758. terrain->updateGrid( Point2I::Zero, Point2I::Max, true );
  759. }