[Unity] Unity3D  实操OpenGL函数

查看:834 |回复:2 | 2017-5-22 17:03:49

您需要 登录 才可以下载或查看,没有账号?注册

x
今天,和大家学习一下unity3d 里面的OpenGL 自带的API ,我们可以通过简单的使用OpenGL来在场景中利用相机渲染绘制图形。
   
官方学习文档:https://docs.unity3d.com/ScriptReference/GL.html
    1.第一步,通过相关官方的文档,我们可以了解GL接口以及一些标准OpenGL函数;

    2.第二步,我们直接进入实操模式;

      2.0 新建项目,在场景中创建Camera,并且附加OpenGLTest.cs 组件;
upload-ueditor-image-20170522-1495422333682062087.jpg

      2.1 OpenGLTest.cs 代码如下(具体可以直接参考官方文档);
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;

  4. public class OpenGLTest : MonoBehaviour {

  5.         public int lineCount = 80;
  6.         public float radius = 4f;

  7.         static Material lineMaterial;


  8.         public void OnRenderObject()
  9.         {
  10.                 CreateLineMaterial();
  11.                 // Apply the line material
  12.                 lineMaterial.SetPass(0);

  13.                 GL.PushMatrix();
  14.                 // Set transformation matrix for drawing to
  15.                 // match our transform
  16.                 GL.MultMatrix(transform.localToWorldMatrix);

  17.                 // Draw lines
  18.                 GL.Begin(GL.LINES);
  19.                 for (int i = 0; i < lineCount; ++i)
  20.                 {
  21.                         float a = i / (float)lineCount;
  22.                         float angle = a * Mathf.PI * 2;
  23.                         // Vertex colors change from red to green
  24.                         GL.Color(new Color(a, 1 - a, 0, 0.8F));
  25.                         // One vertex at transform position
  26.                         GL.Vertex3(0, 0, 0);
  27.                         // Another vertex at edge of circle
  28.                         GL.Vertex3(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius, 0);
  29.                 }
  30.                 GL.End();
  31.                 GL.PopMatrix();
  32.         }

  33.        
  34.         void OnPostRender()
  35.         {
  36.                 // Set your materials
  37.                 GL.PushMatrix();
  38.                 // yourMaterial.SetPass( );
  39.                 // Draw your stuff
  40.                 GL.PopMatrix();

  41.         }

  42.         static void CreateLineMaterial()
  43.         {
  44.                 if (!lineMaterial)
  45.                 {
  46.                         // Unity has a built-in shader that is useful for drawing
  47.                         // simple colored things.
  48.                         Shader shader = Shader.Find("Hidden/Internal-Colored");
  49.                         lineMaterial = new Material(shader);
  50.                         lineMaterial.hideFlags = HideFlags.HideAndDontSave;
  51.                         // Turn on alpha blending
  52.                         lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  53.                         lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  54.                         // Turn backface culling off
  55.                         lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
  56.                         // Turn off depth writes
  57.                         lineMaterial.SetInt("_ZWrite", 0);
  58.                 }
  59.         }
  60. }
点击此处复制文本

      2.2 运行项目看绘制的线条;
    upload-ueditor-image-20170522-1495422585477055306.jpg



2017-5-22 17:03:49  
 赞 赞 0

使用道具 登录

2个回答,把该问题分享到群,邀请大神一起回答。
2#
试试
回复 收起回复
2018-2-5 17:58:26   回复
 赞 赞 0

使用道具 登录

3#
你们这么牛逼啊
回复 收起回复
2023-2-28 11:09:52   回复
 赞 赞 0

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表