(六)Graphics基本應用


1.前言

本文主要針對Graphics類進行texture和mesh的繪制。

2.Draw Texture

使用Graphics類直接進行Texture繪制時,由於屬於直接繪制到平面上,所以需要轉換到平面像素空間內,所以需要用到LoadPixelMatrix方法。對於空間轉換可以參考這一節

2.1 ToScreen

示例代碼:

    public void Dmainxture() { GL.PushMatrix(); //GL.LoadPixelMatrix(); GL.LoadPixelMatrix(0,Screen.width,Screen.height,0); Graphics.DrawTexture(new Rect(0, 0, 200, 100), mainTexture); GL.PopMatrix(); } 

代碼中Graphics.DrawTexture使用的是最基本的方法,即將mainTexture繪制到屏幕new Rect(0, 0, 200, 100)的范圍內。此方法有很多重載,可以根據自己的需求選擇不同的方法。
使用GL.LoadPixelMatrix()(代碼中注釋掉的部分)進行坐標轉換時,mainTexture會被繪制在屏幕左下角區域,但是像素上下是反的。這是由於不同的圖形接口,texture對應的坐標原點不同。OpenGl為左下角,D3d為左上角。如果使用GL.LoadPixelMatrix(0,Screen.width,Screen.height,0)則像素不會反轉,但是由於坐標變換矩陣變成從上到下,所以繪制屏幕的左上角。

2.1.1 調用位置

由於是繪制在屏幕上,所以只能在OnGui方法和OnPostRender中調用,在update中則會被camera渲染時會clear掉。但是如果將texture繪制到一個RenderTexture中則可以在update中可以。

2.2 ToTarget

示例代碼:

    public void DrawTextureToTarget() { Graphics.SetRenderTarget(target); clearBuffer.Clear(); clearBuffer.ClearRenderTarget(true, true, clearColor); Graphics.ExecuteCommandBuffer(clearBuffer); GL.PushMatrix(); GL.LoadPixelMatrix(0, target.width, target.height, 0); //GL.LoadPixelMatrix(0, target.width, 0, target.height); Graphics.DrawTexture(new Rect(0, 0, target.width, target.height), mainTexture); GL.PopMatrix(); } 

Graphics.SetRenderTarget(target);將繪制結果繪制在一個RenderTexture類型的變量target上,所以屏幕變換需要使用GL.LoadPixelMatrix(0, target.width, target.height, 0);,此時可以在update中調用。

3.Draw Mesh

3.1 Update中調用

示例代碼:

    public void DrawMesh() { Graphics.DrawMesh(Graphics00Mesh.Instance.GetMesh(10, 5), Matrix4x4.identity, material, 0); //Graphics.DrawMesh(Graphics00Mesh.Instance.GetMesh(10, 5), center,Quaternion.identity, material, 0); } 

Graphics.DrawMesh同樣有很多重載,可以滿足眾多需求,文中只給出了兩個示例,一個通過提供矩陣進行坐標變換,另一個(注釋掉的方法)則通過提供mesh所在的位置和旋轉在進行定位。由於時繪制的模型,所以只能在update中調用。

3.2 OnPostRender中調用

在渲染階段調用只能使用Graphics.DrawMeshNow方法,讓指令立即生效。

4.完整代碼

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; public enum DrawLocation { ONGUI, POSTRENDER, UPDATE } public class Graphics06Graphics : MonoBehaviour { public DrawLocation location = DrawLocation.ONGUI; public bool toTarget = false; public Texture mainTexture; public RenderTexture target; public Color clearColor = Color.red; CommandBuffer clearBuffer; void Draw() { if (toTarget) { DrawTextureToTarget(); } else { DrawTexture(); } } public void DrawTexture() { GL.PushMatrix(); //GL.LoadPixelMatrix(); GL.LoadPixelMatrix(0,Screen.width,Screen.height,0); Graphics.DrawTexture(new Rect(0, 0, 200, 100), mainTexture); GL.PopMatrix(); } public void DrawTextureToTarget() { Graphics.SetRenderTarget(target); clearBuffer.Clear(); clearBuffer.ClearRenderTarget(true, true, clearColor); Graphics.ExecuteCommandBuffer(clearBuffer); GL.PushMatrix(); GL.LoadPixelMatrix(0, target.width, target.height, 0); //GL.LoadPixelMatrix(0, target.width, 0, target.height); Graphics.DrawTexture(new Rect(0, 0, target.width, target.height), mainTexture); GL.PopMatrix(); } private void Start() { clearBuffer = new CommandBuffer() { name = "Clear Buffer" }; } private void OnGUI() { if (location != DrawLocation.ONGUI) return; if (Event.current.type.Equals(EventType.Repaint)) { Draw(); } } private void Update() { if (location != DrawLocation.UPDATE) return; //如果此時繪制到屏幕上,則不會看到繪制的結果 Draw(); } private void OnPostRender() { if (location != DrawLocation.POSTRENDER) return; Draw(); } } 

5.結語

由於將mesh繪制到RenderTexture上稍微麻煩一點,還涉及到貼圖等問題,所以單獨在下一節中講解分析。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM