Unity several ways to draw lines

【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. 例1:屏幕画线

  2.  
  3. using UnityEngine;

  4. using System.Collections;

  5.  
  6. public class GLTest : MonoBehaviour {

  7.  
  8. public Material mat;

  9. void OnPostRender() {

  10. if (!mat) {

  11. Debug.LogError("Please Assign a material on the inspector");

  12. return;

  13. }

  14. GL.PushMatrix(); //保存当前Matirx

  15. mat.SetPass(0); //刷新当前材质

  16. GL.LoadPixelMatrix();//设置pixelMatrix

  17. GL.Color(Color.yellow);

  18. GL.Begin(GL.LINES);

  19. GL.Vertex3(0, 0, 0);

  20. GL.Vertex3(Screen.width, Screen.height, 0);

  21. GL.End();

  22. GL.PopMatrix();//读取之前的Matrix

  23. }

  24. }

 

 
  1. 例2:截图

  2.  
  3. using System.IO;

  4. using UnityEngine;

  5. using System.Collections;

  6.  
  7. public class ScreenShot : MonoBehaviour {

  8. void Start() {

  9. StartCoroutine(UploadPNG() );

  10. }

  11. IEnumerator UploadPNG() {

  12. yield return new WaitForEndOfFrame();

  13. print ("yuuuuu");

  14. int width = Screen.width;

  15. int height = Screen.height;

  16. Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

  17. tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);

  18. tex.Apply();

  19. byte[] bytes = tex.EncodeToPNG();

  20. File.WriteAllBytes(Application.dataPath+"/ss.png",bytes);

  21. UnityEditor.AssetDatabase.Refresh();

  22. }

  23. }

 

 
  1. 例3:展示Alpha

  2.  
  3.  
  4. using UnityEngine;

  5. using System.Collections;

  6.  
  7. public class GLTest : MonoBehaviour {

  8. public Shader shader;

  9. public Texture2D t2d;

  10. private Material mat;

  11. void Start()

  12. {

  13. mat = new Material(shader);

  14. mat.mainTexture = t2d;

  15. }

  16. void OnPostRender() {

  17. if (!mat) {

  18. Debug.LogError("Please Assign a material on the inspector");

  19. return;

  20. }

  21. GL.PushMatrix();

  22. mat.SetPass(0);

  23. GL.LoadOrtho();

  24. GL.Begin(GL.QUADS);

  25. GL.Vertex3(0, 0, 0.1F);

  26. GL.Vertex3(1f, 0, 0.1F);

  27. GL.Vertex3(1f, 1, 0.1F);

  28. GL.Vertex3(0, 1, 0.1F);

  29. GL.End();

  30. GL.PopMatrix();

  31. }

  32. }

  33. Shader "Custom/GLDrawLine" {

  34. Properties {

  35. _MainTex ("Base (RGB)", 2D) = "white" {}

  36. }

  37. SubShader {

  38. Pass {

  39. Cull off

  40. Blend DstAlpha zero

  41. Color(1,1,1,1)

  42. }

  43. }

  44. }

Guess you like

Origin blog.csdn.net/u011105442/article/details/110789602