日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(六)Graphics基本应用

發布時間:2023/12/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (六)Graphics基本应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1.前言
  • 2.Draw Texture
    • 2.1 ToScreen
      • 2.1.1 調用位置
    • 2.2 ToTarget
  • 3.Draw Mesh
    • 3.1 Update中調用
    • 3.2 OnPostRender中調用
  • 4.完整代碼
  • 5.結語

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上稍微麻煩一點,還涉及到貼圖等問題,所以單獨在下一節中講解分析。

總結

以上是生活随笔為你收集整理的(六)Graphics基本应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。