mapbox-unity-sdk icon indicating copy to clipboard operation
mapbox-unity-sdk copied to clipboard

Cannot disable the Terrain layer

Open Luunynliny opened this issue 4 years ago • 2 comments

I'm working on a Unity 2019.4.18f1 project with the Mapbox SDK v2.1.1, adn I would to make an elevation map with 3D buildings where I will be able to hide the map terrain and just display the building.

While playing with the AbstractMap inspector in player mode, I noticed that turning the TERRAIN/ Data Source from Mapbox Terrain to None hidden the terrain, exactly what I was looking for.

Now in order to play with this more easily I tried to reproduced this in code, but I cannot achieve this.

The first thing I tried was this: Terrain.SetLayerSource(ElevationSourceType.None); but I have this warning: Invalid style - trying to set None as default style! UnityEngine.Debug:LogWarning(Object) Mapbox.Unity.Map.TerrainLayer:SetLayerSource(ElevationSourceType) (at Assets/Mabox-SDK/Mapbox/Unity/SourceLayers/TerrainLayer.cs:224) and the terrain is still visible.

I also tried a second way after reading TerrainLayer_SetLayerSource: Terrain.SetLayerSource("None"); I got no errors but it remove terrain elevation instead of hiding the terrain.

Could it be possible that there is a bug with the SetLayerSource() function or am I missing something?

Luunynliny avatar Feb 12 '21 10:02 Luunynliny

Hey @Eccsx! I would to make an elevation map with 3D buildings That's an interesting case if I understand it correct. So you want to have 3d buildings on elevated terrain but you want terrain to be invisible?

If you disable the terrain layer itself, sdk won't fetch elevation data at all. I think creating a regular map and disabling tile mesh renderer might be an option. Or a custom shader for tiles/terrain. I mean there are a few options here but can't do that out of box I guess.

brnkhy avatar Mar 05 '21 21:03 brnkhy

@Eccsx I also tried to use Terrain.SetLayerSource(ElevationSourceType.None) to toggle terrain between none to default. It didn't work at first. Trying Terrain.SetLayerSource("None") switches the terrain to custom, instead of none

Here is what work for me: I locate the method Terrain.SetLayerSource in visual studio code. It's in the TerrainLayer.cs file The original code looks like this

public virtual void SetLayerSource(ElevationSourceType terrainSource = ElevationSourceType.MapboxTerrain)
{
	if (terrainSource != ElevationSourceType.Custom && terrainSource != ElevationSourceType.None)
	{
		_layerProperty.sourceType = terrainSource;
		_layerProperty.sourceOptions.layerSource = MapboxDefaultElevation.GetParameters(terrainSource);
		_layerProperty.HasChanged = true;
	}
	else
	{
		Debug.LogWarning("Invalid style - trying to set " + terrainSource.ToString() + " as default style!");
	}
}

This if statement will always go into the else section even if you pass in ElevationSourceType.None. It will never satisfy the premise. The terrainSource just can't be both custom and none at the same time.

Long story short and easy fix for me was :

change && to ||, making it either or.

Hope it helps

Elenalalala avatar Aug 19 '22 04:08 Elenalalala

thanks for the fix @Elenalalala ! 🙇

brnkhy avatar Oct 17 '22 06:10 brnkhy