Skip to content

Commit 6d31d3d

Browse files
committed
Fix mesh bounds on terrain tiles
1 parent 9fb7f8c commit 6d31d3d

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

Runtime/Mapbox/BaseModule/Unity/UnityTileTerrainContainer.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,44 @@ public void SetTerrainData(TerrainData terrainData, bool useShaderElevation, Til
4343
TerrainData.ElevationValuesUpdated += OnElevationValuesUpdated;
4444

4545
_unityMapTile.Material.SetFloat(_elevationMultiplierFieldNameID, useShaderElevation ? 1 : 0);
46+
FixMeshBounds(useShaderElevation);
47+
}
48+
49+
private void FixMeshBounds(bool useShaderElevation)
50+
{
51+
Mesh mesh = _unityMapTile.MeshFilter.mesh;
52+
if (mesh == null)
53+
{
54+
return;
55+
}
56+
57+
float elevation = useShaderElevation ? GetMaxExtent() : 0f;
58+
Vector3 newExtents = mesh.bounds.extents;
59+
newExtents.y = elevation;
60+
mesh.bounds = new Bounds(mesh.bounds.center, newExtents);
61+
}
62+
63+
private float GetMaxExtent()
64+
{
65+
if (TerrainData == null || TerrainData.ElevationValues == null)
66+
{
67+
return 0;
68+
}
69+
70+
float max = 0;
71+
float min = float.MaxValue;
72+
for (int i = 0; i < TerrainData.ElevationValues.Length; i++)
73+
{
74+
if (TerrainData.ElevationValues[i] > max)
75+
{
76+
max = TerrainData.ElevationValues[i];
77+
}
78+
if (TerrainData.ElevationValues[i] < min)
79+
{
80+
min = TerrainData.ElevationValues[i];
81+
}
82+
}
83+
return Mathf.Max(Mathf.Abs(min), max);
4684
}
4785

4886
public void OnTerrainUpdated()
@@ -130,4 +168,4 @@ public enum TileContainerState
130168
Temporary,
131169
Final
132170
}
133-
}
171+
}

0 commit comments

Comments
 (0)