(6) Pyramid

1 Overview

This article provides a method for generating polygonal pyramids, which controls the number of edges of the pyramid through parameters. However, due to the problem of common vertices, the normal is not set, and the normal can be set according to the cube method.

2. Code

2.1 Base class

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 Polygonal Pyramid

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

Guess you like

Origin blog.csdn.net/ttod/article/details/130300274