unity mesh生成凹形模型

基础知识中的基础,直接上代码

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

public class cube : MonoBehaviour
{
    public Material mat;
    // Start is called before the first frame update
    void Start()
    {
        gameObject.GetComponent<MeshRenderer>().material = mat;
        gameObject.GetComponent<MeshRenderer>().material.color = Color.white;
        Mesh mesh = GetComponent<MeshFilter>().mesh;
        mesh.Clear();

        //设置顶点
        mesh.vertices = new Vector3[] {

            //前
            new Vector3(0, 0, 0),
            new Vector3(0, 2, 0),
            new Vector3(1, 2, 0),
            new Vector3(1, 0, 0),

            new Vector3(1, 1, 0),
            new Vector3(2, 1, 0),
            new Vector3(2, 0, 0),

            new Vector3(2,2,0),
            new Vector3(3,2,0),
            new Vector3(3,0,0),

             //后
            new Vector3(0, 0, 3),
            new Vector3(0, 2, 3),
            new Vector3(1, 2, 3),
            new Vector3(1, 0, 3),

            new Vector3(1, 1, 3),
            new Vector3(2, 1, 3),
            new Vector3(2, 0, 3),

            new Vector3(2,2,3),
            new Vector3(3,2,3),
            new Vector3(3,0,3),

            //左
            new Vector3(0,0,0),
            new Vector3 (0,2,0),
            new Vector3 (0,2,3),
            new Vector3 (0,0,3),

             //右
            new Vector3(3,0,0),
            new Vector3 (3,2,0),
            new Vector3 (3,2,3),
            new Vector3 (3,0,3),

            //上
            new Vector3(0,2,0),
            new Vector3(0,2,3),
            new Vector3(1,2,3),
            new Vector3(1,2,0),

            new Vector3(1,1,3),
            new Vector3(1,1,0),

            new Vector3(2,2,0),
            new Vector3(2,2,3),
            new Vector3(3,2,3),
            new Vector3(3,2,0),

            new Vector3(2,1,3),
            new Vector3(2,1,0),

            new Vector3(1,1,0),
            new Vector3(1,1,3),
            new Vector3(2,1,3),
            new Vector3(2,1,0)



    };
        //设置三角形顶点顺序,顺时针设置
        mesh.triangles = new int[] {
            0, 1, 2,  //前
            0,2,3,
            3,4,5,
            3,5,6,
            6,7,8,
            6,8,9,

            9,8,6,
            8,7,6,
            6,5,3,
            5,4,3,
            3,2,0,
            2,1,0,


           10, 11, 12,  //后
            10,12,13,
            13,14,15,
            13,15,16,
            16,17,18,
            16,18,19,

            19,18,16,
            18,17,16,
            16,15,13,
            15,14,13,
            13,12,10,
            12,11,10,

            20,21,22,  //左
            20,22,23,
            23,22,20,
            22,21,20,

            24,25,26,  //右
            24,26,27,
            27,26,24,
            26,25,24,

            28,29,30, //上
            28,30,31,
            31,30,28,
            30,29,28,

            31,30,32,
            31,32,33,
            33,32,31,
            32,30,31,

            34,35,36,
            34,36,37,
            37,36,34,
            36,35,34,

            34,35,38,
            34,38,39,
            39,38,34,
            38,35,34,

            40,41,42,
            40,42,43,
            43,42,40,
            42,41,40


        };

    }
}

,效果图片

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山竹炒大蒜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值