Exemple #1
0
            // Parser Post Apply Event
            public void PostApply(ConfigNode node)
            {
                // If an orbit is defined, we orbit something
                if (orbit != null)
                {
                    // If this body needs orbit controllers, create them
                    if (generatedBody.orbitDriver == null)
                    {
                        generatedBody.orbitDriver   = generatedBody.celestialBody.gameObject.AddComponent <OrbitDriver> ();
                        generatedBody.orbitRenderer = generatedBody.celestialBody.gameObject.AddComponent <OrbitRenderer> ();
                    }

                    // Setup orbit
                    generatedBody.orbitDriver.updateMode = OrbitDriver.UpdateMode.UPDATE;
                    orbit.Apply(generatedBody);
                }

                // If a PQS version was definied
                if (pqs != null)
                {
                    // Assign the generated PQS to our new world
                    generatedBody.pqsVersion = pqs.pqsVersion;

                    // Adjust the radius of the PQSs appropriately
                    foreach (PQS p in generatedBody.pqsVersion.GetComponentsInChildren(typeof(PQS), true))
                    {
                        p.radius = generatedBody.celestialBody.Radius;
                    }
                }

                // We need to generate new scaled space meshes if
                //   a) we are using a template and we've change either the radius or type of body
                //   b) we aren't using a template
                if (((template != null) && (Math.Abs(template.radius - generatedBody.celestialBody.Radius) > 1.0 || template.type != scaledVersion.type.value)) ||
                    template == null)
                {
                    const double rJool   = 6000000.0;
                    const float  rScaled = 1000.0f;

                    // Compute scale between Jool and this body
                    float scale = (float)(generatedBody.celestialBody.Radius / rJool);
                    generatedBody.scaledVersion.transform.localScale = new Vector3(scale, scale, scale);

                    // Attempt to load a cached version of the scale space
                    string CacheDirectory = KSPUtil.ApplicationRootPath + ScaledSpaceCacheDirectory;
                    string CacheFile      = CacheDirectory + "/" + generatedBody.name + ".bin";
                    Directory.CreateDirectory(CacheDirectory);
                    if (File.Exists(CacheFile))
                    {
                        Debug.Log("[Kopernicus]: Body.PostApply(ConfigNode): Loading cached scaled space mesh: " + generatedBody.name);
                        generatedBody.scaledVersion.GetComponent <MeshFilter> ().sharedMesh = Utility.DeserializeMesh(CacheFile);
                    }

                    // Otherwise we have to generate the mesh
                    else
                    {
                        Debug.Log("[Kopernicus]: Body.PostApply(ConfigNode): Generating scaled space mesh: " + generatedBody.name);
                        Mesh scaledVersionMesh = ComputeScaledSpaceMesh(generatedBody);
                        generatedBody.scaledVersion.GetComponent <MeshFilter> ().sharedMesh = scaledVersionMesh;
                        Utility.SerializeMesh(scaledVersionMesh, CacheFile);
                    }

                    // Apply mesh to the body
                    SphereCollider collider = generatedBody.scaledVersion.GetComponent <SphereCollider>();
                    if (collider != null)
                    {
                        collider.radius = rScaled;
                    }

                    // If we have an atmosphere, generate that too
                    if (generatedBody.celestialBody.atmosphere)
                    {
                        // Find atmosphere from ground
                        AtmosphereFromGround[] afgs = generatedBody.scaledVersion.GetComponentsInChildren <AtmosphereFromGround>(true);
                        if (afgs.Length > 0)
                        {
                            // Get the atmosphere from ground
                            AtmosphereFromGround atmosphereFromGround = afgs[0];

                            // We need to get the body for Jool (to steal it's mesh)
                            PSystemBody Jool = Utility.FindBody(PSystemManager.Instance.systemPrefab.rootBody, "Jool");

                            // Generate mesh using Jool as a template
                            Mesh mesh = Utility.DuplicateMesh(Jool.scaledVersion.GetComponent <MeshFilter> ().sharedMesh);
                            //Utility.ScaleVerts (mesh, (float)(generatedBody.celestialBody.Radius / rJool));
                            atmosphereFromGround.GetComponent <MeshFilter>().sharedMesh = mesh;
                        }
                    }
                }

                // Post gen celestial body
                Utility.DumpObjectFields(generatedBody.celestialBody, " Celestial Body ");
            }
Exemple #2
0
            // Parser Post Apply Event
            public void PostApply(ConfigNode node)
            {
                // If an orbit is defined, we orbit something
                if (orbit != null)
                {
                    // If this body needs orbit controllers, create them
                    if (generatedBody.orbitDriver == null)
                    {
                        generatedBody.orbitDriver   = generatedBody.celestialBody.gameObject.AddComponent <OrbitDriver> ();
                        generatedBody.orbitRenderer = generatedBody.celestialBody.gameObject.AddComponent <OrbitRenderer> ();
                    }

                    // Setup orbit
                    generatedBody.orbitDriver.updateMode = OrbitDriver.UpdateMode.UPDATE;
                    orbit.Apply(generatedBody);
                }

                // If a PQS version was definied
                if (pqs != null)
                {
                    // ----------- DEBUG -------------
                                        #if DEBUG
                    Utility.DumpObjectProperties(pqs.pqsVersion.surfaceMaterial, " ---- Surface Material (Post PQS Loader) ---- ");
                    Utility.GameObjectWalk(pqs.pqsVersion.gameObject, "  ");
                                        #endif
                    // -------------------------------

                    // Assign the generated PQS to our new world
                    generatedBody.pqsVersion = pqs.pqsVersion;

                    // Adjust the radius of the PQSs appropriately
                    foreach (PQS p in generatedBody.pqsVersion.GetComponentsInChildren(typeof(PQS), true))
                    {
                        p.radius = generatedBody.celestialBody.Radius;
                    }
                }

                // Create our RingLoaders
                foreach (RingLoader ring in rings)
                {
                    RingLoader.AddRing(generatedBody.scaledVersion.gameObject, ring.ring);
                }

                // If this body is a star
                if (scaledVersion.type.value == BodyType.Star)
                {
                    // Get the Kopernicus star component from the scaled version
                    StarComponent component = generatedBody.scaledVersion.GetComponent <StarComponent> ();

                    // If we have defined a custom power curve, load it
                    if (solarPowerCurve != null)
                    {
                        component.powerCurve = solarPowerCurve.curve;
                    }
                }

                // We need to generate new scaled space meshes if
                //   a) we are using a template and we've change either the radius or type of body
                //   b) we aren't using a template
                if (((template != null) && (Math.Abs(template.radius - generatedBody.celestialBody.Radius) > 1.0 || template.type != scaledVersion.type.value)) ||
                    template == null)
                {
                    const double rJool   = 6000000.0;
                    const float  rScaled = 1000.0f;

                    // Compute scale between Jool and this body
                    float scale = (float)(generatedBody.celestialBody.Radius / rJool);
                    generatedBody.scaledVersion.transform.localScale = new Vector3(scale, scale, scale);

                    // Attempt to load a cached version of the scale space
                    string CacheDirectory = KSPUtil.ApplicationRootPath + ScaledSpaceCacheDirectory;
                    string CacheFile      = CacheDirectory + "/" + generatedBody.name + ".bin";
                    Directory.CreateDirectory(CacheDirectory);
                    if (File.Exists(CacheFile))
                    {
                        if (!File.Exists(ScaledSpaceCacheDirectory + "/DEBUG"))
                        {
                            Logger.Active.Log("[Kopernicus]: Body.PostApply(ConfigNode): Loading cached scaled space mesh: " + generatedBody.name);
                            generatedBody.scaledVersion.GetComponent <MeshFilter>().sharedMesh = Utility.DeserializeMesh(CacheFile);
                        }
                    }

                    // Otherwise we have to generate the mesh
                    else
                    {
                        Logger.Active.Log("[Kopernicus]: Body.PostApply(ConfigNode): Generating scaled space mesh: " + generatedBody.name);
                        Mesh scaledVersionMesh = ComputeScaledSpaceMesh(generatedBody);
                        generatedBody.scaledVersion.GetComponent <MeshFilter> ().sharedMesh = scaledVersionMesh;
                        if (!File.Exists(ScaledSpaceCacheDirectory + "/DEBUG"))
                        {
                            Utility.SerializeMesh(scaledVersionMesh, CacheFile);
                        }
                    }

                    // Apply mesh to the body
                    SphereCollider collider = generatedBody.scaledVersion.GetComponent <SphereCollider>();
                    if (collider != null)
                    {
                        collider.radius = rScaled;
                    }
                }

                // Post gen celestial body
                Utility.DumpObjectFields(generatedBody.celestialBody, " Celestial Body ");
            }