平面环形

Posted llstart-new0201

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了平面环形相关的知识,希望对你有一定的参考价值。

概述

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

代码

基类

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

以上是关于平面环形的主要内容,如果未能解决你的问题,请参考以下文章

在片段着色器中,为啥我不能使用平面输入整数来索引 sampler2D 的统一数组?

如何使用在质心处计算的光来实现平面着色?

HERE-API识别建筑物的地板/地平面

如何将噪声着色器放入我的平面几何体中

寻求一 HTML(用CSS也可以)环形或圆形图片滚动代码

小程序里echarts画的饼图在安卓手机上效果不正常?