EditorTerrain.as 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. // Urho3D terrain editor
  2. const uint TERRAIN_EDITMODE_RAISELOWERHEIGHT = 0, TERRAIN_EDITMODE_SETHEIGHT = 1, TERRAIN_EDITMODE_SMOOTHHEIGHT = 3,
  3. TERRAIN_EDITMODE_PAINTBRUSH = 4, TERRAIN_EDITMODE_PAINTTREES = 5, TERRAIN_EDITMODE_PAINTFOLIAGE = 6;
  4. funcdef bool TerrainEditorShowCallback();
  5. class TerrainEditorUpdateChanges {
  6. IntVector2 offset;
  7. Image@ oldImage;
  8. Image@ newImage;
  9. }
  10. class TerrainEditorBrushVisualizer
  11. {
  12. Node@ node;
  13. CustomGeometry@ customGeometry;
  14. private bool addedToOctree = false;
  15. void Create()
  16. {
  17. node = Node();
  18. customGeometry = node.CreateComponent("CustomGeometry");
  19. customGeometry.numGeometries = 1;
  20. customGeometry.material = cache.GetResource("Material", "Materials/VColUnlit.xml");
  21. customGeometry.occludee = false;
  22. customGeometry.enabled = true;
  23. }
  24. void Hide()
  25. {
  26. node.enabled = false;
  27. addedToOctree = false;
  28. }
  29. void Update(Terrain@ terrainComponent, Vector3 position, float radius)
  30. {
  31. node.enabled = true;
  32. node.position = Vector3(position.x, 0, position.z);
  33. // Generate the circle
  34. customGeometry.BeginGeometry(0, LINE_STRIP);
  35. for (uint i = 0; i < 364; i += 4)
  36. {
  37. float angle = i * M_PI / 180;
  38. float x = radius * Cos(angle / 0.0174532925);
  39. float z = radius * Sin(angle / 0.0174532925);
  40. float y = terrainComponent.GetHeight(Vector3(position.x + x, 0, position.z + z));
  41. customGeometry.DefineVertex(Vector3(x, y + 0.25, z));
  42. customGeometry.DefineColor(Color(0, 1, 0));
  43. }
  44. customGeometry.Commit();
  45. if (editorScene.octree !is null && addedToOctree == false)
  46. {
  47. editorScene.octree.AddManualDrawable(customGeometry);
  48. addedToOctree = true;
  49. }
  50. }
  51. }
  52. class TerrainEditor
  53. {
  54. private bool dirty = true;
  55. private uint editMode = 0;
  56. private Window@ window;
  57. private UIElement@ toolbar;
  58. private Text@ currentToolDescText;
  59. private Array<Image@> brushes;
  60. private CheckBox@ selectedBrush;
  61. private Image@ selectedBrushImage;
  62. private Image@ scaledSelectedBrushImage;
  63. private Slider@ brushSizeSlider;
  64. private Slider@ brushOpacitySlider;
  65. private Slider@ brushHeightSlider;
  66. private TerrainEditorBrushVisualizer brushVisualizer;
  67. private Array<Terrain@> terrainsEdited;
  68. private Color targetColor;
  69. bool targetColorSelected = false;
  70. // Create the Terrain Editor window and initialize it
  71. void Create()
  72. {
  73. if (window !is null)
  74. return;
  75. window = LoadEditorUI("UI/EditorTerrainWindow.xml");
  76. ui.root.AddChild(window);
  77. window.opacity = uiMaxOpacity;
  78. BorderImage@ currentToolDesc = window.GetChild("CurrentToolDesc", true);
  79. currentToolDesc.layoutBorder = IntRect(8, 8, 8, 8);
  80. currentToolDescText = window.GetChild("CurrentToolDescText", true);
  81. ListView@ brushesContainer = window.GetChild("BrushesContainer", true);
  82. brushesContainer.SetScrollBarsVisible(true, false);
  83. brushesContainer.contentElement.layoutMode = LM_HORIZONTAL;
  84. brushesContainer.SetFixedHeight(84);
  85. BorderImage@ settingsArea = window.GetChild("SettingsArea", true);
  86. settingsArea.layoutBorder = IntRect(8, 8, 8, 8);
  87. LineEdit@ createTerrainValue = window.GetChild("CreateTerrainValue", true);
  88. createTerrainValue.text = "1024";
  89. brushSizeSlider = window.GetChild("BrushSize", true);
  90. brushOpacitySlider = window.GetChild("BrushOpacity", true);
  91. brushHeightSlider = window.GetChild("BrushHeight", true);
  92. window.height = 300;
  93. window.SetPosition(ui.root.width - 10 - window.width, attributeInspectorWindow.position.y + attributeInspectorWindow.height + 10);
  94. SubscribeToEvent(window.GetChild("RaiseLowerHeight", true), "Toggled", "OnEditModeSelected");
  95. SubscribeToEvent(window.GetChild("SetHeight", true), "Toggled", "OnEditModeSelected");
  96. SubscribeToEvent(window.GetChild("SmoothHeight", true), "Toggled", "OnEditModeSelected");
  97. //SubscribeToEvent(window.GetChild("PaintBrush", true), "Toggled", "OnEditModeSelected");
  98. //SubscribeToEvent(window.GetChild("PaintTrees", true), "Toggled", "OnEditModeSelected");
  99. //SubscribeToEvent(window.GetChild("PaintFoliage", true), "Toggled", "OnEditModeSelected");
  100. SubscribeToEvent(window.GetChild("CloseButton", true), "Released", "Hide");
  101. SubscribeToEvent(window.GetChild("CreateTerrainButton", true), "Released", "CreateTerrain");
  102. SubscribeToEvent(brushSizeSlider, "DragEnd", "UpdateScaledBrush");
  103. LoadBrushes();
  104. Show();
  105. brushVisualizer.Create();
  106. }
  107. // Hide the window
  108. void Hide()
  109. {
  110. window.visible = false;
  111. }
  112. void HideBrushVisualizer()
  113. {
  114. brushVisualizer.Hide();
  115. }
  116. void UpdateBrushVisualizer(Terrain@ terrainComponent, Vector3 position)
  117. {
  118. if (scaledSelectedBrushImage is null)
  119. {
  120. brushVisualizer.Hide();
  121. return;
  122. }
  123. brushVisualizer.Update(terrainComponent, position, scaledSelectedBrushImage.width / 2);
  124. }
  125. // Save all the terrains we have edited
  126. void Save()
  127. {
  128. for (uint i = 0; i < terrainsEdited.length; ++i)
  129. {
  130. // Make sure its not null (it may have been deleted since last save)
  131. if (terrainsEdited[i] !is null)
  132. {
  133. String fileLocation = sceneResourcePath + terrainsEdited[i].GetAttribute("Height Map").GetResourceRef().name;
  134. Array<String> chunks = fileLocation.Split('/');
  135. Array<String> parts = chunks[chunks.length - 1].Split('.');
  136. String fileType = parts[1];
  137. if (fileType == "png")
  138. {
  139. terrainsEdited[i].heightMap.SavePNG(fileLocation);
  140. }
  141. else if (fileType == "jpg")
  142. {
  143. // Save with the highest quality
  144. terrainsEdited[i].heightMap.SaveJPG(fileLocation, 100);
  145. }
  146. else if (fileType == "bmp")
  147. {
  148. terrainsEdited[i].heightMap.SaveBMP(fileLocation);
  149. }
  150. else if (fileType == "tga")
  151. {
  152. terrainsEdited[i].heightMap.SaveTGA(fileLocation);
  153. }
  154. }
  155. }
  156. // Clean up terrainsEdited array by clearing it
  157. // (this will remove any terrains that may have been deleted)
  158. terrainsEdited.Clear();
  159. }
  160. // Show the window
  161. bool Show()
  162. {
  163. window.visible = true;
  164. window.BringToFront();
  165. return true;
  166. }
  167. // Update the UI
  168. void UpdateDirty()
  169. {
  170. if (!dirty)
  171. return;
  172. CheckBox@ raiseLowerHeight = window.GetChild("RaiseLowerHeight", true);
  173. CheckBox@ setHeight = window.GetChild("SetHeight", true);
  174. CheckBox@ smoothHeight = window.GetChild("SmoothHeight", true);
  175. //CheckBox@ paintBrush = window.GetChild("PaintBrush", true);
  176. //CheckBox@ paintTrees = window.GetChild("PaintTrees", true);
  177. //CheckBox@ paintFoliage = window.GetChild("PaintFoliage", true);
  178. raiseLowerHeight.checked = (editMode == TERRAIN_EDITMODE_RAISELOWERHEIGHT) ? true : false;
  179. setHeight.checked = (editMode == TERRAIN_EDITMODE_SETHEIGHT) ? true : false;
  180. smoothHeight.checked = (editMode == TERRAIN_EDITMODE_SMOOTHHEIGHT) ? true : false;
  181. //paintBrush.checked = (editMode == TERRAIN_EDITMODE_PAINTBRUSH) ? true : false;
  182. //paintTrees.checked = (editMode == TERRAIN_EDITMODE_PAINTTREES) ? true : false;
  183. //paintFoliage.checked = (editMode == TERRAIN_EDITMODE_PAINTFOLIAGE) ? true : false;
  184. raiseLowerHeight.enabled = !raiseLowerHeight.checked;
  185. setHeight.enabled = !setHeight.checked;
  186. smoothHeight.enabled = !smoothHeight.checked;
  187. ListView@ terrainBrushes = window.GetChild("BrushesContainer", true);
  188. for (uint i = 0; i < terrainBrushes.numItems; ++i)
  189. {
  190. CheckBox@ checkbox = cast<CheckBox>(terrainBrushes.items[i]);
  191. checkbox.checked = terrainBrushes.items[i] is selectedBrush;
  192. checkbox.enabled = !checkbox.checked;
  193. }
  194. dirty = false;
  195. }
  196. // This gets called when we want to do something to a terrain
  197. void Work(Terrain@ terrainComponent, Vector3 position)
  198. {
  199. // Only work if a brush is selected
  200. if (selectedBrushImage is null || scaledSelectedBrushImage is null)
  201. return;
  202. SetSceneModified();
  203. // Add that terrain to the terrainsEdited if its not already in there
  204. if (terrainsEdited.FindByRef(terrainComponent) == -1)
  205. terrainsEdited.Push(terrainComponent);
  206. TerrainEditorUpdateChanges@ updateChanges = TerrainEditorUpdateChanges();
  207. IntVector2 pos = terrainComponent.WorldToHeightMap(position);
  208. switch (editMode)
  209. {
  210. case TERRAIN_EDITMODE_RAISELOWERHEIGHT:
  211. UpdateTerrainRaiseLower(terrainComponent.heightMap, pos, updateChanges);
  212. break;
  213. case TERRAIN_EDITMODE_SETHEIGHT:
  214. UpdateTerrainSetHeight(terrainComponent.heightMap, pos, updateChanges);
  215. break;
  216. case TERRAIN_EDITMODE_SMOOTHHEIGHT:
  217. UpdateTerrainSmooth(terrainComponent.heightMap, pos, updateChanges);
  218. break;
  219. }
  220. terrainComponent.ApplyHeightMap();
  221. UpdateBrushVisualizer(terrainComponent, position);
  222. ModifyTerrainAction action;
  223. action.Define(terrainComponent, updateChanges.offset, updateChanges.oldImage, updateChanges.newImage);
  224. SaveEditAction(action);
  225. }
  226. private uint NearestPowerOf2(uint value) {
  227. if (value < 2)
  228. return 2;
  229. for (uint i = 1; i <= 2048; i *= 2)
  230. {
  231. if (value == i)
  232. return i;
  233. if (value < i || value > i * 2)
  234. continue;
  235. return value < (i + (i / 2)) ? i : i * 2;
  236. }
  237. return 2048;
  238. }
  239. private void CreateTerrain()
  240. {
  241. String fileName = "Textures/heightmap-" + time.timeSinceEpoch + ".png";
  242. String fileLocation = sceneResourcePath + fileName;
  243. Node@ node = CreateNode(LOCAL);
  244. node.position = Vector3(0, 0, 0);
  245. LineEdit@ lineEdit = window.GetChild("CreateTerrainValue", true);
  246. uint lineEditLength = lineEdit.text.Trimmed().ToUInt();
  247. // The parse failed, so use a decent default
  248. if (lineEditLength == 0)
  249. lineEditLength = 1024;
  250. Image@ image = Image();
  251. uint length = NearestPowerOf2(lineEditLength) + 1;
  252. image.SetSize(length, length, 3);
  253. UpdateTerrainSetConstantHeight(image, 0);
  254. if (!fileSystem.DirExists(GetPath(fileLocation)))
  255. fileSystem.CreateDir(GetPath(fileLocation));
  256. image.SavePNG(fileLocation);
  257. Terrain@ terrain = node.CreateComponent("Terrain");
  258. terrain.heightMap = image;
  259. Resource@ res = cache.GetResource("Image", fileLocation);
  260. ResourceRef ref = ResourceRef();
  261. ref.type = res.type;
  262. ref.name = fileName;
  263. terrain.SetAttribute("Height Map", Variant(ref));
  264. terrain.ApplyAttributes();
  265. SelectComponent(terrain, false);
  266. }
  267. // Utility function, returns the difference of the two numbers
  268. private float Difference(float a, float b)
  269. {
  270. return (a > b) ? a - b : b - a;
  271. }
  272. // Returns a brush based on its name (its filename minus the extension)
  273. private Image@ GetBrushImage(String brushName)
  274. {
  275. for (uint i = 0; i < brushes.length; ++i)
  276. {
  277. if (brushes[i].name == brushName)
  278. {
  279. return brushes[i];
  280. }
  281. }
  282. return null;
  283. }
  284. // Creates a brush element
  285. private UIElement@ LoadBrush(String fileLocation)
  286. {
  287. Array<String> chunks = fileLocation.Split('/');
  288. Array<String> parts = chunks[chunks.length - 1].Split('.');
  289. // We use this when editing the terrain
  290. Image@ image = cache.GetResource("Image", fileLocation);
  291. image.name = parts[0];
  292. brushes.Push(image);
  293. // This is for the brush icon
  294. Texture2D@ texture = cache.GetResource("Texture2D", fileLocation);
  295. CheckBox@ brush = CheckBox(parts[0]);
  296. brush.defaultStyle = uiStyle;
  297. brush.style = "TerrainEditorCheckbox";
  298. brush.SetFixedSize(64, 64);
  299. SubscribeToEvent(brush, "Toggled", "OnBrushSelected");
  300. BorderImage@ icon = BorderImage("Icon");
  301. icon.defaultStyle = iconStyle;
  302. icon.texture = texture;
  303. icon.imageRect = IntRect(0, 0, texture.width, texture.height);
  304. icon.SetFixedSize(64, 64);
  305. brush.AddChild(icon);
  306. return brush;
  307. }
  308. // Loads all the brushes from a hard-coded folder
  309. private void LoadBrushes()
  310. {
  311. ListView@ terrainBrushes = window.GetChild("BrushesContainer", true);
  312. Array<String>@ resourceDirs = cache.resourceDirs;
  313. String brushesFileLocation;
  314. for (uint i = 0; i < resourceDirs.length; ++i)
  315. {
  316. brushesFileLocation = resourceDirs[i] + "Textures/Editor/TerrainBrushes";
  317. if (fileSystem.DirExists(brushesFileLocation))
  318. break;
  319. }
  320. Array<String> files = fileSystem.ScanDir(brushesFileLocation, "*.*", SCAN_FILES, false);
  321. for (uint i = 0; i < files.length; ++i)
  322. {
  323. terrainBrushes.AddItem(LoadBrush(brushesFileLocation + "/" + files[i]));
  324. }
  325. }
  326. private void OnEditModeSelected(StringHash eventType, VariantMap& eventData)
  327. {
  328. CheckBox@ edit = eventData["Element"].GetPtr();
  329. if (!edit.checked)
  330. return;
  331. if (edit.name == "RaiseLowerHeight")
  332. SetEditMode(TERRAIN_EDITMODE_RAISELOWERHEIGHT, "Raise or lower terrain");
  333. else if (edit.name == "SetHeight")
  334. SetEditMode(TERRAIN_EDITMODE_SETHEIGHT, "Set height to specified height");
  335. else if (edit.name == "SmoothHeight")
  336. SetEditMode(TERRAIN_EDITMODE_SMOOTHHEIGHT, "Smooth the terrain");
  337. else if (edit.name == "PaintBrush")
  338. SetEditMode(TERRAIN_EDITMODE_PAINTBRUSH, "Paint textures onto the terrain");
  339. else if (edit.name == "PaintTrees")
  340. SetEditMode(TERRAIN_EDITMODE_PAINTTREES, "Paint trees onto the terrain");
  341. else if (edit.name == "PaintFoliage")
  342. SetEditMode(TERRAIN_EDITMODE_PAINTFOLIAGE, "Paint foliage onto the terrain");
  343. }
  344. private void OnBrushSelected(StringHash eventType, VariantMap& eventData)
  345. {
  346. CheckBox@ checkbox = cast<CheckBox>(eventData["Element"].GetPtr());
  347. if (checkbox.checked == false)
  348. return;
  349. selectedBrush = checkbox;
  350. selectedBrushImage = GetBrushImage(selectedBrush.name);
  351. UpdateScaledBrush();
  352. dirty = true;
  353. }
  354. private void SetEditMode(uint mode, String description)
  355. {
  356. window.GetChild("BrushOpacityLabel", true).visible = (mode == TERRAIN_EDITMODE_RAISELOWERHEIGHT);
  357. window.GetChild("BrushHeightLabel", true).visible = (mode == TERRAIN_EDITMODE_SETHEIGHT);
  358. window.GetChild("BrushOpacity", true).visible = (mode == TERRAIN_EDITMODE_RAISELOWERHEIGHT);
  359. window.GetChild("BrushHeight", true).visible = (mode == TERRAIN_EDITMODE_SETHEIGHT);
  360. editMode = mode;
  361. currentToolDescText.text = description;
  362. dirty = true;
  363. // force the window to resize its height to fit its children
  364. window.height = 0;
  365. }
  366. // Utility function, returns the smaller of the two numbers
  367. private float Smaller(float a, float b)
  368. {
  369. return (a > b) ? b : a;
  370. }
  371. private void UpdateScaledBrush()
  372. {
  373. if (selectedBrushImage is null)
  374. return;
  375. float size = (brushSizeSlider.value / 25) + 0.5;
  376. scaledSelectedBrushImage = selectedBrushImage.GetSubimage(IntRect(0, 0, selectedBrushImage.width, selectedBrushImage.height));
  377. scaledSelectedBrushImage.Resize(int(selectedBrushImage.width * size), int(selectedBrushImage.height * size));
  378. }
  379. private void UpdateTerrainRaiseLower(Image@ terrainImage, IntVector2 position, TerrainEditorUpdateChanges@ updateChanges)
  380. {
  381. uint brushImageWidth = scaledSelectedBrushImage.width;
  382. uint brushImageHeight = scaledSelectedBrushImage.height;
  383. updateChanges.offset = IntVector2(position.x - (brushImageWidth / 2), position.y - (brushImageHeight / 2));
  384. if (updateChanges.offset.x < 0) updateChanges.offset.x = 0;
  385. if (updateChanges.offset.y < 0) updateChanges.offset.y = 0;
  386. IntRect boundsRect = IntRect(updateChanges.offset.x, updateChanges.offset.y, updateChanges.offset.x + brushImageWidth, updateChanges.offset.y + brushImageHeight);
  387. boundsRect = ClipIntRectToHeightmapBounds(terrainImage, boundsRect);
  388. updateChanges.oldImage = terrainImage.GetSubimage(boundsRect);
  389. // lower or raise (respectively), multiply this by the brush opacity
  390. float opacity = brushOpacitySlider.value / 25;
  391. float modifier = (input.keyDown[KEY_SHIFT] ? -opacity : opacity) * 0.05;
  392. // Iterate over the entire brush image
  393. for (int y = 0; y < brushImageHeight; ++y)
  394. {
  395. for (int x = 0; x < brushImageWidth; ++x)
  396. {
  397. // Get the current terrain height at that position (centred to the brush's size)
  398. IntVector2 pos = IntVector2(position.x + x - (brushImageWidth / 2), position.y + y - (brushImageHeight / 2));
  399. Color newColor = terrainImage.GetPixel(pos.x, pos.y);
  400. Color brushColor = scaledSelectedBrushImage.GetPixel(x, y);
  401. // Now apply the brush to the terrain (we only use the alpha)
  402. newColor.r += brushColor.a * modifier;
  403. newColor.g += brushColor.a * modifier;
  404. newColor.b += brushColor.a * modifier;
  405. terrainImage.SetPixel(pos.x, pos.y, newColor);
  406. }
  407. }
  408. // Smooth the terrain a bit
  409. TerrainEditorUpdateChanges@ smoothUpdateChanges = TerrainEditorUpdateChanges();
  410. UpdateTerrainSmooth(terrainImage, position, smoothUpdateChanges);
  411. updateChanges.newImage = smoothUpdateChanges.newImage;
  412. }
  413. private void UpdateTerrainSmooth(Image@ terrainImage, IntVector2 position, TerrainEditorUpdateChanges@ updateChanges)
  414. {
  415. uint brushImageWidth = scaledSelectedBrushImage.width;
  416. uint brushImageHeight = scaledSelectedBrushImage.height;
  417. updateChanges.offset = IntVector2(position.x - (brushImageWidth / 2), position.y - (brushImageHeight / 2));
  418. if (updateChanges.offset.x < 0) updateChanges.offset.x = 0;
  419. if (updateChanges.offset.y < 0) updateChanges.offset.y = 0;
  420. IntRect boundsRect = IntRect(updateChanges.offset.x, updateChanges.offset.y, updateChanges.offset.x + brushImageWidth, updateChanges.offset.y + brushImageHeight);
  421. boundsRect = ClipIntRectToHeightmapBounds(terrainImage, boundsRect);
  422. updateChanges.oldImage = terrainImage.GetSubimage(boundsRect);
  423. // Iterate over the entire brush image
  424. for (int y = 0; y < brushImageHeight; ++y)
  425. {
  426. for (int x = 0; x < brushImageWidth; ++x)
  427. {
  428. Color brushColor = scaledSelectedBrushImage.GetPixel(x, y);
  429. // Only take opaque pixels into consideration for now
  430. if (brushColor.a == 0)
  431. continue;
  432. // Get the current terrain height at that position (centred to the brush's size)
  433. IntVector2 pos = IntVector2(position.x + x - (brushImageWidth / 2), position.y + y - (brushImageHeight / 2));
  434. // Make sure the pixel we're working on is atleast one pixel inside the terrainImage
  435. // as we need an adjacent pixel on all sides for the smoothing algorithm to work
  436. if (pos.x < 0 || pos.y < 0 || pos.x > terrainImage.width - 1 || pos.y > terrainImage.height - 1)
  437. continue;
  438. Color brp = terrainImage.GetPixel(pos.x + 1, pos.y + 1); // bottomRightPixel
  439. Color rp = terrainImage.GetPixel(pos.x + 1, pos.y); // rightPixel
  440. Color trp = terrainImage.GetPixel(pos.x + 1, pos.y - 1); // topRightPixel
  441. Color blp = terrainImage.GetPixel(pos.x - 1, pos.y + 1); // bottomLeftPixel
  442. Color lp = terrainImage.GetPixel(pos.x - 1, pos.y); // leftPixel
  443. Color tlp = terrainImage.GetPixel(pos.x - 1, pos.y - 1); // topLeftPixel
  444. Color bp = terrainImage.GetPixel(pos.x, pos.y + 1); // bottomPixel
  445. Color cp = terrainImage.GetPixel(pos.x, pos.y); // centerPixel
  446. Color tp = terrainImage.GetPixel(pos.x, pos.y - 1); // topPixel
  447. Color avgColor = Color(
  448. ((brp.r + rp.r + trp.r + blp.r + lp.r + tlp.r + bp.r + cp.r + tp.r) / 9),
  449. ((brp.g + rp.g + trp.g + blp.g + lp.g + tlp.g + bp.g + cp.g + tp.g) / 9),
  450. ((brp.b + rp.b + trp.b + blp.b + lp.b + tlp.b + bp.b + cp.b + tp.b) / 9)
  451. );
  452. Color newColor = Color(
  453. (Difference(cp.r, avgColor.r) * brushColor.a) + Smaller(cp.r, avgColor.r),
  454. (Difference(cp.g, avgColor.g) * brushColor.a) + Smaller(cp.g, avgColor.g),
  455. (Difference(cp.b, avgColor.b) * brushColor.a) + Smaller(cp.b, avgColor.b)
  456. );
  457. terrainImage.SetPixel(position.x + x - (brushImageWidth / 2), position.y + y - (brushImageHeight / 2), avgColor);
  458. }
  459. }
  460. updateChanges.newImage = terrainImage.GetSubimage(boundsRect);
  461. }
  462. private void UpdateTerrainSetHeight(Image@ terrainImage, IntVector2 position, TerrainEditorUpdateChanges@ updateChanges)
  463. {
  464. uint brushImageWidth = scaledSelectedBrushImage.width;
  465. uint brushImageHeight = scaledSelectedBrushImage.height;
  466. updateChanges.offset = IntVector2(position.x - (brushImageWidth / 2), position.y - (brushImageHeight / 2));
  467. if (updateChanges.offset.x < 0) updateChanges.offset.x = 0;
  468. if (updateChanges.offset.y < 0) updateChanges.offset.y = 0;
  469. IntRect boundsRect = IntRect(updateChanges.offset.x, updateChanges.offset.y, updateChanges.offset.x + brushImageWidth, updateChanges.offset.y + brushImageHeight);
  470. boundsRect = ClipIntRectToHeightmapBounds(terrainImage, boundsRect);
  471. updateChanges.oldImage = terrainImage.GetSubimage(boundsRect);
  472. float targetHeight = brushHeightSlider.value / 25;
  473. // Iterate over the entire brush image
  474. for (int y = 0; y < brushImageHeight; ++y)
  475. {
  476. for (int x = 0; x < brushImageWidth; ++x)
  477. {
  478. // Get the current terrain height at that position (centred to the brush's size)
  479. IntVector2 pos = IntVector2(position.x + x - (brushImageWidth / 2), position.y + y - (brushImageHeight / 2));
  480. Color newColor = terrainImage.GetPixel(pos.x, pos.y);
  481. Color brushColor = scaledSelectedBrushImage.GetPixel(x, y);
  482. // Ease the height to the target height (using the brush alpha as the 'speed'), making sure the alpha isn't 0
  483. newColor.r += (targetHeight - newColor.r) * brushColor.a;
  484. newColor.g += (targetHeight - newColor.g) * brushColor.a;
  485. newColor.b += (targetHeight - newColor.b) * brushColor.a;
  486. // Set it to target if its close enough
  487. if (Difference(targetHeight, newColor.r) < 0.01f) newColor.r = targetHeight;
  488. if (Difference(targetHeight, newColor.g) < 0.01f) newColor.g = targetHeight;
  489. if (Difference(targetHeight, newColor.b) < 0.01f) newColor.b = targetHeight;
  490. terrainImage.SetPixel(pos.x, pos.y, newColor);
  491. }
  492. }
  493. updateChanges.newImage = terrainImage.GetSubimage(boundsRect);
  494. }
  495. private void UpdateTerrainSetConstantHeight(Image@ terrainImage, float height)
  496. {
  497. height = Clamp(height, 0.0, 1.0);
  498. Color newColor = Color(height, height, height);
  499. // Iterate over the entire brush image
  500. for (int y = 0; y < terrainImage.height; ++y)
  501. {
  502. for (int x = 0; x < terrainImage.width; ++x)
  503. {
  504. terrainImage.SetPixel(x, y, newColor);
  505. }
  506. }
  507. }
  508. private IntRect ClipIntRectToHeightmapBounds(Image@ terrainImage, IntRect intRect) {
  509. if (intRect.left > terrainImage.width)
  510. intRect.left = terrainImage.width;
  511. if (intRect.right > terrainImage.width)
  512. intRect.right = terrainImage.width;
  513. if (intRect.top > terrainImage.height)
  514. intRect.top = terrainImage.height;
  515. if (intRect.bottom > terrainImage.height)
  516. intRect.bottom = terrainImage.height;
  517. return intRect;
  518. }
  519. }