//private void ListChildren(PSystemBody body, int idx) //{ // StringBuilder sb = new StringBuilder(); // for(int i=0; i< idx; ++i) sb.Append(" "); // sb.Append("Body "); // sb.Append(body.celestialBody.name); // Debug.Log(sb.ToString()); // for(int i=0; i<body.children.Count; ++i) // { // ListChildren(body.children[i], idx + 1); // } //} //-------------------------------------------------------------------- // GenerateBodyFlares // Iterate over the celestial bodies and generate flares for each of // them. Add the flare info to the dictionary. private void GenerateBodyFlares() { // If Kerbin is parented to the Sun, set its SMA - otherwise iterate // through celestial bodies to locate which is parented to the Sun // and has Kerbin as a child. Set the highest parent's SMA to kerbinSMA. if (BodyFlare.kerbinSMA <= 0.0) { if (FlightGlobals.Bodies[1].referenceBody == FlightGlobals.Bodies[0]) { BodyFlare.kerbinSMA = FlightGlobals.Bodies[1].orbit.semiMajorAxis; } else { foreach (CelestialBody current in FlightGlobals.Bodies) { if (current != FlightGlobals.Bodies[0]) { if (current.referenceBody == FlightGlobals.Bodies[0] && current.HasChild(FlightGlobals.Bodies[1])) { BodyFlare.kerbinSMA = current.orbit.semiMajorAxis; } } } if (BodyFlare.kerbinSMA <= 0.0) { throw new Exception("Distant Object -- Unable to find Kerbin's relationship to Kerbol."); } } BodyFlare.kerbinRadius = FlightGlobals.Bodies[1].Radius; } bodyFlares.Clear(); Dictionary <CelestialBody, Color> bodyColors = new Dictionary <CelestialBody, Color>(); foreach (UrlDir.UrlConfig node in GameDatabase.Instance.GetConfigs("CelestialBodyColor")) { CelestialBody body = FlightGlobals.Bodies.Find(n => n.name == node.config.GetValue("name")); if (FlightGlobals.Bodies.Contains(body)) { Color color = ConfigNode.ParseColor(node.config.GetValue("color")); color.r = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.r / 255.0f))); color.g = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.g / 255.0f))); color.b = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.b / 255.0f))); color.a = 1.0f; if (!bodyColors.ContainsKey(body)) { bodyColors.Add(body, color); } } } GameObject flare = GameDatabase.Instance.GetModel("DistantObject/Flare/model"); double largestSMA = 0.0; foreach (CelestialBody body in FlightGlobals.Bodies) { if (body != FlightGlobals.Bodies[0]) { largestSMA = Math.Max(largestSMA, body.orbit.semiMajorAxis); BodyFlare bf = new BodyFlare(); GameObject flareMesh = Mesh.Instantiate(flare) as GameObject; Destroy(flareMesh.GetComponent <Collider>()); flareMesh.name = body.bodyName; flareMesh.SetActive(true); //Star detection CelestialBody refStarBody; if (body.scaledBody.GetComponentsInChildren <SunShaderController>(true).Length > 0) { refStarBody = body.orbit.referenceBody; } else { refStarBody = body; } while (refStarBody.scaledBody.GetComponentsInChildren <SunShaderController>(true).Length <= 0) { refStarBody = refStarBody.orbit.referenceBody; } MeshRenderer flareMR = flareMesh.GetComponentInChildren <MeshRenderer>(); // With KSP 1.0, putting these on layer 10 introduces // ghost flares that render for a while before fading away. // These flares were moved to 10 because of an // interaction with PlanetShine. However, I don't see // that problem any longer (where flares changed brightness // during sunrise / sunset). Valerian proposes instead using 15. flareMR.gameObject.layer = 15; flareMR.material.shader = Shader.Find("KSP/Alpha/Unlit Transparent"); if (bodyColors.ContainsKey(body)) { flareMR.material.color = bodyColors[body]; } else { flareMR.material.color = Color.white; } flareMR.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; flareMR.receiveShadows = false; Renderer scaledRenderer = body.MapObject.transform.GetComponent <Renderer>(); bf.body = body; bf.bodyMesh = flareMesh; bf.meshRenderer = flareMR; bf.scaledRenderer = scaledRenderer; bf.color = flareMR.material.color; bf.hslColor = Utility.RGB2HSL(flareMR.material.color); bf.relativeRadiusSquared = Math.Pow(body.Radius / FlightGlobals.Bodies[1].Radius, 2.0); bf.bodyRadiusSquared = body.Radius * body.Radius; bf.bodyMesh.SetActive(DistantObjectSettings.DistantFlare.flaresEnabled); bf.starBody = refStarBody; bodyFlares.Add(bf); } } BodyFlare.bodyFlareDistanceScalar = BodyFlare.FlareDistanceRange / largestSMA; DestroyObject(flare); }
//private void ListChildren(PSystemBody body, int idx) //{ // StringBuilder sb = new StringBuilder(); // for(int i=0; i< idx; ++i) sb.Append(" "); // sb.Append("Body "); // sb.Append(body.celestialBody.name); // Debug.Log(sb.ToString()); // for(int i=0; i<body.children.Count; ++i) // { // ListChildren(body.children[i], idx + 1); // } //} //-------------------------------------------------------------------- // GenerateBodyFlares // Iterate over the celestial bodies and generate flares for each of // them. Add the flare info to the dictionary. private void GenerateBodyFlares() { //--- HACK++ //PSystemManager sm = PSystemManager.Instance; //Debug.Log("PSystemManager scaledSpaceFactor = " + sm.scaledSpaceFactor); //ListChildren(sm.systemPrefab.rootBody, 0); //--- HACK-- //If Kerbin is parented to the Sun, set its SMA- otherwise iterate //through celestial bodies to locate which is parented to the Sun //and has Kerbin as a child. Set the highest parents SMA to kerbinSMA if(BodyFlare.kerbinSMA <= 0.0) { if(FlightGlobals.Bodies[1].referenceBody == FlightGlobals.Bodies[0]) BodyFlare.kerbinSMA = FlightGlobals.Bodies[1].orbit.semiMajorAxis; else { foreach(CelestialBody current in FlightGlobals.Bodies) { if(current != FlightGlobals.Bodies[0]) if (current.referenceBody == FlightGlobals.Bodies[0] && current.HasChild(FlightGlobals.Bodies[1])) BodyFlare.kerbinSMA = current.orbit.semiMajorAxis; } } BodyFlare.kerbinRadius = FlightGlobals.Bodies[1].Radius; } bodyFlares.Clear(); Dictionary<CelestialBody, Color> bodyColors = new Dictionary<CelestialBody, Color>(); foreach (UrlDir.UrlConfig node in GameDatabase.Instance.GetConfigs("CelestialBodyColor")) { CelestialBody body = FlightGlobals.Bodies.Find(n => n.name == node.config.GetValue("name")); if (FlightGlobals.Bodies.Contains(body)) { Color color = ConfigNode.ParseColor(node.config.GetValue("color")); color.r = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.r / 255.0f))); color.g = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.g / 255.0f))); color.b = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.b / 255.0f))); color.a = 1; if (!bodyColors.ContainsKey(body)) { bodyColors.Add(body, color); } } } GameObject flare = GameDatabase.Instance.GetModel("DistantObject/Flare/model"); foreach (CelestialBody body in FlightGlobals.Bodies) { if (body != FlightGlobals.Bodies[0]) { BodyFlare bf = new BodyFlare(); GameObject flareMesh = Mesh.Instantiate(flare) as GameObject; Destroy(flareMesh.collider); flareMesh.name = body.bodyName; flareMesh.SetActive(true); MeshRenderer flareMR = flareMesh.GetComponentInChildren<MeshRenderer>(); // With KSP 1.0, putting these on layer 10 introduces // ghost flares that render for a while before fading away. // These flares were moved to 10 because of an // interaction with PlanetShine. However, I don't see // that problem any longer (where flares changed brightness // during sunrise / sunset). Valerian proposes instead using 15. flareMR.gameObject.layer = 15; flareMR.material.shader = Shader.Find("KSP/Alpha/Unlit Transparent"); if (bodyColors.ContainsKey(body)) { flareMR.material.color = bodyColors[body]; } else { flareMR.material.color = Color.white; } flareMR.castShadows = false; flareMR.receiveShadows = false; bf.body = body; bf.bodyMesh = flareMesh; bf.meshRenderer = flareMR; bf.color = flareMR.material.color; bf.relativeRadiusSquared = Math.Pow(body.Radius / FlightGlobals.Bodies[1].Radius, 2.0); bf.bodyRadiusSquared = body.Radius * body.Radius; bodyFlares.Add(bf); } } DestroyObject(flare); }
//private void ListChildren(PSystemBody body, int idx) //{ // StringBuilder sb = new StringBuilder(); // for(int i=0; i< idx; ++i) sb.Append(" "); // sb.Append("Body "); // sb.Append(body.celestialBody.name); // Debug.Log(sb.ToString()); // for(int i=0; i<body.children.Count; ++i) // { // ListChildren(body.children[i], idx + 1); // } //} //-------------------------------------------------------------------- // GenerateBodyFlares // Iterate over the celestial bodies and generate flares for each of // them. Add the flare info to the dictionary. private void GenerateBodyFlares() { //--- HACK++ //PSystemManager sm = PSystemManager.Instance; //Debug.Log("PSystemManager scaledSpaceFactor = " + sm.scaledSpaceFactor); //ListChildren(sm.systemPrefab.rootBody, 0); //--- HACK-- //If Kerbin is parented to the Sun, set its SMA- otherwise iterate //through celestial bodies to locate which is parented to the Sun //and has Kerbin as a child. Set the highest parents SMA to kerbinSMA if (BodyFlare.kerbinSMA <= 0.0) { if (FlightGlobals.Bodies[1].referenceBody == FlightGlobals.Bodies[0]) { BodyFlare.kerbinSMA = FlightGlobals.Bodies[1].orbit.semiMajorAxis; } else { foreach (CelestialBody current in FlightGlobals.Bodies) { if (current != FlightGlobals.Bodies[0]) { if (current.referenceBody == FlightGlobals.Bodies[0] && current.HasChild(FlightGlobals.Bodies[1])) { BodyFlare.kerbinSMA = current.orbit.semiMajorAxis; } } } } BodyFlare.kerbinRadius = FlightGlobals.Bodies[1].Radius; } bodyFlares.Clear(); Dictionary <CelestialBody, Color> bodyColors = new Dictionary <CelestialBody, Color>(); foreach (UrlDir.UrlConfig node in GameDatabase.Instance.GetConfigs("CelestialBodyColor")) { CelestialBody body = FlightGlobals.Bodies.Find(n => n.name == node.config.GetValue("name")); if (FlightGlobals.Bodies.Contains(body)) { Color color = ConfigNode.ParseColor(node.config.GetValue("color")); color.r = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.r / 255.0f))); color.g = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.g / 255.0f))); color.b = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.b / 255.0f))); color.a = 1; if (!bodyColors.ContainsKey(body)) { bodyColors.Add(body, color); } } } GameObject flare = GameDatabase.Instance.GetModel("DistantObject/Flare/model"); foreach (CelestialBody body in FlightGlobals.Bodies) { if (body != FlightGlobals.Bodies[0]) { BodyFlare bf = new BodyFlare(); GameObject flareMesh = Mesh.Instantiate(flare) as GameObject; Destroy(flareMesh.collider); flareMesh.name = body.bodyName; flareMesh.SetActive(true); MeshRenderer flareMR = flareMesh.GetComponentInChildren <MeshRenderer>(); // With KSP 1.0, putting these on layer 10 introduces // ghost flares that render for a while before fading away. // These flares were moved to 10 because of an // interaction with PlanetShine. However, I don't see // that problem any longer (where flares changed brightness // during sunrise / sunset). Valerian proposes instead using 15. flareMR.gameObject.layer = 15; flareMR.material.shader = Shader.Find("KSP/Alpha/Unlit Transparent"); if (bodyColors.ContainsKey(body)) { flareMR.material.color = bodyColors[body]; } else { flareMR.material.color = Color.white; } flareMR.castShadows = false; flareMR.receiveShadows = false; bf.body = body; bf.bodyMesh = flareMesh; bf.meshRenderer = flareMR; bf.color = flareMR.material.color; bf.relativeRadiusSquared = Math.Pow(body.Radius / FlightGlobals.Bodies[1].Radius, 2.0); bf.bodyRadiusSquared = body.Radius * body.Radius; bodyFlares.Add(bf); } } DestroyObject(flare); }
//private void ListChildren(PSystemBody body, int idx) //{ // StringBuilder sb = new StringBuilder(); // for(int i=0; i< idx; ++i) sb.Append(" "); // sb.Append("Body "); // sb.Append(body.celestialBody.name); // Debug.Log(sb.ToString()); // for(int i=0; i<body.children.Count; ++i) // { // ListChildren(body.children[i], idx + 1); // } //} //-------------------------------------------------------------------- // GenerateBodyFlares // Iterate over the celestial bodies and generate flares for each of // them. Add the flare info to the dictionary. private void GenerateBodyFlares() { // If Kerbin is parented to the Sun, set its SMA - otherwise iterate // through celestial bodies to locate which is parented to the Sun // and has Kerbin as a child. Set the highest parent's SMA to kerbinSMA. if (BodyFlare.kerbinSMA <= 0.0) { if (FlightGlobals.Bodies[1].referenceBody == FlightGlobals.Bodies[0]) { BodyFlare.kerbinSMA = FlightGlobals.Bodies[1].orbit.semiMajorAxis; } else { foreach (CelestialBody current in FlightGlobals.Bodies) { if (current != FlightGlobals.Bodies[0]) { if (current.referenceBody == FlightGlobals.Bodies[0] && current.HasChild(FlightGlobals.Bodies[1])) { BodyFlare.kerbinSMA = current.orbit.semiMajorAxis; } } } if (BodyFlare.kerbinSMA <= 0.0) { throw new Exception("Distant Object -- Unable to find Kerbin's relationship to Kerbol."); } } BodyFlare.kerbinRadius = FlightGlobals.Bodies[1].Radius; } bodyFlares.Clear(); Dictionary<CelestialBody, Color> bodyColors = new Dictionary<CelestialBody, Color>(); foreach (UrlDir.UrlConfig node in GameDatabase.Instance.GetConfigs("CelestialBodyColor")) { CelestialBody body = FlightGlobals.Bodies.Find(n => n.name == node.config.GetValue("name")); if (FlightGlobals.Bodies.Contains(body)) { Color color = ConfigNode.ParseColor(node.config.GetValue("color")); color.r = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.r / 255.0f))); color.g = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.g / 255.0f))); color.b = 1.0f - (DistantObjectSettings.DistantFlare.flareSaturation * (1.0f - (color.b / 255.0f))); color.a = 1.0f; if (!bodyColors.ContainsKey(body)) { bodyColors.Add(body, color); } } } GameObject flare = GameDatabase.Instance.GetModel("DistantObject/Flare/model"); double largestSMA = 0.0; foreach (CelestialBody body in FlightGlobals.Bodies) { if (body != FlightGlobals.Bodies[0]) { largestSMA = Math.Max(largestSMA, body.orbit.semiMajorAxis); BodyFlare bf = new BodyFlare(); GameObject flareMesh = Mesh.Instantiate(flare) as GameObject; Destroy(flareMesh.GetComponent<Collider>()); flareMesh.name = body.bodyName; flareMesh.SetActive(true); MeshRenderer flareMR = flareMesh.GetComponentInChildren<MeshRenderer>(); // With KSP 1.0, putting these on layer 10 introduces // ghost flares that render for a while before fading away. // These flares were moved to 10 because of an // interaction with PlanetShine. However, I don't see // that problem any longer (where flares changed brightness // during sunrise / sunset). Valerian proposes instead using 15. flareMR.gameObject.layer = 15; flareMR.material.shader = Shader.Find("KSP/Alpha/Unlit Transparent"); if (bodyColors.ContainsKey(body)) { flareMR.material.color = bodyColors[body]; } else { flareMR.material.color = Color.white; } flareMR.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; flareMR.receiveShadows = false; Renderer scaledRenderer = body.MapObject.transform.GetComponent<Renderer>(); bf.body = body; bf.bodyMesh = flareMesh; bf.meshRenderer = flareMR; bf.scaledRenderer = scaledRenderer; bf.color = flareMR.material.color; bf.hslColor = Utility.RGB2HSL(flareMR.material.color); bf.relativeRadiusSquared = Math.Pow(body.Radius / FlightGlobals.Bodies[1].Radius, 2.0); bf.bodyRadiusSquared = body.Radius * body.Radius; bf.bodyMesh.SetActive(DistantObjectSettings.DistantFlare.flaresEnabled); bodyFlares.Add(bf); } } BodyFlare.bodyFlareDistanceScalar = BodyFlare.FlareDistanceRange / largestSMA; DestroyObject(flare); }