Skip to content

Commit 3406261

Browse files
committed
Bugfix: used extent instead of size in bounds constructor
1 parent 6d31d3d commit 3406261

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

Runtime/Mapbox/BaseModule/Unity/UnityTileTerrainContainer.cs

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using Mapbox.BaseModule.Data.DataFetchers;
32
using Mapbox.BaseModule.Data.Tiles;
43
using UnityEngine;
54
using TerrainData = Mapbox.BaseModule.Data.DataFetchers.TerrainData;
@@ -49,38 +48,34 @@ public void SetTerrainData(TerrainData terrainData, bool useShaderElevation, Til
4948
private void FixMeshBounds(bool useShaderElevation)
5049
{
5150
Mesh mesh = _unityMapTile.MeshFilter.mesh;
52-
if (mesh == null)
51+
if (mesh != null && useShaderElevation)
5352
{
54-
return;
53+
mesh.RecalculateBounds();
54+
mesh.bounds = GetBoundsAdjustedForElevation();
5555
}
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);
6156
}
6257

63-
private float GetMaxExtent()
58+
private Bounds GetBoundsAdjustedForElevation()
6459
{
60+
Mesh mesh = _unityMapTile.MeshFilter.mesh;
6561
if (TerrainData == null || TerrainData.ElevationValues == null)
6662
{
67-
return 0;
63+
return mesh.bounds;
6864
}
6965

70-
float max = 0;
71-
float min = float.MaxValue;
72-
for (int i = 0; i < TerrainData.ElevationValues.Length; i++)
66+
float maxY = float.MinValue;
67+
float minY = float.MaxValue;
68+
foreach (float t in TerrainData.ElevationValues)
7369
{
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-
}
70+
float elevationScaled = t * _unityMapTile.TileScale;
71+
maxY = Mathf.Max(maxY, elevationScaled);
72+
minY = Mathf.Min(minY, elevationScaled);
8273
}
83-
return Mathf.Max(Mathf.Abs(min), max);
74+
Vector3 center = mesh.bounds.center;
75+
center.y = (maxY + minY) / 2;
76+
Vector3 size = mesh.bounds.size;
77+
size.y = maxY - minY;
78+
return new Bounds(center, size);
8479
}
8580

8681
public void OnTerrainUpdated()

0 commit comments

Comments
 (0)