Terrain.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Copyright (c) 2008-2014 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. #pragma once
  23. #include "Component.h"
  24. namespace Urho3D
  25. {
  26. class Image;
  27. class Material;
  28. class Node;
  29. class TerrainPatch;
  30. /// Heightmap terrain component.
  31. class URHO3D_API Terrain : public Component
  32. {
  33. OBJECT(Terrain);
  34. public:
  35. /// Construct.
  36. Terrain(Context* context);
  37. /// Destruct.
  38. ~Terrain();
  39. /// Register object factory.
  40. static void RegisterObject(Context* context);
  41. /// Handle attribute write access.
  42. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  43. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  44. virtual void ApplyAttributes();
  45. /// Handle enabled/disabled state change.
  46. virtual void OnSetEnabled();
  47. /// Set patch quads per side. Must be a power of two.
  48. void SetPatchSize(int size);
  49. /// Set vertex (XZ) and height (Y) spacing.
  50. void SetSpacing(const Vector3& spacing);
  51. /// Set smoothing of heightmap.
  52. void SetSmoothing(bool enable);
  53. /// Set heightmap image. Dimensions should be a power of two + 1. Uses 8-bit grayscale, or optionally red as MSB and green as LSB for 16-bit accuracy. Return true if successful.
  54. bool SetHeightMap(Image* image);
  55. /// Set material.
  56. void SetMaterial(Material* material);
  57. /// Set draw distance for patches.
  58. void SetDrawDistance(float distance);
  59. /// Set shadow draw distance for patches.
  60. void SetShadowDistance(float distance);
  61. /// Set LOD bias for patches. Affects which terrain LOD to display.
  62. void SetLodBias(float bias);
  63. /// Set view mask for patches. Is and'ed with camera's view mask to see if the object should be rendered.
  64. void SetViewMask(unsigned mask);
  65. /// Set light mask for patches. Is and'ed with light's and zone's light mask to see if the object should be lit.
  66. void SetLightMask(unsigned mask);
  67. /// Set shadow mask for patches. Is and'ed with light's light mask and zone's shadow mask to see if the object should be rendered to a shadow map.
  68. void SetShadowMask(unsigned mask);
  69. /// Set zone mask for patches. Is and'ed with zone's zone mask to see if the object should belong to the zone.
  70. void SetZoneMask(unsigned mask);
  71. /// Set maximum number of per-pixel lights for patches. Default 0 is unlimited.
  72. void SetMaxLights(unsigned num);
  73. /// Set shadowcaster flag for patches.
  74. void SetCastShadows(bool enable);
  75. /// Set occlusion flag for patches. Occlusion uses the coarsest LOD and may potentially be too aggressive, so use with caution.
  76. void SetOccluder(bool enable);
  77. /// Set occludee flag for patches.
  78. void SetOccludee(bool enable);
  79. /// Return patch quads per side.
  80. int GetPatchSize() const { return patchSize_; }
  81. /// Return vertex and height spacing.
  82. const Vector3& GetSpacing() const { return spacing_; }
  83. /// Return heightmap size in vertices.
  84. const IntVector2& GetNumVertices() const { return numVertices_; }
  85. /// Return heightmap size in patches.
  86. const IntVector2& GetNumPatches() const { return numPatches_; }
  87. /// Return whether smoothing is in use.
  88. bool GetSmoothing() const { return smoothing_; }
  89. /// Return heightmap image.
  90. Image* GetHeightMap() const;
  91. /// Return material.
  92. Material* GetMaterial() const;
  93. /// Return patch by index.
  94. TerrainPatch* GetPatch(unsigned index) const;
  95. /// Return patch by patch coordinates.
  96. TerrainPatch* GetPatch(int x, int z) const;
  97. /// Return height at world coordinates.
  98. float GetHeight(const Vector3& worldPosition) const;
  99. /// Return normal at world coordinates.
  100. Vector3 GetNormal(const Vector3& worldPosition) const;
  101. /// Return raw height data.
  102. SharedArrayPtr<float> GetHeightData() const { return heightData_; }
  103. /// Return draw distance.
  104. float GetDrawDistance() const { return drawDistance_; }
  105. /// Return shadow draw distance.
  106. float GetShadowDistance() const { return shadowDistance_; }
  107. /// Return LOD bias.
  108. float GetLodBias() const { return lodBias_; }
  109. /// Return view mask.
  110. unsigned GetViewMask() const { return viewMask_; }
  111. /// Return light mask.
  112. unsigned GetLightMask() const { return lightMask_; }
  113. /// Return shadow mask.
  114. unsigned GetShadowMask() const { return shadowMask_; }
  115. /// Return zone mask.
  116. unsigned GetZoneMask() const { return zoneMask_; }
  117. /// Return maximum number of per-pixel lights.
  118. unsigned GetMaxLights() const { return maxLights_; }
  119. /// Return visible flag.
  120. bool IsVisible() const { return visible_; }
  121. /// Return shadowcaster flag.
  122. bool GetCastShadows() const { return castShadows_; }
  123. /// Return occluder flag.
  124. bool IsOccluder() const { return occluder_; }
  125. /// Return occludee flag.
  126. bool IsOccludee() const { return occludee_; }
  127. /// Regenerate patch geometry.
  128. void CreatePatchGeometry(TerrainPatch* patch);
  129. /// Update patch based on LOD and neighbor LOD.
  130. void UpdatePatchLod(TerrainPatch* patch);
  131. /// Set heightmap attribute.
  132. void SetHeightMapAttr(ResourceRef value);
  133. /// Set material attribute.
  134. void SetMaterialAttr(ResourceRef value);
  135. /// Set patch size attribute.
  136. void SetPatchSizeAttr(int value);
  137. /// Return heightmap attribute.
  138. ResourceRef GetHeightMapAttr() const;
  139. /// Return material attribute.
  140. ResourceRef GetMaterialAttr() const;
  141. private:
  142. /// Fully regenerate terrain geometry.
  143. void CreateGeometry();
  144. /// Filter the heightmap.
  145. void SmoothHeightMap();
  146. /// Create index data shared by all patches.
  147. void CreateIndexData();
  148. /// Return an uninterpolated terrain height value, clamping to edges.
  149. float GetRawHeight(int x, int z) const;
  150. /// Return interpolated height for a specific LOD level.
  151. float GetLodHeight(int x, int z, unsigned lodLevel) const;
  152. /// Get slope-based terrain normal at position.
  153. Vector3 GetRawNormal(int x, int z) const;
  154. /// Calculate LOD errors for a patch.
  155. void CalculateLodErrors(TerrainPatch* patch);
  156. /// Set neighbors for a patch.
  157. void SetNeighbors(TerrainPatch* patch);
  158. /// Set heightmap image and optionally recreate the geometry immediately. Return true if successful.
  159. bool SetHeightMapInternal(Image* image, bool recreateNow);
  160. /// Handle heightmap image reload finished.
  161. void HandleHeightMapReloadFinished(StringHash eventType, VariantMap& eventData);
  162. /// Shared index buffer.
  163. SharedPtr<IndexBuffer> indexBuffer_;
  164. /// Heightmap image.
  165. SharedPtr<Image> heightMap_;
  166. /// Height data.
  167. SharedArrayPtr<float> heightData_;
  168. /// Material.
  169. SharedPtr<Material> material_;
  170. /// Terrain patches.
  171. Vector<WeakPtr<TerrainPatch> > patches_;
  172. /// Draw ranges for different LODs and stitching combinations.
  173. PODVector<Pair<unsigned, unsigned> > drawRanges_;
  174. /// Vertex and height spacing.
  175. Vector3 spacing_;
  176. /// Origin of patches on the XZ-plane.
  177. Vector2 patchWorldOrigin_;
  178. /// Size of a patch on the XZ-plane.
  179. Vector2 patchWorldSize_;
  180. /// Terrain size in vertices.
  181. IntVector2 numVertices_;
  182. /// Terrain size in patches.
  183. IntVector2 numPatches_;
  184. /// Patch size, quads per side.
  185. int patchSize_;
  186. /// Number of terrain LOD levels.
  187. unsigned numLodLevels_;
  188. /// Smoothing enable flag.
  189. bool smoothing_;
  190. /// Visible flag.
  191. bool visible_;
  192. /// Shadowcaster flag.
  193. bool castShadows_;
  194. /// Occluder flag.
  195. bool occluder_;
  196. /// Occludee flag.
  197. bool occludee_;
  198. /// View mask.
  199. unsigned viewMask_;
  200. /// Light mask.
  201. unsigned lightMask_;
  202. /// Shadow mask.
  203. unsigned shadowMask_;
  204. /// Zone mask.
  205. unsigned zoneMask_;
  206. /// Draw distance.
  207. float drawDistance_;
  208. /// Shadow distance.
  209. float shadowDistance_;
  210. /// LOD bias.
  211. float lodBias_;
  212. /// Maximum lights.
  213. unsigned maxLights_;
  214. /// Terrain needs regeneration flag.
  215. bool recreateTerrain_;
  216. };
  217. }