zoukankan      html  css  js  c++  java
  • (四)平面环形

    概述

    本文在上一篇平面圆形基础上生成平面环形。

    代码

    基类

    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);
            }
        }
    }
    
    

    环形类

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CreateRing : CreateMeshBase
    {
        [Range(3,100)]
        public int count = 4;
        public float outerRadius = 8;
        public float innerRadius = 4;
    
        public bool showHalf = false;
    
        protected override string MeshName
        {
            get
            {
                return "Ring Mesh";
            }
        }
    
        protected override Vector3[] Vertices
        {
            get
            {
                Vector3[] vertices = new Vector3[count * 2];
                float delta = 2 * Mathf.PI / count;
    
                for (int i = 0; i < count; i++)
                {
                    int a = i * 2;
                    float sinValue = Mathf.Sin(i * delta);
                    float cosValue = Mathf.Cos(i * delta);
    
                    vertices[a] = new Vector3(innerRadius * cosValue, innerRadius * sinValue, 0);
                    vertices[a + 1] = new Vector3(outerRadius * cosValue, outerRadius * sinValue, 0);
    
                    //Debug.LogFormat("Uv value of {0} and {1} is {2} and {3}", a, a + 1, vertices[a], vertices[a + 1]);
                }
                return vertices;
            }
        }
    
        protected override int[] Triangles
        {
            get
            {
                int[] triangles = new int[count * 2 * 3];
    
                for (int i = 0; i < count; i++)
                {
                    if (showHalf)
                    {
                        if (i % 2 == 0) continue;
                    }
    
                    int baseIndex = i * 6;
                    int vertexIndex = 2 * i;
    
    
                    triangles[baseIndex] = vertexIndex;
                    triangles[baseIndex + 2] = vertexIndex + 1;
    
                    if (i >= count - 1)
                    {
                        triangles[baseIndex + 1] = 0;
                        triangles[baseIndex + 3] = 0;
                        triangles[baseIndex + 5] = vertexIndex + 1;
                        triangles[baseIndex + 4] = 1;
                    }
                    else
                    {
                        triangles[baseIndex + 1] = vertexIndex + 2;
                        triangles[baseIndex + 3] = vertexIndex + 2;
                        triangles[baseIndex + 5] = vertexIndex + 1;
                        triangles[baseIndex + 4] = vertexIndex + 3;
                    }
                }
                return triangles;
            }
        }
    
        protected override Vector2[] Uvs
        {
            get
            {
                Vector2[] uvs = new Vector2[count * 2];
                float delta = 2 * Mathf.PI / count;
    
                for (int i = 0; i < count; i++)
                {
                    int a = i * 2;
                    float sinValue = Mathf.Sin(i * delta);
                    float cosValue = Mathf.Cos(i * delta);
    
                    Vector2 noramlized = new Vector2(cosValue, sinValue);
                    Vector2 half = new Vector2(0.5f, 0.5f);
    
                    uvs[a + 1] = noramlized * 0.5f + half;
                    uvs[a] = half + noramlized * 0.5f * innerRadius/outerRadius;
                    //Debug.LogFormat("Uv value of {0} and {1} is {2} and {3}", a, a + 1, uvs[a], uvs[a + 1]);
                }
                return uvs;
            }
        }
    }
    
    
  • 相关阅读:
    kNN算法python实现和简单数字识别的方法
    python3.4学习笔记(二十) python strip()函数 去空格 函数的用法
    SolrCloud分布式集群部署步骤
    hbases索引技术:Lily HBase Indexer介绍
    HBase1.0以上版本的API改变
    Hbase1.0 客户端api
    java.io.IOException: Could not locate executable nullinwinutils.exe in the Hadoop binaries
    Resharp最新破解方法
    Cloudera Manager 5和CDH5离线安装
    Apache Thrift
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/12253072.html
Copyright © 2011-2022 走看看