Line in Unity 3d – Vectrosity 1.4 Tutorial 2 – A simple editor script for straight continuous lines

I made a simple editor script for Vectrosity 1.4. Its purpose is to allow users to draw straight continuous lines ( actually polylines, i.e continuous line segments ) in the scene view. Think of it as a primitive line editor.
It consists of two parts: The first part is the IppoLine1.js script that deals with line definition and update in runtime.

// Add - Remove points in inspector
var linePoints:Vector3[];
// Proposed material : VectorLine
var myMat:Material;
var line : VectorLine;
function Start ()
{
	//Define the line, I found a width of 4.0 to be satisfacatory
	line = new VectorLine("Line", linePoints, myMat, 4.0, LineType.Continuous);
	// Draw the line
	Vector.DrawLine3DAuto(line, transform);
}

function Update()
{
	//This line forces the line to Update
	// and places it at (0,0,0) of local space of gameObject
	Vector.DrawLine3D(line, transform);
}

The second script, CustomEditorIppoLine1.js deals with the manipulation of the line points in Scene view and drawing of a line in it.

@CustomEditor( IppoLine1 )

class CustomEditorIppoLine1 extends Editor
{
	//Those Vector3 will define the points through which the line will pass in Scene view
	var positions : Vector3[];
	//This allows to do stuff in Scene view
	function OnSceneGUI()
	{
		Handles.color = Color.white;
		positions = new Vector3[target.linePoints.Length];
		var i :int =0;
		for(var item:Vector3 in target.linePoints )
		{
			// itemWorld is the position in world space of the FreeMoveHandle
			var itemWorld:Vector3 = Handles.FreeMoveHandle( target.transform.TransformPoint(item), Quaternion.identity, 0.2, Vector3.zero, Handles.RectangleCap);
			// move the linePoint[x] to the itemWorld position, translated in local space
			item = target.transform.InverseTransformPoint(itemWorld);
			Handles.DotCap(0, itemWorld, Quaternion.identity, 0.1);
			positions[i] = itemWorld;
			++i;
		}

		if (GUI.changed)
		{
			EditorUtility.SetDirty (target);
		}
		//Draw line in Scene view
		Handles.DrawAAPolyLine( 0.1, positions);
	}
}

Usage: Create a new project. Add VectrosityDemos_Unity3.unitypackage in it.
Create one folder called Editor and place CustomEditorIppoLine1.js in it. Add IppoLine1.js in your project too.
Make a new scene, add a new GameObject, place it at 0,0,0, name it (say) Line1, add IppoLine1.js to it.
Select the Line1 in the Hierarchy. In the Inspector, below the IppoLine1(script) select VectorLine as the Material.
On the left of LinePoints there is a small arrow. Click it. Set size to (say) 2. Set Element 0 x as -1 and Element 1 x as 1.
Now you will notice that in the scene view 2 little squared appear on both sides of the game Object. Drag them around.
Those define the begining and the end of the line. If you align the scene view with the camera view, what you see in the scene
will be identical with what you will see in runtime.
To add points, set size (as mentioned before) to 3 e.t.c. The trick is that the new points can be dragged out by selecting in the scene the last point or by setting manually their value in the Elements section.
I hope you will find these scripts useful.

Line in Unity 3d – Vectrosity 1.4 Tutorial 1

Vectrosity 1.4 is out !
Congrats to all who bought it. It is a very solid packet.
If you start with Vectrosity now, here is a small walkthrough with various tips and tricks.
Start by importing VectrosityDemos_Unity3.unitypackage in an empty project.
In the Project view open the scene _Simple2DLine located in Vectrosity ->_Scenes.
You see nothing in the scene editor and it is ok. In the Hierarchy view select Main Camera.
In the Inspector view you will notice that besides the usual camera stuff there are 4 extra scripts, from which the one called Line is checked.
Lets hit play… Ta-ta ! A beautiful line appears across the scene ! Wait… It is kinda jaggy. Where are those nice lines you expected ?
Lets open the script. Pretty explanatory really.
At line 8 it states ” // Make a VectorLine object using the above points and the default material, with a width of 2 pixels”
Default material results in jaggy lines. Lets change this.
We will use the following variant of VectorLine:
VectorLine (name : String, points : Vector2[] or Vector3[],material : Material, lineWidth : float, lineType : LineType = LineType.Discrete, joins : Joins = Joins.None)
It is documented in the Vectrosity Reference Guide.pdf that comes with Vectrosity. This variant allows as to add a material.
Lets change the script to

var myMat:Material;
function Start () {
	// Make Vector2 array; in this case we just use 2 elements...
	var linePoints = [Vector2(0, Random.Range(0, Screen.height)),				// ...one on the left side of the screen somewhere
					  Vector2(Screen.width-1, Random.Range(0, Screen.height))];	// ...and one on the right

	// Changed null to myMat so we can choose a material in the inspector, width to 4.0 from 2.0.
	// Method signature: VectorLine (name : String, points : Vector2[] or Vector3[], material : Material, lineWidth : float, lineType : LineType = LineType.Discrete, joins : Joins = Joins.None)
	var line = new VectorLine("Line", linePoints, myMat, 4.0);

	// Draw the line
	Vector.DrawLine(line);
}

Now assign in the Inspector the material called VectorLine in the MyMat slot.
Save scene, press play and you havea nice, anti-aliased line :)
If you like this kind of lines, make sure that you use the VectorLine material.

GLSL shaders for Unity 3d – Simple Texture Transparent

In this post we will make a shader that displays a transparent texture. There are also some reference links regarding rendering order and blending options in Unity 3d.
If you tried to use the previous shader with a texture that is partially transparent, then you noticed that it does not show up correctly. Perhaps you also noticed that this also happens to the most built-in non Transparent shaders. You have to Continue reading