Skip to content

Commit

Permalink
[location-component] Improve model scaling using expression. (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengdev committed Jan 25, 2021
1 parent a53787d commit 5bcfe47
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class LocationComponentActivity : AppCompatActivity() {
mapView.getLocationComponentPlugin().let {
if (it.locationPuck == null) {
it.locationPuck = ThreeDLocationPuck(
modelUri = "asset://race_car_model.gltf"
modelUri = "asset://race_car_model.gltf",
modelScale = listOf(0.1f, 0.1f, 0.1f)
)
} else {
it.locationPuck = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,6 @@ class LocationComponentPluginImpl : LocationComponentPlugin, LocationComponentSe
locationProvider?.unRegisterLocationConsumer(this)
}

/**
* Called whenever camera position changes.
*/
override fun onCameraMove(
lat: Double,
lon: Double,
zoom: Double,
pitch: Double,
bearing: Double,
padding: Array<Double>?,
anchor: Pair<Double, Double>?
) {
locationPuckManager?.updateCurrentZoomLevel(zoom)
}

/**
* Bind the ViewPlugin with current map context. This will create a View that
* will be added to the MapView.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ internal class LocationIndicatorLayerRenderer(
setLayerLocation(latLng)
}

override fun setZoomLevel(zoomLevel: Double) {
// not supported
}

override fun setBearing(bearing: Float) {
setLayerBearing(bearing.toDouble())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ internal interface LocationLayerRenderer {

fun setLatLng(latLng: Point)

fun setZoomLevel(zoomLevel: Double)

fun setBearing(bearing: Float)

fun setAccuracyRadius(accuracy: Float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ internal interface LocationPuckManager {
fun updateCurrentPosition(point: Point)

fun updateCurrentBearing(bearing: Float)

fun updateCurrentZoomLevel(zoomLevel: Double)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.mapbox.maps.plugin.ThreeDLocationPuck
import com.mapbox.maps.plugin.TwoDLocationPuck
import com.mapbox.maps.plugin.delegates.MapDelegateProvider
import com.mapbox.maps.plugin.locationcomponent.generated.LocationComponentSettings
import kotlin.math.pow

internal class LocationPuckManagerImpl(
override var settings: LocationComponentSettings,
Expand All @@ -30,8 +31,6 @@ internal class LocationPuckManagerImpl(

private var lastBearing: Float = 0.0f

private var lastZoomLevel: Double = delegateProvider.mapCameraDelegate.getZoom()

private fun getLocationPuck(locationSettings: LocationComponentSettings): LocationPuck {
return locationSettings.locationPuck ?: presetProvider.getPresetPuck(settings.presetPuckStyle)
}
Expand All @@ -58,7 +57,6 @@ internal class LocationPuckManagerImpl(
updateCurrentPosition(it)
}
updateCurrentBearing(lastBearing)
updateCurrentZoomLevel(lastZoomLevel)
setLocationsStale(lastStaleState)
if (settings.enabled) {
show()
Expand Down Expand Up @@ -113,11 +111,6 @@ internal class LocationPuckManagerImpl(
locationLayerRenderer.setBearing(bearing)
}

override fun updateCurrentZoomLevel(zoomLevel: Double) {
lastZoomLevel = zoomLevel
locationLayerRenderer.setZoomLevel(zoomLevel)
}

private fun prepareLocationIndicatorLayerBitmaps(puck: TwoDLocationPuck) {
val topBitmap = puck.topImage?.let { bitmapProvider.generateBitmap(it, puck.topTintColor) }
val topStaleBitmap =
Expand All @@ -142,15 +135,55 @@ internal class LocationPuckManagerImpl(
}

private fun styleScaling(settings: LocationComponentSettings) {
val scaleExpression = arrayListOf(
Value("interpolate"),
Value(arrayListOf(Value("linear"))),
Value(arrayListOf(Value("zoom"))),
Value(delegateProvider.mapTransformDelegate.getBounds().minZoom!!),
Value(settings.minZoomIconScale.toDouble()),
Value(delegateProvider.mapTransformDelegate.getBounds().maxZoom!!),
Value(settings.maxZoomIconScale.toDouble())
)
val puck = getLocationPuck(settings)
val minZoom = delegateProvider.mapTransformDelegate.getBounds().minZoom ?: 0.0
val maxZoom = delegateProvider.mapTransformDelegate.getBounds().maxZoom ?: 19.0
val scaleExpression = when (puck) {
is TwoDLocationPuck -> {
arrayListOf(
Value("interpolate"),
Value(arrayListOf(Value("linear"))),
Value(arrayListOf(Value("zoom"))),
Value(minZoom),
Value(settings.minZoomIconScale.toDouble()),
Value(maxZoom),
Value(settings.maxZoomIconScale.toDouble())
)
}
is ThreeDLocationPuck -> {
arrayListOf(
Value("interpolate"),
Value(arrayListOf(Value("exponential"), Value(0.5))),
Value(arrayListOf(Value("zoom"))),
Value(minZoom),
Value(
arrayListOf(
Value("literal"),
Value(
arrayListOf(
Value(2.0.pow(maxZoom - minZoom) * puck.modelScale[0].toDouble()),
Value(2.0.pow(maxZoom - minZoom) * puck.modelScale[1].toDouble()),
Value(2.0.pow(maxZoom - minZoom) * puck.modelScale[2].toDouble())
)
)
)
),
Value(maxZoom),
Value(
arrayListOf(
Value("literal"),
Value(
arrayListOf(
Value(puck.modelScale[0].toDouble()),
Value(puck.modelScale[1].toDouble()),
Value(puck.modelScale[2].toDouble())
)
)
)
)
)
}
}
locationLayerRenderer.styleScaling(scaleExpression)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.mapbox.geojson.Point
import com.mapbox.maps.StyleManagerInterface
import com.mapbox.maps.plugin.ThreeDLocationPuck
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE
import kotlin.math.pow

internal class ModelLayerRenderer(
layerSourceProvider: LayerSourceProvider,
Expand Down Expand Up @@ -50,11 +49,6 @@ internal class ModelLayerRenderer(
setLayerLocation(latLng)
}

override fun setZoomLevel(zoomLevel: Double) {
val scale = 2.0.pow(MAX_ZOOM_LEVEL - zoomLevel)
modelLayer.modelScale(locationModelLayerOptions.modelScale.map { it * scale })
}

override fun setBearing(bearing: Float) {
setLayerBearing(bearing.toDouble())
}
Expand All @@ -63,6 +57,7 @@ internal class ModelLayerRenderer(
}

override fun styleScaling(scaleExpression: List<Value>) {
modelLayer.modelScaleExpression(scaleExpression)
}

override fun setLocationStale(isStale: Boolean) {
Expand Down Expand Up @@ -110,8 +105,4 @@ internal class ModelLayerRenderer(

override fun clearBitmaps() {
}

companion object {
internal const val MAX_ZOOM_LEVEL = 19
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ internal class ModelLayerWrapper(

fun modelScale(scale: List<Double>) = updateProperty("model-scale", Value(scale.map { Value(it) }))

fun modelScaleExpression(scaleExpression: List<Value>) = updateProperty("model-scale", Value(scaleExpression))

fun modelRotation(rotation: List<Double>) = updateProperty("model-rotation", Value(rotation.map { Value(it) }))
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.mapbox.maps.plugin.locationcomponent

import com.mapbox.maps.plugin.ContextBinder
import com.mapbox.maps.plugin.LifecyclePlugin
import com.mapbox.maps.plugin.MapCameraPlugin
import com.mapbox.maps.plugin.MapStyleObserverPlugin
import com.mapbox.maps.plugin.*
import com.mapbox.maps.plugin.locationcomponent.generated.LocationComponentSettingsInterface

/**
* Define the interfaces for the Location plugin.
*/
interface LocationComponentPlugin :
MapPlugin,
MapStyleObserverPlugin,
LifecyclePlugin,
MapCameraPlugin,
ContextBinder,
LocationConsumer,
LocationComponentSettingsInterface {
Expand Down

0 comments on commit 5bcfe47

Please sign in to comment.