consolidate all repos to one for archive
This commit is contained in:
262
projektna_naloga/ar_map/Assets/Scripts/SpawnObjects.cs
Normal file
262
projektna_naloga/ar_map/Assets/Scripts/SpawnObjects.cs
Normal file
@@ -0,0 +1,262 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Tracing;
|
||||
using System.Linq;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UI;
|
||||
|
||||
|
||||
public class SpawnObjects : MonoBehaviour
|
||||
{
|
||||
public class LocationData
|
||||
{
|
||||
public int location_id;
|
||||
public string name;
|
||||
public double x_coordinate;
|
||||
public double z_coordinate;
|
||||
public string imageUrl;
|
||||
}
|
||||
|
||||
public class CarData
|
||||
{
|
||||
public int location_id;
|
||||
public int car_count;
|
||||
}
|
||||
|
||||
private const string apiUrl = "https://school.petrovv.com/api/location/csv"; // Example API endpoint
|
||||
private const string apiCarData = "https://school.petrovv.com/api/data/year/2023/month/5/day/30/hour/17/csv";
|
||||
|
||||
private Camera mainCamera;
|
||||
|
||||
public GameObject spherePrefab;
|
||||
|
||||
// Materials
|
||||
public Material redMaterial;
|
||||
public Material greenMaterial;
|
||||
public Material yellowMaterial;
|
||||
|
||||
public RawImage cameraImage;
|
||||
public Texture2D emptyPng;
|
||||
|
||||
private List<LocationData> locationDataList = new List<LocationData>();
|
||||
private List<CarData> carDataList = new List<CarData>();
|
||||
List<int> listIndex = new List<int>(249);
|
||||
List<GameObject> totalSpheres = new List<GameObject>();
|
||||
|
||||
private bool spawned = false;
|
||||
private bool isCarDataDone = false;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
|
||||
|
||||
for (int i = 0; i < listIndex.Count; i++)
|
||||
{
|
||||
listIndex[i] = 0;
|
||||
}
|
||||
|
||||
cameraImage.texture = emptyPng;
|
||||
|
||||
StartCoroutine(GetCarDataFromApi());
|
||||
StartCoroutine(GetLocationDataFromApi());
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (!spawned && isCarDataDone && locationDataList.Count > 0)
|
||||
{
|
||||
SpawnCameraSpheres();
|
||||
spawned = true;
|
||||
}
|
||||
|
||||
|
||||
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
|
||||
{
|
||||
// Cast a ray from the screen touch position
|
||||
Ray ray = mainCamera.ScreenPointToRay(Input.GetTouch(0).position);
|
||||
|
||||
|
||||
// Check if the ray hits a game element
|
||||
if (Physics.Raycast(ray, out RaycastHit hit))
|
||||
{
|
||||
// Get the touched game element
|
||||
GameObject touchedElement = hit.collider.gameObject;
|
||||
|
||||
int ind = totalSpheres.FindIndex(elem => {
|
||||
return elem.transform.position == touchedElement.transform.position;
|
||||
});
|
||||
|
||||
string url = locationDataList[ind].imageUrl;
|
||||
ChangeCameraImageTexture(url);
|
||||
}
|
||||
else
|
||||
{
|
||||
cameraImage.texture = emptyPng;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// private void ChangeCameraImageTexture(GameObject sphere)
|
||||
private void ChangeCameraImageTexture(string imageUrl)
|
||||
{
|
||||
// string imageUrl = locationDataList[0].imageUrl; // Replace with your sphere image URL
|
||||
StartCoroutine(LoadImage(imageUrl));
|
||||
}
|
||||
|
||||
IEnumerator GetLocationDataFromApi()
|
||||
{
|
||||
using (UnityWebRequest webRequest = UnityWebRequest.Get(apiUrl))
|
||||
{
|
||||
// Send the request and wait for a response
|
||||
yield return webRequest.SendWebRequest();
|
||||
|
||||
// Error handling from the web request
|
||||
if (webRequest.result == UnityWebRequest.Result.ConnectionError)
|
||||
{
|
||||
Debug.LogError("Error: " + webRequest.error);
|
||||
}
|
||||
else
|
||||
{
|
||||
string webRequestResult = webRequest.downloadHandler.text;
|
||||
|
||||
string[] lines = webRequestResult.Split('\n');
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
{
|
||||
string[] values = line.Split(',');
|
||||
|
||||
LocationData locationData = new LocationData
|
||||
{
|
||||
location_id = int.Parse(values[0]),
|
||||
name = values[1],
|
||||
z_coordinate = double.Parse(values[2]),
|
||||
x_coordinate = double.Parse(values[3]),
|
||||
imageUrl = values[4]
|
||||
};
|
||||
locationDataList.Add(locationData);
|
||||
}
|
||||
}
|
||||
|
||||
isCarDataDone = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator GetCarDataFromApi()
|
||||
{
|
||||
using (UnityWebRequest webRequest = UnityWebRequest.Get(apiCarData))
|
||||
{
|
||||
// Send the request and wait for a response
|
||||
yield return webRequest.SendWebRequest();
|
||||
|
||||
// Error handling from the web request
|
||||
if (webRequest.result == UnityWebRequest.Result.ConnectionError)
|
||||
{
|
||||
Debug.LogError("Error: " + webRequest.error);
|
||||
}
|
||||
else
|
||||
{
|
||||
string webRequestResult = webRequest.downloadHandler.text;
|
||||
|
||||
string[] lines = webRequestResult.Split('\n');
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
{
|
||||
string[] values = line.Split(',');
|
||||
int index = int.Parse(values[0]);
|
||||
int value = int.Parse(values[1]);
|
||||
|
||||
listIndex.Insert(index, value);
|
||||
}
|
||||
}
|
||||
|
||||
isCarDataDone = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add this method to handle loading images asynchronously
|
||||
private IEnumerator LoadImage(string url)
|
||||
{
|
||||
UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
|
||||
yield return www.SendWebRequest();
|
||||
|
||||
if (www.result == UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
Debug.LogError("Error loading image: " + www.error);
|
||||
}
|
||||
else
|
||||
{
|
||||
Texture2D texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
|
||||
cameraImage.texture = texture;
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnCameraSpheres()
|
||||
{
|
||||
Vector3 coordinates = new Vector3(0f, 0f, 0f);
|
||||
double mappedX;
|
||||
double mappedZ;
|
||||
|
||||
foreach (LocationData location in locationDataList)
|
||||
{
|
||||
mappedX = CalculateX((double)location.x_coordinate);
|
||||
mappedZ = CalculateZ((double)location.z_coordinate);
|
||||
coordinates.x = (float)mappedX;
|
||||
coordinates.z = (float)mappedZ;
|
||||
|
||||
GameObject sphere = Instantiate(spherePrefab);
|
||||
sphere.transform.position = coordinates;
|
||||
sphere.transform.localScale = new Vector3(0.02f, 0.02f, 0.02f);
|
||||
|
||||
Renderer sphereRenderer = sphere.GetComponent<Renderer>();
|
||||
|
||||
if (location.location_id == 999)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (listIndex.ElementAt(location.location_id) <= 100)
|
||||
{
|
||||
sphereRenderer.material = greenMaterial;
|
||||
}
|
||||
else if (listIndex.ElementAt(location.location_id) <= 250)
|
||||
{
|
||||
sphereRenderer.material = yellowMaterial;
|
||||
}
|
||||
else
|
||||
{
|
||||
sphereRenderer.material = redMaterial;
|
||||
}
|
||||
|
||||
totalSpheres.Add(sphere);
|
||||
}
|
||||
}
|
||||
|
||||
// X is equivelant to longitude
|
||||
double CalculateX(double val)
|
||||
{
|
||||
return Map(val, 13.356836, 16.596429, 1, -1);
|
||||
}
|
||||
|
||||
// Z is equivelant to latitude
|
||||
double CalculateZ(double val)
|
||||
{
|
||||
return Map(val, 45.411120, 46.875651, 0.6665, -0.6665);
|
||||
}
|
||||
|
||||
// Mapping function
|
||||
double Map(double x, double inMin, double inMax, double outMin, double outMax)
|
||||
{
|
||||
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
|
||||
}
|
||||
}
|
11
projektna_naloga/ar_map/Assets/Scripts/SpawnObjects.cs.meta
Normal file
11
projektna_naloga/ar_map/Assets/Scripts/SpawnObjects.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f5b0a3d4c69ffb4f935922ecc8c2d5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
103
projektna_naloga/ar_map/Assets/Scripts/TrackUser.cs
Normal file
103
projektna_naloga/ar_map/Assets/Scripts/TrackUser.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class TrackUser : MonoBehaviour
|
||||
{
|
||||
public GameObject userLocationPrefab;
|
||||
public TextMeshProUGUI gpsStatus;
|
||||
|
||||
private float latitudeGPS = 0;
|
||||
private float longitudeGPS = 0;
|
||||
private double mappedLongitude = 0;
|
||||
private double mappedLatitude = 0;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
StartCoroutine(GPSLocation());
|
||||
}
|
||||
|
||||
IEnumerator GPSLocation()
|
||||
{
|
||||
// Check if location is enabled
|
||||
if (!Input.location.isEnabledByUser)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Start service before querying locaction
|
||||
Input.location.Start();
|
||||
|
||||
// Wait until service is intilized
|
||||
int maxWait = 20;
|
||||
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
|
||||
{
|
||||
yield return new WaitForSeconds(1);
|
||||
maxWait -= 1;
|
||||
}
|
||||
|
||||
// Service didn't initilize in 20 seconds
|
||||
if (maxWait < 1)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (Input.location.status == LocationServiceStatus.Failed)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// access granted
|
||||
InvokeRepeating("UpdateGPSData", 0.5f, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGPSData()
|
||||
{
|
||||
if (Input.location.status == LocationServiceStatus.Running)
|
||||
{
|
||||
// Access granted to GPS value and it has been initlized
|
||||
latitudeGPS = Input.location.lastData.latitude;
|
||||
longitudeGPS = Input.location.lastData.longitude;
|
||||
}
|
||||
else
|
||||
{
|
||||
// service stopped
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (longitudeGPS != 0 || latitudeGPS != 0)
|
||||
{
|
||||
mappedLongitude = CalculateX((double)longitudeGPS);
|
||||
mappedLatitude = CalculateZ((double)latitudeGPS);
|
||||
|
||||
Vector3 coordinates = new((float)mappedLongitude, 0.05f, (float)mappedLatitude);
|
||||
|
||||
userLocationPrefab.transform.position = coordinates;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// X is equivelant to longitude
|
||||
double CalculateX(double val)
|
||||
{
|
||||
return Map(val, 13.356836, 16.596429, 1, -1);
|
||||
}
|
||||
|
||||
// Z is equivelant to latitude
|
||||
double CalculateZ(double val)
|
||||
{
|
||||
return Map(val, 45.411120, 46.875651, 0.6665, -0.6665);
|
||||
}
|
||||
|
||||
// Mapping function
|
||||
double Map(double x, double inMin, double inMax, double outMin, double outMax)
|
||||
{
|
||||
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
|
||||
}
|
||||
}
|
11
projektna_naloga/ar_map/Assets/Scripts/TrackUser.cs.meta
Normal file
11
projektna_naloga/ar_map/Assets/Scripts/TrackUser.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1e0b9c6e050b5d44e8220217891390da
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
74
projektna_naloga/ar_map/Assets/Scripts/gps_location.cs
Normal file
74
projektna_naloga/ar_map/Assets/Scripts/gps_location.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class gps_location : MonoBehaviour
|
||||
{
|
||||
public TextMeshProUGUI status;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
StartCoroutine(GPSLocation());
|
||||
}
|
||||
|
||||
IEnumerator GPSLocation()
|
||||
{
|
||||
// Check if location is enabled
|
||||
if (!Input.location.isEnabledByUser)
|
||||
{
|
||||
status.text = "Location not enabled";
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Start service before querying locaction
|
||||
Input.location.Start();
|
||||
|
||||
// Wait until service is intilized
|
||||
int maxWait = 20;
|
||||
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
|
||||
{
|
||||
status.text = "Calibrating...";
|
||||
|
||||
yield return new WaitForSeconds(1);
|
||||
maxWait -= 1;
|
||||
}
|
||||
|
||||
// Service didn't initilize in 20 seconds
|
||||
if (maxWait < 1)
|
||||
{
|
||||
status.text = "Service didn't initilize in 20s";
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (Input.location.status == LocationServiceStatus.Failed)
|
||||
{
|
||||
status.text = "Failed to connect to location";
|
||||
yield break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// access granted
|
||||
InvokeRepeating("UpdateGPSData", 0.5f, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void UpdateGPSData()
|
||||
{
|
||||
if (Input.location.status == LocationServiceStatus.Running)
|
||||
{
|
||||
// Access granted to GPS value and it has been initlized
|
||||
status.text = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
status.text = "Service stopped";
|
||||
// service stopped
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
11
projektna_naloga/ar_map/Assets/Scripts/gps_location.cs.meta
Normal file
11
projektna_naloga/ar_map/Assets/Scripts/gps_location.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4614f375cf9bd44e8e3b3361abd6865
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user