• (三)平面圆形


    概述

    由于基础篇已经比较详细,此篇只贴代码。

    平面圆形代码

    圆形也是由三角形组成的,三角形个数越多则越园。

    基类

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    [RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
    public class CreateMeshBase : MonoBehaviour
    {
        MeshFilter meshFilter;
    
        protected Mesh mesh;
    
        protected virtual Vector3[] Vertices { get; }
        protected virtual int[] Triangles { get; }
        protected virtual Vector3[] Normals { get; }
        protected virtual Vector2[] Uvs { get; }
        protected virtual string MeshName { get; }
    
        protected virtual void Start()
        {
            GetMeshFilter();
        }
    
        protected virtual void Reset()
        {
            GetMeshFilter();
        }
    
        protected virtual void OnValidate()
        {
            GetMeshFilter();
        }
    
        void GetMeshFilter()
        {
            if (meshFilter == null)
            {
                meshFilter = GetComponent<MeshFilter>();
                mesh = new Mesh();            
            }
    
            mesh.triangles = null;
            mesh.uv = null;
            mesh.vertices = null;
    
            mesh.name = MeshName;
            mesh.vertices = Vertices;
            mesh.triangles = Triangles;
            mesh.uv = Uvs;
    
            meshFilter.mesh = mesh;
        }
    
        private void OnDrawGizmos()
        {
            if (Vertices == null) return;
    
            Gizmos.color = Color.red;
            Gizmos.DrawSphere(Vector3.zero, 0.5f);
    
            Gizmos.color = Color.blue;
    
            for (int i = 0; i < Vertices.Length; i++)
            {
                Gizmos.DrawSphere(Vertices[i], 0.3f);
            }
        }
    }
    
    

    圆形网格

    其中triCount控制三角形个数,radius控制圆大小。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CreateCircle : CreateMeshBase
    {
        [Range(3,100)]
        public int triCount = 6;
        public float radius = 5;
        public bool showHalf = false;
    
        protected override string MeshName
        {
            get
            {
                return "Circle Mesh";
            }
        }
    
        protected override Vector3[] Vertices
        {
            get
            {
                Vector3[] vertices = new Vector3[triCount + 1];
                vertices[0] = Vector3.zero;
                float angleDelta = 2 * Mathf.PI / triCount;
    
                for (int i = 0; i < triCount; i++)
                {
                    float angle = angleDelta * i;
                    float x = radius * Mathf.Cos(angle);
                    float y = radius * Mathf.Sin(angle);
    
                    vertices[i + 1] = new Vector3(x, y, 0);
                }
    
                return vertices;
            }
        }
    
        protected override int[] Triangles
        {
            get
            {
                int[] triangles = new int[triCount * 3];
    
                for (int i = 0; i < triCount; i++)
                {
                    if (showHalf)
                    {
                        if (i % 2 == 0) continue;
                    }
    
                    triangles[i * 3] = 0;
                    triangles[i * 3 + 2] = i + 1;
    
                    if (i + 2 > triCount)
                    {
                        triangles[i * 3 + 1] = 1;
                    }else
                    {
                        triangles[i * 3 + 1] = i + 2;
                    }
                }
                return triangles;
            }
        }
    
        protected override Vector2[] Uvs
        {
            get
            {
                Vector2[] uvs = new Vector2[triCount + 1];
                uvs[0] = new Vector2(0.5f,0.5f);
                float angleDelta = 2 * Mathf.PI / triCount;
    
                for (int i = 0; i < triCount; i++)
                {
                    float angle = angleDelta * i;
                    float x = Mathf.Cos(angle) * 0.5f + 0.5f;
                    float y = Mathf.Sin(angle) * 0.5f + 0.5f;
    
                    uvs[i + 1] = new Vector2(x, y);
                }
                return uvs;
            }
        }
    }
    
    
  • 相关阅读:
    各种算法七
    各种算法六
    使用URLConnection调用axis1.4开发的webservice
    JDBC结果集rs.next()注意事项
    URLConnection调用接口
    axis1.4开发webservice客户端(快速入门)-基于jdk1.4
    axis1.4开发webservice服务端(快速入门)-基于jdk1.4
    FMDB数据库的简单实用
    Xcode5 取消项目ARC,或者单个类ARC切换
    用CornerStone配置SVN,HTTP及svn简单使用说明
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/12253053.html
Copyright © 2020-2023  润新知