Unity使用GL画线

阅读: 评论:0

Unity使用GL画线

Unity使用GL画线

Unity画线工具

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WD_GLSprite : MonoBehaviour {public float range = 50.0f;//顶点到中心点的距离长度public int sideCount = 3;//顶点数public Material material;//线材质public List<Vector3> positions;//坐标集合public float offsetX;//横向偏移public float offsetY;//纵向偏移public enum State {center, inner, all, none}public State state = ;//绘制状态private int isActive;//工具栏选项void Update() {if (Input.GetKeyDown(KeyCode.A)) {//StartCoroutine(new WD_LoadAssetBundle().LoadScene("file://" + Application.dataPath + "/packages/", "Project_Test"));}}void OnGUI() {GUI.Label(new Rect(10, 10, 100, 20), "顶点数:");sideCount = (int)GUI.HorizontalSlider(new Rect(10, 30, 400, 20), sideCount, 3, 40);GUI.Label(new Rect(10, 50, 100, 20), "顶心距:");range = GUI.HorizontalSlider(new Rect(10, 70, 400, 20), range, 10, 400);GUI.Label(new Rect(10, 90, 100, 20), "横向偏移:");offsetX = GUI.HorizontalSlider(new Rect(10, 110, 400, 20), offsetX, 0, 800);GUI.Label(new Rect(10, 130, 400, 20), "纵向偏移:");offsetY = GUI.HorizontalSlider(new Rect(10, 150, 400, 20), offsetY, 0, 400);isActive = GUI.Toolbar(new Rect(10, 170, 400, 20), isActive, new string[] { "外接图形", "内接图形", "全图" });state = (State)isActive;}void OnPostRender() {RotateAlgorithm();switch (state) {://绘制外接图形for (int i = 1; i < positions.Count; i++) {Vector3 posBegin = positions[i - 1];Vector3 posEnd = positions[i];DrawLine(posBegin.x, posBegin.y, posEnd.x, posEnd.y, material);}DrawLine(positions[positions.Count - 1].x, positions[positions.Count - 1].y, positions[0].x, positions[0].y, material);break;case State.inner://绘制内接图形for (int i = 0; i < positions.Count; i++) {for (int j = i + 2; j < positions.Count; j++) {if (i == 0 && j == positions.Count - 1)continue;DrawLine(positions[i].x, positions[i].y, positions[j].x, positions[j].y, material);}}break;case State.all://绘制全图for (int i = 0; i < positions.Count; i++) {for (int j = 0; j <= i; j++) {DrawLine(positions[i].x, positions[i].y, positions[i - j].x, positions[i - j].y, material);}}break;:break;}}/// <summary>/// 角度算法/// </summary>void RotateAlgorithm() {positions.Clear();float degree = 2 * Mathf.PI / sideCount;for (int i = 0; i < sideCount; i++) {Vector3 posTemp = new Vector3((Mathf.Sin(degree * i) * range) + range + offsetX, (Mathf.Cos(degree * i) * range) + range + offsetY, 0);positions.Add(posTemp);}}/// <summary>/// 绘制直线/// </summary>void DrawLine(float x1, float y1, float x2, float y2, Material mat) {GL.PushMatrix();mat.SetPass(3);GL.LoadOrtho();GL.Begin(GL.LINES);GL.Vertex3(x1 / Screen.width, y1 / Screen.height, 0);GL.Vertex3(x2 / Screen.width, y2 / Screen.height, 0);GL.End();GL.PopMatrix();}/// <summary>/// 绘制三角形/// </summary>void DrawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, Material mat) {GL.PushMatrix();mat.SetPass(3);GL.LoadOrtho();GL.Begin(GL.TRIANGLES);GL.Vertex3(x1 / Screen.width, y1 / Screen.height, 0);GL.Vertex3(x2 / Screen.width, y2 / Screen.height, 0);GL.Vertex3(x3 / Screen.width, y3 / Screen.height, 0);GL.End();GL.PopMatrix();}/// <summary>/// 绘制规则四边形/// </summary>void DrawRect(float x, float y, float width, float height, Material mat) {GL.PushMatrix();mat.SetPass(3);GL.LoadOrtho();GL.Begin(GL.QUADS);GL.Vertex3(x / Screen.width, y / Screen.height, 0);GL.Vertex3(x / Screen.width, (y + height) / Screen.height, 0);GL.Vertex3((x + width) / Screen.width, (y + height) / Screen.height, 0);GL.Vertex3((x + width) / Screen.width, y / Screen.height, 0);GL.End();GL.PopMatrix();}/// <summary>/// 绘制不规则四边形/// </summary>void DrawQuads(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, Material mat) {GL.PushMatrix();mat.SetPass(3);GL.LoadOrtho();GL.Begin(GL.QUADS);GL.Vertex3(x1 / Screen.width, y1 / Screen.height, 0);GL.Vertex3(x2 / Screen.width, y2 / Screen.height, 0);GL.Vertex3(x3 / Screen.width, y3 / Screen.height, 0);GL.Vertex3(x4 / Screen.width, y4 / Screen.height, 0);GL.End();GL.PopMatrix();}
}

本文发布于:2024-01-30 14:07:21,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170659484520540.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:画线   Unity   GL
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23