// This function generates the biomes for the planet. (Coupled with an // appropriate Texture2D.) public static CBAttributeMapSO GenerateCBAttributeMapSO(string name) { Debug.Log("[Kopernicus] GenerateCBAttributeMapSO begins r4"); CBAttributeMapSO rv = ScriptableObject.CreateInstance <CBAttributeMapSO>(); Texture2D map = new Texture2D(4, 4, TextureFormat.RGB24, false); Debug.Log("[Kopernicus] map=" + map); map.LoadImage(System.IO.File.ReadAllBytes(KSPUtil.ApplicationRootPath + PluginDirectory + "/Planets/" + name + "/Biomes.png")); //map.Compress(true); // This might make the edges funny. map.Compress(false); // If it will let us take in an indexed color PNG that would be preferable - bcs map.Apply(true, false); rv.CreateMap(MapSO.MapDepth.RGB, map); //rv.nonExactThreshold = -1f; // This will theoretically return the "best match". It also depends on float comparison, but at least it's to a constant. rv.nonExactThreshold = 0.05f; // 0.6 0.0 0.4 matches 1 0 0 at .33 so .1 is fairly generous and should be reduced for maps with many colors // "exact" match is a broken concept (floats should never be compared to floats with == or !=) and (as implemented as of KSP .24.2) should // never be used. rv.exactSearch = false; rv.Attributes = new CBAttributeMapSO.MapAttribute[3]; rv.Attributes[0] = new CBAttributeMapSO.MapAttribute(); rv.Attributes[0].name = "PolarCaps"; rv.Attributes[0].value = 1.5f; rv.Attributes[0].mapColor = new Color(0f, 1f, 0f); rv.Attributes[1] = new CBAttributeMapSO.MapAttribute(); rv.Attributes[1].name = "Mares"; rv.Attributes[1].value = 1.0f; rv.Attributes[1].mapColor = new Color(0f, 0f, 1f); rv.Attributes[2] = new CBAttributeMapSO.MapAttribute(); rv.Attributes[2].name = "Dunes"; rv.Attributes[2].value = 1.0f; rv.Attributes[2].mapColor = new Color(1f, 0f, 0f); //rv.defaultAttribute = rv.Attributes [0]; /* * // Troubleshooting fosssil * Vector4 pixelBilinear; * Vector4 color; * for (float lat = -3f; lat < 3f; lat += .1f) * for (float lon = 0f; lon < 6f; lon += .1f) { * float innerlon = lon - 1.5707963267948966f; * * float v = (float)(lat / 3.1415926535897931) + 0.5f; * float u = (float)(innerlon / 6.2831853071795862f); * pixelBilinear = rv.Map.GetPixelBilinear (u, v); * color = rv.Attributes [2].mapColor; * * * Debug.Log ("X<A "+ lat + " "+lon+" clr="+ color + " pbl=" + pixelBilinear+" rv=" + rv.GetAtt (lat, lon).name + ":" + * (color-pixelBilinear).sqrMagnitude); * } */ Debug.Log("[Kopernicus] GenerateCBAttributeMapSO ends"); return(rv); }
/// <summary> /// Renders an editor for the object /// </summary> protected void RenderObject(Object @object) { // Null check if (@object == null) { return; } // Get all parseable MemberInfos MemberInfo[] infos = @object.GetType().GetMembers() .Where(m => m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property) .Where(m => !(m as FieldInfo)?.IsLiteral ?? true) .Where(m => m is PropertyInfo ? (m as PropertyInfo).CanRead && (m as PropertyInfo).CanWrite : true) .ToArray(); // Loop through all fields and display them foreach (MemberInfo info in infos) { // Get the type and the value of the member Type FieldType = info.GetMemberType(); Object value = info.GetValue(@object); if (FieldType == typeof(String)) { Label(info.Name); index--; TextField(value.ToString(), v => info.SetValue(@object, v), new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(Boolean)) { Label(info.Name); index--; TextField((Boolean)value, v => info.SetValue(@object, v), new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(Int32)) { Label(info.Name); index--; TextField((Int32)value, v => info.SetValue(@object, v), new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(Single)) { Label(info.Name); index--; TextField((Single)value, v => info.SetValue(@object, v), new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(Double)) { Label(info.Name); index--; TextField((Double)value, v => info.SetValue(@object, v), new Rect(200, index * distance + 10, 170, 20)); } else if (value is Enum) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT, () => { UIController.Instance.SetEditedObject(KittopiaWindows.Enum, value, c => info.SetValue(@object, c)); UIController.Instance.EnableWindow(KittopiaWindows.Enum); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(Color)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_COLOR, () => { UIController.Instance.SetEditedObject(KittopiaWindows.Color, (Color)value, c => info.SetValue(@object, c)); UIController.Instance.EnableWindow(KittopiaWindows.Color); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(Vector3)) { Label(info.Name); index--; Vector3 value_ = (Vector3)value; TextField(value_.x, f => { value_.x = f; info.SetValue(@object, value_); }, new Rect(200, index * distance + 10, 50, 20)); index--; TextField(value_.y, f => { value_.y = f; info.SetValue(@object, value_); }, new Rect(260, index * distance + 10, 50, 20)); index--; TextField(value_.z, f => { value_.z = f; info.SetValue(@object, value_); }, new Rect(320, index * distance + 10, 50, 20)); } else if (FieldType == typeof(Vector3d)) { Label(info.Name); index--; Vector3d value_ = (Vector3d)value; TextField(value_.x, f => { value_.x = f; info.SetValue(@object, value_); }, new Rect(200, index * distance + 10, 50, 20)); index--; TextField(value_.y, f => { value_.y = f; info.SetValue(@object, value_); }, new Rect(260, index * distance + 10, 50, 20)); index--; TextField(value_.z, f => { value_.z = f; info.SetValue(@object, value_); }, new Rect(320, index * distance + 10, 50, 20)); } else if (FieldType == typeof(Vector2)) { Label(info.Name); index--; Vector2 value_ = (Vector2)value; TextField(value_.x, f => { value_.x = f; info.SetValue(@object, value_); }, new Rect(200, index * distance + 10, 50, 20)); index--; TextField(value_.y, f => { value_.y = f; info.SetValue(@object, value_); }, new Rect(285, index * distance + 10, 50, 20)); } else if (FieldType == typeof(Vector2d)) { Label(info.Name); index--; Vector2d value_ = (Vector2d)value; TextField(value_.x, f => { value_.x = f; info.SetValue(@object, value_); }, new Rect(200, index * distance + 10, 50, 20)); index--; TextField(value_.y, f => { value_.y = f; info.SetValue(@object, value_); }, new Rect(285, index * distance + 10, 50, 20)); } else if (FieldType == typeof(CBAttributeMapSO)) { // Load the MapSO Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_LOAD_CBMAP, () => { FileWindow.type = FieldType; UIController.Instance.SetEditedObject(KittopiaWindows.Files, value == null ? "" : PlanetExporter.Format(value as UnityEngine.Object), location => { if (File.Exists(location)) { String path = location.Replace(Path.Combine(Directory.GetCurrentDirectory(), "GameData") + Path.DirectorySeparatorChar, ""); Texture2D texture = Utility.LoadTexture(path, false, false, false); texture.name = path.Replace("\\", "/"); CBAttributeMapSO mapSO = ScriptableObject.CreateInstance <CBAttributeMapSO>(); mapSO.exactSearch = false; mapSO.nonExactThreshold = 0.05f; mapSO.CreateMap(MapSO.MapDepth.RGB, texture); mapSO.Attributes = (value as CBAttributeMapSO).Attributes; mapSO.name = path.Replace("\\", "/"); info.SetValue(@object, mapSO); } else { info.SetValue(@object, Resources.FindObjectsOfTypeAll <CBAttributeMapSO>().FirstOrDefault(m => m.name == location)); } }); UIController.Instance.EnableWindow(KittopiaWindows.Files); }, new Rect(200, index * distance + 10, 170, 20)); // Edit the Biome-Definitions Button(Localization.LOC_KITTOPIATECH_EDIT_BIOMES, () => { UIController.Instance.SetEditedObject(KittopiaWindows.Biome, (value as CBAttributeMapSO).Attributes, att => { (value as CBAttributeMapSO).Attributes = att; info.SetValue(@object, value); }); UIController.Instance.EnableWindow(KittopiaWindows.Biome); }); } else if (FieldType == typeof(Texture2D) || FieldType == typeof(Texture)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_LOAD_TEXTURE, () => { FileWindow.type = FieldType; UIController.Instance.SetEditedObject(KittopiaWindows.Files, value == null ? "" : PlanetExporter.Format(value as UnityEngine.Object), location => { if (File.Exists(location)) { String path = location.Replace(Path.Combine(Directory.GetCurrentDirectory(), "GameData") + Path.DirectorySeparatorChar, ""); Texture2D texture = Utility.LoadTexture(path, false, false, false); texture.name = path.Replace("\\", "/"); info.SetValue(@object, texture); } else { info.SetValue(@object, Resources.FindObjectsOfTypeAll <Texture>().FirstOrDefault(m => m.name == location)); } }); UIController.Instance.EnableWindow(KittopiaWindows.Files); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(PQSLandControl.LandClass[])) { Button(Localization.LOC_KITTOPIATECH_EDIT_LANDCLASSES, () => { UIController.Instance.SetEditedObject(KittopiaWindows.LandClass, (PQSLandControl.LandClass[])value, lc => info.SetValue(@object, lc)); UIController.Instance.EnableWindow(KittopiaWindows.LandClass); }); } else if (FieldType == typeof(PQSMod_VertexPlanet.LandClass[])) { Button(Localization.LOC_KITTOPIATECH_EDIT_LANDCLASSES, () => { UIController.Instance.SetEditedObject(KittopiaWindows.LandClass, (PQSMod_VertexPlanet.LandClass[])value, lc => info.SetValue(@object, lc)); UIController.Instance.EnableWindow(KittopiaWindows.LandClass); }); } else if (FieldType == typeof(PQSMod_HeightColorMap.LandClass[])) { Button(Localization.LOC_KITTOPIATECH_EDIT_LANDCLASSES, () => { UIController.Instance.SetEditedObject(KittopiaWindows.LandClass, (PQSMod_HeightColorMap.LandClass[])value, lc => info.SetValue(@object, lc)); UIController.Instance.EnableWindow(KittopiaWindows.LandClass); }); } else if (FieldType == typeof(PQSMod_HeightColorMap2.LandClass[])) { Button(Localization.LOC_KITTOPIATECH_EDIT_LANDCLASSES, () => { UIController.Instance.SetEditedObject(KittopiaWindows.LandClass, (PQSMod_HeightColorMap2.LandClass[])value, lc => info.SetValue(@object, lc)); UIController.Instance.EnableWindow(KittopiaWindows.LandClass); }); } else if (FieldType == typeof(PQSMod_HeightColorMapNoise.LandClass[])) { Button(Localization.LOC_KITTOPIATECH_EDIT_LANDCLASSES, () => { UIController.Instance.SetEditedObject(KittopiaWindows.LandClass, (PQSMod_HeightColorMapNoise.LandClass[])value, lc => info.SetValue(@object, lc)); UIController.Instance.EnableWindow(KittopiaWindows.LandClass); }); } else if (FieldType == typeof(PQSLandControl.LerpRange)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_LERPRANGE, () => { UIController.Instance.SetEditedObject(KittopiaWindows.LerpRange, (PQSLandControl.LerpRange)value, lc => info.SetValue(@object, lc)); UIController.Instance.EnableWindow(KittopiaWindows.LerpRange); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(PQSMod_VertexPlanet.SimplexWrapper)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_SIMPLEX, () => { UIController.Instance.SetEditedObject(KittopiaWindows.Simplex, (PQSMod_VertexPlanet.SimplexWrapper)value, lc => info.SetValue(@object, lc)); UIController.Instance.EnableWindow(KittopiaWindows.Simplex); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(PQSMod_VertexPlanet.NoiseModWrapper)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_NOISEMOD, () => { UIController.Instance.SetEditedObject(KittopiaWindows.NoiseMod, (PQSMod_VertexPlanet.NoiseModWrapper)value, lc => info.SetValue(@object, lc)); UIController.Instance.EnableWindow(KittopiaWindows.NoiseMod); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(MapSO)) { // Depth if (mapDepth == 5 && value != null) { mapDepth = (Int32)(value as MapSO).Depth; } else if (mapDepth == 5 && value == null) { mapDepth = 0; } // Load the MapSO Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_LOAD_MAPSO, () => { FileWindow.type = FieldType; UIController.Instance.SetEditedObject(KittopiaWindows.Files, value == null ? "" : PlanetExporter.Format(value as UnityEngine.Object), location => { if (File.Exists(location)) { String path = location.Replace(Path.Combine(Directory.GetCurrentDirectory(), "GameData") + Path.DirectorySeparatorChar, ""); Texture2D texture = Utility.LoadTexture(path, false, false, false); texture.name = path.Replace("\\", "/"); MapSO mapSO = ScriptableObject.CreateInstance <MapSO>(); mapSO.CreateMap((MapSO.MapDepth)mapDepth, texture); mapSO.name = path.Replace("\\", "/"); info.SetValue(@object, mapSO); } else { info.SetValue(@object, Resources.FindObjectsOfTypeAll <MapSO>().FirstOrDefault(m => m.name == location)); } }); UIController.Instance.EnableWindow(KittopiaWindows.Files); }, new Rect(200, index * distance + 10, 170, 20)); mapDepth = GUI.SelectionGrid(new Rect(20, index * distance + 10, 350, 20), mapDepth, new [] { "Greyscale", "HeightAlpha", "RGB", "RGBA" }, 4); index++; } else if (FieldType == typeof(PQS)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_SPHERE, () => { UIController.Instance.SetEditedObject(KittopiaWindows.Selector, value as PQS ?? new PQS(), s => info.SetValue(@object, s)); UIController.Instance.EnableWindow(KittopiaWindows.Selector); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(CelestialBody)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_BODY, () => { UIController.Instance.SetEditedObject(KittopiaWindows.Selector, value ?? new CelestialBody(), b => info.SetValue(@object, b)); UIController.Instance.EnableWindow(KittopiaWindows.Selector); }, new Rect(200, index * distance + 10, 170, 20)); } else if (value is Material) // Kopernicus creates Wrappers for the Materials, so key.FieldType == typeof(Material) would return false. :/ { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_MATERIAL, () => { UIController.Instance.SetEditedObject(KittopiaWindows.Material, value as Material, m => info.SetValue(@object, m)); UIController.Instance.EnableWindow(KittopiaWindows.Material); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(FloatCurve)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_CURVE, () => { UIController.Instance.SetEditedObject(KittopiaWindows.Curve, (FloatCurve)value, lc => info.SetValue(@object, lc)); UIController.Instance.EnableWindow(KittopiaWindows.Curve); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(AnimationCurve)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_CURVE, () => { UIController.Instance.SetEditedObject(KittopiaWindows.Curve, new FloatCurve(((AnimationCurve)value).keys), lc => info.SetValue(@object, lc.Curve)); UIController.Instance.EnableWindow(KittopiaWindows.Curve); }, new Rect(200, index * distance + 10, 170, 20)); } else if (FieldType == typeof(Mesh)) { Label(info.Name); index--; Button(Localization.LOC_KITTOPIATECH_EDIT_MESH, () => { FileWindow.type = FieldType; UIController.Instance.SetEditedObject(KittopiaWindows.Files, value == null ? "" : PlanetExporter.Format(value as UnityEngine.Object), location => { if (File.Exists(location)) { String path = location.Replace(Path.Combine(Directory.GetCurrentDirectory(), "GameData") + Path.DirectorySeparatorChar, ""); MeshParser parser = new MeshParser(value as Mesh); parser.SetFromString(path); parser.value.name = path.Replace("\\", "/"); info.SetValue(@object, parser.value); } else { info.SetValue(@object, Resources.FindObjectsOfTypeAll <Mesh>().FirstOrDefault(m => m.name == location)); } }); UIController.Instance.EnableWindow(KittopiaWindows.Files); }, new Rect(200, index * distance + 10, 170, 20)); } } }
//Loads and replaces biomes private void ReplaceBiomes(String set) { if (_debug) { Debug.Log("(CB) Searching for replacement biomes in " + set); } _replaced = true; _biomes = new String[FlightGlobals.Bodies.Count]; int i = 0; char asc = Path.AltDirectorySeparatorChar; char dsc = Path.AltDirectorySeparatorChar; String app = Application.dataPath; //if (_debug) Debug.Log("(CB) App datapath: "+app); app = app.Remove(app.LastIndexOf(dsc) + 1); if (_debug) { Debug.Log("(CB) App: " + app); } if (app.Contains("KSP.app")) { if (_debug) { Debug.Log("(CB) KSP.app detected, attempting OSX fix."); } app = app.Remove(app.LastIndexOf(dsc)); app = app.Remove(app.LastIndexOf(dsc) + 1); if (_debug) { Debug.Log("(CB) App: " + app); } } string path = app + "GameData" + asc + "CustomBiomes" + asc + "PluginData" + asc + "CustomBiomes" + asc + set + asc; if (_debug) { Debug.Log("(CB) Path: " + path); } //String path = Directory.GetCurrentDirectory() + dsc + "GameData" + dsc + "CustomBiomes" + dsc + "PluginData" + dsc + "CustomBiomes" + dsc + set + dsc; foreach (CelestialBody body in FlightGlobals.Bodies) { String name = body.name; /*if (name.Length > 3) * { * if (name.Substring(0,3).Contains("the")) * { * name = name.Remove(0, 4); * if (_debug) Debug.Log("(CB) 'the' removed from "+name); * } * } */ if (_debug) { Debug.Log("(CB) Looking at " + name); } _biomes[i] = name; i++; CBAttributeMapSO biome = body.BiomeMap; //Try loading a texture file try { byte[] bytes; if (_debug) { Debug.Log("(CB) Looking for " + name + ".png..."); } System.IO.FileStream file = new System.IO.FileStream(path + name + ".png", System.IO.FileMode.Open); System.IO.BinaryReader reader = new System.IO.BinaryReader(file); bytes = reader.ReadBytes((int)file.Length); if (_debug) { Debug.Log("(CB) Replacing " + name + "'s biome map."); } Texture2D tex = new Texture2D(1024, 512); tex.LoadImage(bytes); biome.CreateMap(MapSO.MapDepth.RGB, tex); reader.Close(); file.Close(); } catch { //Don't actually care... if (_debug) { Debug.Log("(CB) No map found."); } } //Try loading an attribute file try { if (_debug) { Debug.Log("(CB) Looking for " + name + ".att ..."); } System.IO.FileStream file = new System.IO.FileStream(path + name + ".att", System.IO.FileMode.Open); System.IO.StreamReader reader = new StreamReader(file); if (_debug) { Debug.Log("(CB) Replacing " + name + "'s biome attributes."); } String locale; float a, r, g, b, e; e = float.Parse(reader.ReadLine()); if (e != 0.0) { biome.nonExactThreshold = e; biome.exactSearch = true; } else { biome.exactSearch = false; } CBAttributeMapSO.MapAttribute[] attributes = new CBAttributeMapSO.MapAttribute[0]; while (!reader.EndOfStream) { locale = reader.ReadLine(); CBAttributeMapSO.MapAttribute[] old = new CBAttributeMapSO.MapAttribute[0]; old = attributes; attributes = new CBAttributeMapSO.MapAttribute[old.Length + 1]; old.CopyTo(attributes, 0); a = float.Parse(reader.ReadLine()); if (a > 1) { a = a / 255f; } r = float.Parse(reader.ReadLine()); if (r > 1) { r = r / 255f; } g = float.Parse(reader.ReadLine()); if (g > 1) { g = g / 255f; } b = float.Parse(reader.ReadLine()); if (b > 1) { b = b / 255f; } CBAttributeMapSO.MapAttribute temp = new CBAttributeMapSO.MapAttribute(); temp.name = locale; temp.mapColor = new Color(r, g, b, a); temp.value = a; attributes[attributes.Length - 1] = temp; } biome.Attributes = attributes; //biome.defaultAttribute = attributes[0]; //Seems as good as any reader.Close(); //Close my files file.Close(); } catch { if (_debug) { Debug.Log("(CB) No attributes found."); } } } try //to read the science results { if (_debug) { Debug.Log("(CB) Looking for " + "ScienceResults.txt ..."); } System.IO.FileStream file = new System.IO.FileStream(path + "ScienceResults.txt", System.IO.FileMode.Open); System.IO.StreamReader reader = new StreamReader(file); if (_debug) { Debug.Log("(CB) Parsing Science Results..."); } readResults(reader); } catch { //No science results file. } }