[스크립트] Unity3d에서 GL로 선을 그리는 방법

var e : Event;
private var cur : int = 0;
private var orgPos;
private var endPos;
static var lineMaterial : Material;

var cube1 : Transform;
var cube2 : Transform;

private var canDrawLines : boolean = false;

class joint
{
    var org : Vector3;
    var end : Vector3;
}

private var posAL : ArrayList;



static function 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;
}
}
function OnGUI()
{
    e = Event.current;
    if(GUI.Button(Rect(0,0,100,50),"Begin Lines"))
    {
        canDrawLines = true;
    }
    if(GUI.Button(Rect(150,0,100,50),"End Lines"))
    {
        ClearLines();
    }
}

function Start()
{
    //GLDrawLine(Vector3.zero,Vector3.zero);
    //OnPostRender();
    posAL=new ArrayList();
}

function GLDrawLine(beg : Vector3,end : Vector3)
{
    //print("GLDrawLine???");
    if(!canDrawLines)
    return;
    GL.PushMatrix ();
    GL.LoadOrtho ();
    
    beg.x=beg.x/Screen.width;
    end.x=end.x/Screen.width;
     beg.y=beg.y/Screen.height;
     end.y=end.y/Screen.height;
     
     var tmpJoint : joint = new joint();
     tmpJoint.org=beg;
     tmpJoint.end=end;
     
     posAL.Add(tmpJoint);
     
    CreateLineMaterial();
    lineMaterial.SetPass( 0 );
    GL.Begin( GL.LINES );
    GL.Color( Color(1,1,1,0.5) );
    for(var i : int = 0;i<posAL.Count;i++)
    {
        var tj : joint=posAL;
        var tmpBeg : Vector3 = tj.org;
        var tmpEnd : Vector3=tj.end;
        GL.Vertex3( tmpBeg.x,tmpBeg.y,tmpBeg.z );
        GL.Vertex3( tmpEnd.x,tmpEnd.y,tmpEnd.z );
    }
    //GL.Vertex3( 0,0,0);
    //GL.Vertex3( 0.5,0.5,0);
    GL.End();
    //print("beg is:"+beg+"end is:"+end);
    GL.PopMatrix ();
    Debug.DrawLine(beg,end);
}

function OnPostRender() {
        GLDrawLine(orgPos,endPos);
} 

function ClearLines()
{
    canDrawLines = false;
     posAL.Clear();
}

function Update () {
    if(e && canDrawLines)
    {
        if(e.type == EventType.MouseDown)
        {
            orgPos=Input.mousePosition;
            endPos=Input.mousePosition;
            
        }
        if(e.type==EventType.MouseDrag)
        {
            endPos=Input.mousePosition;
            GLDrawLine(orgPos,endPos);
            orgPos=Input.mousePosition;
        }
        if(e.type==EventType.MouseUp)
        {
            //orgPos=Input.mousePosition;
            endPos=Input.mousePosition;
        }
    }
    //GLDrawLine(Vector3.zero,Vector3.zero);
}

좋은 웹페이지 즐겨찾기