【Foreword】
Graphical debugging can speed up development.
For example, in battle, you may need to know the hatred value of all units. If all this information is logged, it is difficult to have an intuitive feeling.
And if there is a ball above the unit's head in the Scene window, the redder means the higher the hatred, and the darker means the lower the hatred, then debugging is more intuitive than logging.
[One graphical debugging]
There are four main types of graphical debugging in Unity
Debug.Draw
Gizmos.Draw
Graphic.DrawMesh
GL
Just the debug image displayed in the Scene window
Always displayed OnDrawGizmos + Gizmos.Draw
Select the displayed OnDrawGizmosSelected + Gizmos.Draw
Script-controlled Update + Debug.Draw
The debugging image that needs to be displayed on the actual device screen
Update+Graphic.DrawMesh
OnRenderObject + GL
Graphic.DrawMesh and Debug.Draw call are the same, both are in the Update system
Graphic.DrawMesh is similar to GL display, both are displayed in each window, and the material can be set.
Comparison of four ways
(1)Debug.Draw
=1=Generally called in Update/Fixed Update/LateUpdate
=2=Only displayed in the Scene window
=3=and cannot set the material
void Update()
{
Debug.DrawLine (worldPos1, worldPos2,Color.yellow);
}
(2)Gizmos.Draw
=1=Called in OnDrawGizmos /OnDrawGizmosSelected
=2=Only displayed in the Scene window
=3=and cannot set the material
public void OnDrawGizmosSelected() {
Gizmos.DrawLine(Vector3.zero, new Vector3(0,3f,0));
}
(3)Graphic.DrawMesh
=1=Generally called in Update/Fixed Update/LateUpdate
=2=Both the actual screen and the Scene window can be displayed
=3=The material can be set
画Mesh Ok
void Update()
{
Graphics.DrawMesh(mesh, worldPos, worldRotation, material, 0);
}
(4)GL,
=1=Generally called in OnRenderObject of the object or OnPostRender of the camera
=2=Both the actual screen and the Scene window can be displayed
=3=The material can be set
The rendering in a GL.Begin/GL.End is automatically merged, usually a Drawcall
Draw some lines, triangles are fine. If you use GL . TRIANGLES to display the entire Mesh, it will be super stuck.
Example: rendering wireframe
void OnRenderObject ()
{ mat.SetPass (0);
GL.wireframe = true;
GL.Color (new Color (1,1, 0, 0.8F));
GL.PushMatrix();
GL.Begin(GL.TRIANGLES);
for(int i=0;i<</span>mesh.triangles.Length-2;i+=3)
{
GL.Vertex(mesh.vertices[mesh.triangles[i]]);
GL.Vertex(mesh.vertices[mesh.triangles[i+1]]);
GL.Vertex(mesh.vertices[mesh.triangles[i+2]]);
}
GL.End();
GL.PopMatrix();
GL.wireframe = false;
}
[2 GL]
In addition to debugging, GL can be used for functions, such as LineRenderer, grid, etc.
GL stands for Graphics Library. Low-Level Graphics Library. Calculate matrices, issue rendering commands similar to OpenGL's immediate mode, and other low-level image tasks. Graphic.DrawMesh() is more efficient than GL.
The GL immediate drawing function only uses the current material settings. So unless you specify mat, mat can be any material. And GL may change the material.
GL is executed immediately, if you call in Update(), they will be executed before the camera rendering, the camera rendering will clear the screen, and the GL effect will not be visible.
Usually the GL usage is
to paste the script on the camera and execute it in OnPostRender().
It can also be hung on any GameObject and executed in OnRenderObject().
Or hang on an object
note:
1. The basic primitives such as lines of GL do not have uv. All are not mapped by texture mapping. The shader only does a single color calculation or processes the previous image.
2. There must be a Cull off command in the shader used by GL, otherwise the display will become as follows
3. If it is a line, the color is GL .Color (new Color (1, 1, 1, 0.5f) ); the color set
If it is GL.TRIANGLES or GL.QUADS, the color is the color in the shader.
1.
GL.PushMatrix ()
Save the matrices to the matrix stack.
GL.PopMatrix()
Read matrices from the matrix stack.
2.
GL.LoadPixelMatrix()
Change the MVP matrix so that the xy in the transform directly corresponds to the pixel, (0, 0) represents the lower left corner of the screen viewport, and the range of z is (-1, 1). This function changes the camera parameters, so GL.PushMatrix() is required. Save and GL.PopMatrix() read.
The value of GL.Vertex3() ranges from (0,0,0) in the lower left corner to (Screen.width,Screen.height,0) in the upper right corner
GL.LoadOrtho ()
Set the ortho perspective, which is the horizontal perspective. After calling LoadOrtho, the viewing frustum goes from (0,0,-1) to (1,1,100). Mainly used to draw primitives in pure 2D.
GL.Vertex3() ranges from (0,0,0) in the lower left corner to (1,1,0) in the upper right corner
3.
OnPostRender ()
A function that will be called only when there is an active camera on the object. It will be called after the camera finishes rendering the scene and draws all the objects.
OnPostRender can become a co-routine, just add a yield statement.
WaitForEndOfFrame()
After waiting until all drawing, end of frame, just before displaying the frame to the screen. You can take a screenshot. You can use this function on any object.
-
例1:屏幕画线
-
using UnityEngine;
-
using System.Collections;
-
public class GLTest : MonoBehaviour {
-
public Material mat;
-
void OnPostRender() {
-
if (!mat) {
-
Debug.LogError("Please Assign a material on the inspector");
-
return;
-
}
-
GL.PushMatrix(); //保存当前Matirx
-
mat.SetPass(0); //刷新当前材质
-
GL.LoadPixelMatrix();//设置pixelMatrix
-
GL.Color(Color.yellow);
-
GL.Begin(GL.LINES);
-
GL.Vertex3(0, 0, 0);
-
GL.Vertex3(Screen.width, Screen.height, 0);
-
GL.End();
-
GL.PopMatrix();//读取之前的Matrix
-
}
-
}
-
例2:截图
-
using System.IO;
-
using UnityEngine;
-
using System.Collections;
-
public class ScreenShot : MonoBehaviour {
-
void Start() {
-
StartCoroutine(UploadPNG() );
-
}
-
IEnumerator UploadPNG() {
-
yield return new WaitForEndOfFrame();
-
print ("yuuuuu");
-
int width = Screen.width;
-
int height = Screen.height;
-
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
-
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
-
tex.Apply();
-
byte[] bytes = tex.EncodeToPNG();
-
File.WriteAllBytes(Application.dataPath+"/ss.png",bytes);
-
UnityEditor.AssetDatabase.Refresh();
-
}
-
}
-
例3:展示Alpha
-
using UnityEngine;
-
using System.Collections;
-
public class GLTest : MonoBehaviour {
-
public Shader shader;
-
public Texture2D t2d;
-
private Material mat;
-
void Start()
-
{
-
mat = new Material(shader);
-
mat.mainTexture = t2d;
-
}
-
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.Vertex3(0, 0, 0.1F);
-
GL.Vertex3(1f, 0, 0.1F);
-
GL.Vertex3(1f, 1, 0.1F);
-
GL.Vertex3(0, 1, 0.1F);
-
GL.End();
-
GL.PopMatrix();
-
}
-
}
-
Shader "Custom/GLDrawLine" {
-
Properties {
-
_MainTex ("Base (RGB)", 2D) = "white" {}
-
}
-
SubShader {
-
Pass {
-
Cull off
-
Blend DstAlpha zero
-
Color(1,1,1,1)
-
}
-
}
-
}