Terrain.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "DrawableEvents.h"
  26. #include "Geometry.h"
  27. #include "Image.h"
  28. #include "IndexBuffer.h"
  29. #include "Log.h"
  30. #include "Material.h"
  31. #include "Node.h"
  32. #include "Octree.h"
  33. #include "Profiler.h"
  34. #include "ResourceCache.h"
  35. #include "ResourceEvents.h"
  36. #include "Scene.h"
  37. #include "StringUtils.h"
  38. #include "Terrain.h"
  39. #include "TerrainPatch.h"
  40. #include "VertexBuffer.h"
  41. #include "DebugNew.h"
  42. OBJECTTYPESTATIC(Terrain);
  43. static const unsigned DEFAULT_PATCH_SIZE = 16;
  44. static const unsigned DEFAULT_LOD_LEVELS = 3;
  45. static const unsigned MAX_LOD_LEVELS = 4;
  46. static const unsigned MIN_PATCH_SIZE = 4;
  47. static const unsigned MAX_PATCH_SIZE = 128;
  48. static const Vector3 DEFAULT_SPACING(1.0f, 0.25f, 1.0f);
  49. Terrain::Terrain(Context* context) :
  50. Component(context),
  51. indexBuffer_(new IndexBuffer(context)),
  52. patchSize_(DEFAULT_PATCH_SIZE),
  53. spacing_(DEFAULT_SPACING),
  54. size_(IntVector2::ZERO),
  55. patchWorldSize_(Vector2::ZERO),
  56. patchWorldOrigin_(Vector2::ZERO),
  57. patchesX_(0),
  58. patchesZ_(0),
  59. numLodLevels_(DEFAULT_LOD_LEVELS),
  60. visible_(true),
  61. castShadows_(false),
  62. occluder_(false),
  63. occludee_(true),
  64. viewMask_(DEFAULT_VIEWMASK),
  65. lightMask_(DEFAULT_LIGHTMASK),
  66. shadowMask_(DEFAULT_SHADOWMASK),
  67. zoneMask_(DEFAULT_ZONEMASK),
  68. drawDistance_(0.0f),
  69. shadowDistance_(0.0f),
  70. lodBias_(1.0f),
  71. maxLights_(0),
  72. recreateTerrain_(false)
  73. {
  74. indexBuffer_->SetShadowed(true);
  75. }
  76. Terrain::~Terrain()
  77. {
  78. }
  79. void Terrain::RegisterObject(Context* context)
  80. {
  81. context->RegisterFactory<Terrain>();
  82. ACCESSOR_ATTRIBUTE(Terrain, VAR_RESOURCEREF, "Height Map", GetHeightMapAttr, SetHeightMapAttr, ResourceRef, ResourceRef(Image::GetTypeStatic()), AM_DEFAULT);
  83. ACCESSOR_ATTRIBUTE(Terrain, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  84. ATTRIBUTE(Terrain, VAR_VECTOR3, "Vertex Spacing", spacing_, DEFAULT_SPACING, AM_DEFAULT);
  85. ACCESSOR_ATTRIBUTE(Terrain, VAR_INT, "Patch Size", GetPatchSize, SetPatchSizeAttr, unsigned, DEFAULT_PATCH_SIZE, AM_DEFAULT);
  86. ACCESSOR_ATTRIBUTE(Terrain, VAR_BOOL, "Is Visible", IsVisible, SetVisible, bool, true, AM_DEFAULT);
  87. ACCESSOR_ATTRIBUTE(Terrain, VAR_BOOL, "Is Occluder", IsOccluder, SetOccluder, bool, false, AM_DEFAULT);
  88. ACCESSOR_ATTRIBUTE(Terrain, VAR_BOOL, "Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  89. ACCESSOR_ATTRIBUTE(Terrain, VAR_BOOL, "Cast Shadows", GetCastShadows, SetCastShadows, bool, false, AM_DEFAULT);
  90. ACCESSOR_ATTRIBUTE(Terrain, VAR_FLOAT, "Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  91. ACCESSOR_ATTRIBUTE(Terrain, VAR_FLOAT, "Shadow Distance", GetShadowDistance, SetShadowDistance, float, 0.0f, AM_DEFAULT);
  92. ACCESSOR_ATTRIBUTE(Terrain, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
  93. ACCESSOR_ATTRIBUTE(Terrain, VAR_INT, "Max Lights", GetMaxLights, SetMaxLights, unsigned, 0, AM_DEFAULT);
  94. ACCESSOR_ATTRIBUTE(Terrain, VAR_INT, "View Mask", GetViewMask, SetViewMask, unsigned, DEFAULT_VIEWMASK, AM_DEFAULT);
  95. ACCESSOR_ATTRIBUTE(Terrain, VAR_INT, "Light Mask", GetLightMask, SetLightMask, unsigned, DEFAULT_LIGHTMASK, AM_DEFAULT);
  96. ACCESSOR_ATTRIBUTE(Terrain, VAR_INT, "Shadow Mask", GetShadowMask, SetShadowMask, unsigned, DEFAULT_SHADOWMASK, AM_DEFAULT);
  97. ACCESSOR_ATTRIBUTE(Terrain, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_SHADOWMASK, AM_DEFAULT);
  98. }
  99. void Terrain::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  100. {
  101. Component::OnSetAttribute(attr, src);
  102. // Change of any non-accessor attribute requires recreation of the terrain
  103. if (!attr.accessor_)
  104. recreateTerrain_ = true;
  105. }
  106. void Terrain::ApplyAttributes()
  107. {
  108. if (recreateTerrain_)
  109. {
  110. CreateGeometry();
  111. recreateTerrain_ = false;
  112. }
  113. }
  114. void Terrain::SetSpacing(const Vector3& spacing)
  115. {
  116. if (spacing != spacing_)
  117. {
  118. spacing_ = spacing;
  119. CreateGeometry();
  120. MarkNetworkUpdate();
  121. }
  122. }
  123. void Terrain::SetPatchSize(unsigned size)
  124. {
  125. if (size < MIN_PATCH_SIZE || size > MAX_PATCH_SIZE || !IsPowerOfTwo(size))
  126. return;
  127. if (size != patchSize_)
  128. {
  129. patchSize_ = size;
  130. CreateGeometry();
  131. MarkNetworkUpdate();
  132. }
  133. }
  134. bool Terrain::SetHeightMap(Image* image)
  135. {
  136. bool success = SetHeightMapInternal(image, true);
  137. MarkNetworkUpdate();
  138. return success;
  139. }
  140. void Terrain::SetMaterial(Material* material)
  141. {
  142. material_ = material;
  143. for (unsigned i = 0; i < patches_.Size(); ++i)
  144. {
  145. if (patches_[i])
  146. patches_[i]->batches_[0].material_ = material;
  147. }
  148. MarkNetworkUpdate();
  149. }
  150. void Terrain::SetDrawDistance(float distance)
  151. {
  152. drawDistance_ = distance;
  153. for (unsigned i = 0; i < patches_.Size(); ++i)
  154. {
  155. if (patches_[i])
  156. patches_[i]->SetDrawDistance(distance);
  157. }
  158. MarkNetworkUpdate();
  159. }
  160. void Terrain::SetShadowDistance(float distance)
  161. {
  162. shadowDistance_ = distance;
  163. for (unsigned i = 0; i < patches_.Size(); ++i)
  164. {
  165. if (patches_[i])
  166. patches_[i]->SetShadowDistance(distance);
  167. }
  168. MarkNetworkUpdate();
  169. }
  170. void Terrain::SetLodBias(float bias)
  171. {
  172. lodBias_ = bias;
  173. for (unsigned i = 0; i < patches_.Size(); ++i)
  174. {
  175. if (patches_[i])
  176. patches_[i]->SetLodBias(bias);
  177. }
  178. MarkNetworkUpdate();
  179. }
  180. void Terrain::SetViewMask(unsigned mask)
  181. {
  182. viewMask_ = mask;
  183. for (unsigned i = 0; i < patches_.Size(); ++i)
  184. {
  185. if (patches_[i])
  186. patches_[i]->SetViewMask(mask);
  187. }
  188. MarkNetworkUpdate();
  189. }
  190. void Terrain::SetLightMask(unsigned mask)
  191. {
  192. lightMask_ = mask;
  193. for (unsigned i = 0; i < patches_.Size(); ++i)
  194. {
  195. if (patches_[i])
  196. patches_[i]->SetLightMask(mask);
  197. }
  198. MarkNetworkUpdate();
  199. }
  200. void Terrain::SetShadowMask(unsigned mask)
  201. {
  202. shadowMask_ = mask;
  203. for (unsigned i = 0; i < patches_.Size(); ++i)
  204. {
  205. if (patches_[i])
  206. patches_[i]->SetShadowMask(mask);
  207. }
  208. MarkNetworkUpdate();
  209. }
  210. void Terrain::SetZoneMask(unsigned mask)
  211. {
  212. zoneMask_ = mask;
  213. for (unsigned i = 0; i < patches_.Size(); ++i)
  214. {
  215. if (patches_[i])
  216. patches_[i]->SetZoneMask(mask);
  217. }
  218. MarkNetworkUpdate();
  219. }
  220. void Terrain::SetMaxLights(unsigned num)
  221. {
  222. maxLights_ = num;
  223. for (unsigned i = 0; i < patches_.Size(); ++i)
  224. {
  225. if (patches_[i])
  226. patches_[i]->SetMaxLights(num);
  227. }
  228. MarkNetworkUpdate();
  229. }
  230. void Terrain::SetVisible(bool enable)
  231. {
  232. visible_ = enable;
  233. for (unsigned i = 0; i < patches_.Size(); ++i)
  234. {
  235. if (patches_[i])
  236. patches_[i]->SetVisible(enable);
  237. }
  238. MarkNetworkUpdate();
  239. }
  240. void Terrain::SetCastShadows(bool enable)
  241. {
  242. castShadows_ = enable;
  243. for (unsigned i = 0; i < patches_.Size(); ++i)
  244. {
  245. if (patches_[i])
  246. patches_[i]->SetCastShadows(enable);
  247. }
  248. MarkNetworkUpdate();
  249. }
  250. void Terrain::SetOccluder(bool enable)
  251. {
  252. occluder_ = enable;
  253. for (unsigned i = 0; i < patches_.Size(); ++i)
  254. {
  255. if (patches_[i])
  256. patches_[i]->SetOccluder(enable);
  257. }
  258. MarkNetworkUpdate();
  259. }
  260. void Terrain::SetOccludee(bool enable)
  261. {
  262. occluder_ = enable;
  263. for (unsigned i = 0; i < patches_.Size(); ++i)
  264. {
  265. if (patches_[i])
  266. patches_[i]->SetOccludee(enable);
  267. }
  268. MarkNetworkUpdate();
  269. }
  270. Image* Terrain::GetHeightMap() const
  271. {
  272. return heightMap_;
  273. }
  274. Material* Terrain::GetMaterial() const
  275. {
  276. return material_;
  277. }
  278. TerrainPatch* Terrain::GetPatch(unsigned index) const
  279. {
  280. return index < patches_.Size() ? patches_[index] : (TerrainPatch*)0;
  281. }
  282. float Terrain::GetHeight(const Vector3& worldPosition) const
  283. {
  284. if (node_)
  285. {
  286. Vector3 position = node_->GetWorldTransform().Inverse() * worldPosition;
  287. float xPos = (position.x_ - patchWorldOrigin_.x_) / spacing_.x_;
  288. float zPos = (position.z_ - patchWorldOrigin_.y_) / spacing_.z_;
  289. float xFrac = xPos - floorf(xPos);
  290. float zFrac = zPos - floorf(zPos);
  291. float h1, h2, h3;
  292. if (xFrac + zFrac >= 1.0f)
  293. {
  294. h1 = GetRawHeight((unsigned)xPos + 1, (unsigned)zPos + 1);
  295. h2 = GetRawHeight((unsigned)xPos, (unsigned)zPos + 1);
  296. h3 = GetRawHeight((unsigned)xPos + 1, (unsigned)zPos);
  297. xFrac = 1.0f - xFrac;
  298. zFrac = 1.0f - zFrac;
  299. }
  300. else
  301. {
  302. h1 = GetRawHeight((unsigned)xPos, (unsigned)zPos);
  303. h2 = GetRawHeight((unsigned)xPos + 1, (unsigned)zPos);
  304. h3 = GetRawHeight((unsigned)xPos, (unsigned)zPos + 1);
  305. }
  306. float h = h1 * (1.0f - xFrac - zFrac) + h2 * xFrac + h3 * zFrac;
  307. /// \todo This assumes that the terrain scene node is upright
  308. return node_->GetWorldScale().y_ * h + node_->GetWorldPosition().y_;
  309. }
  310. else
  311. return 0.0f;
  312. }
  313. void Terrain::UpdatePatchGeometry(TerrainPatch* patch)
  314. {
  315. BoundingBox box;
  316. unsigned vertexDataRow = patchSize_ + 1;
  317. VertexBuffer* vertexBuffer = patch->vertexBuffer_;
  318. if (vertexBuffer->GetVertexCount() != vertexDataRow * vertexDataRow)
  319. vertexBuffer->SetSize(vertexDataRow * vertexDataRow, MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1 | MASK_TANGENT);
  320. SharedArrayPtr<unsigned char> cpuVertexData(new unsigned char[vertexDataRow * vertexDataRow * sizeof(Vector3)]);
  321. float* vertexData = (float*)vertexBuffer->Lock(0, vertexBuffer->GetVertexCount());
  322. float* positionData = (float*)cpuVertexData.Get();
  323. if (vertexData)
  324. {
  325. unsigned x = patch->x_;
  326. unsigned z = patch->z_;
  327. for (unsigned z1 = 0; z1 <= patchSize_; ++z1)
  328. {
  329. for (unsigned x1 = 0; x1 <= patchSize_; ++x1)
  330. {
  331. int xPos = x * patchSize_ + x1;
  332. int zPos = z * patchSize_ + z1;
  333. // Position
  334. Vector3 position((float)x1 * spacing_.x_, GetRawHeight(xPos, zPos), (float)z1 * spacing_.z_);
  335. *vertexData++ = position.x_;
  336. *vertexData++ = position.y_;
  337. *vertexData++ = position.z_;
  338. *positionData++ = position.x_;
  339. *positionData++ = position.y_;
  340. *positionData++ = position.z_;
  341. box.Merge(position);
  342. // Normal
  343. Vector3 normal = GetNormal(xPos, zPos);
  344. *vertexData++ = normal.x_;
  345. *vertexData++ = normal.y_;
  346. *vertexData++ = normal.z_;
  347. // Texture coordinate
  348. Vector2 texCoord((float)xPos / (float)size_.x_, 1.0f - (float)zPos / (float)size_.y_);
  349. *vertexData++ = texCoord.x_;
  350. *vertexData++ = texCoord.y_;
  351. // Tangent
  352. Vector3 xyz = (Vector3::RIGHT - normal * normal.DotProduct(Vector3::RIGHT)).Normalized();
  353. *vertexData++ = xyz.x_;
  354. *vertexData++ = xyz.y_;
  355. *vertexData++ = xyz.z_;
  356. *vertexData++ = 1.0f;
  357. }
  358. }
  359. vertexBuffer->Unlock();
  360. vertexBuffer->ClearDataLost();
  361. }
  362. patch->boundingBox_ = box;
  363. patch->geometry_->SetIndexBuffer(indexBuffer_);
  364. patch->geometry_->SetDrawRange(TRIANGLE_LIST, 0, indexBuffer_->GetIndexCount());
  365. patch->geometry_->SetRawVertexData(cpuVertexData, sizeof(Vector3), MASK_POSITION);
  366. patch->OnMarkedDirty(patch->GetNode());
  367. }
  368. void Terrain::UpdatePatchLOD(TerrainPatch* patch, unsigned lod, unsigned northLod, unsigned southLod, unsigned westLod,
  369. unsigned eastLod)
  370. {
  371. }
  372. void Terrain::SetMaterialAttr(ResourceRef value)
  373. {
  374. ResourceCache* cache = GetSubsystem<ResourceCache>();
  375. SetMaterial(cache->GetResource<Material>(value.id_));
  376. }
  377. void Terrain::SetHeightMapAttr(ResourceRef value)
  378. {
  379. ResourceCache* cache = GetSubsystem<ResourceCache>();
  380. Image* image = cache->GetResource<Image>(value.id_);
  381. SetHeightMapInternal(image, false);
  382. }
  383. void Terrain::SetPatchSizeAttr(unsigned value)
  384. {
  385. if (value < MIN_PATCH_SIZE || value > MAX_PATCH_SIZE || !IsPowerOfTwo(value))
  386. return;
  387. if (value != patchSize_)
  388. {
  389. patchSize_ = value;
  390. recreateTerrain_ = true;
  391. }
  392. }
  393. ResourceRef Terrain::GetMaterialAttr() const
  394. {
  395. return GetResourceRef(material_, Material::GetTypeStatic());
  396. }
  397. ResourceRef Terrain::GetHeightMapAttr() const
  398. {
  399. return GetResourceRef(heightMap_, Image::GetTypeStatic());
  400. }
  401. void Terrain::CreateGeometry()
  402. {
  403. recreateTerrain_ = false;
  404. if (!node_)
  405. return;
  406. PROFILE(CreateTerrainGeometry);
  407. unsigned prevNumPatches = patches_.Size();
  408. // Determine number of LOD levels
  409. unsigned lodSize = patchSize_;
  410. numLodLevels_ = 1;
  411. while (lodSize > MIN_PATCH_SIZE && numLodLevels_ < MAX_LOD_LEVELS)
  412. {
  413. lodSize >>= 1;
  414. ++numLodLevels_;
  415. }
  416. // Determine total terrain size
  417. patchWorldSize_ = Vector2(spacing_.x_ * (float)patchSize_, spacing_.z_ * (float)patchSize_);
  418. if (heightMap_)
  419. {
  420. patchesX_ = (heightMap_->GetWidth() - 1) / patchSize_;
  421. patchesZ_ = (heightMap_->GetHeight() - 1) / patchSize_;
  422. size_ = IntVector2(patchesX_ * patchSize_ + 1, patchesZ_ * patchSize_ + 1);
  423. patchWorldOrigin_ = Vector2(-0.5f * (float)patchesX_ * patchWorldSize_.x_, -0.5f * (float)patchesZ_ * patchWorldSize_.y_);
  424. heightData_ = new float[size_.x_ * size_.y_];
  425. }
  426. else
  427. {
  428. patchesX_ = 0;
  429. patchesZ_ = 0;
  430. size_ = IntVector2::ZERO;
  431. patchWorldOrigin_ = Vector2::ZERO;
  432. heightData_.Reset();
  433. }
  434. // Remove old patch nodes which are not needed
  435. PODVector<Node*> oldPatchNodes;
  436. node_->GetChildrenWithComponent<TerrainPatch>(oldPatchNodes);
  437. for (PODVector<Node*>::Iterator i = oldPatchNodes.Begin(); i != oldPatchNodes.End(); ++i)
  438. {
  439. bool nodeOk = false;
  440. Vector<String> coords = (*i)->GetName().Substring(6).Split('_');
  441. if (coords.Size() == 2)
  442. {
  443. unsigned x = ToUInt(coords[0]);
  444. unsigned z = ToUInt(coords[1]);
  445. if (x < patchesX_ && z < patchesZ_)
  446. nodeOk = true;
  447. }
  448. if (!nodeOk)
  449. node_->RemoveChild(*i);
  450. }
  451. patches_.Clear();
  452. if (heightMap_)
  453. {
  454. // Copy heightmap data
  455. const unsigned char* src = heightMap_->GetData();
  456. float* dest = heightData_;
  457. unsigned imgComps = heightMap_->GetComponents();
  458. unsigned imgRow = heightMap_->GetWidth() * imgComps;
  459. for (int z = 0; z < size_.y_; ++z)
  460. {
  461. for (int x = 0; x < size_.x_; ++x)
  462. *dest++ = (float)src[imgRow * (size_.y_ - 1 - z) + imgComps * x] * spacing_.y_;
  463. }
  464. // Create patches and set node transforms
  465. for (unsigned z = 0; z < patchesZ_; ++z)
  466. {
  467. for (unsigned x = 0; x < patchesX_; ++x)
  468. {
  469. String nodeName = "Patch_" + String(x) + "_" + String(z);
  470. Node* patchNode = node_->GetChild(nodeName);
  471. if (!patchNode)
  472. patchNode = node_->CreateChild(nodeName, LOCAL);
  473. patchNode->SetPosition(Vector3(patchWorldOrigin_.x_ + (float)x * patchWorldSize_.x_, 0.0f, patchWorldOrigin_.y_ +
  474. (float)z * patchWorldSize_.y_));
  475. TerrainPatch* patch = patchNode->GetOrCreateComponent<TerrainPatch>();
  476. patch->owner_ = this;
  477. patch->x_ = x;
  478. patch->z_ = z;
  479. // Copy initial drawable parameters
  480. patch->batches_[0].material_ = material_;
  481. patch->SetDrawDistance(drawDistance_);
  482. patch->SetShadowDistance(shadowDistance_);
  483. patch->SetLodBias(lodBias_);
  484. patch->SetViewMask(viewMask_);
  485. patch->SetLightMask(lightMask_);
  486. patch->SetShadowMask(shadowMask_);
  487. patch->SetZoneMask(zoneMask_);
  488. patch->SetMaxLights(maxLights_);
  489. patch->SetVisible(visible_);
  490. patch->SetCastShadows(castShadows_);
  491. patch->SetOccluder(occluder_);
  492. patch->SetOccludee(occludee_);
  493. patches_.Push(WeakPtr<TerrainPatch>(patch));
  494. }
  495. }
  496. // Create the shared index data
  497. /// \todo Create LOD levels
  498. indexBuffer_->SetSize(patchSize_ * patchSize_ * 6, false);
  499. unsigned vertexDataRow = patchSize_ + 1;
  500. unsigned short* indexData = (unsigned short*)indexBuffer_->Lock(0, indexBuffer_->GetIndexCount());
  501. if (indexData)
  502. {
  503. for (unsigned z = 0; z < patchSize_; ++z)
  504. {
  505. for (unsigned x = 0; x < patchSize_; ++x)
  506. {
  507. *indexData++ = x + (z + 1) * vertexDataRow;
  508. *indexData++ = x + z * vertexDataRow + 1;
  509. *indexData++ = x + z * vertexDataRow;
  510. *indexData++ = x + (z + 1) * vertexDataRow;
  511. *indexData++ = x + (z + 1) * vertexDataRow + 1;
  512. *indexData++ = x + z * vertexDataRow + 1;
  513. }
  514. }
  515. indexBuffer_->Unlock();
  516. }
  517. // Create vertex data for patches
  518. for (Vector<WeakPtr<TerrainPatch> >::Iterator i = patches_.Begin(); i != patches_.End(); ++i)
  519. UpdatePatchGeometry(*i);
  520. }
  521. // Send event only if new geometry was generated, or the old was cleared
  522. if (patches_.Size() || prevNumPatches)
  523. {
  524. using namespace TerrainCreated;
  525. VariantMap eventData;
  526. eventData[P_NODE] = (void*)node_;
  527. node_->SendEvent(E_TERRAINCREATED, eventData);
  528. }
  529. }
  530. float Terrain::GetRawHeight(unsigned x, unsigned z) const
  531. {
  532. if (!heightData_)
  533. return 0.0f;
  534. x = Clamp((int)x, 0, (int)size_.x_ - 1);
  535. z = Clamp((int)z, 0, (int)size_.y_ - 1);
  536. return heightData_[z * size_.x_ + x];
  537. }
  538. Vector3 Terrain::GetNormal(unsigned x, unsigned z) const
  539. {
  540. float baseHeight = GetRawHeight(x, z);
  541. float nSlope = GetRawHeight(x, z - 1) - baseHeight;
  542. float neSlope = GetRawHeight(x + 1, z - 1) - baseHeight;
  543. float eSlope = GetRawHeight(x + 1, z) - baseHeight;
  544. float seSlope = GetRawHeight(x + 1, z + 1) - baseHeight;
  545. float sSlope = GetRawHeight(x, z + 1) - baseHeight;
  546. float swSlope = GetRawHeight(x - 1, z + 1) - baseHeight;
  547. float wSlope = GetRawHeight(x - 1, z) - baseHeight;
  548. float nwSlope = GetRawHeight(x - 1, z - 1) - baseHeight;
  549. return (Vector3(0.0f, 1.0f, nSlope) +
  550. Vector3(-neSlope, 1.0f, neSlope) +
  551. Vector3(-eSlope, 1.0f, 0.0f) +
  552. Vector3(-seSlope, 1.0f, -seSlope) +
  553. Vector3(0.0f, 1.0f, -sSlope) +
  554. Vector3(swSlope, 1.0f, -swSlope) +
  555. Vector3(wSlope, 1.0f, 0.0f) +
  556. Vector3(nwSlope, 1.0f, nwSlope)).Normalized();
  557. }
  558. bool Terrain::SetHeightMapInternal(Image* image, bool recreateNow)
  559. {
  560. if (image && image->IsCompressed())
  561. {
  562. LOGERROR("Can not use a compressed image as a terrain heightmap");
  563. return false;
  564. }
  565. // Unsubscribe from the reload event of previous image (if any), then subscribe to the new
  566. if (heightMap_)
  567. UnsubscribeFromEvent(heightMap_, E_RELOADFINISHED);
  568. if (image)
  569. SubscribeToEvent(image, E_RELOADFINISHED, HANDLER(Terrain, HandleHeightMapReloadFinished));
  570. heightMap_ = image;
  571. if (recreateNow)
  572. CreateGeometry();
  573. else
  574. recreateTerrain_ = true;
  575. return true;
  576. }
  577. void Terrain::HandleHeightMapReloadFinished(StringHash eventType, VariantMap& eventData)
  578. {
  579. CreateGeometry();
  580. }