(六)多棱锥

1.概述

本文提供一种多棱锥的生成方法,通过参数控制锥的棱数。但是因为公用顶点的问题,所以未进行法线设置,可根据cube方法设置法线。

2.代码

2.1 基类

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 Vector4[] Tangents { 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.tangents = null;

        mesh.name = MeshName;
        mesh.vertices = Vertices;
        mesh.triangles = Triangles;
        mesh.uv = Uvs;
        mesh.normals = Normals;
        mesh.tangents = Tangents;

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

2.2 多棱锥

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreateMultiCone : CreateMeshBase
{
    public float radius = 10;
    [Range(3,30)]
    public int coneSize = 6;

    protected override string MeshName
    {
        get
        {
            return "Multi Mesh";
        }
    }

    protected override Vector3[] Vertices
    {
        get
        {
            Vector3[] vertices = new Vector3[coneSize + 2];

            vertices[0] = new Vector3(0, radius, 0);
            vertices[1] = new Vector3(0, -radius, 0);

            float delta = 2 * Mathf.PI / coneSize;

            for (int i = 0; i < coneSize; i++)
            {
                vertices[i + 2] = new Vector3(radius * Mathf.Cos(i * delta), 0, radius * Mathf.Sin(i * delta));
            }

            return vertices;
        }
    }

    protected override int[] Triangles
    {
        get
        {
            int[] triangles = new int[coneSize * 2 * 3];

            for (int j = 0; j < coneSize; j++)
            {
                int index = 3 * j;
                triangles[index] = 0;
                triangles[index + 2] = j + 2;

                if (j + 3 > Vertices.Length - 1)
                {
                    triangles[index + 1] = 2;
                }
                else
                {
                    triangles[index + 1] = j + 3;
                }
            }

            for (int j = 0; j < coneSize; j++)
            {
                int index = 3 * j + coneSize * 3;
                triangles[index] = 1;
                triangles[index + 1] = j + 2;

                if (j + 3 > Vertices.Length - 1)
                {
                    triangles[index + 2] = 2;
                }
                else
                {
                    triangles[index + 2] = j + 3;
                }
            }
            return triangles;
        }
    }
}

posted @ 2020-02-02 21:19  81192  阅读(355)  评论(0编辑  收藏  举报