/// <summary> /// Creates a new ScaledVersion Loader from a spawned CelestialBody. /// </summary> public ScaledVersionLoader(CelestialBody body) { // Is this a spawned body? if (body?.scaledBody == null || Injector.IsInPrefab) { throw new InvalidOperationException("The body must be already spawned by the PSystemManager."); } // Get the scaled version object Value = body; coronas = Value.scaledBody.GetComponentsInChildren <SunCoronas>(true).Select(c => new CoronaLoader(c)) .ToList(); if (!coronas.Any()) { coronas = null; } // Figure out what kind of body we are if (Value.scaledBody.GetComponentsInChildren <SunShaderController>(true).Length > 0) { type = BodyType.Star; } else if (Value.atmosphere) { type = BodyType.Atmospheric; } else { type = BodyType.Vacuum; } if (type != BodyType.Star) { ScaledSpaceFader fader = Value.scaledBody.GetComponent <ScaledSpaceFader>(); if (!(fader is SharedScaledSpaceFader)) { Utility.CopyObjectFields(fader, Value.scaledBody.AddComponent <SharedScaledSpaceFader>()); UnityEngine.Object.Destroy(fader); } } else { SunShaderController controller = Value.scaledBody.GetComponent <SunShaderController>(); if (controller != null && !(controller is SharedSunShaderController)) { Utility.CopyObjectFields(controller, Value.scaledBody.AddComponent <SharedSunShaderController>()); UnityEngine.Object.Destroy(controller); } } // Assign the proper scaled space loaders if (material == null) { return; } if (ScaledPlanetSimple.UsesSameShader(material)) { material = new ScaledPlanetSimpleLoader(material); } if (ScaledPlanetRimAerial.UsesSameShader(material)) { material = new ScaledPlanetRimAerialLoader(material); } if (EmissiveMultiRampSunspots.UsesSameShader(material)) { material = new EmissiveMultiRampSunspotsLoader(material); } if (options == null) { options = new PlanetTextureExporter.TextureOptions(); if (body.pqsController != null) { options.Resolution = body.pqsController.mapFilesize; } } }
// Parser apply event void IParserEventSubscriber.Apply(ConfigNode node) { // Get any existing material we might have on this scaled version ConfigNode data = node.GetNode(materialNodeName); // Check for bad condition (no material, no new material) if (material == null && data == null) { throw new Exception("Scaled version has no material information"); } // Are we a planet or moon? if (type.Value != BodyType.Star) { // If we are not a star, we need a scaled space fader and a sphere collider ScaledSpaceFader fader = Value.scaledBody.GetComponent <ScaledSpaceFader>(); if (fader == null) { fader = Value.scaledBody.AddComponent <SharedScaledSpaceFader>(); fader.floatName = "_Opacity"; fader.celestialBody = Value; } else if (!(fader is SharedScaledSpaceFader)) { Utility.CopyObjectFields(fader, Value.scaledBody.AddComponent <SharedScaledSpaceFader>()); UnityEngine.Object.DestroyImmediate(fader); } // Add a sphere collider if we need one if (Value.scaledBody.GetComponent <SphereCollider>() == null) { SphereCollider collider = Value.scaledBody.AddComponent <SphereCollider>(); collider.center = Vector3.zero; collider.radius = 1000.0f; } // Generate new atmospheric body material if (type.Value == BodyType.Atmospheric) { if (material != null && ScaledPlanetRimAerial.UsesSameShader(material)) { material = new ScaledPlanetRimAerialLoader(material); } else { material = new ScaledPlanetRimAerialLoader(); } material.name = Guid.NewGuid().ToString(); } // Generate new vacuum body material else { if (material != null && ScaledPlanetSimple.UsesSameShader(material)) { material = new ScaledPlanetSimpleLoader(material); } else { material = new ScaledPlanetSimpleLoader(); } material.name = Guid.NewGuid().ToString(); } } // Otherwise we are a star else { // Add the SunShaderController behavior SunShaderController controller = Value.scaledBody.GetComponent <SunShaderController>(); if (controller == null) { Value.scaledBody.AddComponent <SharedSunShaderController>(); } else if (!(controller is SharedSunShaderController)) { Utility.CopyObjectFields(controller, Value.scaledBody.AddComponent <SharedSunShaderController>()); UnityEngine.Object.DestroyImmediate(controller); } // Add the ScaledSun behavior // TODO - apparently there can only be one of these (or it destroys itself) if (Value.scaledBody.GetComponent <ScaledSun>() == null) { Value.scaledBody.AddComponent <ScaledSun>(); } // Add the Kopernicus star componenet Value.scaledBody.AddComponent <StarComponent>(); // Generate a new material for the star EmissiveMultiRampSunspotsLoader newMaterial = null; if (material != null && EmissiveMultiRampSunspots.UsesSameShader(material)) { material = new EmissiveMultiRampSunspotsLoader(material); } else { material = new EmissiveMultiRampSunspotsLoader(); } material.name = Guid.NewGuid().ToString(); // Backup existing coronas foreach (SunCoronas corona in Value.scaledBody.GetComponentsInChildren <SunCoronas>(true)) { corona.transform.parent = Utility.Deactivator; } } // Event Events.OnScaledVersionLoaderApply.Fire(this, node); }