Skip to content
Merged
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
1 change: 1 addition & 0 deletions documentation/docs/05-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Add two new modules, KdTreeCollection and AddToCollection Gameobject modifier.
- Add Collider option for vector features.
- Add scale factor for extrusion value derived from feature property.
- Add camera script with zoom & pan support for TabletopAR scene.

##### Bug Fixes
- Change `Style Name` to `Data Source`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class QuadTreeCameraMovement : MonoBehaviour
private Vector3 _mousePositionPrevious;
private bool _shouldDrag;
private bool _isInitialized = false;
private Plane _groundPlane = new Plane(Vector3.up, 0);

void Awake()
{
Expand Down Expand Up @@ -151,6 +152,19 @@ void PanMapUsingTouchOrMouse()

void UseMeterConversion()
{
if (Input.GetMouseButtonUp(1))
{
var mousePosScreen = Input.mousePosition;
//assign distance of camera to ground plane to z, otherwise ScreenToWorldPoint() will always return the position of the camera
//http://answers.unity3d.com/answers/599100/view.html
mousePosScreen.z = _referenceCamera.transform.localPosition.y;
var pos = _referenceCamera.ScreenToWorldPoint(mousePosScreen);

var latlongDelta = _mapManager.WorldToGeoPosition(pos);
Debug.Log("Latitude: " + latlongDelta.x + " Longitude: " + latlongDelta.y);
//_mapManager.UpdateMap(latlongDelta, _mapManager.Zoom);
}

if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject())
{
var mousePosScreen = Input.mousePosition;
Expand Down Expand Up @@ -268,5 +282,12 @@ void UseDegreeConversion()
}
}
}

private Vector3 getGroundPlaneHitPoint(Ray ray)
{
float distance;
if (!_groundPlane.Raycast(ray, out distance)) { return Vector3.zero; }
return ray.GetPoint(distance);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,12 @@
using System.Linq;
using UnityEngine;
using Mapbox.Map;
using System.Collections.Generic;
using Mapbox.Utils;
using Mapbox.Unity.Utilities;

public class RangeAroundTransformTileProvider : AbstractTileProvider
{
//[SerializeField]
//private Transform _targetTransform;

//[SerializeField]
//private int _visibleBuffer;

//[SerializeField]
//private int _disposeBuffer;

RangeAroundTransformTileProviderOptions _rangeTileProviderOptions;

private bool _initialized = false;
Expand Down Expand Up @@ -44,19 +36,35 @@ private void Update()
{
if (!_initialized) return;

_currentTile = TileCover.CoordinateToTileId(_rangeTileProviderOptions.targetTransform.localPosition.GetGeoPosition(_map.CenterMercator, _map.WorldRelativeScale), _map.AbsoluteZoom);
var activeTiles = _activeTiles.Keys.ToList();

List<UnwrappedTileId> tilesToRequest = new List<UnwrappedTileId>();
_currentTile = TileCover.CoordinateToTileId(_map.WorldToGeoPosition(_rangeTileProviderOptions.targetTransform.localPosition), _map.AbsoluteZoom);

if (!_currentTile.Equals(_cachedTile))
{
for (int x = _currentTile.X - _rangeTileProviderOptions.visibleBuffer; x <= (_currentTile.X + _rangeTileProviderOptions.visibleBuffer); x++)
{
for (int y = _currentTile.Y - _rangeTileProviderOptions.visibleBuffer; y <= (_currentTile.Y + _rangeTileProviderOptions.visibleBuffer); y++)
{
AddTile(new UnwrappedTileId(_map.AbsoluteZoom, x, y));
tilesToRequest.Add(new UnwrappedTileId(_map.AbsoluteZoom, x, y));
}
}
_cachedTile = _currentTile;
Cleanup(_currentTile);

var finalTilesNeeded = tilesToRequest.Except(activeTiles);

foreach (var tile in activeTiles)
{
// Reposition tiles in case we panned.
RepositionTile(tile);
}

foreach (var tile in finalTilesNeeded)
{
AddTile(tile);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
namespace Mapbox.Examples
{
using Mapbox.Unity.Map;
using Mapbox.Unity.Utilities;
using Mapbox.Utils;
using UnityEngine;
using UnityEngine.EventSystems;
using System;

public class ARTableTopCameraMovement : MonoBehaviour
{
[SerializeField]
[Range(.1f, 10f)]
public float _panSpeed = 1.0f;

[SerializeField]
float _zoomSpeed = 0.25f;

[SerializeField]
public Camera _referenceCamera;

[SerializeField]
AbstractMap _mapManager;

private Vector3 _origin;
private Vector3 _mousePosition;
private Vector3 _mousePositionPrevious;
private bool _shouldDrag;
private bool _isInitialized = false;

void Awake()
{
if (null == _referenceCamera)
{
_referenceCamera = GetComponent<Camera>();
if (null == _referenceCamera) { Debug.LogErrorFormat("{0}: reference camera not set", this.GetType().Name); }
}
_mapManager.OnInitialized += () =>
{
_isInitialized = true;
};
}


private void LateUpdate()
{
if (!_isInitialized) { return; }

if (Input.touchSupported && Input.touchCount > 0)
{
HandleTouch();
}
else
{
HandleMouseAndKeyBoard();
}
}

void HandleMouseAndKeyBoard()
{
// zoom
float scrollDelta = 0.0f;
scrollDelta = Input.GetAxis("Mouse ScrollWheel");
ZoomMapUsingTouchOrMouse(scrollDelta);

//pan mouse
PanMapUsingTouchOrMouse();
}

void HandleTouch()
{
float zoomFactor = 0.0f;
//pinch to zoom.
switch (Input.touchCount)
{
case 1:
{
PanMapUsingTouchOrMouse();
}
break;
case 2:
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);

// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

// Find the difference in the distances between each frame.
zoomFactor = 0.01f * (touchDeltaMag - prevTouchDeltaMag);
}
ZoomMapUsingTouchOrMouse(zoomFactor);
break;
default:
break;
}
}

void ZoomMapUsingTouchOrMouse(float zoomFactor)
{
var zoom = Mathf.Max(0.0f, Mathf.Min(_mapManager.Zoom + zoomFactor * _zoomSpeed, 21.0f));

_mapManager.UpdateMap(_mapManager.CenterLatitudeLongitude, zoom);
}

void PanMapUsingKeyBoard(float xMove, float zMove)
{
if (Math.Abs(xMove) > 0.0f || Math.Abs(zMove) > 0.0f)
{
// Get the number of degrees in a tile at the current zoom level.
// Divide it by the tile width in pixels ( 256 in our case)
// to get degrees represented by each pixel.
// Keyboard offset is in pixels, therefore multiply the factor with the offset to move the center.
float factor = (_panSpeed / 100) * (Conversions.GetTileScaleInDegrees((float)_mapManager.CenterLatitudeLongitude.x, _mapManager.AbsoluteZoom));
var latitudeLongitude = new Vector2d(_mapManager.CenterLatitudeLongitude.x + zMove * factor * 2.0f, _mapManager.CenterLatitudeLongitude.y + xMove * factor * 4.0f);
_mapManager.UpdateMap(latitudeLongitude, _mapManager.Zoom);
}
}

void PanMapUsingTouchOrMouse()
{
if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject())
{
var mousePosScreen = Input.mousePosition;
_mousePosition = mousePosScreen;

if (_shouldDrag == false)
{
_shouldDrag = true;
_origin = mousePosScreen;
}
}
else
{
_shouldDrag = false;
}

if (_shouldDrag == true)
{

var changeFromPreviousPosition = _mousePositionPrevious - _mousePosition;
if (Mathf.Abs(changeFromPreviousPosition.x) > 0.0f || Mathf.Abs(changeFromPreviousPosition.y) > 0.0f)
{
_mousePositionPrevious = _mousePosition;

var offsetDelta = _origin - _mousePosition;
var offset = new Vector3(offsetDelta.x, 0f, offsetDelta.y);
offset = Camera.main.transform.rotation * offset;

if (Mathf.Abs(offset.x) > 0.0f || Mathf.Abs(offset.z) > 0.0f)
{
PanMapUsingKeyBoard(offset.x, offset.z);
}
_origin = _mousePosition;
}
else
{
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
_mousePositionPrevious = _mousePosition;
_origin = _mousePosition;
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading