유니티 GL로 화면에 그림 그리기
참고
GL은 로우 레벨(Low-level) 그래픽 라이브러리다.
아래의 예시를 실행시켜보자.
https://docs.unity3d.com/kr/530/ScriptReference/GL.QUADS.html
먼저 shader가 필요하다. 빈 shader를 만들자.
Draw.shader
Shader "Draw/Quads"
{
SubShader
{
Pass
{
}
}
}
Start에서 shader를 가져와 Material에 할당한다.
Material mat;
void Start()
{
mat = new Material(Shader.Find("Draw/Quads"));
}
이때, Shader는 Draw.shader가 아닌 아래의 Shader 옆에 정의된 이름이다.
GL을 그리기 위해서는 Material의 SetPass(0)을 실행시켜야 한다.
mat.SetPass(0);
링크의 코드를 현재 코드와 합친다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GLTest : MonoBehaviour
{
Material mat;
void OnPostRender()
{
if (!mat)
{
Debug.LogError("Please Assign a material on the inspector");
return;
}
GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.QUADS);
GL.Color(Color.red);
GL.Vertex3(0, 0.5F, 0);
GL.Vertex3(0.5F, 1, 0);
GL.Vertex3(1, 0.5F, 0);
GL.Vertex3(0.5F, 0, 0);
GL.Color(Color.cyan);
GL.Vertex3(0, 0, 0);
GL.Vertex3(0, 0.25F, 0);
GL.Vertex3(0.25F, 0.25F, 0);
GL.Vertex3(0.25F, 0, 0);
GL.End();
GL.PopMatrix();
}
void Start()
{
mat = new Material(Shader.Find("Draw/Quads"));
}
}
OnPostRender는 카메라가 모든 오브젝트의 렌더링을 마친 후에 호출되는 함수이다.
따라서 카메라의 스크립트로 사용해야 한다.
GLTest.cs를 카메라에 추가하고 실행하면 아래와 같이 로우 레벨 그래픽이 그려진다.
Unity Plus:
Easy 2D, 3D, VR, & AR software for cross-platform development of games and mobile apps. - Unity Store
Have a 2D, 3D, VR, or AR project that needs cross-platform functionality? We can help. Take a look at the easy-to-use Unity Plus real-time dev platform!
store.unity.com
Unity Pro:
Unity Pro
The complete solutions for professionals to create and operate.
unity.com
Unity 프리미엄 학습:
Unity Learn
Advance your Unity skills with live sessions and over 750 hours of on-demand learning content designed for creators at every skill level.
unity.com