Unity3D 实操OpenGL函数
来源:互联网 发布:网络咨询医生话术 编辑:程序博客网 时间:2024/06/05 00:27
今天让大家学习一下Unity3d里面的OpenGL 自带的API ,我们可以通过简单的使用OpenGL来在场景中利用相机渲染绘制图形。官方学习文档:https://docs.unity3d.com/ScriptReference/GL.html
1.第一步:通过相关官方的文档,我们可以了解GL接口以及一些标准OpenGL函数;
2.第二步:我们直接进入实操模式;
2.0 新建项目,在场景中创建Camera,并且附加OpenGLTest.cs 组件;
2.1 OpenGLTest.cs 代码如下(具体可以直接参考官方文档);
using System.Collections;using System.Collections.Generic;using UnityEngine;public class OpenGLTest : 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); } }}
2.2 运行项目看绘制的线条;
阅读全文
0 0
- Unity3D 实操OpenGL函数
- OpenGL 内置函数 和 unity3d 内置函数对照表
- uniGUI 实操感受
- DB2 卸载实操
- LINUX命令实操
- 跨境电商运营实操
- CocoaPods实操
- 冒泡排序实操
- 工厂模式实操
- Linux 实操
- C#多线程实操
- sqlite 数据库实操
- 拆装机实操
- Docker入门实操
- OpenGL函数
- OpenGL函数
- openGL函数
- OpenGL函数
- inux 日志切割工具cronolog配置步骤
- PR菜鸟入门 -- PR下载安装
- 词根词缀
- Nginx基于TCP的负载均衡的配置例子
- iOS UI学习之路07
- Unity3D 实操OpenGL函数
- 多线程知识巩固(六) dispatch_semaphore_t
- 如何将本地jar批量同步到nexus私服
- String、StringBuffer与StringBuilder之间区别
- Codevs1250 Fibonacci数列
- Java 继承
- vue2.0
- OPENGL—改进Bresenham画直线
- 内网转发ngrok