Code cleanup

This commit is contained in:
2025-11-15 20:05:19 +01:00
parent 0cb7d4b64d
commit 39b42e1e90
4 changed files with 37 additions and 51 deletions

View File

@@ -11,12 +11,12 @@ using UnityEngine.Networking;
public class MapRenderer : MonoBehaviour
{
[Header("Overpass settings")]
public string overpassUrl = "https://mapz.honzuvkod.dev/api/interpreter";
public const string overpassUrl = "https://mapz.honzuvkod.dev/api/interpreter";
public float queryRadiusMeters = 200f; // radius around lat/lon to query
[Header("Location (lat, lon)")]
public double latitude; // example Prague
public double longitude;
private double latitude = 50.7727878;
private double longitude = 15.0718625;
[Header("Building settings")]
public Material buildingMaterial;
@@ -31,35 +31,27 @@ public class MapRenderer : MonoBehaviour
public float metersPerUnit = 1f; // scale: 1 unit = 1 meter
public bool autoStart = true;
// Internal storage
[Header("Storage")]
Dictionary<long, Vector2> nodes = new Dictionary<long, Vector2>(); // id -> latlon
List<Way> parsedWays = new List<Way>();
void Start()
{
if (autoStart)
StartCoroutine(GenerateForLocation(latitude, longitude));
if (autoStart) { StartCoroutine(RenderMap()); }
}
// Public entry for other scripts
public void StartGenerating(double lat, double lon)
public void StartGenerating()
{
latitude = lat; longitude = lon;
StartCoroutine(GenerateForLocation(lat, lon));
StartCoroutine(RenderMap());
}
IEnumerator GenerateForLocation(double lat, double lon)
IEnumerator RenderMap()
{
ClearChildren();
// compute bbox from radius
float degPerMeter = 1f / 111000f; // approximate
double delta = queryRadiusMeters * degPerMeter;
double south = lat - delta;
double north = lat + delta;
double west = lon - delta;
double east = lon + delta;
//TODO: GPS update
string q = $"[out:xml][timeout:25];(way[\"building\"]({south.ToString().Replace(",",".")},{west.ToString().Replace(",", ".")},{north.ToString().Replace(",", ".")},{east.ToString().Replace(",", ".")});way[\"highway\"]({south.ToString().Replace(",", ".")},{west.ToString().Replace(",", ".")},{north.ToString().Replace(",", ".")},{east.ToString().Replace(",", ".")}););(._;>;);out body;";
string q = $"[out:xml][timeout:90];(way[\"building\"](around:{queryRadiusMeters.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")},{longitude.ToString().Replace(",", ".")});way[\"highway\"](around:{queryRadiusMeters.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")},{longitude.ToString().Replace(",", ".")}););(._;>;);out body;";
WWWForm form = new WWWForm();
form.AddField("data", q);
@@ -78,14 +70,12 @@ public class MapRenderer : MonoBehaviour
string xml = www.downloadHandler.text;
ParseOverpassXml(xml);
// create separate GameObjects for buildings and roads
GameObject buildingsRoot = new GameObject("Buildings");
buildingsRoot.transform.parent = this.transform;
GameObject roadsRoot = new GameObject("Roads");
roadsRoot.transform.parent = this.transform;
// iterate parsed ways
foreach (var w in parsedWays)
{
if (w.tags.ContainsKey("building"))
@@ -106,7 +96,6 @@ public class MapRenderer : MonoBehaviour
void ClearChildren()
{
// remove existing generated children
List<GameObject> toDestroy = new List<GameObject>();
foreach (Transform t in transform)
toDestroy.Add(t.gameObject);
@@ -122,7 +111,7 @@ public class MapRenderer : MonoBehaviour
public Dictionary<string, string> tags = new Dictionary<string, string>();
}
List<Way> parsedWays = new List<Way>();
void ParseOverpassXml(string xmlText)
{

View File

@@ -0,0 +1,84 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: TestMaterial
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0.1254902, g: 0.1254902, b: 0.1254902, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
m_BuildTextureStacks: []
m_AllowLocking: 1