I have a set of GL lines, which I am using to represent transform handles in an editor.
I have got them to display correctly and distinguish themselves from the floor grid, but cannot work out how to get the movement functionality or rotation which will be needed for the editor.
To use what I have posted below, place the TransformGizmo scrpt on the camera, and put an object in the Target slot, then place the material in the Line Material slot and press play.
The lines will be rendered similarly to the Unity handles (just without the cones on the end as I don’t know how to render those).
The Script
using UnityEngine;
using System.Collections;
public class TransformGizmo : MonoBehaviour
{
public Material lineMaterial;
public enum TransformSpace
{
Global,
Local
}
public Transform target;
public TransformSpace space = TransformSpace.Global;
public float handleLength = 5.0f;
void OnPostRender()
{
Vector3 xAxisEnd;
Vector3 yAxisEnd;
Vector3 zAxisEnd;
if (space == TransformSpace.Global)
{
xAxisEnd = target.transform.position + Vector3.right * handleLength;
yAxisEnd = target.transform.position + Vector3.up * handleLength;
zAxisEnd = target.transform.position + Vector3.forward * handleLength;
}
else
{
xAxisEnd = target.transform.position + target.transform.TransformDirection(Vector3.right * handleLength);
yAxisEnd = target.transform.position + target.transform.TransformDirection(Vector3.up * handleLength);
zAxisEnd = target.transform.position + target.transform.TransformDirection(Vector3.forward * handleLength);
}
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
// X line
GL.Color(new Color(1, 0, 0, 1f));
GL.Vertex3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
GL.Vertex3(xAxisEnd.x, xAxisEnd.y, xAxisEnd.z);
// Y line
GL.Color(new Color(0, 1, 0, 1f));
GL.Vertex3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
GL.Vertex3(yAxisEnd.x, yAxisEnd.y, yAxisEnd.z);
// Z line
GL.Color(new Color(0, 0, 1, 1f));
GL.Vertex3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
GL.Vertex3(zAxisEnd.x, zAxisEnd.y, zAxisEnd.z);
GL.End();
}
}
The Material Shader (Keep it white)
Shader "Unlit/GLMat"{
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
}
SubShader {
Lighting Off
Cull Off
ZWrite Off
ZTest always
Fog { Mode Off }
Tags { "Queue"="Transparent+1" }
Pass {
ZWrite On
ZTest LEqual
Cull off
Fog {Mode Off}
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Color;
struct v2f {
float4 pos : SV_POSITION;
float4 col : COLOR;
};
v2f vert (appdata_full vInput) {
v2f OUT;
OUT.pos = mul (UNITY_MATRIX_MVP, vInput.vertex);
OUT.col = vInput.color;
return OUT;
}
half4 frag (v2f fInput) : COLOR {
return _Color * fInput.col;
}
ENDCG
}
}
FallBack "Diffuse"
}