首页 > 其他分享 >【Unity】GL绘制图形和网格

【Unity】GL绘制图形和网格

时间:2024-10-14 08:48:58浏览次数:1  
标签:void 网格 private height Unity GL Screen rect

新建 GLSquare脚本,绑定主相机。

Helper

public class GLSquare : MonoBehaviour
{
	//线材质
	private Material m_material;
	
	//在摄像机渲染场景后,将调用 OnRenderObject。
	private void OnRenderObject()
	{
	    Draw();//调用 绘制
	}
	
	//绘制调用 Begin end
	void Begin()
	{
	    if (m_material == null)
	        //shader选择GUI/Text Shader后打包的软件可以看见线条
	        m_material = new Material(Shader.Find("GUI/Text Shader");//("Unlit/Color"));
	    m_material.SetPass(0);
	    //GL.LoadOrtho();//正交视图
	    GL.Begin(GL.LINES);
	    m_material.SetColor("_Color", Color.white);//定义颜色
	}
	void End()
	{
	    GL.End();
	}
	
	void Draw()
	{
	    Begin();
	
	    //若干 GL.Vertex 绘制 两个点一条线
	    for (int i = 0; i < m_linePoints.Count; i++)
	    {        
	        GL.Vertex(m_linePoints[i][0]);
	        GL.Vertex(m_linePoints[i][1]);
	    }
	
	    End();
	}
	public static GLSquare Instance;
	private void Awake()
	{
	    Instance = this;
	
	    initPoints();//定义顶点
	}
}

直接取点绘制网格

//定义顶点
//此处举例: 10*10 单元格为1的网格
List<Vector3[]> m_linePoints = new List<Vector3[]>();
void initPoints()
{
    (-5, 0, -5)(5, 0, -5)
    (-5, 0, 5)(-5, 0, -5)
    (-5, 0, -4)(5, 0, -4)
    (-4, 0, 5)(-4, 0, -5)
    ...
    for (int i = -5; i <= 5; i++)
    {
        Vector3[] rows = new Vector3[2];
        rows[0] = new Vector3(-5, 0, i);
        rows[1] = new Vector3(5, 0, i);
        m_linePoints.Add(rows);
        Vector3[] clos = new Vector3[2];
        clos[0] = new Vector3(i, 0, 5);
        clos[1] = new Vector3(i, 0, -5);
        m_linePoints.Add(clos);
    }
}

绘制标准网格

//调用,参数 单元格边长,网格宽度和高度(单元格个数)
//initPoints(float cellsize, int width, int height)

private bool isReady = false;

//此方法类似update是一只刷新的
private void OnRenderObject()
{
    //isReady作为开关
    if (isReady)
    {
        DrawArea();
    }
}

public void clearLine() {
    m_linePoints.Clear();
}

//定义顶点
public void initPoints(float cellsize, int width, int height)
{
    //重新绘制时,清楚之前的绘制图案
    clearLine();
    for (float i = 0; i <= height; i++)
    {
        Vector3[] rows = new Vector3[2];
        rows[0] = new Vector3(0, 0, i * cellsize);
        rows[1] = new Vector3(width * cellsize, 0, i * cellsize);
        m_linePoints.Add(rows);
    }
    for (float i = 0; i <= width; i++)
    {
        Vector3[] clos = new Vector3[2];
        clos[0] = new Vector3(i * cellsize, 0, height * cellsize);
        clos[1] = new Vector3(i * cellsize, 0, 0);
        m_linePoints.Add(clos);
    }
    isReady = true;
}

正交视图鼠标框选矩形

private Vector3 m_startPos;
private Vector3 m_endPos;

private bool Drow = false;

private void OnRenderObject()
{
    if(Drow)
    {
        DrawAreaByPos();
    }
}

private void Update()
{
    //鼠标框选:鼠标按下,抬起 点击屏幕,拖拽鼠标,绘制矩形
    //鼠标按下,开始绘制
    if (Input.GetMouseButtonDown(0))
    {
        Drow = true;
        m_startPos = GetScreenTouchPos();//起点
    }

    if (Drow)
    {
        m_endPos = GetScreenTouchPos();
    }
	//鼠标抬起,
    if (Input.GetMouseButtonUp(0))
    {
        Drow = false;
        m_startPos = Vector3.Zero;
        m_endPos = Vector3.Zero;
        DrawAreaByPos();
    }
}
public static Vector3 GetScreenTouchPos()
{
    return GetTouchPosition();
}

public static Vector3 GetTouchPosition()
{
    #if UNITY_EDITOR || UNITY_STANDALONE_WIN
        return Input.mousePosition;
    #else
        return Input.touches[0].position;
    #endif
}
}

void DrawAreaByPos()
{
    DrawArea(
        new Rect(
            math.min(m_startPos.x, m_endPos.x), 
            math.min(m_startPos.y, m_endPos.y),
            math.max(m_startPos.x, m_endPos.x) - math.min(m_startPos.x, m_endPos.x),
            math.max(m_startPos.y, m_endPos.y) - math.min(m_startPos.y, m_endPos.y)
        )
    );
}
//绘制顶点坐标
void DrawArea(Rect rect)
{
    Begin();

    GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
    GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0);

    GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
    GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0);

    GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
    GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0);

    GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
    GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0);

    End();
}

标签:void,网格,private,height,Unity,GL,Screen,rect
From: https://www.cnblogs.com/sitarblogs/p/18463391

相关文章

  • 在启智AI平台实践ChatGLM4-9B聊天机器人@MindSpore
    前段时间在昇思训练营发现一个好东西,就是昇思AI实验室:昇思大模型平台在官方提供的jupyterAI编程实践样例中,发现了这个项目:ChatGLM4-9B实践样例GLM-4-9B是智谱AI推出的最新一代预训练模型GLM-4系列中的开源版本。在语义、数学、推理、代码和知识等多方面的数据集测评中,......
  • vs断点调试unity安卓包
    要在Android设备上使用VisualStudio调试Unity项目并设置断点,你可以按照以下步骤进行配置。通过这个调试方式,你可以实时调试Unity应用,查看变量的状态,进行断点调试,从而更好地发现和解决问题。前提条件已安装Unity和VisualStudio:你需要安装Unity和带有Unity工具......
  • ERROR [org.hibernate.transaction.JDBCTransaction] - Could not toggle autocommit
    错误描述: DEBUG[org.hibernate.SQL]-SELECTorp.ATTR6FROMDISTRIBUT_VIEWd WHEREd.state='1'ANDd.oper_logLIKE'%下单%' GROUPBYorp.ATTR6 ERROR[org.hibernate.transaction.JDBCTransaction]-Couldnottoggleautocommitjava.sql.SQLE......
  • TypeError: add_triangle_mesh(): incompatible function arguments. The following a
    12024.10.1214:52Traceback(mostrecentcalllast):File"terrain_creation.py",line119,in<module>gym.add_triangle_mesh(sim,vertices.flatten(),triangles.flatten(),tm_params)TypeError:add_triangle_mesh():incompatiblefunct......
  • Unity 摄像机照再RawImg上
    publicvoidRefresh(){if(!renderImage||!renderCamera)return;varrect=renderImage.rectTransform.rect;varw=(int)rect.width;varh=(int)rect.height;if(w==0)w=Sc......
  • C221110C. SolarPea与网格
    C221110C.SolarPea与网格是怎么想到dp定义的?思考下面这个情景:如果一个人在\(x\),另一个人在\(y\(x\lty)\),那么在\(x\)的人会把\(x\lti\lty\)的所有\(i\)全走一遍,走完之后\(x+1=y\)。对于这个情景,我们想到记\(f[i]\)表示一个人在\(i-1\),一个人在......
  • 【Unity基础】Unity用脚本实现内购(IAP)
    本文介绍了如何使用脚本实现内购功能。先看下脚本,代码中根据执行过程添加了序号。usingUnityEngine;usingUnityEngine.Purchasing;usingUnityEngine.UI;namespaceSamples.Purchasing.Core.BuyingConsumables{publicclassBuyingConsumables:MonoBehaviour,......
  • 避雷!Google Adsense联盟营销七大投放误区
    你是否在使用GoogleAdSense进行广告投放?你是否想进一步优化你的投放策略?那么这篇文章你不可错过啦!GoogleAdSense为跨境商家提供了一个平台,我们可以通过展示相关广告来赚取收入。然而,即使是最有经验的商家也可能在广告投放策略上犯一些常见的错误,而这些错误可能会影响我们的......
  • Unity实现3D模型子物体伸出折线并显示各自名称的功能
    一、灵感    在unity项目中我们经常会遇到很多介绍产品的场景需求,所以咱干脆写一个脚本来简单介绍把!话不多说,直接开干!二、场景搭建1、搭建一个简单的场景,创建一个cube作为最大的父物体,接着再创建一个球和一个圆柱作为cube的子物体。具体场景结构如下图:三、实现......
  • 【unity】内置鼠标监听方法(小白版)--当鼠标放置到技能按钮处显示该技能的描述
    为了实现鼠标放置到技能按钮处显示该技能描述的效果,参考了许多资料,由于我是初学者,研究了许久才看明白,现在分享一下学习心得。效果展示图代码如下usingUnityEngine;usingUnityEngine.EventSystems;usingUnityEngine.UI;publicclassSkillDataDisplay:MonoBehaviou......