美文网首页
Unity GL.Color设置颜色无效

Unity GL.Color设置颜色无效

作者: 114105lijia | 来源:发表于2022-12-28 14:16 被阅读0次

需要设置材质 lineMaterial.SetPass(0);

Material lineMaterial;

void CreateLineMaterial()
    {
        Shader shader = Shader.Find("Hidden/Internal-Colored");
        lineMaterial = new Material(shader);
        lineMaterial.hideFlags = HideFlags.HideAndDontSave;
        //设置参数
        lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
        lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
        //设置参数
        lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
        //设置参数
        lineMaterial.SetInt("_ZWrite", 0);
    }

    private void Awake()
    {
        CreateLineMaterial();
    }

private void OnPostRender()
    {
        DrawMutilLine();
    }

void DrawMutilLine()
    {
        List<Vector3> posList = new List<Vector3>();
        posList.Add(new Vector3(0.3f, 0.6f, 0));
        posList.Add(new Vector3(1.2f, 1.8f, 0));
        posList.Add(new Vector3(0.6f, 2.4f, 0));
        posList.Add(new Vector3(1.5f, 2.7f, 0));

        if (posList.Count < 2) return;

        lineMaterial.SetPass(0);

        GL.Begin(GL.LINES);
        GL.Color(Color.green);
        for (int i = 0; i < posList.Count - 1; i++)
        {
            GL.Vertex(posList[i]);
            GL.Vertex(posList[i + 1]);
        }
        GL.End();
    }

相关文章

网友评论

      本文标题:Unity GL.Color设置颜色无效

      本文链接:https://www.haomeiwen.com/subject/obwoqdtx.html