Terrain.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Core/Context.h"
  23. #include "../Graphics/DrawableEvents.h"
  24. #include "../Graphics/Geometry.h"
  25. #include "../Resource/Image.h"
  26. #include "../Graphics/IndexBuffer.h"
  27. #include "../IO/Log.h"
  28. #include "../Graphics/Material.h"
  29. #include "../Scene/Node.h"
  30. #include "../Graphics/Octree.h"
  31. #include "../Core/Profiler.h"
  32. #include "../Resource/ResourceCache.h"
  33. #include "../Resource/ResourceEvents.h"
  34. #include "../Scene/Scene.h"
  35. #include "../Atomic3D/Terrain.h"
  36. #include "../Atomic3D/TerrainPatch.h"
  37. #include "../Graphics/VertexBuffer.h"
  38. #include "../DebugNew.h"
  39. namespace Atomic
  40. {
  41. extern const char* GEOMETRY_CATEGORY;
  42. static const Vector3 DEFAULT_SPACING(1.0f, 0.25f, 1.0f);
  43. static const unsigned MAX_LOD_LEVELS = 4;
  44. static const int DEFAULT_PATCH_SIZE = 32;
  45. static const int MIN_PATCH_SIZE = 4;
  46. static const int MAX_PATCH_SIZE = 128;
  47. static const unsigned STITCH_NORTH = 1;
  48. static const unsigned STITCH_SOUTH = 2;
  49. static const unsigned STITCH_WEST = 4;
  50. static const unsigned STITCH_EAST = 8;
  51. inline void GrowUpdateRegion(IntRect& updateRegion, int x, int y)
  52. {
  53. if (updateRegion.left_ < 0)
  54. {
  55. updateRegion.left_ = updateRegion.right_ = x;
  56. updateRegion.top_ = updateRegion.bottom_ = y;
  57. }
  58. else
  59. {
  60. if (x < updateRegion.left_)
  61. updateRegion.left_ = x;
  62. if (x > updateRegion.right_)
  63. updateRegion.right_ = x;
  64. if (y < updateRegion.top_)
  65. updateRegion.top_ = y;
  66. if (y > updateRegion.bottom_)
  67. updateRegion.bottom_ = y;
  68. }
  69. }
  70. Terrain::Terrain(Context* context) :
  71. Component(context),
  72. indexBuffer_(new IndexBuffer(context)),
  73. spacing_(DEFAULT_SPACING),
  74. lastSpacing_(Vector3::ZERO),
  75. patchWorldOrigin_(Vector2::ZERO),
  76. patchWorldSize_(Vector2::ZERO),
  77. numVertices_(IntVector2::ZERO),
  78. lastNumVertices_(IntVector2::ZERO),
  79. numPatches_(IntVector2::ZERO),
  80. patchSize_(DEFAULT_PATCH_SIZE),
  81. lastPatchSize_(0),
  82. numLodLevels_(1),
  83. smoothing_(false),
  84. visible_(true),
  85. castShadows_(false),
  86. occluder_(false),
  87. occludee_(true),
  88. viewMask_(DEFAULT_VIEWMASK),
  89. lightMask_(DEFAULT_LIGHTMASK),
  90. shadowMask_(DEFAULT_SHADOWMASK),
  91. zoneMask_(DEFAULT_ZONEMASK),
  92. drawDistance_(0.0f),
  93. shadowDistance_(0.0f),
  94. lodBias_(1.0f),
  95. maxLights_(0),
  96. recreateTerrain_(false)
  97. {
  98. indexBuffer_->SetShadowed(true);
  99. }
  100. Terrain::~Terrain()
  101. {
  102. }
  103. void Terrain::RegisterObject(Context* context)
  104. {
  105. context->RegisterFactory<Terrain>(GEOMETRY_CATEGORY);
  106. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  107. MIXED_ACCESSOR_ATTRIBUTE("Height Map", GetHeightMapAttr, SetHeightMapAttr, ResourceRef, ResourceRef(Image::GetTypeStatic()), AM_DEFAULT);
  108. MIXED_ACCESSOR_ATTRIBUTE("Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  109. ATTRIBUTE("Vertex Spacing", Vector3, spacing_, DEFAULT_SPACING, AM_DEFAULT);
  110. ACCESSOR_ATTRIBUTE("Patch Size", GetPatchSize, SetPatchSizeAttr, int, DEFAULT_PATCH_SIZE, AM_DEFAULT);
  111. ATTRIBUTE("Smooth Height Map", bool, smoothing_, false, AM_DEFAULT);
  112. ACCESSOR_ATTRIBUTE("Is Occluder", IsOccluder, SetOccluder, bool, false, AM_DEFAULT);
  113. ACCESSOR_ATTRIBUTE("Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  114. ACCESSOR_ATTRIBUTE("Cast Shadows", GetCastShadows, SetCastShadows, bool, false, AM_DEFAULT);
  115. ACCESSOR_ATTRIBUTE("Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  116. ACCESSOR_ATTRIBUTE("Shadow Distance", GetShadowDistance, SetShadowDistance, float, 0.0f, AM_DEFAULT);
  117. ACCESSOR_ATTRIBUTE("LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  118. ACCESSOR_ATTRIBUTE("Max Lights", GetMaxLights, SetMaxLights, unsigned, 0, AM_DEFAULT);
  119. ACCESSOR_ATTRIBUTE("View Mask", GetViewMask, SetViewMask, unsigned, DEFAULT_VIEWMASK, AM_DEFAULT);
  120. ACCESSOR_ATTRIBUTE("Light Mask", GetLightMask, SetLightMask, unsigned, DEFAULT_LIGHTMASK, AM_DEFAULT);
  121. ACCESSOR_ATTRIBUTE("Shadow Mask", GetShadowMask, SetShadowMask, unsigned, DEFAULT_SHADOWMASK, AM_DEFAULT);
  122. ACCESSOR_ATTRIBUTE("Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  123. }
  124. void Terrain::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  125. {
  126. Serializable::OnSetAttribute(attr, src);
  127. // Change of any non-accessor attribute requires recreation of the terrain
  128. if (!attr.accessor_)
  129. recreateTerrain_ = true;
  130. }
  131. void Terrain::ApplyAttributes()
  132. {
  133. if (recreateTerrain_)
  134. CreateGeometry();
  135. }
  136. void Terrain::OnSetEnabled()
  137. {
  138. bool enabled = IsEnabledEffective();
  139. for (unsigned i = 0; i < patches_.Size(); ++i)
  140. {
  141. if (patches_[i])
  142. patches_[i]->SetEnabled(enabled);
  143. }
  144. }
  145. void Terrain::SetPatchSize(int size)
  146. {
  147. if (size < MIN_PATCH_SIZE || size > MAX_PATCH_SIZE || !IsPowerOfTwo(size))
  148. return;
  149. if (size != patchSize_)
  150. {
  151. patchSize_ = size;
  152. CreateGeometry();
  153. MarkNetworkUpdate();
  154. }
  155. }
  156. void Terrain::SetSpacing(const Vector3& spacing)
  157. {
  158. if (spacing != spacing_)
  159. {
  160. spacing_ = spacing;
  161. CreateGeometry();
  162. MarkNetworkUpdate();
  163. }
  164. }
  165. void Terrain::SetSmoothing(bool enable)
  166. {
  167. if (enable != smoothing_)
  168. {
  169. smoothing_ = enable;
  170. CreateGeometry();
  171. MarkNetworkUpdate();
  172. }
  173. }
  174. bool Terrain::SetHeightMap(Image* image)
  175. {
  176. bool success = SetHeightMapInternal(image, true);
  177. MarkNetworkUpdate();
  178. return success;
  179. }
  180. void Terrain::SetMaterial(Material* material)
  181. {
  182. material_ = material;
  183. for (unsigned i = 0; i < patches_.Size(); ++i)
  184. {
  185. if (patches_[i])
  186. patches_[i]->SetMaterial(material);
  187. }
  188. MarkNetworkUpdate();
  189. }
  190. void Terrain::SetDrawDistance(float distance)
  191. {
  192. drawDistance_ = distance;
  193. for (unsigned i = 0; i < patches_.Size(); ++i)
  194. {
  195. if (patches_[i])
  196. patches_[i]->SetDrawDistance(distance);
  197. }
  198. MarkNetworkUpdate();
  199. }
  200. void Terrain::SetShadowDistance(float distance)
  201. {
  202. shadowDistance_ = distance;
  203. for (unsigned i = 0; i < patches_.Size(); ++i)
  204. {
  205. if (patches_[i])
  206. patches_[i]->SetShadowDistance(distance);
  207. }
  208. MarkNetworkUpdate();
  209. }
  210. void Terrain::SetLodBias(float bias)
  211. {
  212. lodBias_ = bias;
  213. for (unsigned i = 0; i < patches_.Size(); ++i)
  214. {
  215. if (patches_[i])
  216. patches_[i]->SetLodBias(bias);
  217. }
  218. MarkNetworkUpdate();
  219. }
  220. void Terrain::SetViewMask(unsigned mask)
  221. {
  222. viewMask_ = mask;
  223. for (unsigned i = 0; i < patches_.Size(); ++i)
  224. {
  225. if (patches_[i])
  226. patches_[i]->SetViewMask(mask);
  227. }
  228. MarkNetworkUpdate();
  229. }
  230. void Terrain::SetLightMask(unsigned mask)
  231. {
  232. lightMask_ = mask;
  233. for (unsigned i = 0; i < patches_.Size(); ++i)
  234. {
  235. if (patches_[i])
  236. patches_[i]->SetLightMask(mask);
  237. }
  238. MarkNetworkUpdate();
  239. }
  240. void Terrain::SetShadowMask(unsigned mask)
  241. {
  242. shadowMask_ = mask;
  243. for (unsigned i = 0; i < patches_.Size(); ++i)
  244. {
  245. if (patches_[i])
  246. patches_[i]->SetShadowMask(mask);
  247. }
  248. MarkNetworkUpdate();
  249. }
  250. void Terrain::SetZoneMask(unsigned mask)
  251. {
  252. zoneMask_ = mask;
  253. for (unsigned i = 0; i < patches_.Size(); ++i)
  254. {
  255. if (patches_[i])
  256. patches_[i]->SetZoneMask(mask);
  257. }
  258. MarkNetworkUpdate();
  259. }
  260. void Terrain::SetMaxLights(unsigned num)
  261. {
  262. maxLights_ = num;
  263. for (unsigned i = 0; i < patches_.Size(); ++i)
  264. {
  265. if (patches_[i])
  266. patches_[i]->SetMaxLights(num);
  267. }
  268. MarkNetworkUpdate();
  269. }
  270. void Terrain::SetCastShadows(bool enable)
  271. {
  272. castShadows_ = enable;
  273. for (unsigned i = 0; i < patches_.Size(); ++i)
  274. {
  275. if (patches_[i])
  276. patches_[i]->SetCastShadows(enable);
  277. }
  278. MarkNetworkUpdate();
  279. }
  280. void Terrain::SetOccluder(bool enable)
  281. {
  282. occluder_ = enable;
  283. for (unsigned i = 0; i < patches_.Size(); ++i)
  284. {
  285. if (patches_[i])
  286. patches_[i]->SetOccluder(enable);
  287. }
  288. MarkNetworkUpdate();
  289. }
  290. void Terrain::SetOccludee(bool enable)
  291. {
  292. occludee_ = enable;
  293. for (unsigned i = 0; i < patches_.Size(); ++i)
  294. {
  295. if (patches_[i])
  296. patches_[i]->SetOccludee(enable);
  297. }
  298. MarkNetworkUpdate();
  299. }
  300. void Terrain::ApplyHeightMap()
  301. {
  302. if (heightMap_)
  303. CreateGeometry();
  304. }
  305. Image* Terrain::GetHeightMap() const
  306. {
  307. return heightMap_;
  308. }
  309. Material* Terrain::GetMaterial() const
  310. {
  311. return material_;
  312. }
  313. TerrainPatch* Terrain::GetPatch(unsigned index) const
  314. {
  315. return index < patches_.Size() ? patches_[index] : (TerrainPatch*)0;
  316. }
  317. TerrainPatch* Terrain::GetPatch(int x, int z) const
  318. {
  319. if (x < 0 || x >= numPatches_.x_ || z < 0 || z >= numPatches_.y_)
  320. return 0;
  321. else
  322. return GetPatch(z * numPatches_.x_ + x);
  323. }
  324. float Terrain::GetHeight(const Vector3& worldPosition) const
  325. {
  326. if (node_)
  327. {
  328. Vector3 position = node_->GetWorldTransform().Inverse() * worldPosition;
  329. float xPos = (position.x_ - patchWorldOrigin_.x_) / spacing_.x_;
  330. float zPos = (position.z_ - patchWorldOrigin_.y_) / spacing_.z_;
  331. float xFrac = xPos - floorf(xPos);
  332. float zFrac = zPos - floorf(zPos);
  333. float h1, h2, h3;
  334. if (xFrac + zFrac >= 1.0f)
  335. {
  336. h1 = GetRawHeight((unsigned)xPos + 1, (unsigned)zPos + 1);
  337. h2 = GetRawHeight((unsigned)xPos, (unsigned)zPos + 1);
  338. h3 = GetRawHeight((unsigned)xPos + 1, (unsigned)zPos);
  339. xFrac = 1.0f - xFrac;
  340. zFrac = 1.0f - zFrac;
  341. }
  342. else
  343. {
  344. h1 = GetRawHeight((unsigned)xPos, (unsigned)zPos);
  345. h2 = GetRawHeight((unsigned)xPos + 1, (unsigned)zPos);
  346. h3 = GetRawHeight((unsigned)xPos, (unsigned)zPos + 1);
  347. }
  348. float h = h1 * (1.0f - xFrac - zFrac) + h2 * xFrac + h3 * zFrac;
  349. /// \todo This assumes that the terrain scene node is upright
  350. return node_->GetWorldScale().y_ * h + node_->GetWorldPosition().y_;
  351. }
  352. else
  353. return 0.0f;
  354. }
  355. Vector3 Terrain::GetNormal(const Vector3& worldPosition) const
  356. {
  357. if (node_)
  358. {
  359. Vector3 position = node_->GetWorldTransform().Inverse() * worldPosition;
  360. float xPos = (position.x_ - patchWorldOrigin_.x_) / spacing_.x_;
  361. float zPos = (position.z_ - patchWorldOrigin_.y_) / spacing_.z_;
  362. float xFrac = xPos - floorf(xPos);
  363. float zFrac = zPos - floorf(zPos);
  364. Vector3 n1, n2, n3;
  365. if (xFrac + zFrac >= 1.0f)
  366. {
  367. n1 = GetRawNormal((unsigned)xPos + 1, (unsigned)zPos + 1);
  368. n2 = GetRawNormal((unsigned)xPos, (unsigned)zPos + 1);
  369. n3 = GetRawNormal((unsigned)xPos + 1, (unsigned)zPos);
  370. xFrac = 1.0f - xFrac;
  371. zFrac = 1.0f - zFrac;
  372. }
  373. else
  374. {
  375. n1 = GetRawNormal((unsigned)xPos, (unsigned)zPos);
  376. n2 = GetRawNormal((unsigned)xPos + 1, (unsigned)zPos);
  377. n3 = GetRawNormal((unsigned)xPos, (unsigned)zPos + 1);
  378. }
  379. Vector3 n = (n1 * (1.0f - xFrac - zFrac) + n2 * xFrac + n3 * zFrac).Normalized();
  380. return node_->GetWorldRotation() * n;
  381. }
  382. else
  383. return Vector3::UP;
  384. }
  385. IntVector2 Terrain::WorldToHeightMap(const Vector3& worldPosition) const
  386. {
  387. if (!node_)
  388. return IntVector2::ZERO;
  389. Vector3 position = node_->GetWorldTransform().Inverse() * worldPosition;
  390. int xPos = (int)((position.x_ - patchWorldOrigin_.x_) / spacing_.x_);
  391. int zPos = (int)((position.z_ - patchWorldOrigin_.y_) / spacing_.z_);
  392. Clamp(xPos, 0, numVertices_.x_);
  393. Clamp(zPos, 0, numVertices_.y_);
  394. return IntVector2(xPos, numVertices_.y_ - zPos);
  395. }
  396. void Terrain::CreatePatchGeometry(TerrainPatch* patch)
  397. {
  398. PROFILE(CreatePatchGeometry);
  399. unsigned row = patchSize_ + 1;
  400. VertexBuffer* vertexBuffer = patch->GetVertexBuffer();
  401. Geometry* geometry = patch->GetGeometry();
  402. Geometry* maxLodGeometry = patch->GetMaxLodGeometry();
  403. Geometry* minLodGeometry = patch->GetMinLodGeometry();
  404. if (vertexBuffer->GetVertexCount() != row * row)
  405. vertexBuffer->SetSize(row * row, MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1 | MASK_TANGENT);
  406. SharedArrayPtr<unsigned char> cpuVertexData(new unsigned char[row * row * sizeof(Vector3)]);
  407. float* vertexData = (float*)vertexBuffer->Lock(0, vertexBuffer->GetVertexCount());
  408. float* positionData = (float*)cpuVertexData.Get();
  409. BoundingBox box;
  410. if (vertexData)
  411. {
  412. const IntVector2& coords = patch->GetCoordinates();
  413. for (int z = 0; z <= patchSize_; ++z)
  414. {
  415. for (int x = 0; x <= patchSize_; ++x)
  416. {
  417. int xPos = coords.x_ * patchSize_ + x;
  418. int zPos = coords.y_ * patchSize_ + z;
  419. // Position
  420. Vector3 position((float)x * spacing_.x_, GetRawHeight(xPos, zPos), (float)z * spacing_.z_);
  421. *vertexData++ = position.x_;
  422. *vertexData++ = position.y_;
  423. *vertexData++ = position.z_;
  424. *positionData++ = position.x_;
  425. *positionData++ = position.y_;
  426. *positionData++ = position.z_;
  427. box.Merge(position);
  428. // Normal
  429. Vector3 normal = GetRawNormal(xPos, zPos);
  430. *vertexData++ = normal.x_;
  431. *vertexData++ = normal.y_;
  432. *vertexData++ = normal.z_;
  433. // Texture coordinate
  434. Vector2 texCoord((float)xPos / (float)numVertices_.x_, 1.0f - (float)zPos / (float)numVertices_.y_);
  435. *vertexData++ = texCoord.x_;
  436. *vertexData++ = texCoord.y_;
  437. // Tangent
  438. Vector3 xyz = (Vector3::RIGHT - normal * normal.DotProduct(Vector3::RIGHT)).Normalized();
  439. *vertexData++ = xyz.x_;
  440. *vertexData++ = xyz.y_;
  441. *vertexData++ = xyz.z_;
  442. *vertexData++ = 1.0f;
  443. }
  444. }
  445. vertexBuffer->Unlock();
  446. vertexBuffer->ClearDataLost();
  447. }
  448. patch->SetBoundingBox(box);
  449. if (drawRanges_.Size())
  450. {
  451. unsigned lastDrawRange = drawRanges_.Size() - 1;
  452. geometry->SetIndexBuffer(indexBuffer_);
  453. geometry->SetDrawRange(TRIANGLE_LIST, drawRanges_[0].first_, drawRanges_[0].second_, false);
  454. geometry->SetRawVertexData(cpuVertexData, sizeof(Vector3), MASK_POSITION);
  455. maxLodGeometry->SetIndexBuffer(indexBuffer_);
  456. maxLodGeometry->SetDrawRange(TRIANGLE_LIST, drawRanges_[0].first_, drawRanges_[0].second_, false);
  457. maxLodGeometry->SetRawVertexData(cpuVertexData, sizeof(Vector3), MASK_POSITION);
  458. minLodGeometry->SetIndexBuffer(indexBuffer_);
  459. minLodGeometry->SetDrawRange(TRIANGLE_LIST, drawRanges_[lastDrawRange].first_, drawRanges_[lastDrawRange].second_, false);
  460. minLodGeometry->SetRawVertexData(cpuVertexData, sizeof(Vector3), MASK_POSITION);
  461. }
  462. // Offset the occlusion geometry by vertex spacing to reduce possibility of over-aggressive occlusion
  463. patch->SetOcclusionOffset(-0.5f * (spacing_.x_ + spacing_.z_));
  464. patch->ResetLod();
  465. }
  466. void Terrain::UpdatePatchLod(TerrainPatch* patch)
  467. {
  468. Geometry* geometry = patch->GetGeometry();
  469. // All LOD levels except the coarsest have 16 versions for stitching
  470. unsigned lodLevel = patch->GetLodLevel();
  471. unsigned drawRangeIndex = lodLevel << 4;
  472. if (lodLevel < numLodLevels_ - 1)
  473. {
  474. TerrainPatch* north = patch->GetNorthPatch();
  475. TerrainPatch* south = patch->GetSouthPatch();
  476. TerrainPatch* west = patch->GetWestPatch();
  477. TerrainPatch* east = patch->GetEastPatch();
  478. if (north && north->GetLodLevel() > lodLevel)
  479. drawRangeIndex |= STITCH_NORTH;
  480. if (south && south->GetLodLevel() > lodLevel)
  481. drawRangeIndex |= STITCH_SOUTH;
  482. if (west && west->GetLodLevel() > lodLevel)
  483. drawRangeIndex |= STITCH_WEST;
  484. if (east && east->GetLodLevel() > lodLevel)
  485. drawRangeIndex |= STITCH_EAST;
  486. }
  487. if (drawRangeIndex < drawRanges_.Size())
  488. geometry->SetDrawRange(TRIANGLE_LIST, drawRanges_[drawRangeIndex].first_, drawRanges_[drawRangeIndex].second_, false);
  489. }
  490. void Terrain::SetMaterialAttr(const ResourceRef& value)
  491. {
  492. ResourceCache* cache = GetSubsystem<ResourceCache>();
  493. SetMaterial(cache->GetResource<Material>(value.name_));
  494. }
  495. void Terrain::SetHeightMapAttr(const ResourceRef& value)
  496. {
  497. ResourceCache* cache = GetSubsystem<ResourceCache>();
  498. Image* image = cache->GetResource<Image>(value.name_);
  499. SetHeightMapInternal(image, false);
  500. }
  501. void Terrain::SetPatchSizeAttr(int value)
  502. {
  503. if (value < MIN_PATCH_SIZE || value > MAX_PATCH_SIZE || !IsPowerOfTwo(value))
  504. return;
  505. if (value != patchSize_)
  506. {
  507. patchSize_ = value;
  508. recreateTerrain_ = true;
  509. }
  510. }
  511. ResourceRef Terrain::GetMaterialAttr() const
  512. {
  513. return GetResourceRef(material_, Material::GetTypeStatic());
  514. }
  515. ResourceRef Terrain::GetHeightMapAttr() const
  516. {
  517. return GetResourceRef(heightMap_, Image::GetTypeStatic());
  518. }
  519. void Terrain::CreateGeometry()
  520. {
  521. recreateTerrain_ = false;
  522. if (!node_)
  523. return;
  524. PROFILE(CreateTerrainGeometry);
  525. unsigned prevNumPatches = patches_.Size();
  526. // Determine number of LOD levels
  527. unsigned lodSize = patchSize_;
  528. numLodLevels_ = 1;
  529. while (lodSize > MIN_PATCH_SIZE && numLodLevels_ < MAX_LOD_LEVELS)
  530. {
  531. lodSize >>= 1;
  532. ++numLodLevels_;
  533. }
  534. // Determine total terrain size
  535. patchWorldSize_ = Vector2(spacing_.x_ * (float)patchSize_, spacing_.z_ * (float)patchSize_);
  536. bool updateAll = false;
  537. if (heightMap_)
  538. {
  539. numPatches_ = IntVector2((heightMap_->GetWidth() - 1) / patchSize_, (heightMap_->GetHeight() - 1) / patchSize_);
  540. numVertices_ = IntVector2(numPatches_.x_ * patchSize_ + 1, numPatches_.y_ * patchSize_ + 1);
  541. patchWorldOrigin_ = Vector2(-0.5f * (float)numPatches_.x_ * patchWorldSize_.x_, -0.5f * (float)numPatches_.y_ *
  542. patchWorldSize_.y_);
  543. if (numVertices_ != lastNumVertices_ || lastSpacing_ != spacing_ || patchSize_ != lastPatchSize_ )
  544. updateAll = true;
  545. unsigned newDataSize = numVertices_.x_ * numVertices_.y_;
  546. // Create new height data if terrain size changed
  547. if (!heightData_ || updateAll)
  548. heightData_ = new float[newDataSize];
  549. // Ensure that the source (unsmoothed) data exists if smoothing is active
  550. if (smoothing_ && (!sourceHeightData_ || updateAll))
  551. {
  552. sourceHeightData_ = new float[newDataSize];
  553. updateAll = true;
  554. }
  555. else if (!smoothing_)
  556. sourceHeightData_.Reset();
  557. }
  558. else
  559. {
  560. numPatches_ = IntVector2::ZERO;
  561. numVertices_ = IntVector2::ZERO;
  562. patchWorldOrigin_ = Vector2::ZERO;
  563. heightData_.Reset();
  564. sourceHeightData_.Reset();
  565. }
  566. lastNumVertices_ = numVertices_;
  567. lastPatchSize_ = patchSize_;
  568. lastSpacing_ = spacing_;
  569. // Remove old patch nodes which are not needed
  570. if (updateAll)
  571. {
  572. PROFILE(RemoveOldPatches);
  573. PODVector<Node*> oldPatchNodes;
  574. node_->GetChildrenWithComponent<TerrainPatch>(oldPatchNodes);
  575. for (PODVector<Node*>::Iterator i = oldPatchNodes.Begin(); i != oldPatchNodes.End(); ++i)
  576. {
  577. bool nodeOk = false;
  578. Vector<String> coords = (*i)->GetName().Substring(6).Split('_');
  579. if (coords.Size() == 2)
  580. {
  581. int x = ToInt(coords[0]);
  582. int z = ToInt(coords[1]);
  583. if (x < numPatches_.x_ && z < numPatches_.y_)
  584. nodeOk = true;
  585. }
  586. if (!nodeOk)
  587. node_->RemoveChild(*i);
  588. }
  589. }
  590. // Keep track of which patches actually need an update
  591. PODVector<bool> dirtyPatches(numPatches_.x_ * numPatches_.y_);
  592. for (unsigned i = 0; i < dirtyPatches.Size(); ++i)
  593. dirtyPatches[i] = updateAll;
  594. patches_.Clear();
  595. if (heightMap_)
  596. {
  597. // Copy heightmap data
  598. const unsigned char* src = heightMap_->GetData();
  599. float* dest = smoothing_ ? sourceHeightData_ : heightData_;
  600. unsigned imgComps = heightMap_->GetComponents();
  601. unsigned imgRow = heightMap_->GetWidth() * imgComps;
  602. IntRect updateRegion(-1, -1, -1, -1);
  603. if (imgComps == 1)
  604. {
  605. PROFILE(CopyHeightData);
  606. for (int z = 0; z < numVertices_.y_; ++z)
  607. {
  608. for (int x = 0; x < numVertices_.x_; ++x)
  609. {
  610. float newHeight = (float)src[imgRow * (numVertices_.y_ - 1 - z) + x] * spacing_.y_;
  611. if (updateAll)
  612. *dest = newHeight;
  613. else
  614. {
  615. if (*dest != newHeight)
  616. {
  617. *dest = newHeight;
  618. GrowUpdateRegion(updateRegion, x, z);
  619. }
  620. }
  621. ++dest;
  622. }
  623. }
  624. }
  625. else
  626. {
  627. PROFILE(CopyHeightData);
  628. // If more than 1 component, use the green channel for more accuracy
  629. for (int z = 0; z < numVertices_.y_; ++z)
  630. {
  631. for (int x = 0; x < numVertices_.x_; ++x)
  632. {
  633. float newHeight = ((float)src[imgRow * (numVertices_.y_ - 1 - z) + imgComps * x] + (float)src[imgRow *
  634. (numVertices_.y_ - 1 - z) + imgComps * x + 1] / 256.0f) * spacing_.y_;
  635. if (updateAll)
  636. *dest = newHeight;
  637. else
  638. {
  639. if (*dest != newHeight)
  640. {
  641. *dest = newHeight;
  642. GrowUpdateRegion(updateRegion, x, z);
  643. }
  644. }
  645. ++dest;
  646. }
  647. }
  648. }
  649. // If updating a region of the heightmap, check which patches change
  650. if (!updateAll)
  651. {
  652. int lodExpand = 1 << (numLodLevels_ - 1);
  653. // Expand the right & bottom 1 pixel more, as patches share vertices at the edge
  654. updateRegion.left_ -= lodExpand;
  655. updateRegion.right_ += lodExpand + 1;
  656. updateRegion.top_ -= lodExpand;
  657. updateRegion.bottom_ += lodExpand + 1;
  658. int sX = Max(updateRegion.left_ / patchSize_, 0);
  659. int eX = Min(updateRegion.right_ / patchSize_, numPatches_.x_ - 1);
  660. int sY = Max(updateRegion.top_ / patchSize_, 0);
  661. int eY = Min(updateRegion.bottom_ / patchSize_, numPatches_.y_ - 1);
  662. for (int y = sY; y <= eY; ++y)
  663. {
  664. for (int x = sX; x <= eX; ++x)
  665. dirtyPatches[y * numPatches_.x_ + x] = true;
  666. }
  667. }
  668. patches_.Reserve(numPatches_.x_ * numPatches_.y_);
  669. bool enabled = IsEnabledEffective();
  670. {
  671. PROFILE(CreatePatches);
  672. // Create patches and set node transforms
  673. for (int z = 0; z < numPatches_.y_; ++z)
  674. {
  675. for (int x = 0; x < numPatches_.x_; ++x)
  676. {
  677. String nodeName = "Patch_" + String(x) + "_" + String(z);
  678. Node* patchNode = node_->GetChild(nodeName);
  679. if (!patchNode)
  680. {
  681. // Create the patch scene node as local and temporary so that it is not unnecessarily serialized to either
  682. // file or replicated over the network
  683. patchNode = node_->CreateChild(nodeName, LOCAL);
  684. patchNode->SetTemporary(true);
  685. }
  686. patchNode->SetPosition(Vector3(patchWorldOrigin_.x_ + (float)x * patchWorldSize_.x_, 0.0f, patchWorldOrigin_.y_ +
  687. (float)z * patchWorldSize_.y_));
  688. TerrainPatch* patch = patchNode->GetComponent<TerrainPatch>();
  689. if (!patch)
  690. {
  691. patch = patchNode->CreateComponent<TerrainPatch>();
  692. patch->SetOwner(this);
  693. patch->SetCoordinates(IntVector2(x, z));
  694. // Copy initial drawable parameters
  695. patch->SetEnabled(enabled);
  696. patch->SetMaterial(material_);
  697. patch->SetDrawDistance(drawDistance_);
  698. patch->SetShadowDistance(shadowDistance_);
  699. patch->SetLodBias(lodBias_);
  700. patch->SetViewMask(viewMask_);
  701. patch->SetLightMask(lightMask_);
  702. patch->SetShadowMask(shadowMask_);
  703. patch->SetZoneMask(zoneMask_);
  704. patch->SetMaxLights(maxLights_);
  705. patch->SetCastShadows(castShadows_);
  706. patch->SetOccluder(occluder_);
  707. patch->SetOccludee(occludee_);
  708. }
  709. patches_.Push(WeakPtr<TerrainPatch>(patch));
  710. }
  711. }
  712. }
  713. // Create the shared index data
  714. if (updateAll)
  715. CreateIndexData();
  716. // Create vertex data for patches. First update smoothing to ensure normals are calculated correctly across patch borders
  717. if (smoothing_)
  718. {
  719. PROFILE(UpdateSmoothing);
  720. for (unsigned i = 0; i < patches_.Size(); ++i)
  721. {
  722. if (dirtyPatches[i])
  723. {
  724. TerrainPatch* patch = patches_[i];
  725. const IntVector2& coords = patch->GetCoordinates();
  726. int startX = coords.x_ * patchSize_;
  727. int endX = startX + patchSize_;
  728. int startZ = coords.y_ * patchSize_;
  729. int endZ = startZ + patchSize_;
  730. for (int z = startZ; z <= endZ; ++z)
  731. {
  732. for (int x = startX; x <= endX; ++x)
  733. {
  734. float smoothedHeight = (
  735. GetSourceHeight(x - 1, z - 1) + GetSourceHeight(x, z - 1) * 2.0f + GetSourceHeight(x + 1, z - 1) +
  736. GetSourceHeight(x - 1, z) * 2.0f + GetSourceHeight(x, z) * 4.0f + GetSourceHeight(x + 1, z) * 2.0f +
  737. GetSourceHeight(x - 1, z + 1) + GetSourceHeight(x, z + 1) * 2.0f + GetSourceHeight(x + 1, z + 1)
  738. ) / 16.0f;
  739. heightData_[z * numVertices_.x_ + x] = smoothedHeight;
  740. }
  741. }
  742. }
  743. }
  744. }
  745. for (unsigned i = 0; i < patches_.Size(); ++i)
  746. {
  747. TerrainPatch* patch = patches_[i];
  748. if (dirtyPatches[i])
  749. {
  750. CreatePatchGeometry(patch);
  751. CalculateLodErrors(patch);
  752. }
  753. SetNeighbors(patch);
  754. }
  755. }
  756. // Send event only if new geometry was generated, or the old was cleared
  757. if (patches_.Size() || prevNumPatches)
  758. {
  759. using namespace TerrainCreated;
  760. VariantMap& eventData = GetEventDataMap();
  761. eventData[P_NODE] = node_;
  762. node_->SendEvent(E_TERRAINCREATED, eventData);
  763. }
  764. }
  765. void Terrain::CreateIndexData()
  766. {
  767. PROFILE(CreateIndexData);
  768. PODVector<unsigned short> indices;
  769. drawRanges_.Clear();
  770. unsigned row = patchSize_ + 1;
  771. /* Build index data for each LOD level. Each LOD level except the lowest can stitch to the next lower LOD from the edges:
  772. north, south, west, east, or any combination of them, requiring 16 different versions of each LOD level's index data
  773. Normal edge: Stitched edge:
  774. +----+----+ +---------+
  775. |\ |\ | |\ /|
  776. | \ | \ | | \ / |
  777. | \ | \ | | \ / |
  778. | \| \| | \ / |
  779. +----+----+ +----+----+
  780. */
  781. for (unsigned i = 0; i < numLodLevels_; ++i)
  782. {
  783. unsigned combinations = (i < numLodLevels_ - 1) ? 16 : 1;
  784. int skip = 1 << i;
  785. for (unsigned j = 0; j < combinations; ++j)
  786. {
  787. unsigned indexStart = indices.Size();
  788. int zStart = 0;
  789. int xStart = 0;
  790. int zEnd = patchSize_;
  791. int xEnd = patchSize_;
  792. if (j & STITCH_NORTH)
  793. zEnd -= skip;
  794. if (j & STITCH_SOUTH)
  795. zStart += skip;
  796. if (j & STITCH_WEST)
  797. xStart += skip;
  798. if (j & STITCH_EAST)
  799. xEnd -= skip;
  800. // Build the main grid
  801. for (int z = zStart; z < zEnd; z += skip)
  802. {
  803. for (int x = xStart; x < xEnd; x += skip)
  804. {
  805. indices.Push((z + skip) * row + x);
  806. indices.Push(z * row + x + skip);
  807. indices.Push(z * row + x);
  808. indices.Push((z + skip) * row + x);
  809. indices.Push((z + skip) * row + x + skip);
  810. indices.Push(z * row + x + skip);
  811. }
  812. }
  813. // Build the north edge
  814. if (j & STITCH_NORTH)
  815. {
  816. int z = patchSize_ - skip;
  817. for (int x = 0; x < patchSize_; x += skip * 2)
  818. {
  819. if (x > 0 || (j & STITCH_WEST) == 0)
  820. {
  821. indices.Push((z + skip) * row + x);
  822. indices.Push(z * row + x + skip);
  823. indices.Push(z * row + x);
  824. }
  825. indices.Push((z + skip) * row + x);
  826. indices.Push((z + skip) * row + x + 2 * skip);
  827. indices.Push(z * row + x + skip);
  828. if (x < patchSize_ - skip * 2 || (j & STITCH_EAST) == 0)
  829. {
  830. indices.Push((z + skip) * row + x + 2 * skip);
  831. indices.Push(z * row + x + 2 * skip);
  832. indices.Push(z * row + x + skip);
  833. }
  834. }
  835. }
  836. // Build the south edge
  837. if (j & STITCH_SOUTH)
  838. {
  839. int z = 0;
  840. for (int x = 0; x < patchSize_; x += skip * 2)
  841. {
  842. if (x > 0 || (j & STITCH_WEST) == 0)
  843. {
  844. indices.Push((z + skip) * row + x);
  845. indices.Push((z + skip) * row + x + skip);
  846. indices.Push(z * row + x);
  847. }
  848. indices.Push(z * row + x);
  849. indices.Push((z + skip) * row + x + skip);
  850. indices.Push(z * row + x + 2 * skip);
  851. if (x < patchSize_ - skip * 2 || (j & STITCH_EAST) == 0)
  852. {
  853. indices.Push((z + skip) * row + x + skip);
  854. indices.Push((z + skip) * row + x + 2 * skip);
  855. indices.Push(z * row + x + 2 * skip);
  856. }
  857. }
  858. }
  859. // Build the west edge
  860. if (j & STITCH_WEST)
  861. {
  862. int x = 0;
  863. for (int z = 0; z < patchSize_; z += skip * 2)
  864. {
  865. if (z > 0 || (j & STITCH_SOUTH) == 0)
  866. {
  867. indices.Push(z * row + x);
  868. indices.Push((z + skip) * row + x + skip);
  869. indices.Push(z * row + x + skip);
  870. }
  871. indices.Push((z + 2 * skip) * row + x);
  872. indices.Push((z + skip) * row + x + skip);
  873. indices.Push(z * row + x);
  874. if (x < patchSize_ - skip * 2 || (j & STITCH_NORTH) == 0)
  875. {
  876. indices.Push((z + 2 * skip) * row + x);
  877. indices.Push((z + 2 * skip) * row + x + skip);
  878. indices.Push((z + skip) * row + x + skip);
  879. }
  880. }
  881. }
  882. // Build the east edge
  883. if (j & STITCH_EAST)
  884. {
  885. int x = patchSize_ - skip;
  886. for (int z = 0; z < patchSize_; z += skip * 2)
  887. {
  888. if (z > 0 || (j & STITCH_SOUTH) == 0)
  889. {
  890. indices.Push(z * row + x);
  891. indices.Push((z + skip) * row + x);
  892. indices.Push(z * row + x + skip);
  893. }
  894. indices.Push((z + skip) * row + x);
  895. indices.Push((z + 2 * skip) * row + x + skip);
  896. indices.Push(z * row + x + skip);
  897. if (z < patchSize_ - skip * 2 || (j & STITCH_NORTH) == 0)
  898. {
  899. indices.Push((z + skip) * row + x);
  900. indices.Push((z + 2 * skip) * row + x);
  901. indices.Push((z + 2 * skip) * row + x + skip);
  902. }
  903. }
  904. }
  905. drawRanges_.Push(MakePair(indexStart, indices.Size() - indexStart));
  906. }
  907. }
  908. indexBuffer_->SetSize(indices.Size(), false);
  909. indexBuffer_->SetData(&indices[0]);
  910. }
  911. float Terrain::GetRawHeight(int x, int z) const
  912. {
  913. if (!heightData_)
  914. return 0.0f;
  915. x = Clamp(x, 0, numVertices_.x_ - 1);
  916. z = Clamp(z, 0, numVertices_.y_ - 1);
  917. return heightData_[z * numVertices_.x_ + x];
  918. }
  919. float Terrain::GetSourceHeight(int x, int z) const
  920. {
  921. if (!sourceHeightData_)
  922. return 0.0f;
  923. x = Clamp(x, 0, numVertices_.x_ - 1);
  924. z = Clamp(z, 0, numVertices_.y_ - 1);
  925. return sourceHeightData_[z * numVertices_.x_ + x];
  926. }
  927. float Terrain::GetLodHeight(int x, int z, unsigned lodLevel) const
  928. {
  929. unsigned offset = 1 << lodLevel;
  930. float divisor = (float)offset;
  931. float xFrac = (float)(x % offset) / divisor;
  932. float zFrac = (float)(z % offset) / divisor;
  933. float h1, h2, h3;
  934. if (xFrac + zFrac >= 1.0f)
  935. {
  936. h1 = GetRawHeight(x + offset, z + offset);
  937. h2 = GetRawHeight(x, z + offset);
  938. h3 = GetRawHeight(x + offset, z);
  939. xFrac = 1.0f - xFrac;
  940. zFrac = 1.0f - zFrac;
  941. }
  942. else
  943. {
  944. h1 = GetRawHeight(x, z);
  945. h2 = GetRawHeight(x + offset, z);
  946. h3 = GetRawHeight(x, z + offset);
  947. }
  948. return h1 * (1.0f - xFrac - zFrac) + h2 * xFrac + h3 * zFrac;
  949. }
  950. Vector3 Terrain::GetRawNormal(int x, int z) const
  951. {
  952. float baseHeight = GetRawHeight(x, z);
  953. float nSlope = GetRawHeight(x, z - 1) - baseHeight;
  954. float neSlope = GetRawHeight(x + 1, z - 1) - baseHeight;
  955. float eSlope = GetRawHeight(x + 1, z) - baseHeight;
  956. float seSlope = GetRawHeight(x + 1, z + 1) - baseHeight;
  957. float sSlope = GetRawHeight(x, z + 1) - baseHeight;
  958. float swSlope = GetRawHeight(x - 1, z + 1) - baseHeight;
  959. float wSlope = GetRawHeight(x - 1, z) - baseHeight;
  960. float nwSlope = GetRawHeight(x - 1, z - 1) - baseHeight;
  961. float up = 0.5f * (spacing_.x_ + spacing_.z_);
  962. return (Vector3(0.0f, up, nSlope) +
  963. Vector3(-neSlope, up, neSlope) +
  964. Vector3(-eSlope, up, 0.0f) +
  965. Vector3(-seSlope, up, -seSlope) +
  966. Vector3(0.0f, up, -sSlope) +
  967. Vector3(swSlope, up, -swSlope) +
  968. Vector3(wSlope, up, 0.0f) +
  969. Vector3(nwSlope, up, nwSlope)).Normalized();
  970. }
  971. void Terrain::CalculateLodErrors(TerrainPatch* patch)
  972. {
  973. PROFILE(CalculateLodErrors);
  974. const IntVector2& coords = patch->GetCoordinates();
  975. PODVector<float>& lodErrors = patch->GetLodErrors();
  976. lodErrors.Clear();
  977. lodErrors.Reserve(numLodLevels_);
  978. int xStart = coords.x_ * patchSize_;
  979. int zStart = coords.y_ * patchSize_;
  980. int xEnd = xStart + patchSize_;
  981. int zEnd = zStart + patchSize_;
  982. for (unsigned i = 0; i < numLodLevels_; ++i)
  983. {
  984. float maxError = 0.0f;
  985. int divisor = 1 << i;
  986. if (i > 0)
  987. {
  988. for (int z = zStart; z <= zEnd; ++z)
  989. {
  990. for (int x = xStart; x <= xEnd; ++x)
  991. {
  992. if (x % divisor || z % divisor)
  993. {
  994. float error = Abs(GetLodHeight(x, z, i) - GetRawHeight(x, z));
  995. maxError = Max(error, maxError);
  996. }
  997. }
  998. }
  999. // Set error to be at least same as (half vertex spacing x LOD) to prevent horizontal stretches getting too inaccurate
  1000. maxError = Max(maxError, 0.25f * (spacing_.x_ + spacing_.z_) * (float)(1 << i));
  1001. }
  1002. lodErrors.Push(maxError);
  1003. }
  1004. }
  1005. void Terrain::SetNeighbors(TerrainPatch* patch)
  1006. {
  1007. const IntVector2& coords = patch->GetCoordinates();
  1008. patch->SetNeighbors(GetPatch(coords.x_, coords.y_ + 1), GetPatch(coords.x_, coords.y_ - 1),
  1009. GetPatch(coords.x_ - 1, coords.y_), GetPatch(coords.x_ + 1, coords.y_));
  1010. }
  1011. bool Terrain::SetHeightMapInternal(Image* image, bool recreateNow)
  1012. {
  1013. if (image && image->IsCompressed())
  1014. {
  1015. LOGERROR("Can not use a compressed image as a terrain heightmap");
  1016. return false;
  1017. }
  1018. // Unsubscribe from the reload event of previous image (if any), then subscribe to the new
  1019. if (heightMap_)
  1020. UnsubscribeFromEvent(heightMap_, E_RELOADFINISHED);
  1021. if (image)
  1022. SubscribeToEvent(image, E_RELOADFINISHED, HANDLER(Terrain, HandleHeightMapReloadFinished));
  1023. heightMap_ = image;
  1024. if (recreateNow)
  1025. CreateGeometry();
  1026. else
  1027. recreateTerrain_ = true;
  1028. return true;
  1029. }
  1030. void Terrain::HandleHeightMapReloadFinished(StringHash eventType, VariantMap& eventData)
  1031. {
  1032. CreateGeometry();
  1033. }
  1034. }