Jump to content

Drawing lines? Complete Noob Question


Recommended Posts

I'm rather familiar with OOP, I'm more familiar with VB and C++ than with C#, but I'm getting the hang of it, it's not too difficult.

However, I'm more of an active/visual learner and have always found examples more beneficial than documentation. I usually only use documentation as a reference to understand examples, because without a context to put something into, I often have difficulty conceptualizing things.

That being said, I have an idea for a mod and I've been tinkering around in Visual Studio and KSP for a little over a week. I've gotten used to the basics, and have had success building GUIs and Windows to display information and such. But the one thing that has eluded me is the ability to draw something in space. Examples, MechJeb's Landing Marker, LazerTech lasers and planet markers, Hydrotech, etc etc and so forth.

Thus far I've had moderate success with using LineRenderer, but have run into a limitation as Unity only allows a single instance to be attached to a gameobject and I've yet to figure a work around. I've had zero success using GL. I've tried using examples from the Unity Reference and have yet to see anything displayed on screen.

I've spent the last few days digging through MechJeb's source code to figure out how to draw something as simple as Square or Triangle in front of the vessel, but have thus far been unable to accomplish anything displaying on the screen.

Anyone care to point me in the right direction or know of a simple straight forward example I can play around with to figure out how it works?

Thanks again, NOS

Link to comment
Share on other sites

Here is an example of the test I'm doing with GL. Thus far, I've been completely unable to have anything display on the screen and no errors appear in the log. I don't know if I'm doing the material wrong, the GL calls wrong, or if the position is wrong. Any advice?

[KSPAddon(KSPAddon.Startup.Flight, false)]
public class GLTest : PartModule
{
static Material lineMaterial;
static void CreateLineMaterial()
{
if (!lineMaterial)
{
lineMaterial = new Material("Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
}
}

public void OnPostRender()
{

Vector3 firstPosition = transform.position;

CreateLineMaterial();
lineMaterial.SetPass( 0 );
GL.Begin( GL.LINES );
GL.Color(Color.red);
GL.Vertex3(firstPosition.x, firstPosition.y, firstPosition.z);
GL.Color(Color.black);
GL.Vertex3(firstPosition.x * 2, firstPosition.y * 2, firstPosition.z * 2);
GL.Color(Color.blue);
GL.Vertex3(firstPosition.x * 4, firstPosition.y * 4, firstPosition.z * 4);
GL.Color(Color.cyan);
GL.Vertex3(firstPosition.x * 8, firstPosition.y * 8, firstPosition.z * 8);
GL.Color(Color.gray);
GL.Vertex3(firstPosition.x * -2, firstPosition.y * -2, firstPosition.z * -2);
GL.Color(Color.green);
GL.Vertex3(firstPosition.x * -4, firstPosition.y * -4, firstPosition.z * -4);
GL.Color(Color.magenta);
GL.Vertex3(firstPosition.x * -8, firstPosition.y * -8, firstPosition.z * -8);
GL.End();
}

P.S. Yes, I have a part on the ship. I've had limited success with LineRenderer showing a line extending from the ship, but have been unable (yet) to figure out how to effectively manipulate the resulting line from LineRenderer.

Link to comment
Share on other sites

  • 3 months later...

Interesting, I've attempted to do the same, both in KSP and in the actual Unity engine, I have not had any luck drawing lines nor quads to any effect. Apparently the GL calls do require you to have Unity Pro, which would explain this setup not working in the engine; however, it is very peculiar that it would not work with KSP (being built in Pro), either through some fluke of its design or whatnot.

If nothing else, you may want to try using Unity's LineRenderer class to achieve the same; it should provide similar (if not greater) functionality than what hand-coded OpenGL can achieve.

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