(七)多棱柱

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 CreatePrism : CreateMeshBase
{
    [Range(3,30)]
    public int prismSize = 5;
    public float prismRadius = 5;
    public float prismLength = 15;

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

    protected override Vector3[] Vertices
    {
        get
        {
            Vector3[] vertices = new Vector3[prismSize * 2 + 2];
            float delta = 2 * Mathf.PI / prismSize;

            vertices[0] = Vector3.zero;
            vertices[1] = new Vector3(0, prismLength, 0);

            for (int i = 2; i < prismSize+2; i++)
            {
                float angle = (i - 2) * delta;
                vertices[i] = new Vector3(prismRadius * Mathf.Cos(angle), 0, prismRadius * Mathf.Sin(angle));
                vertices[i + prismSize] = new Vector3(prismRadius * Mathf.Cos(angle), prismLength, prismRadius * Mathf.Sin(angle));
            }
            return vertices;
        }
    }

    protected override int[] Triangles
    {
        get
        {
            int[] triangles = new int[prismSize * 4 * 3];

            for (int i = 0; i < prismSize; i++)
            {
                int bottomFace = i * 3;
                triangles[bottomFace] = 0;
                triangles[bottomFace + 1] = i + 2;

                int topFace = prismSize * 3 + i * 3;
                triangles[topFace] = 1;
                triangles[topFace + 2] = i + prismSize + 2;

                int sideFace = prismSize * 3 * 2 + 6 * i;
                triangles[sideFace] = i + 2;
                triangles[sideFace + 1] = i + 2 + prismSize;
                triangles[sideFace + 4] = i + 2 + prismSize;

                if (i >= prismSize - 1)
                {
                    triangles[bottomFace + 2] = 2;
                    triangles[topFace + 1] = 2 + prismSize;
                    triangles[sideFace + 2] = 2;
                    triangles[sideFace + 3] = 2;
                    triangles[sideFace + 5] = 2 + prismSize;
                }
                else
                {
                    triangles[bottomFace + 2] = i + 2 + 1;
                    triangles[topFace + 1] = i + prismSize + 2 + 1;
                    triangles[sideFace + 2] = i + 2 + 1;
                    triangles[sideFace + 3] = i + 2 + 1;
                    triangles[sideFace + 5] = i + 2 + prismSize + 1;
                }
            }
            return triangles;
        }
    }
}

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