Commit c4e78290 authored by Rory's avatar Rory
Browse files

Add Mapzen map visualisation

Based off MapzenGo: https://github.com/brnkhy/MapzenGo

Customized `DynamicTileManager` script to be properly centered on
player in a way that is easier (for me) to understand.
parent 20bc5cc8
fileFormatVersion: 2
guid: 762ac72a3872cd54c96ca6010561c679
folderAsset: yes
timeCreated: 1488880890
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: d12b1ac9a1db1f549b867cbf9ce73104
guid: 6478355bf2d9dcd4ab6339ddc03a45bf
folderAsset: yes
timeCreated: 1484721597
timeCreated: 1468788713
licenseType: Free
DefaultImporter:
userData:
......
using UnityEngine;
using System.Collections;
public class Billboard : MonoBehaviour
{
private Camera _camera;
// Use this for initialization
void Start()
{
_camera = Camera.main;
}
// Update is called once per frame
void Update()
{
transform.LookAt(_camera.transform);
}
}
fileFormatVersion: 2
guid: dbb5141d19cca9f49a95cded04c06fd6
timeCreated: 1474993816
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 5262c1e5ff24e41438d1b7081f69573d
folderAsset: yes
timeCreated: 1468942295
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
#pragma strict
public class DoubleClickDetector {
private var numberOfClicks : int = 0;
private var timer : float = 0.0f;
function IsDoubleClick() {
var isDoubleClick = numberOfClicks == 2;
if (isDoubleClick)
numberOfClicks = 0;
return isDoubleClick;
}
function Update () {
timer += Time.deltaTime;
if (timer > 0.3f) {
numberOfClicks = 0;
}
if (Input.GetMouseButtonDown(0)) {
numberOfClicks++;
timer = 0.0f;
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: ba175d5c4ca8adc498ceecddf90d5cf1
timeCreated: 1468943260
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
#pragma strict
import UnityEngine.UI;
// -------------------------- Configuration --------------------------
public var terrain : Terrain;
public var panSpeed : float = 15.0f;
public var zoomSpeed : float = 100.0f;
public var rotationSpeed : float = 50.0f;
public var mousePanMultiplier : float = 0.1f;
public var mouseRotationMultiplier : float = 0.2f;
public var mouseZoomMultiplier : float = 5.0f;
public var minZoomDistance : float = 20.0f;
public var maxZoomDistance : float = 200.0f;
public var smoothingFactor : float = 0.1f;
public var goToSpeed : float = 0.1f;
public var useKeyboardInput : boolean = true;
public var useMouseInput : boolean = true;
public var adaptToTerrainHeight : boolean = true;
public var increaseSpeedWhenZoomedOut : boolean = true;
public var correctZoomingOutRatio : boolean = true;
public var smoothing : boolean = true;
public var allowDoubleClickMovement : boolean = false;
public var allowScreenEdgeMovement : boolean = true;
public var screenEdgeSize : int = 10;
public var screenEdgeSpeed : float = 1.0f;
public var objectToFollow : GameObject;
public var cameraTarget : Vector3;
// -------------------------- Private Attributes --------------------------
private var currentCameraDistance : float;
private var lastMousePos : Vector3;
private var lastPanSpeed : Vector3 = Vector3.zero;
private var goingToCameraTarget : Vector3 = Vector3.zero;
private var doingAutoMovement : boolean = false;
private var doubleClickDetector : DoubleClickDetector;
// -------------------------- Public Methods --------------------------
function Start () {
currentCameraDistance = minZoomDistance + ((maxZoomDistance - minZoomDistance) / 2.0f);
lastMousePos = Vector3.zero;
doubleClickDetector = new DoubleClickDetector();
}
function Update () {
if (allowDoubleClickMovement) {
doubleClickDetector.Update();
UpdateDoubleClick();
}
UpdatePanning();
UpdateRotation();
UpdateZooming();
UpdatePosition();
UpdateAutoMovement();
lastMousePos = Input.mousePosition;
}
function GoTo(position : Vector3) {
doingAutoMovement = true;
goingToCameraTarget = position;
objectToFollow = null;
}
function Follow(gameObjectToFollow : GameObject) {
objectToFollow = gameObjectToFollow;
}
// -------------------------- Private Methods --------------------------
private function UpdateDoubleClick() {
if (doubleClickDetector.IsDoubleClick() && terrain && terrain.GetComponent(Collider)) {
var cameraTargetY = cameraTarget.y;
var collider : Collider = terrain.GetComponent(Collider);
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
var pos : Vector3;
if (collider.Raycast(ray, hit, Mathf.Infinity)) {
pos = hit.point;
pos.y = cameraTargetY;
GoTo(pos);
}
}
}
private function UpdatePanning() {
var moveVector : Vector3 = new Vector3(0, 0, 0);
if (useKeyboardInput) {
if (Input.GetKey(KeyCode.A)) {
moveVector += Vector3(-1, 0, 0);
}
if (Input.GetKey(KeyCode.S)) {
moveVector += Vector3(0, 0, -1);
}
if (Input.GetKey(KeyCode.D)) {
moveVector += Vector3(1, 0, 0);
}
if (Input.GetKey(KeyCode.W)) {
moveVector += Vector3(0, 0, 1);
}
}
if (allowScreenEdgeMovement) {
if (Input.mousePosition.x < screenEdgeSize) {
moveVector.x -= screenEdgeSpeed;
} else if (Input.mousePosition.x > Screen.width - screenEdgeSize) {
moveVector.x += screenEdgeSpeed;
}
if (Input.mousePosition.y < screenEdgeSize) {
moveVector.z -= screenEdgeSpeed;
} else if (Input.mousePosition.y > Screen.height - screenEdgeSize) {
moveVector.z += screenEdgeSpeed;
}
}
if (useMouseInput) {
if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftShift)) {
var deltaMousePos : Vector3 = (Input.mousePosition - lastMousePos);
moveVector += Vector3(-deltaMousePos.x, 0, -deltaMousePos.y) * mousePanMultiplier;
}
}
if (moveVector != Vector3.zero) {
objectToFollow = null;
doingAutoMovement = false;
}
var effectivePanSpeed = moveVector;
if (smoothing) {
effectivePanSpeed = Vector3.Lerp(lastPanSpeed, moveVector, smoothingFactor);
lastPanSpeed = effectivePanSpeed;
}
var oldRotation : float = transform.localEulerAngles.x;
transform.localEulerAngles.x = 0.0f;
var panMultiplier : float = increaseSpeedWhenZoomedOut ? (Mathf.Sqrt(currentCameraDistance)) : 1.0f;
cameraTarget = cameraTarget + transform.TransformDirection(effectivePanSpeed) * panSpeed * panMultiplier * Time.deltaTime;
transform.localEulerAngles.x = oldRotation;
}
private function UpdateRotation() {
var deltaAngleH : float = 0.0f;
var deltaAngleV : float = 0.0f;
if (useKeyboardInput) {
if (Input.GetKey(KeyCode.Q)) {
deltaAngleH = 1.0f;
}
if (Input.GetKey(KeyCode.E)) {
deltaAngleH = -1.0f;
}
}
if (useMouseInput) {
if (Input.GetMouseButton(2) && !Input.GetKey(KeyCode.LeftShift)) {
var deltaMousePos : Vector3 = (Input.mousePosition - lastMousePos);
deltaAngleH += deltaMousePos.x * mouseRotationMultiplier;
deltaAngleV -= deltaMousePos.y * mouseRotationMultiplier;
}
}
transform.localEulerAngles.y = transform.localEulerAngles.y + deltaAngleH * Time.deltaTime * rotationSpeed;
transform.localEulerAngles.x = Mathf.Min(80.0f, Mathf.Max(5.0f, transform.localEulerAngles.x + deltaAngleV * Time.deltaTime * rotationSpeed));
}
private function UpdateZooming() {
var deltaZoom : float = 0.0f;
if (useKeyboardInput) {
if (Input.GetKey(KeyCode.F)) {
deltaZoom = 1.0f;
}
if (Input.GetKey(KeyCode.R)) {
deltaZoom = -1.0f;
}
}
if (useMouseInput) {
var scroll : float = Input.GetAxis("Mouse ScrollWheel");
deltaZoom -= scroll * mouseZoomMultiplier;
}
var zoomedOutRatio : float = correctZoomingOutRatio ? (currentCameraDistance - minZoomDistance) / (maxZoomDistance - minZoomDistance) : 0.0f;
currentCameraDistance = Mathf.Max(minZoomDistance, Mathf.Min(maxZoomDistance, currentCameraDistance + deltaZoom * Time.deltaTime * zoomSpeed * (zoomedOutRatio * 2.0f + 1.0f)));
}
private function UpdatePosition() {
if (objectToFollow != null) {
cameraTarget = Vector3.Lerp(cameraTarget, objectToFollow.transform.position, goToSpeed);
}
transform.position = cameraTarget;
transform.Translate(Vector3.back * currentCameraDistance);
if (adaptToTerrainHeight && terrain != null) {
transform.position.y = Mathf.Max(terrain.SampleHeight(transform.position) + terrain.transform.position.y + 10.0f, transform.position.y);
}
}
private function UpdateAutoMovement() {
if (doingAutoMovement) {
cameraTarget = Vector3.Lerp(cameraTarget, goingToCameraTarget, goToSpeed);
if (Vector3.Distance(goingToCameraTarget, cameraTarget) < 1.0f) {
doingAutoMovement = false;
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: b1d2b60cd87ac2b45b37de3a0b5136d0
timeCreated: 1468943255
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
fileFormatVersion: 2
guid: aa03e0e70ae0d6347ab2aba679080877
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapzenGo.Helpers.VectorD;
using UnityEngine;
namespace MapzenGo.Helpers
{
/// <summary>
/// Mapzen based geolocation helper functions.
/// </summary>
/// <remarks>
/// Written by Baran Kahyaoglu for MapzenGo - <see href="http://barankahyaoglu.com"/>, <see href="https://github.com/brnkhy/MapzenGo"/>.
/// <para>
/// Edited by Rory Juranovich for Geolocation Unity Plugin - <see href="http://deovolente.games/"/>.
/// </para>
/// <seealso href="http://stackoverflow.com/questions/12896139/geographic-coordinates-converter"/>
/// </remarks>
public static class GM
{
public const int TileSize = 256;
public const int EarthRadius = 6378137;
public const double InitialResolution = 2 * Math.PI * EarthRadius / TileSize;
public const double OriginShift = 2 * Math.PI * EarthRadius / 2;
public static Vector2d LatLonToMeters(Vector2d v)
{
return LatLonToMeters(v.x, v.y);
}
/// <summary>
/// Converts given lat/lon in WGS84 Datum to Spherical Mercator EPSG:900913.
/// </summary>
/// <param name="lat">Latitude in WGS84 Datum.</param>
/// <param name="lon">Longitude in WGS84 Datum.</param>
/// <returns>Vector2d of Spherical Mercator EPSG:900913 coordinates.</returns>
public static Vector2d LatLonToMeters(double lat, double lon)
{
var p = new Vector2d();
p.x = (lon * OriginShift / 180);
p.y = (Math.Log(Math.Tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180));
p.y = (p.y * OriginShift / 180);
return new Vector2d(p.x, p.y);
}
/// <summary>
/// Converts pixel coordinates in given zoom level of pyramid pixel to Spherical Mercator EPSG:900913.
/// </summary>
/// <param name="p">Vector2d of pixel coordinates in pyramid pixel.</param>
/// <param name="zoom">TMS zoom level.</param>
/// <returns>Vector2d of Spherical Mercator EPSG:900913 coordinates.</returns>
public static Vector2d PixelsToMeters(Vector2d p, int zoom)
{
var res = Resolution(zoom);
var met = new Vector2d();
met.x = (p.x * res - OriginShift);
met.y = -(p.y * res - OriginShift);
return met;
}
/// <summary>
/// Converts Spherical Mercator EPSG:900913 to pyramid pixel coordinates in given zoom level.
/// </summary>
/// <param name="m">Vector2d of Spherical Mercator EPSG:900913 coordinates.</param>
/// <param name="zoom">TMS zoom level.</param>
/// <returns>Vector2d of pyramid pixel coordinates.</returns>
public static Vector2d MetersToPixels(Vector2d m, int zoom)
{
var res = Resolution(zoom);
var pix = new Vector2d();
pix.x = ((m.x + OriginShift) / res);
pix.y = ((-m.y + OriginShift) / res);
return pix;
}
/// <summary>
/// Returns a TMS (NOT Google!) tile containing given pyramid pixel coordinates.
/// </summary>
/// <param name="p">Vector2d of pyramid pixel coordinates.</param>
/// <returns>Vector2d of TMS Tile X,Y coordinates.</returns>
public static Vector2d PixelsToTile(Vector2d p)
{
var t = new Vector2d();
t.x = (int)Math.Ceiling(p.x / (double)TileSize) - 1;
t.y = (int)Math.Ceiling(p.y / (double)TileSize) - 1;
return t;
}
public static Vector2d PixelsToRaster(Vector2d p, int zoom)
{
var mapSize = TileSize << zoom;
return new Vector2d(p.x, mapSize - p.y);
}
/// <summary>
/// Returns TMS tile containing given Spherical Mercator EPSG:900913 coordinates.
/// </summary>
/// <remarks>
/// <seealso cref="MetersToPixels"/>
/// <seealso cref="PixelsToTile"/>
/// </remarks>
/// <param name="m">Vector2d of Spherical Mercator EPSG:900913 coordinates.</param>
/// <param name="zoom">TMS zoom level.</param>
/// <returns>Vector2d of TMS Tile X,Y coordinates.</returns>
public static Vector2d MetersToTile(Vector2d m, int zoom)
{
var p = MetersToPixels(m, zoom);
return PixelsToTile(p);
}
/// <summary>
/// Returns bounds of the given TMS tile in Spherical Mercator EPSG:900913.
/// </summary>
/// <remarks>
/// Bounds start with "origin" (minimum) of tile in south-west.
/// </remarks>
/// <param name="t">Tile in TMS coordinates.</param>
/// <param name="zoom">TMS zoom level.</param>
/// <returns>Vector2d of Spherical Mercator EPSG:900913 bounds.</returns>
public static RectD TileBounds(Vector2d t, int zoom)
{
var min = PixelsToMeters(new Vector2d(t.x * TileSize, t.y * TileSize), zoom);
var max = PixelsToMeters(new Vector2d((t.x + 1) * TileSize, (t.y + 1) * TileSize), zoom);
return new RectD(min, max - min);
}
public static Vector2d MetersToLatLon(Vector2d m)
{
var ll = new Vector2d();
ll.x = (m.x / OriginShift) * 180;
ll.y = (m.y / OriginShift) * 180;
ll.y = 180 / Math.PI * (2 * Math.Atan(Math.Exp(ll.y * Math.PI / 180)) - Math.PI / 2);
return ll;
}
//Returns bounds of the given tile in latutude/longitude using WGS84 datum
//public static RectD TileLatLonBounds(Vector2d t, int zoom)
//{
// var bound = TileBounds(t, zoom);
// var min = MetersToLatLon(new Vector2d(bound.Min.x, bound.Min.y));
// var max = MetersToLatLon(new Vector2d(bound.Min.x + bound.Size.x, bound.Min.y + bound.Size.y));
// return new RectD(min.x, min.y, Math.Abs(max.x - min.x), Math.Abs(max.y - min.y));
//}
//Resolution (meters/pixel) for given zoom level (measured at Equator)
public static double Resolution(int zoom)
{
return InitialResolution / (Math.Pow(2, zoom));
}
public static double ZoomForPixelSize(double pixelSize)
{
for (var i = 0; i < 30; i++)
if (pixelSize > Resolution(i))
return i != 0 ? i - 1 : 0;
throw new InvalidOperationException();
}
// Switch to Google Tile representation from TMS
public static Vector2d ToGoogleTile(Vector2d t, int zoom)
{
return new Vector2d(t.x, ((int)Math.Pow(2, zoom) - 1) - t.y);
}
// Switch to TMS Tile representation from Google
public static Vector2d ToTmsTile(Vector2d t, int zoom)
{
return new Vector2d(t.x, ((int)Math.Pow(2, zoom) - 1) - t.y);
}
}
}
fileFormatVersion: 2
guid: f68fd7cf5108133479a8b970e9cd4ce2
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
This diff is collapsed.
fileFormatVersion: 2
guid: 5d884f9bb44e9ee418a318fe6ee86ab9
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace MapzenGo.Helpers
{
public class MeshData
{
public MeshData()
{
Vertices = new List<Vector3>();
Indices = new List<int>();
UV = new List<Vector2>();
}
public List<Vector3> Vertices { get; set; }
public List<int> Indices { get; set; }
public List<Vector2> UV { get; set; }
}
}
fileFormatVersion: 2
guid: 38da3f8109481884cb5f4fa28322db68
timeCreated: 1484721606
guid: 25a69165b7e31b74c95196760a7c7b02
timeCreated: 1473887126
licenseType: Free
MonoImporter:
serializedVersion: 2
......
fileFormatVersion: 2
guid: 72f2fb2a0d6122e44826a023ced3e1ce
folderAsset: yes
timeCreated: 1473606327
licenseType: Free
DefaultImporter: