Skip to content

Commit de3380d

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

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

Runtime/Mapbox/BaseModule/Unity/UnityTileTerrainContainer.cs

+19-19
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;
@@ -54,33 +53,34 @@ private void FixMeshBounds(bool useShaderElevation)
5453
return;
5554
}
5655

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);
56+
if (useShaderElevation)
57+
{
58+
mesh.RecalculateBounds();
59+
mesh.bounds = GetBoundsAdjustedForElevation();
60+
}
6161
}
6262

63-
private float GetMaxExtent()
63+
private Bounds GetBoundsAdjustedForElevation()
6464
{
65+
Mesh mesh = _unityMapTile.MeshFilter.mesh;
6566
if (TerrainData == null || TerrainData.ElevationValues == null)
6667
{
67-
return 0;
68+
return mesh.bounds;
6869
}
6970

70-
float max = 0;
71-
float min = float.MaxValue;
72-
for (int i = 0; i < TerrainData.ElevationValues.Length; i++)
71+
float maxY = float.MinValue;
72+
float minY = float.MaxValue;
73+
foreach (float t in TerrainData.ElevationValues)
7374
{
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-
}
75+
float elevationScaled = t * _unityMapTile.TileScale;
76+
maxY = Mathf.Max(maxY, elevationScaled);
77+
minY = Mathf.Min(minY, elevationScaled);
8278
}
83-
return Mathf.Max(Mathf.Abs(min), max);
79+
Vector3 center = mesh.bounds.center;
80+
center.y = (maxY + minY) / 2;
81+
Vector3 size = mesh.bounds.size;
82+
size.y = maxY - minY;
83+
return new Bounds(center, size);
8484
}
8585

8686
public void OnTerrainUpdated()

0 commit comments

Comments
 (0)