Unity GL
Unity GL
2023-09-27 14:25:33 时间
画线:
DrawLine.cs 脚本挂到一个新的 GameObject 上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 屏幕坐标画线
/// </summary>
public class DrawLine : MonoBehaviour {
private static Material s_lineMaterial;
private static void CreateLineMaterial () {
// 如果材质球不存在
if (!s_lineMaterial) {
// 用代码的方式实例一个材质球
Shader shader = Shader.Find("Hidden/Internal-Colored");
s_lineMaterial = new Material(shader);
s_lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// 设置参数
s_lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
s_lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// 设置参数
s_lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// 设置参数
s_lineMaterial.SetInt("_ZWrite", 0);
}
}
private void OnRenderObject () {
// 创建材质球
CreateLineMaterial();
// 激活第一个着色器 Pass(本例中,它是唯一的 Pass)
s_lineMaterial.SetPass(0);
// 渲染入栈 在Push——Pop之间写GL代码
GL.PushMatrix();
{
// 1.用屏幕坐标绘图,移动对象时图像保持不动
GL.LoadPixelMatrix();
// 2.使用局部坐标绘图,移动对象时图像也会移动
// 矩阵相乘,将物体坐标转化为世界坐标
//GL.MultMatrix(transform.localToWorldMatrix);
// 1,2 都不使用时,使用世界坐标绘图,移动对象时图像保持不动
// 开始画线 在Begin——End之间写画线方式
// GL.LINES 画线
GL.Begin(GL.LINES);
{
GL.Color(Color.green);
GL.Vertex3(0, 0, 0);
GL.Vertex3(500, 500, 0);
}
GL.End();
}
// 渲染出栈
GL.PopMatrix();
}
}
相关文章
- Unity Batches与glDrawElements的关系
- 【Unity 3D 游戏开发】Unity3D 入门 - 工作区域介绍 与 入门演示样例
- 《Unity虚拟现实开发实战》——第3章,第3.4节为Google Cardboard构建项目
- 《Unity 3D NGUI 实战教程》一2.5 创建第一个UI
- 【Unity Shader入门】Shader数学基础:矩阵变换
- Unity Shader 之 Stencil 模板测试相关讲解,及简单实例
- Unity 进阶 之 判断基于物体所处位置(前后左右)/ 方法原理 /注意事项的简单整理
- Unity ML-Agents 之 环境配置(Anaconda 下载安装等),简单的搭建场景实现简单训练的Demo(内含详细步骤)
- Unity Android Studio 混合开发整理(Unity 嵌入Android Studio 中)
- Unity 协程
- 关于Unity中的光照(四)