Jump to content

Partless plugin with 3D mesh in map view


Recommended Posts

Hi,

I'm working on a simple plugin that should display a triangle at the selected celestial body in map (tracking station) view.

I think I'm OK with the Start(), Awake() and Update() callbacks, but the mesh is not displayed.

Could anyone comment?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace KspCelestialOverlay
{
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
internal class KspCelestialOverlay : MonoBehaviour
{
public const int GridLevel = 5;

public static KspCelestialOverlay Instance { get; private set; }

private CelestialBody body;
private Mesh mesh;
private static RenderingManager renderingManager;


public void Awake()
{
var scene = HighLogic.LoadedScene;
Debug.LogError("KspCelestialOverlay Awake scene=" + scene.ToString() + " " + Time.time.ToString("0.0000"));
// we do it in the trackstation only
if (scene != GameScenes.TRACKSTATION)
{
Debug.LogError("KspCelestialOverlay Awake disabling");
enabled = false;
}
}

// called only is enabled == true
public void Start()
{
Debug.LogError("KspCelestialOverlay Start " + Time.time.ToString("0.0000"));

if (Instance != null) Destroy(Instance.gameObject);
Instance = this;

if (HighLogic.LoadedScene != GameScenes.TRACKSTATION)
{
Debug.LogError("KspCelestialOverlay Start not in trackstation wtf?!");
return;
}

mesh = gameObject.AddComponent<MeshFilter>().mesh;
var renderer = gameObject.AddComponent<MeshRenderer>();
}

public void Update()
{
if (HighLogic.LoadedScene != GameScenes.TRACKSTATION)
{
Debug.LogError("KspCelestialOverlay Update not in trackstation wtf?!");
return;
}
if (!MapView.MapIsEnabled || MapView.MapCamera == null)
{
Debug.LogError("KspCelestialOverlay Update mapviewproblem");
gameObject.renderer.enabled = false;
return;
}
var target = MapView.MapCamera.target;
var newBody = getTargetBody(target);
if (newBody == null)
{
Debug.LogError("KspCelestialOverlay Update newbody==null wtf?!");
return;
}
// do it once when a body is selected
if (newBody != body)
{
body = newBody;
Debug.LogError("KspCelestialOverlay Update body.name " + body.name + " " + body.Radius.ToString("0.00"));

Vector3[] vertices = {
new Vector3(0,0,1),
new Vector3(0,1,0),
new Vector3(1,0,0)
};
int[] triangles = { 0, 1, 2 };

mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.Optimize();
gameObject.layer = 10;
gameObject.transform.parent = body.GetTransform();
gameObject.transform.localScale = Vector3.one * 2000000f;
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localRotation = Quaternion.identity;
gameObject.renderer.enabled = true;

}
}

public void OnDestroy()
{
Debug.LogError("KspCelestialOverlay Ondestroy " + Time.time.ToString("0.0000"));
}


private static CelestialBody getTargetBody(MapObject target)
{
if (target == null) return null;
switch (target.type)
{
case MapObject.MapObjectType.CELESTIALBODY:
return target.celestialBody;
case MapObject.MapObjectType.MANEUVERNODE:
return target.maneuverNode.patch.referenceBody;
case MapObject.MapObjectType.VESSEL:
return target.vessel.mainBody;
default:
return null;
}
}
}
}

Link to comment
Share on other sites

The saga continues. Now with a sphere...

Why doesn't it appear?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace KspCelestialOverlay
{
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
internal class KspCelestialOverlay : MonoBehaviour
{
public const int GridLevel = 5;

public static KspCelestialOverlay Instance { get; private set; }

private CelestialBody body;
private Mesh mesh;

static GameObject sphere = null;

public void Awake()
{
var scene = HighLogic.LoadedScene;
Debug.LogError("KspCelestialOverlay Awake scene=" + scene.ToString() + " " + Time.time.ToString("0.0000"));
// we do it in the trackstation only
if (scene != GameScenes.TRACKSTATION)
{
Debug.LogError("KspCelestialOverlay Awake disabling");
enabled = false;
}
}

// called only is enabled == true
public void Start()
{
Debug.LogError("KspCelestialOverlay Start " + Time.time.ToString("0.0000"));

if (Instance != null) Destroy(Instance.gameObject);
Instance = this;

if (HighLogic.LoadedScene != GameScenes.TRACKSTATION)
{
Debug.LogError("KspCelestialOverlay Start not in trackstation wtf?!");
return;
}
/*
mesh = gameObject.AddComponent<MeshFilter>().mesh;
var renderer = gameObject.AddComponent<MeshRenderer>();
*/
Debug.LogError("KspCelestialOverlay Start2 " + Time.time.ToString("0.0000"));

if (sphere == null)
{
Debug.LogError("KspCelestialOverlay createsphere " + Time.time.ToString("0.0000"));
sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.collider.enabled = false;
Destroy(sphere.collider);
}
}

public void Update()
{

if (HighLogic.LoadedScene != GameScenes.TRACKSTATION)
{
Debug.LogError("KspCelestialOverlay Update not in trackstation wtf?!");
return;
}
if (!MapView.MapIsEnabled || MapView.MapCamera == null)
{
Debug.LogError("KspCelestialOverlay Update mapviewproblem");
gameObject.renderer.enabled = false;
return;
}

var target = MapView.MapCamera.target;
var newBody = getTargetBody(target);
if (newBody == null)
{
Debug.LogError("KspCelestialOverlay Update newbody==null wtf?!");
return;
}

// do it once when a body is selected
if (newBody != body)
{
body = newBody;
Debug.LogError("KspCelestialOverlay Update body.name " + body.name + " " + body.Radius.ToString("0.00"));
/*
Vector3[] vertices = {
new Vector3(0,0,0),
new Vector3(0,0,1),
new Vector3(0,1,0),
new Vector3(1,0,0)
};
int[] triangles = { 1,3,2, 0,1,2, 0,1,3, 0,2,3 };

mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.Optimize();

gameObject.layer = 10;
gameObject.transform.parent = ScaledSpace.Instance.scaledSpaceTransforms.Single(t => t.name == body.name);
gameObject.transform.localScale = Vector3.one * 2025f;
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localRotation = Quaternion.identity;
gameObject.renderer.enabled = true;
gameObject.renderer.castShadows = false;
gameObject.renderer.receiveShadows = false;
gameObject.renderer.material.color = Color.green;
*/


sphere.layer = 10;
sphere.transform.parent = ScaledSpace.Instance.scaledSpaceTransforms.Single(t => t.name == body.name);
sphere.transform.localScale = Vector3.one * 1500f;
sphere.transform.localPosition = Vector3.zero;
sphere.transform.localRotation = Quaternion.identity;
sphere.renderer.enabled = true;
sphere.renderer.castShadows = false;
sphere.renderer.receiveShadows = false;
sphere.renderer.material.color = Color.red;
}
}

public void OnDestroy()
{
Debug.LogError("KspCelestialOverlay Ondestroy " + Time.time.ToString("0.0000"));
}


private static CelestialBody getTargetBody(MapObject target)
{
if (target == null) return null;
switch (target.type)
{
case MapObject.MapObjectType.CELESTIALBODY:
return target.celestialBody;
case MapObject.MapObjectType.MANEUVERNODE:
return target.maneuverNode.patch.referenceBody;
case MapObject.MapObjectType.VESSEL:
return target.vessel.mainBody;
default:
return null;
}
}
}
}

Edited by kubi
Link to comment
Share on other sites

The saga continues. Now with a sphere...

Why doesn't it appear?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace KspCelestialOverlay
{
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
internal class KspCelestialOverlay : MonoBehaviour
{
public const int GridLevel = 5;

public static KspCelestialOverlay Instance { get; private set; }

private CelestialBody body;
private Mesh mesh;

static GameObject sphere = null;

public void Awake()
{
var scene = HighLogic.LoadedScene;
Debug.LogError("KspCelestialOverlay Awake scene=" + scene.ToString() + " " + Time.time.ToString("0.0000"));
// we do it in the trackstation only
if (scene != GameScenes.TRACKSTATION)
{
Debug.LogError("KspCelestialOverlay Awake disabling");
enabled = false;
}
}

// called only is enabled == true
public void Start()
{
Debug.LogError("KspCelestialOverlay Start " + Time.time.ToString("0.0000"));

if (Instance != null) Destroy(Instance.gameObject);
Instance = this;

if (HighLogic.LoadedScene != GameScenes.TRACKSTATION)
{
Debug.LogError("KspCelestialOverlay Start not in trackstation wtf?!");
return;
}
/*
mesh = gameObject.AddComponent<MeshFilter>().mesh;
var renderer = gameObject.AddComponent<MeshRenderer>();
*/
Debug.LogError("KspCelestialOverlay Start2 " + Time.time.ToString("0.0000"));

if (sphere == null)
{
Debug.LogError("KspCelestialOverlay createsphere " + Time.time.ToString("0.0000"));
sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.collider.enabled = false;
Destroy(sphere.collider);
}
}

public void Update()
{

if (HighLogic.LoadedScene != GameScenes.TRACKSTATION)
{
Debug.LogError("KspCelestialOverlay Update not in trackstation wtf?!");
return;
}
if (!MapView.MapIsEnabled || MapView.MapCamera == null)
{
Debug.LogError("KspCelestialOverlay Update mapviewproblem");
gameObject.renderer.enabled = false;
return;
}

var target = MapView.MapCamera.target;
var newBody = getTargetBody(target);
if (newBody == null)
{
Debug.LogError("KspCelestialOverlay Update newbody==null wtf?!");
return;
}

// do it once when a body is selected
if (newBody != body)
{
body = newBody;
Debug.LogError("KspCelestialOverlay Update body.name " + body.name + " " + body.Radius.ToString("0.00"));
/*
Vector3[] vertices = {
new Vector3(0,0,0),
new Vector3(0,0,1),
new Vector3(0,1,0),
new Vector3(1,0,0)
};
int[] triangles = { 1,3,2, 0,1,2, 0,1,3, 0,2,3 };

mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.Optimize();

gameObject.layer = 10;
gameObject.transform.parent = ScaledSpace.Instance.scaledSpaceTransforms.Single(t => t.name == body.name);
gameObject.transform.localScale = Vector3.one * 2025f;
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localRotation = Quaternion.identity;
gameObject.renderer.enabled = true;
gameObject.renderer.castShadows = false;
gameObject.renderer.receiveShadows = false;
gameObject.renderer.material.color = Color.green;
*/


sphere.layer = 10;
sphere.transform.parent = ScaledSpace.Instance.scaledSpaceTransforms.Single(t => t.name == body.name);
sphere.transform.localScale = Vector3.one * 1500f;
sphere.transform.localPosition = Vector3.zero;
sphere.transform.localRotation = Quaternion.identity;
sphere.renderer.enabled = true;
sphere.renderer.castShadows = false;
sphere.renderer.receiveShadows = false;
sphere.renderer.material.color = Color.red;
}
}

public void OnDestroy()
{
Debug.LogError("KspCelestialOverlay Ondestroy " + Time.time.ToString("0.0000"));
}


private static CelestialBody getTargetBody(MapObject target)
{
if (target == null) return null;
switch (target.type)
{
case MapObject.MapObjectType.CELESTIALBODY:
return target.celestialBody;
case MapObject.MapObjectType.MANEUVERNODE:
return target.maneuverNode.patch.referenceBody;
case MapObject.MapObjectType.VESSEL:
return target.vessel.mainBody;
default:
return null;
}
}
}
}

Hmmmm... You sure 1500 is big enough to cover the body (it should, but you can't be too careful)? You haven't assigned a material or renderer either. Check your logs. You probably have a few null-reference exceptions from those.

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...