zoukankan      html  css  js  c++  java
  • Unity使用OpenGL绘制线段

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ShowGrid : MonoBehaviour {
        public int lineCount = 80;
        public float radius = 4f;
    
        static Material lineMaterial;
    
    
        public void OnRenderObject()
        {
            CreateLineMaterial();
            // Apply the line material
            lineMaterial.SetPass(0);
    
            GL.PushMatrix();
            // Set transformation matrix for drawing to
            // match our transform
            GL.MultMatrix(transform.localToWorldMatrix);
    
            // Draw lines
            GL.Begin(GL.LINES);
            for (int i = 0; i < lineCount; ++i)
            {
                float a = i / (float)lineCount;
                float angle = a * Mathf.PI * 2;
                // Vertex colors change from red to green
                GL.Color(new Color(a, 1 - a, 0, 0.8F));
                // One vertex at transform position
                GL.Vertex3(0, 0, 0);
                // Another vertex at edge of circle
                GL.Vertex3(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius, 0);
            }
            GL.End();
            GL.PopMatrix();
        }
    
    
        void OnPostRender()
        {
            // Set your materials
            GL.PushMatrix();
            // yourMaterial.SetPass( );
            // Draw your stuff
            GL.PopMatrix();
    
        }
    
        static void CreateLineMaterial()
        {
            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);
                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
                lineMaterial.SetInt("_ZWrite", 0);
            }
        }
    
    }
  • 相关阅读:
    如何配置wamp多站点主机
    一些类和对象问题的探索,简单易懂的命名空间及use的使用
    [4] Git使用流程
    [正则] JS常用正则
    [3] Django返回json数据
    [8] Eclipse各版本代号一览表以及官网上有很多版本的eclipse的比较
    [7] MySQL数据库--学生管理系统数据库设计
    [11]Docker02 Docker重要概念
    [12]Docker03 Centos7安装Docker
    [小程序]小程序环境搭建
  • 原文地址:https://www.cnblogs.com/coolbear/p/8674892.html
Copyright © 2011-2022 走看看