(七)多棱柱

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值