(1) Basic application of Graphics

1 Introduction

This article mainly draws texture and mesh for the Graphics class.

2.Draw Texture

When using the Graphics class to directly draw the Texture, since it is directly drawn on the plane, it needs to be converted to the plane pixel space, so the LoadPixelMatrix method needs to be used. For space conversion can refer to this section

2.1 ToScreen

Sample code:

    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 in the code uses the most basic method, which is to draw mainTexture to the range of new Rect(0, 0, 200, 100) on the screen. This method has many overloads, and you can choose different methods according to your needs.
When using GL.LoadPixelMatrix() (commented out part of the code) for coordinate conversion, the mainTexture will be drawn in the lower left corner of the screen, but the pixels are reversed up and down. This is due to different graphics interfaces, and the coordinate origins corresponding to textures are different. OpenGl is lower left, D3d is upper left. If you use GL.LoadPixelMatrix(0,Screen.width,Screen.height,0) the pixels are not inverted, but since the coordinate transformation matrix becomes top-to-bottom, the upper left corner of the screen is drawn.

2.1.1 Call location

Since it is drawn on the screen, it can only be called in the OnGui method and OnPostRender, and it will be cleared when it is rendered by the camera in update. But if the texture is drawn into a RenderTexture, it can be done in update.

2.2 ToTarget

Sample code:

    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); Draw the drawing result on a variable target of RenderTexture type, so the screen transformation needs to use GL.LoadPixelMatrix(0, target.width, target.height, 0); at this time, it can be called in update .

3.Draw Mesh

3.1 Call in Update

Sample code:

    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 also has many overloads, which can meet many needs. Only two examples are given in this article. One provides coordinate transformation by providing a matrix, and the other (commented out method) provides the position and rotation of the mesh. position. Since the model is drawn, it can only be called in update.

3.2 Call in OnPostRender

Calling in the rendering phase can only use the Graphics.DrawMeshNow method to make the command take effect immediately.

4. Complete code

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 Conclusion

Since it is a little troublesome to draw the mesh on the RenderTexture, and it also involves issues such as textures, the analysis will be explained in the next section separately.

Guess you like

Origin blog.csdn.net/ttod/article/details/130300185