Jump to content

How can I tell if a body is being drawn in Flight?


Recommended Posts

Camera.current.WorldToScreenPoint(Vector3 position)

If I understand the documentation right, this method will return the 2D screen position of a 3D coordinate. If you use that on the 3D position of a celestial you should be able to calculate if the center of that celestial is visible on the screen.

Is this what you are looking for? Or do you want to find out if any part of a celestial is visible on the screen?

Link to comment
Share on other sites

@*Aqua* - no, that's not quite what I was looking for. I want to ask KSP "Are you rendering the model for this body?". For instance, over Kerbin, you can look around and see the sun, Kerbin, Mun, and Minmus, but none of the other worlds are visible. If you fly out to Duna, you can't see any worlds once you've moved away from Kerbin, and then one day as you approach, Duna appears. I want to find out which planets KSP wants to draw while in flight.

The specific problem I'm trying to solve is this: I'm currently maintaining the Distant Object Enhancement plugin. It draws impostors ("flares") to represent each planet, so you can see them in space, even when you're not near them. The original plugin developer used an algorithm to determine whether or not to draw the flare based on the angular size of the world. However, the algorithm isn't perfect, so the impostor for Minmus is drawn over / near the model of Minmus that is rendered in orbit. I want to ask KSP if it's already drawing the model for a world, so I know I don't need to draw the impostor.

@sarbian - renderer is null for all worlds. gameObject.activeSelf == true for all worlds. I've already look at PQS (as a wild guess), but that can't be it, since Jool's is null. I think I looked at body.scaledBody, as well, and that wasn't it, either. I guess I'll keep peeking at values.

Link to comment
Share on other sites

I want to ask KSP if it's already drawing the model for a world, so I know I don't need to draw the impostor.

This snippet might help you, then. You should be able to adapt it to your needs:

5aa7b7eac6.png

[KSPAddon(KSPAddon.Startup.Flight, false)]
public class RenderedPlanetLocations : MonoBehaviour
{
private List<Transform> _scaledTransforms = new List<Transform>();
private Texture2D _redTexture;

private void Start()
{
_scaledTransforms =
ScaledSpace.Instance.scaledSpaceTransforms
.Where(ss => ss.GetComponent<ScaledSpaceFader>() != null)
.ToList();

// create red box texture
_redTexture = new Texture2D(32, 32, TextureFormat.ARGB32, false);

_redTexture.SetPixels(Enumerable.Repeat(Color.clear, _redTexture.width*_redTexture.height).ToArray());

for (int y = 0; y < _redTexture.height; ++y)
for (int x = 0; x < _redTexture.width; ++x)
if (x% (_redTexture.width - 1) == 0 || y% (_redTexture.height - 1)== 0)
_redTexture.SetPixel(x, y, new Color(1f, 0f, 0f, 0.33f));

_redTexture.Apply();

}


private void OnGUI()
{
var ssCamera = ScaledCamera.Instance.camera;

foreach (var t in _scaledTransforms)
{
if (!t.renderer.enabled) // game isn't rendering it at all
continue;

if (!t.renderer.isVisible) // might be valid target for rendering, but is offscreen
continue;

var screenPos = ssCamera.WorldToScreenPoint(t.transform.position);

if (screenPos.z < 0f) // behind the camera
continue;

if (screenPos.x >= 0 && screenPos.y < ssCamera.pixelWidth)
if (screenPos.y >= 0 && screenPos.y < ssCamera.pixelHeight)
{
screenPos.y = Screen.height - screenPos.y;

if (Event.current.type == EventType.Repaint)
Graphics.DrawTexture(new Rect(screenPos.x - _redTexture.width * 0.5f, screenPos.y - _redTexture.height * 0.5f, _redTexture.width, _redTexture.height), _redTexture);

GUI.Label(new Rect(screenPos.x - GUI.skin.label.CalcSize(new GUIContent(t.name)).x * 0.5f, screenPos.y - _redTexture.height * 0.7f, 100f, 28f), t.name);
}
}
}
}

Edited by xEvilReeperx
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...