Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions Runtime/Mapbox/BaseModule/Unity/UnityTileTerrainContainer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Mapbox.BaseModule.Data.DataFetchers;
using Mapbox.BaseModule.Data.Tiles;
using UnityEngine;
using TerrainData = Mapbox.BaseModule.Data.DataFetchers.TerrainData;
Expand Down Expand Up @@ -43,6 +42,40 @@ public void SetTerrainData(TerrainData terrainData, bool useShaderElevation, Til
TerrainData.ElevationValuesUpdated += OnElevationValuesUpdated;

_unityMapTile.Material.SetFloat(_elevationMultiplierFieldNameID, useShaderElevation ? 1 : 0);
FixMeshBounds(useShaderElevation);
}

private void FixMeshBounds(bool useShaderElevation)
{
Mesh mesh = _unityMapTile.MeshFilter.mesh;
if (mesh != null && useShaderElevation)
{
mesh.RecalculateBounds();
mesh.bounds = GetBoundsAdjustedForElevation();
}
}

private Bounds GetBoundsAdjustedForElevation()
{
Mesh mesh = _unityMapTile.MeshFilter.mesh;
if (TerrainData == null || TerrainData.ElevationValues == null)
{
return mesh.bounds;
}

float maxY = float.MinValue;
float minY = float.MaxValue;
foreach (float t in TerrainData.ElevationValues)
{
float elevationScaled = t * _unityMapTile.TileScale;
maxY = Mathf.Max(maxY, elevationScaled);
minY = Mathf.Min(minY, elevationScaled);
}
Vector3 center = mesh.bounds.center;
center.y = (maxY + minY) / 2;
Vector3 size = mesh.bounds.size;
size.y = maxY - minY;
return new Bounds(center, size);
}

public void OnTerrainUpdated()
Expand Down Expand Up @@ -130,4 +163,4 @@ public enum TileContainerState
Temporary,
Final
}
}
}
3 changes: 2 additions & 1 deletion Runtime/Mapbox/VectorModule/VectorLayerVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ protected List<GameObject> GameObjectModifications(CanonicalTileId canonicalTile
if(!_results.ContainsKey(canonicalTileId))
_results.Add(canonicalTileId, new List<VectorEntity>());
_results[canonicalTileId].Add(entity);
entity.Mesh.RecalculateBounds();
OnVectorMeshCreated(entity.GameObject);
}
}
Expand Down Expand Up @@ -312,4 +313,4 @@ protected MeshData CombineMeshData(HashSet<MeshData> meshDataList)
public Action<GameObject> OnVectorMeshCreated = list => { };
public Action<GameObject> OnVectorMeshDestroyed = go => { };
}
}
}