From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Terrain/Heightmap.h | 199 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 Runtime/Terrain/Heightmap.h (limited to 'Runtime/Terrain/Heightmap.h') diff --git a/Runtime/Terrain/Heightmap.h b/Runtime/Terrain/Heightmap.h new file mode 100644 index 0000000..cc294f4 --- /dev/null +++ b/Runtime/Terrain/Heightmap.h @@ -0,0 +1,199 @@ +#pragma once +#include "Configuration/UnityConfigure.h" + +#if ENABLE_TERRAIN + +#include "Runtime/Math/Vector3.h" +#include "Runtime/Geometry/AABB.h" +#include "Runtime/Utilities/Utility.h" +#include "Runtime/Dynamics/Collider.h" +#include "Runtime/Utilities/LinkedList.h" +#include "Runtime/Dynamics/PhysicMaterial.h" + +class Mesh; +class NxHeightField; +class NxHeightFieldDesc; +struct SubMesh; +class TerrainData; +class TerrainCollider; +class TerrainRenderer; +class VBO; + +enum +{ + kDirectionLeft = 0, kDirectionRight = 1, kDirectionUp = 2, kDirectionDown = 3, + kDirectionLeftUp = 0, kDirectionRightUp = 1, kDirectionLeftDown = 2, kDirectionRightDown = 3, +}; + +enum +{ + kDirectionLeftFlag = (1< > TerrainColliderList; + TerrainColliderList& GetTerrainColliders () { return m_TerrainColliders; } + + ///@TODO: THIS IS STILL INCORRECT + Vector3f CalculateNormalSobel (int x, int y) const; + Vector3f CalculateNormalSobelRespectingNeighbors (int x, int y, const TerrainRenderer *renderer) const; + + AABB GetBounds () const; + + void AwakeFromLoad (); + +#if ENABLE_PHYSICS + PPtr GetPhysicMaterial() const { return m_DefaultPhysicMaterial; } + void SetPhysicMaterial(PPtr mat); +#endif + +private: + + TerrainData* m_TerrainData; + /// Raw heightmap + std::vector m_Heights; + + // Precomputed error of every patch + std::vector m_PrecomputedError; + // Precomputed min&max value of each terrain patch + std::vector m_MinMaxPatchHeights; + + TerrainColliderList m_TerrainColliders; + + int m_Width; + int m_Height; + int m_Levels; + Vector3f m_Scale; + + int GetPatchCountX (int level) const { return 1 << (m_Levels - level); } + int GetPatchCountY (int level) const { return 1 << (m_Levels - level); } + + inline float GetPatchHeight (float* data, int x, int y) const + { + return data[y + x * kPatchSize]; + } + + /// Calculates the index of the patch given it's level and x, y index + int GetPatchIndex (int x, int y, int level) const; + + float InterpolatePatchHeight (float* data, float fx, float fy) const; + + float ComputeMaximumHeightError (int xPatch, int yPatch, int level) const; + + void RecalculateMinMaxHeight (int xPatch, int yPatch, int mipLevel); + // Precompute error only on a part of the heightmap + // if forceHighestLod is enabled we simply set the error to infinity + // This casues the heightmap to be rendered at full res (Used while editing) + void PrecomputeError (int minX, int minY, int width, int height, bool forceHighestLod); + +#if ENABLE_PHYSICS + PPtr m_DefaultPhysicMaterial; + NxHeightField* m_NxHeightField; + + void CleanupNx (); + void CreateNx (); + void UpdateNx (); + void BuildDesc (NxHeightFieldDesc& desc); + + void RecreateShapes (); +#endif +}; + +template +void Heightmap::Transfer (TransferFunc& transfer) +{ + TRANSFER(m_Heights); + transfer.Align(); + TRANSFER(m_PrecomputedError); + TRANSFER(m_MinMaxPatchHeights); +#if ENABLE_PHYSICS + TRANSFER(m_DefaultPhysicMaterial); +#endif + TRANSFER(m_Width); + TRANSFER(m_Height); + TRANSFER(m_Levels); + TRANSFER(m_Scale); +} + +#endif // ENABLE_TERRAIN -- cgit v1.1-26-g67d0