public void loadFromJSONFile(string filename) { list.Clear(); using (StreamReader sr = new StreamReader(filename)) { JsonTextReader jtr = new JsonTextReader(sr); Hashtable jobject = new Hashtable(); string property = ""; int width = 5, height = 5, depth = 5; while (jtr.Read()) { switch (jtr.TokenType) { case JsonToken.StartObject: Console.Write("Start object: "); break; case JsonToken.StartArray: break; case JsonToken.EndArray: break; case JsonToken.PropertyName: property = jtr.Value.ToString(); break; case JsonToken.EndObject: if (jobject.Count > 0) { System.Drawing.Color c = System.Drawing.Color.FromArgb(int.Parse(jobject["r"].ToString()), int.Parse(jobject["g"].ToString()), int.Parse(jobject["b"].ToString())); octree.Add(new Volume( new Vector3(int.Parse(jobject["x"].ToString()), int.Parse(jobject["y"].ToString()), int.Parse(jobject["z"].ToString())), new Vector3(1f, 1f, 1f), new Color(c.R, c.G, c.B, c.A))); jobject.Clear(); } break; case JsonToken.Integer: case JsonToken.Float: case JsonToken.String: if (property == "width") { width = int.Parse(jtr.Value.ToString()); } else if (property == "height") { height = int.Parse(jtr.Value.ToString()); } else if (property == "depth") { depth = int.Parse(jtr.Value.ToString()); octree = new Octree <Volume>(new Volume(new Vector3(0, 0, 0), new Vector3(width, height, depth), new Color()), Volume.AddHandler, Volume.RemoveHandler, Volume.SearchHandler, Volume.SetRootHandler, Volume.RemoveAllHandler); } else { jobject.Add(property, jtr.Value); } break; } } } }
public static Octree <Volume> Generate(int width, int depth) { Octree <Volume> octree = new Octree <Volume>(new Volume(new Vector3(0, 0, 0), new Vector3(width, 64, depth), new Color()), Volume.AddHandler, Volume.RemoveHandler, Volume.SearchHandler, Volume.SetRootHandler, Volume.RemoveAllHandler); List <Vector3> coords = new List <Vector3>(); int hills = rand.Next(10); int height = 0; for (int i = 0; i < hills; i++) { coords.Add(new Vector3(rand.Next(width), 0, rand.Next(depth))); } System.Drawing.Color c = System.Drawing.Color.LawnGreen; int r, g, b; for (int i = 0; i < width; i++) { for (int j = 0; j < depth; j++) { octree.Add( new Volume( new Vector3(i, 0, j), new Vector3(1, 1, 1), new Color(c.R, c.G, c.B)) ); } } foreach (Vector3 coord in coords) { for (int x = -10; x < 10; x++) { for (int z = -10; z < 10; z++) { c = System.Drawing.Color.LawnGreen; r = (int)(0.1f * (float)height * (int)c.R); g = (int)c.G; b = (int)(0.1f * (float)height * (int)c.B); if (r > 255) { r = 255; } if (g > 255) { g = 255; } if (b > 255) { b = 255; } height = Math.Min(20 - Math.Abs(z), 20 - Math.Abs(x)); c = System.Drawing.Color.FromArgb(r, g, b); octree.Add( new Volume( new Vector3(coord.X + x, height, coord.Z + z), new Vector3(1, 1, 1), new Color(c.R, c.G, c.B)) ); } } } return(octree); }