UNITY GL 使用

using UnityEngine;

 

public class GL_Base : MonoBehaviour

{

    Camera tempCamera;

 

//----------------------------------------------------

    protected virtual void Awake()

{

        CreateCam();

    }

 

    protected void CreateCam()

    {

        tempCamera = gameObject.AddComponent<Camera>();

        tempCamera.cullingMask = 0;

        tempCamera.renderingPath = RenderingPath.Forward;

        tempCamera.depth = Mathf.Floor(float.MaxValue);

        tempCamera.clearFlags = CameraClearFlags.Depth;

    }

}

 

public class GL_ShapeBase : GL_Base

{

    protected Material lineMaterial;

 

protected override void Awake()

{

        base.Awake();

 

// Unity has a built-in shader that is useful for drawing

// simple colored things.

var shader = Shader.Find("Hidden/Internal-Colored");

lineMaterial = new Material(shader);

lineMaterial.hideFlags = HideFlags.HideAndDontSave;

// Turn on alpha blending

lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);

lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);

// Turn backface culling off

lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);

// Turn off depth writes

lineMaterial.SetInt("_ZWrite", 0);

}

}

using System.Collections.Generic;

using UnityEngine;

 

public class GL_Shape : GL_ShapeBase

{

public enum PointMode

{

GLPoint,

ScreenPoint

}

 

Vector2 ScreenToGLPoint(Vector2 point)

{

point.x /= Screen.width;

point.y /= Screen.height;

 

return point;

}

 

public void DrawLine(Vector2 start, Vector2 end, Color color, PointMode pointMode = PointMode.ScreenPoint)

{

if (pointMode == PointMode.ScreenPoint)

{

start = ScreenToGLPoint(start);

end = ScreenToGLPoint(end);

}

 

GL.PushMatrix();

 

GL.LoadOrtho();

 

GL.Begin(GL.LINES);

 

GL.Color(color);

 

GL.Vertex(start);

GL.Vertex(end);

 

GL.End();

 

GL.PopMatrix();

}

 

public void DrawLines(List<Vector2> points, Color color, PointMode pointMode = PointMode.ScreenPoint)

{

if (points == null || points.Count == 0)

return;

 

if (pointMode == PointMode.ScreenPoint)

{

for (int i = 0; i < points.Count; i++)

points[i] = ScreenToGLPoint(points[i]);

}

 

GL.PushMatrix();

 

GL.LoadOrtho();

 

GL.Begin(GL.LINES);

 

GL.Color(color);

 

for (int i = 0; i < points.Count; i++)

{

GL.Vertex(points[i]);

}

 

GL.End();

 

GL.PopMatrix();

}

 

public void DrawPolygon(List<Vector2> points, Color fillColor, Color lineColor, PointMode pointMode = PointMode.ScreenPoint)

{

if (points == null || points.Count == 0)

return;

 

if (pointMode == PointMode.ScreenPoint)

{

for (int i = 0; i < points.Count; i++)

points[i] = ScreenToGLPoint(points[i]);

}

 

GL.PushMatrix();

 

GL.LoadOrtho();

 

GL.Begin(GL.TRIANGLES);

 

GL.Color(fillColor);

 

for (int i = 0; i < points.Count; i++)

{

if (i < points.Count - 2)

{

GL.Vertex(points[0]);

GL.Vertex(points[i + 1]);

GL.Vertex(points[i + 2]);

}

}

 

GL.End();

 

GL.Begin(GL.LINES);

 

GL.Color(lineColor);

 

for (int i = 0; i < points.Count; i++)

{

GL.Vertex(points[i]);

 

if(i != points.Count - 1)

GL.Vertex(points[i + 1]);

}

 

GL.Vertex(points[0]);

 

GL.End();

 

GL.PopMatrix();

}

 

public virtual void OnRenderObject()

{

lineMaterial.SetPass(0);

}

}

 

 

using UnityEngine;

using System.Collections.Generic;

 

public class Demo : GL_Shape

{

public override void OnRenderObject()

{

base.OnRenderObject();

 

var points = new List<Vector2>();

points.Add(new Vector2(0.1f, 0.2f));

points.Add(new Vector2(0.5f, 0.25f));

points.Add(new Vector2(0.8f, 0.95f));

points.Add(new Vector2(0.7f, 0.88f));

 

DrawPolygon(points, new Color(1, 0.92f, 0.016f, 0.5f), Color.red, PointMode.GLPoint);

 

points = new List<Vector2>();

points.Add(new Vector2(0.3f, 0.3f));

points.Add(new Vector2(0.6f, 0.8f));

points.Add(new Vector2(0.3f, 0.7f));

points.Add(new Vector2(0.2f, 0.7f));

 

DrawPolygon(points, new Color(1, 0.92f, 0.016f, 0.5f), Color.red, PointMode.GLPoint);

 

points = new List<Vector2>();

points.Add(new Vector2(0.1f, 0.2f));

points.Add(new Vector2(0.2f, 0.3f));

points.Add(new Vector2(0.3f, 0.4f));

points.Add(new Vector2(0.4f, 0.7f));

 

DrawLines(points, Color.red, PointMode.GLPoint);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值