unity3d使用GL库画线

unity使用GL库画线


unity3d使用GL库画线_第1张图片
image.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Display the search grid with lines and quads
public class DisplayGrid : MonoBehaviour
{
    //Should we display the grid
    public bool shouldDisplayGrid;

    //The color of the grid - black is too dark
    public Color gridColor = new Color(0.4f, 0.4f, 0.4f);

    //The height of the grid
    public  float gridHeight = 0.015f;


    private void Start()
    {
        shouldDisplayGrid = false;
    }


    //Change if we should display the grid
    public void ChangeDisplay()
    {
        shouldDisplayGrid = !shouldDisplayGrid;
    }


    //Lines should be drawn in OnRenderObject and not in Update
    private void OnRenderObject()
    {
        if (shouldDisplayGrid)
        {
            DisplayGridWithLines();
        }
    }


    //Map data
    //The size of all cells in [m]
    public  int mapWidth = 160;
    //The size of one cell in [m]
    public  float cellWidth = 1f;

    //Display the grid with lines
    private void DisplayGridWithLines()
    {
        Material lineMaterial = GetLineMaterial();

        //Use this material
        //If you dont call SetPass, then you'll get basically a random material (whatever was used before) which is not good
        lineMaterial.SetPass(0);

        GL.PushMatrix();

        //Set transformation matrix for drawing to match the transform
        GL.MultMatrix(transform.localToWorldMatrix);

        //Begin drawing 3D primitives
        GL.Begin(GL.LINES);

        GL.Color(gridColor);

        float xCoord = 0f;
        float zCoord = 0f;

        //The height is actually in local coordinates
        float lineHeight = gridHeight;

        int gridSize = mapWidth;

        float cellSize = cellWidth;

        for (int x = 0; x <= gridSize; x++)
        {
            //x
            Vector3 lineStartX = new Vector3(xCoord, lineHeight, zCoord);

            Vector3 lineEndX = new Vector3(xCoord, lineHeight, zCoord + (gridSize * cellSize));

            //Draw the line
            GL.Vertex(lineStartX);
            GL.Vertex(lineEndX);


            //z
            Vector3 lineStartZ = new Vector3(zCoord, lineHeight, xCoord);

            Vector3 lineEndZ = new Vector3(zCoord + (gridSize * cellSize), lineHeight, xCoord);

            //Draw the line
            GL.Vertex(lineStartZ);
            GL.Vertex(lineEndZ);

            xCoord += cellSize;
        }

        GL.End();

        GL.PopMatrix();
    }







    private Material lineMaterial;

    //
    // Create a material for the line used to display the grid
    //
    public Material GetLineMaterial()
    {
        if (!lineMaterial)
        {
            //Unity has a built-in shader that is useful for drawing simple colored things
            Shader shader = Shader.Find("Hidden/Internal-Colored");

            lineMaterial = new Material(shader);

            //So the material is not saved anywhere
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;

            //Turn on alpha blending
            //lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            //lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);

            //Turn backface culling off
            //lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);

            //Turn off depth writes to make it transparent
            //lineMaterial.SetInt("_ZWrite", 0);

            //If you want the lines to render "above" the object
            //lineMaterial.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
        }

        return lineMaterial;
    }




}


你可能感兴趣的:(unity3d使用GL库画线)