Unity3D 绘制扇形/环形
2024-08-29 01:45:57
using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class Cone : MonoBehaviour { public float anglefov = ;
public int quality = ;
public int[] triangles;
public Vector2[] uvs;
public Vector3[] vertices;
public Vector3[] normals; public float dist_min = 0.0f;
public float dist_max = 1f; public MeshFilter meshFilter; // Use this for initialization
void Awake () {
triangles = new int[quality * * ];
vertices = new Vector3[quality * + ];
uvs = new Vector2[vertices.Length];
normals = new Vector3[vertices.Length]; meshFilter = GetComponent<MeshFilter>();
} // Update is called once per frame
void Update () { meshFilter.mesh.Clear(); Vector3 pos = Vector3.zero;//transform.position;
float angle_lookat = transform.eulerAngles.y;
float angle_fov = anglefov; float angle_start = angle_lookat - angle_fov;
float angle_end = angle_lookat + angle_fov;
float angle_delta = (angle_end - angle_start) / quality; float angle_curr = angle_end; for (int i = ; i < quality + ; i++)
{
Vector3 sphere_curr = new Vector3();
sphere_curr.z = Mathf.Cos(Mathf.Deg2Rad * angle_curr);
sphere_curr.x = Mathf.Sin(Mathf.Deg2Rad * angle_curr); Vector3 pos_curr_min = pos + sphere_curr * dist_min;
Vector3 pos_curr_max = pos + sphere_curr * dist_max; vertices[ * i + ] = pos_curr_min;
vertices[ * i + ] = pos_curr_max; uvs[ * i + ] = new Vector2((float)(quality-i) / quality, );
uvs[ * i + ] = new Vector2((float)(quality-i) / quality, ); normals[ * i + ] = Vector3.up;
normals[ * i + ] = Vector3.up; angle_curr -= angle_delta;
} for (int i = ; i < quality; i++)
{
// 5---3---1
// | /| /|
// | / | / |
// |/ |/ |
// 4---2---0 int index_min_cur = i * + ;
int index_max_cur = i * + ;
int index_min_next = i * + ;
int index_max_next = i * + ; triangles[ * i + ] = index_min_cur;
triangles[ * i + ] = index_min_next;
triangles[ * i + ] = index_max_cur;
triangles[ * i + ] = index_min_next;
triangles[ * i + ] = index_max_next;
triangles[ * i + ] = index_max_cur; } meshFilter.sharedMesh.vertices = vertices;
meshFilter.sharedMesh.triangles = triangles;
meshFilter.sharedMesh.uv = uvs;
}
}
最新文章
- chrome浏览器 开发者工具简介
- Last-Modified、If-Modified-Since 实现缓存和 OutputCache 的区别
- PHP获取一年有几周以及每周开始日期和结束日期
- java类中成员的划分
- lua table序列化和反序列化
- linux下shell脚本学习
- 第二百八十九天 how can I 坚持
- int、bigint、smallint 和 tinyint
- Poco::TCPServer框架解析
- 无法远程连接mysql,连接后也没有权限创建数据库
- 翻译连载 | 附录 B: 谦虚的 Monad-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
- 一个BAT老程序员的忠告!
- exit(0)与exit(1)、return的区别
- DevOps 解决方案 - 腾讯云
- Return array from functions in C++
- Android 包信息工具类
- 斯皮尔曼等级相关(Spearman’s correlation coefficient for ranked data)
- zuul源码分析-探究原生zuul的工作原理
- 在linux上配置Django项目
- ES6--Set之再理解