Jump to content

Rescaling Gas Giants


Recommended Posts

Testing out some ideas for a new contract pack, and one thing I want to do requires me to play the planetary scale a little bit. Note that this wouldn't be a Kopernicus thing, as the scaling is dependent on an event. Also, what I'm doing is far far simpler (in theory) than Kopernicus - just a simple rescale of a body without a PQS.

Anyway, after much trial and error, I've gotten my 50% rescale test working for everything but the atmosphere. I was hoping someone can get me the last mile:

Before:

nEhrpnE.png

After:

kYRb4Xr.png

I know that the general issue is that the atmosphere background isn't being scaled, but I can't seem to figure out what is needed to set the scale for that. Any assistance is appreciated. Here's my test code:

        private void DoRescale()        {
Debug.Log("doing Jool Rescale");
PSystemBody psbJool = PSystemManager.Instance.systemPrefab.rootBody.children.Where(psb => psb.celestialBody.name == "Jool").First();


// This works
MeshFilter joolMeshFilter = psbJool.scaledVersion.GetComponent<MeshFilter>();
ScaleMesh(joolMeshFilter.sharedMesh);


// This does nothing
MeshFilter atmoMeshFilter = psbJool.scaledVersion.GetComponentsInChildren<MeshFilter>(true).Where(mr => mr.name == "Atmosphere").First();
ScaleMesh(atmoMeshFilter.sharedMesh);
}


private void ScaleMesh(Mesh mesh)
{
Vector3[] oldVertices = mesh.vertices;
Vector3[] newVertices = new Vector3[oldVertices.Count()];
for (int i = 0; i < oldVertices.Count(); i++)
{
newVertices[i] = oldVertices[i] * 0.5f;
}
mesh.vertices = newVertices;
}

I've also played around with the AtmosphereFromGround class a bunch to no avail, as well as changing the Atmosphere transform.

Link to comment
Share on other sites

Given who you are, code may be faster than words. ^_^

Search `AtmosphereFromGround` here. https://github.com/NathanKell/RealSolarSystem/blob/master/Source/RealSolarSystem.cs

(tl;dr there's an AtmosphereFromGround object that makes that halo, and you need to update some properties. Easiest to just call Start() on it, but if you want to fine tune it you need the method above.)

Link to comment
Share on other sites

Given who you are, code may be faster than words. ^_^

Search `AtmosphereFromGround` here. https://github.com/NathanKell/RealSolarSystem/blob/master/Source/RealSolarSystem.cs

(tl;dr there's an AtmosphereFromGround object that makes that halo, and you need to update some properties. Easiest to just call Start() on it, but if you want to fine tune it you need the method above.)

Heh, I had already looked at that code file and tried some stuff from it to no avail. But now that I know that's where I should focus my efforts hopefully I'll have more success.

Link to comment
Share on other sites

I may have to take you up on this, I don't seem to be getting anywhere.

I've tried poking at the AtmosphereFromGround component, at first I would get some nasty NullRef exceptions for every method I call (extremely suspiciously, the mainCamera, sunLight and planet fields were all null. Was able to set those and hit the code that updates the shaders, but not real luck on getting rid of the white halo.

So I started to suspect that I must still be barking up the wrong tree, and nuked the Atmosphere GameObject via Destroy. Halo still present. So any thoughts on where I may need to look? Here's a dump of Jool's components from PSystemBody down:


PSystemBody component: PSystemBody (UnityEngine.Transform)
PSystemBody component: PSystemBody (PSystemBody)
PSystemBody component: Jool (UnityEngine.Transform)
PSystemBody component: Jool (CelestialBody)
PSystemBody component: Jool (PResource)
PSystemBody component: Jool (OrbitDriver)
PSystemBody component: Jool (OrbitRenderer)
celestialBody component: Jool (UnityEngine.Transform)
celestialBody component: Jool (CelestialBody)
celestialBody component: Jool (PResource)
celestialBody component: Jool (OrbitDriver)
celestialBody component: Jool (OrbitRenderer)
scaledVersion component: Jool (UnityEngine.Transform)
scaledVersion component: Jool (UnityEngine.MeshFilter)
scaledVersion component: Jool (UnityEngine.MeshRenderer)
scaledVersion component: Jool (UnityEngine.SphereCollider)
scaledVersion component: Jool (ScaledSpaceFader)
scaledVersion component: Jool (MaterialSetDirection)
scaledVersion component: Atmosphere (UnityEngine.Transform)
scaledVersion component: Atmosphere (UnityEngine.MeshFilter)
scaledVersion component: Atmosphere (UnityEngine.MeshRenderer)
scaledVersion component: Atmosphere (AtmosphereFromGround)

Looking over that list, I think I'm going to take a closer look at the ScaledSpaceFader... that's about the only thing left that I haven't messed around with yet.

Link to comment
Share on other sites

Alright, didn't seem to have any luck there.

The other possibility would be that I dismissed the atmosphere stuff too quickly - I assumed that destroying the gameObject actually got rid of everything... but I suppose that it could be leaving some shader behind somewhere that is still getting executed? Not really too familiar with how unity destruction works - I just assumed it'll take care of everything in the hierarchy, but I guess if the Squad code isn't setting it up properly....

Alright, enough for tonight - I'll play around with this some more tomorrow evening... any advice in the meantime is appreciated.

Link to comment
Share on other sites

Since the ScaledSpace uses the same mesh-size for every planet (6000 units) + the localScale property to scale the Mesh down / up, you could simply use this:


public void ScaleJool(float scale)
{
// Find Jool (PSystemManager.Instance.localBodies is replaceable with FlightGlobals.Bodies)
CelestialBody jool = PSystemManager.Instance.localBodies.Find(b => b.name == "Jool");

// Null Check
if (jool == null) return;

// Change Jool's (display-)radius
jool.Radius *= scale;

// Rescale Jool's ScaledVersion Transform
jool.scaledBody.transform.localScale *= scale;

// Rescale the Collider (if it exists)
SphereCollider collider = jool.scaledBody.GetComponent<SphereCollider>();
if (collider != null) collider.radius *= scale;
}

This code must be executed after PSystemSpawn, ie. at KSPAddon.Startup.MainMenu or KSPAddon.Startup.PSystemSpawn in the Start() method. :) You can, of course, transform this into prefab-modification code, but that'd break compatibility with Kopernicus.

Cheers!

Thanks! I swear I'd tried all those things (at least individually) without luck, but it worked. Unfortunately, there's still the white atmospheric "halo" doing it this way, so I still seem to be missing one critical piece.

Link to comment
Share on other sites

Sure!

First, you need to find the right AFG. See here for how I loop through all and match on name.

https://github.com/NathanKell/RealSolarSystem/blob/master/Source/RealSolarSystem.cs#L1912-L1921

Then I actually do the updating. That's this block here:

https://github.com/NathanKell/RealSolarSystem/blob/master/Source/RealSolarSystem.cs#L114-L215

This does default rescaling (the way KSP sets up scaling of these things by default--you need to do it again here if the body changes radius): https://github.com/NathanKell/RealSolarSystem/blob/master/Source/RealSolarSystem.cs#L192-L194

Next we recalculate all the derived members:

https://github.com/NathanKell/RealSolarSystem/blob/master/Source/RealSolarSystem.cs#L196-L205

Finally we apply the changes: https://github.com/NathanKell/RealSolarSystem/blob/master/Source/RealSolarSystem.cs#L208-L209

Link to comment
Share on other sites

So the AtmosphereFromGround object that was tied to the Atmosphere GameObject is NOT the same object that I get when I use your method linked above, which was the problem. I did a dump of the info on the AFG object your method found, and it's also on a GameObject called Atmosphere, with the same components (Transform, MeshFilter, MeshRenderer, AtmosphereFromGround). This bugs me to no end. :)

Anyway, it worked, so thank you both very much for your assistance here!

- - - Updated - - -

Okay, I figured out why I was having so much trouble originally (and Thomas P made reference to it if I had been thinking clearer about the implications). I was messing around on the preFab object, long after the Jool CelestialBody had been instantiated. Which is why I had so much trouble and the only thing that seemed to give me results was doing very destructive things like altering the shared mesh (I was also wondering why altering the localized copy of the mesh via the mesh property hadn't work).

So it all makes sense now, thanks again.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...