Jump to content

Creating a mesh visible in flight


Recommended Posts

I'm currently trying to create a box from the raw vertices, and make this object visible in flight (map view, tracking view, normal view).

Here is my code so far:

using System;
using System.Collections.Generic;
using UnityEngine;

namespace KSPPlugin
{
    [KSPAddon(KSPAddon.Startup.EveryScene, false)]
    public class KSPPlugin : MonoBehaviour
    {
        private float last_time = 0.0f;
        private float interval = 5.0f;
		
		private GameObject testbox_obj = null;
		private MeshRenderer testbox_renderer = null;
		private MeshFilter testbox_filter = null;

		private const int MAP_LAYER = 10;
		private const int FLIGHT_LAYER = 15;

		void Awake()
        {
            
        }
        void Start()
        {
			Debug.Log("TEST ADD CUBE");
			
			testbox_obj = new GameObject("testbox");
			testbox_filter = testbox_obj.AddComponent<MeshFilter>();
			Mesh mesh = testbox_filter.mesh = new Mesh();
			testbox_renderer = testbox_obj.AddComponent<MeshRenderer>();

			float length = 20f;
			float width = 20f;
			float height = 20f;

			#region Vertices
			Vector3 p0 = new Vector3(-length * .5f, -width * .5f, height * .5f);
			Vector3 p1 = new Vector3(length * .5f, -width * .5f, height * .5f);
			Vector3 p2 = new Vector3(length * .5f, -width * .5f, -height * .5f);
			Vector3 p3 = new Vector3(-length * .5f, -width * .5f, -height * .5f);

			Vector3 p4 = new Vector3(-length * .5f, width * .5f, height * .5f);
			Vector3 p5 = new Vector3(length * .5f, width * .5f, height * .5f);
			Vector3 p6 = new Vector3(length * .5f, width * .5f, -height * .5f);
			Vector3 p7 = new Vector3(-length * .5f, width * .5f, -height * .5f);

			Vector3[] vertices = new Vector3[]
			{
				p0, p1, p2, p3,// Bottom
				p7, p4, p0, p3,// Left
				p4, p5, p1, p0,// Front
				p6, p7, p3, p2,// Back
				p5, p6, p2, p1,// Right
				p7, p6, p5, p4// Top
			};
			#endregion

			#region Color
			Color[] colors = new Color[]
			{
				Color.red, Color.red, Color.red, Color.red,
				Color.green, Color.green, Color.green, Color.green,
				Color.blue, Color.blue, Color.blue, Color.blue,
				Color.yellow, Color.yellow, Color.yellow, Color.yellow,
				Color.white, Color.white, Color.white, Color.white,
				Color.gray, Color.gray, Color.gray, Color.gray
			};
			#endregion

			#region Normales
			Vector3 up = Vector3.up;
			Vector3 down = Vector3.down;
			Vector3 front = Vector3.forward;
			Vector3 back = Vector3.back;
			Vector3 left = Vector3.left;
			Vector3 right = Vector3.right;

			Vector3[] normales = new Vector3[]
			{
				down, down, down, down,// Bottom
				left, left, left, left,// Left
				front, front, front, front,// Front
				back, back, back, back,// Back
				right, right, right, right,// Right
				up, up, up, up// Top
			};
			#endregion

			#region UVs
			Vector2 _00 = new Vector2(0f, 0f);
			Vector2 _10 = new Vector2(1f, 0f);
			Vector2 _01 = new Vector2(0f, 1f);
			Vector2 _11 = new Vector2(1f, 1f);

			Vector2[] uvs = new Vector2[]
			{
				_11, _01, _00, _10,// Bottom
				_11, _01, _00, _10,// Left
				_11, _01, _00, _10,// Front
				_11, _01, _00, _10,// Back
				_11, _01, _00, _10,// Right
				_11, _01, _00, _10,// Top
			};
			#endregion

			#region Triangles
			int[] triangles = new int[]
			{
				// Bottom
				3, 1, 0,
				3, 2, 1,			
 
				// Left
				3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
				3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
 
				// Front
				3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
				3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
 
				// Back
				3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
				3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
 
				// Right
				3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
				3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
 
				// Top
				3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
				3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,

			};
			#endregion

			mesh.name = "testbox";
			mesh.vertices = vertices;
			mesh.normals = normales;
			mesh.uv = uvs;
			mesh.triangles = triangles;
			mesh.colors = colors;

			mesh.RecalculateBounds();
			mesh.Optimize();

			testbox_renderer.material = new Material(Shader.Find("Particles/Additive"));

			testbox_renderer.enabled = true;
			testbox_obj.SetActive(true);
		}
        void Update()
        {
            if (Time.time - last_time > interval)
            {
                last_time = Time.time;
                Debug.Log("Update. Is on map? " + MapView.MapIsEnabled.ToString());
            }
			float scale = 1.0f;
			if (MapView.MapIsEnabled)
				scale = ScaledSpace.InverseScaleFactor;
			
			testbox_obj.layer = MapView.MapIsEnabled ? MAP_LAYER : FLIGHT_LAYER;
			testbox_obj.transform.position = FlightGlobals.ActiveVessel.GetWorldPos3D();
		}
        void OnDestroy()
        {
			Destroy(testbox_filter);
			Destroy(testbox_renderer);
			Destroy(testbox_obj);
        }
    }
}

The final goal is to create a sphere (i started with a box because it's easier to debug), center it around a celestial body, and apply a custom shader to it.

I dont see the object and i dont get any errors. How do i make the object visible?

Link to comment
Share on other sites

Your problem is that your cube is not where you want it to be. KPS uses multiple coordinate system to deal with the single vs double precision problem. 

GetWorldPos3D give you the position relative to the sun. In the flight scene your vessel is at vessel.transform.position. In the map scene you need to use ScaledSpace.LocalToScaledSpace(vessel.GetWorldPos3D()) (from memory, I hope I am not mixing up stuff :P )

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