private void load_textures() { try { textures = new tiletype[submesh_n]; reordered_textures = new Texture2D[submesh_n]; for (int submesh = 0; submesh < submesh_n; submesh++) { if (Umaterials[submesh].mainTexture) { // We aren't getting the order we require so; // Load into the format we need (ARGB32) reordered_textures[submesh] = new Texture2D((Umaterials[submesh].mainTexture as Texture2D).width, (Umaterials[submesh].mainTexture as Texture2D).height, TextureFormat.BGRA32, false); Color32[] t_col = (Umaterials[submesh].mainTexture as Texture2D).GetPixels32(); reordered_textures[submesh].SetPixels32(t_col); textures[submesh] = new tiletype(); textures[submesh].height = reordered_textures[submesh].height; textures[submesh].width = reordered_textures[submesh].width; textures[submesh].pitch = textures[submesh].width * Marshal.SizeOf(typeof(Color32)); textures[submesh].first_pixel = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * reordered_textures[submesh].GetRawTextureData().Length); Marshal.Copy(reordered_textures[submesh].GetRawTextureData(), 0, textures[submesh].first_pixel, reordered_textures[submesh].GetRawTextureData().Length); } } } catch (Exception E) { ExceptionHandler("Error while Drawing " + gameObject.name, E); } }
public static double getAltitude(double lat, double lng, double zoom = 16) { short alt = 0; //lat += 1 / 1199.0; //lng -= 1 / 1201f; // lat -35.115676879882812 double // lng 117.94178754638671 double // alt 70 short int x = (lng < 0) ? (int)(lng - 1) : (int)lng; //(int)Math.Floor(lng); int y = (lat < 0) ? (int)(lat - 1) : (int)lat;; //(int)Math.Floor(lat); string ns; if (y > 0) { ns = "N"; } else { ns = "S"; } string ew; if (x > 0) { ew = "E"; } else { ew = "W"; } // running tostring at a high rate was costing cpu if (fnamecache[y] == null) { fnamecache[y] = Math.Abs(y).ToString("00"); } if (fnamecache[1000 + x] == null) { fnamecache[1000 + x] = Math.Abs(x).ToString("000"); } string filename = ns + fnamecache[y] + ew + fnamecache[1000 + x] + ".hgt"; try { if (cache.ContainsKey(filename) || File.Exists(datadirectory + Path.DirectorySeparatorChar + filename)) { // srtm hgt files int size = -1; // add to cache if (!cache.ContainsKey(filename)) { FileStream fs = new FileStream(datadirectory + Path.DirectorySeparatorChar + filename, FileMode.Open, FileAccess.Read, FileShare.Read); if (fs.Length == (1201 * 1201 * 2)) { size = 1201; } else if (fs.Length == (3601 * 3601 * 2)) { size = 3601; } else { return(-1); } byte[] altbytes = new byte[2]; short[,] altdata = new short[size, size]; int altlat = 0; int altlng = 0; while (fs.Read(altbytes, 0, 2) != 0) { altdata[altlat, altlng] = (short)((altbytes[0] << 8) + altbytes[1]); altlat++; if (altlat >= size) { altlng++; altlat = 0; } } fs.Close(); cache[filename] = altdata; } if (cache[filename].Length == (1201 * 1201)) { size = 1201; } else if (cache[filename].Length == (3601 * 3601)) { size = 3601; } else { return(-1); } // remove the base lat long lat -= y; lng -= x; // values should be 0-1199, 1200 is for interpolation double xf = lng * (size - 2); double yf = lat * (size - 2); int x_int = (int)xf; double x_frac = xf - x_int; int y_int = (int)yf; double y_frac = yf - y_int; y_int = (size - 2) - y_int; double alt00 = GetAlt(filename, x_int, y_int); double alt10 = GetAlt(filename, x_int + 1, y_int); double alt01 = GetAlt(filename, x_int, y_int + 1); double alt11 = GetAlt(filename, x_int + 1, y_int + 1); double v1 = avg(alt00, alt10, x_frac); double v2 = avg(alt01, alt11, x_frac); double v = avg(v1, v2, -y_frac); currenttype = tiletype.valid; return(v); } string filename2 = "srtm_" + Math.Round((lng + 2.5 + 180) / 5, 0).ToString("00") + "_" + Math.Round((60 - lat + 2.5) / 5, 0).ToString("00") + ".asc"; if (File.Exists(datadirectory + Path.DirectorySeparatorChar + filename2)) { StreamReader sr = new StreamReader(readFile(datadirectory + Path.DirectorySeparatorChar + filename2)); int nox = 0; int noy = 0; float left = 0; float top = 0; int nodata = -9999; float cellsize = 0; int rowcounter = 0; float wantrow = 0; float wantcol = 0; while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.StartsWith("ncols")) { nox = int.Parse(line.Substring(line.IndexOf(' '))); //hgtdata = new int[nox * noy]; } else if (line.StartsWith("nrows")) { noy = int.Parse(line.Substring(line.IndexOf(' '))); //hgtdata = new int[nox * noy]; } else if (line.StartsWith("xllcorner")) { left = float.Parse(line.Substring(line.IndexOf(' '))); } else if (line.StartsWith("yllcorner")) { top = float.Parse(line.Substring(line.IndexOf(' '))); } else if (line.StartsWith("cellsize")) { cellsize = float.Parse(line.Substring(line.IndexOf(' '))); } else if (line.StartsWith("NODATA_value")) { nodata = int.Parse(line.Substring(line.IndexOf(' '))); } else { string[] data = line.Split(new char[] { ' ' }); if (data.Length == (nox + 1)) { wantcol = (float)((lng - Math.Round(left, 0))); wantrow = (float)((lat - Math.Round(top, 0))); wantrow = (int)(wantrow / cellsize); wantcol = (int)(wantcol / cellsize); wantrow = noy - wantrow; if (rowcounter == wantrow) { Console.WriteLine("{0} {1} {2} {3} ans {4} x {5}", lng, lat, left, top, data[(int)wantcol], (nox + wantcol * cellsize)); return(int.Parse(data[(int)wantcol])); } rowcounter++; } } } //sr.Close(); currenttype = tiletype.valid; return(alt); } else // get something { if (oceantile.Contains(filename)) { currenttype = tiletype.ocean; } if (zoom >= 12) { if (!Directory.Exists(datadirectory)) { Directory.CreateDirectory(datadirectory); } if (requestThread == null) { Console.WriteLine("Getting " + filename); queue.Add(filename); requestThread = new Thread(requestRunner); requestThread.IsBackground = true; requestThread.Name = "SRTM request runner"; requestThread.Start(); } else { lock (objlock) { if (!queue.Contains(filename)) { Console.WriteLine("Getting " + filename); queue.Add(filename); } } } } } } catch { alt = 0; currenttype = tiletype.invalid; } return(alt); }
public static double getAltitude(double lat, double lng, double zoom = 16) { short alt = 0; //lat += 1 / 1199.0; //lng -= 1 / 1201f; // lat -35.115676879882812 double // lng 117.94178754638671 double // alt 70 short int x = (lng < 0) ? (int)(lng - 1) : (int)lng;//(int)Math.Floor(lng); int y = (lat < 0) ? (int)(lat - 1) : (int)lat; ;//(int)Math.Floor(lat); string ns; if (y > 0) ns = "N"; else ns = "S"; string ew; if (x > 0) ew = "E"; else ew = "W"; // running tostring at a high rate was costing cpu if (fnamecache[y] == null) fnamecache[y] = Math.Abs(y).ToString("00"); if (fnamecache[1000 + x] == null) fnamecache[1000 + x] = Math.Abs(x).ToString("000"); string filename = ns + fnamecache[y] + ew + fnamecache[1000 + x] + ".hgt"; try { if (cache.ContainsKey(filename) || File.Exists(datadirectory + Path.DirectorySeparatorChar + filename)) { // srtm hgt files int size = -1; // add to cache if (!cache.ContainsKey(filename)) { FileStream fs = new FileStream(datadirectory + Path.DirectorySeparatorChar + filename, FileMode.Open, FileAccess.Read, FileShare.Read); if (fs.Length == (1201 * 1201 * 2)) { size = 1201; } else if (fs.Length == (3601 * 3601 * 2)) { size = 3601; } else return -1; byte[] altbytes = new byte[2]; short[,] altdata = new short[size, size]; int altlat = 0; int altlng = 0; while (fs.Read(altbytes, 0, 2) != 0) { altdata[altlat, altlng] = (short)((altbytes[0] << 8) + altbytes[1]); altlat++; if (altlat >= size) { altlng++; altlat = 0; } } fs.Close(); cache[filename] = altdata; } if (cache[filename].Length == (1201 * 1201)) { size = 1201; } else if (cache[filename].Length == (3601 * 3601)) { size = 3601; } else return -1; // remove the base lat long lat -= y; lng -= x; // values should be 0-1199, 1200 is for interpolation double xf = lng * (size - 2); double yf = lat * (size - 2); int x_int = (int)xf; double x_frac = xf - x_int; int y_int = (int)yf; double y_frac = yf - y_int; y_int = (size - 2) - y_int; double alt00 = GetAlt(filename, x_int, y_int); double alt10 = GetAlt(filename, x_int + 1, y_int); double alt01 = GetAlt(filename, x_int, y_int + 1); double alt11 = GetAlt(filename, x_int + 1, y_int + 1); double v1 = avg(alt00, alt10, x_frac); double v2 = avg(alt01, alt11, x_frac); double v = avg(v1, v2, -y_frac); currenttype = tiletype.valid; return v; } string filename2 = "srtm_" + Math.Round((lng + 2.5 + 180) / 5, 0).ToString("00") + "_" + Math.Round((60 - lat + 2.5) / 5, 0).ToString("00") + ".asc"; if (File.Exists(datadirectory + Path.DirectorySeparatorChar + filename2)) { StreamReader sr = new StreamReader(readFile(datadirectory + Path.DirectorySeparatorChar + filename2)); int nox = 0; int noy = 0; float left = 0; float top = 0; int nodata = -9999; float cellsize = 0; int rowcounter = 0; float wantrow = 0; float wantcol = 0; while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.StartsWith("ncols")) { nox = int.Parse(line.Substring(line.IndexOf(' '))); //hgtdata = new int[nox * noy]; } else if (line.StartsWith("nrows")) { noy = int.Parse(line.Substring(line.IndexOf(' '))); //hgtdata = new int[nox * noy]; } else if (line.StartsWith("xllcorner")) { left = float.Parse(line.Substring(line.IndexOf(' '))); } else if (line.StartsWith("yllcorner")) { top = float.Parse(line.Substring(line.IndexOf(' '))); } else if (line.StartsWith("cellsize")) { cellsize = float.Parse(line.Substring(line.IndexOf(' '))); } else if (line.StartsWith("NODATA_value")) { nodata = int.Parse(line.Substring(line.IndexOf(' '))); } else { string[] data = line.Split(new char[] { ' ' }); if (data.Length == (nox + 1)) { wantcol = (float)((lng - Math.Round(left, 0))); wantrow = (float)((lat - Math.Round(top, 0))); wantrow = (int)(wantrow / cellsize); wantcol = (int)(wantcol / cellsize); wantrow = noy - wantrow; if (rowcounter == wantrow) { Console.WriteLine("{0} {1} {2} {3} ans {4} x {5}", lng, lat, left, top, data[(int)wantcol], (nox + wantcol * cellsize)); return int.Parse(data[(int)wantcol]); } rowcounter++; } } } //sr.Close(); currenttype = tiletype.valid; return alt; } else // get something { if (oceantile.Contains(filename)) currenttype = tiletype.ocean; if (zoom >= 12) { if (!Directory.Exists(datadirectory)) Directory.CreateDirectory(datadirectory); if (requestThread == null) { Console.WriteLine("Getting " + filename); queue.Add(filename); requestThread = new Thread(requestRunner); requestThread.IsBackground = true; requestThread.Name = "SRTM request runner"; requestThread.Start(); } else { lock (objlock) { if (!queue.Contains(filename)) { Console.WriteLine("Getting " + filename); queue.Add(filename); } } } } } } catch { alt = 0; currenttype = tiletype.invalid; } return alt; }
protected static extern void voxie_drawmeshtex(ref voxie_frame_t vf, ref tiletype texture, poltex_t[] vt, int vtn, int[] mesh, int meshn, int flags, int col);