使用Mesh生成3D物品

效果:

代码实现

棱锥:

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

public class YuanZhui : MonoBehaviour
{
    int num = 30;
    int r = 2;

    // Start is called before the first frame update
    void Start()
    {
        gameObject.AddComponent<MeshFilter>();
        gameObject.AddComponent<MeshCollider>();
        gameObject.AddComponent<MeshRenderer>();

        float angle = 2 * Mathf.PI / num;
        VertexHelper vh = new VertexHelper();
        vh.AddVert(Vector3.up * 3, Color.blue, Vector2.zero);

        for (int i = 0; i < num; i++)
        {
            float x = Mathf.Sin(i * angle) * r;
            float z = Mathf.Cos(i * angle) * r;

            vh.AddVert(new Vector3(x, 0, z), Color.blue, Vector2.zero);

            if (i == 0)
            {
                vh.AddTriangle(0, num, 1);
            }
            else
            {
                vh.AddTriangle(0, i, i + 1);
            }
        }

        //共用顶点
        vh.AddVert(Vector3.zero, Color.black, Vector2.zero);
        for (int i = 0; i < num; i++)
        {
            if (i == 0)
            {
                vh.AddTriangle(1, num, num + 1);
            }
            else
            {
                vh.AddTriangle(i + 1, i, num + 1);
            }
        }

        //不共用顶点
        //vh.AddVert(Vector3.zero, Color.white, Vector2.zero);
        //for (int i = 0; i < num; i++)
        //{
        //    float x = Mathf.Sin(i * angle) * r;
        //    float z = Mathf.Cos(i * angle) * r;

        //    vh.AddVert(new Vector3(x, 0, z), Color.white, Vector2.zero);

        //    if (i == 0)
        //    {
        //        vh.AddTriangle(num + 2, 2 * num + 1, num + 1);
        //    }
        //    else
        //    {
        //        vh.AddTriangle(num + 2 + i, num + 1 + i, num + 1);
        //    }
        //}

        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);

        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
        GetComponent<MeshRenderer>().material.color = Color.cyan;

    }
}

圆柱:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static System.Net.Mime.MediaTypeNames;

public class YuanZhu : MonoBehaviour
{
    int num = 10;
    int r = 2;

    // Start is called before the first frame update
    void Start()
    {
        gameObject.AddComponent<MeshFilter>();
        gameObject.AddComponent<MeshRenderer>();
        gameObject.AddComponent<MeshCollider>();

        VertexHelper vh = new VertexHelper();
        float angle = 2 * Mathf.PI / num;


        for (int i = 0; i < num; i++)
        {
            float x = Mathf.Sin(angle * i) * r;
            float z = Mathf.Cos(angle * i) * r;
            vh.AddVert(new Vector3(x, 0, z), Color.white, Vector2.zero);
            vh.AddVert(new Vector3(x, 3, z), Color.white, Vector2.zero);
        }
        vh.AddVert(Vector3.zero, Color.white, Vector2.zero);
        vh.AddVert(Vector3.up * 3, Color.white, Vector2.zero);


        for (int i = 0; i < num; i++)
        {
            //四周
            if (i == num - 1)
            {
                vh.AddTriangle(1, i * 2 + 1, i * 2);
                vh.AddTriangle(0, 1, i * 2);
            }
            else
            {
                vh.AddTriangle((i + 1) * 2 + 1, i * 2 + 1, i * 2);
                vh.AddTriangle((i + 1) * 2, (i + 1) * 2 + 1, i * 2);
            }

            //上下
            if (i == 0)
            {
                vh.AddTriangle(1, 2 * num + 1, 2 * (num - 1) + 1);//上
                vh.AddTriangle(2 * (num - 1), 2 * num, 0);//下
            }
            else
            {
                vh.AddTriangle(2 * (i - 1) + 1, 2 * i + 1, 2 * num + 1);
                vh.AddTriangle(2 * i, 2 * (i - 1), 2 * num);
            }
        }
        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);

        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
        GetComponent<MeshRenderer>().material.color = Color.gray;
    }
}

球:

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

public class Qiu : MonoBehaviour
{
    public int num = 8;
    public float r = 5;

    // Start is called before the first frame update
    void Start()
    {
        gameObject.AddComponent<MeshFilter>();
        gameObject.AddComponent<MeshCollider>();
        gameObject.AddComponent<MeshRenderer>();

        VertexHelper vh = new VertexHelper();
        float angle = 2 * Mathf.PI / num;
        for (int i = 0; i < num / 2 + 1; i++)
        {
            float y = Mathf.Cos(angle * i) * r;
            float xr = Mathf.Sin(angle * i) * r;
            float uvy = (float)i / (num / 2);
            for (int j = 0; j < num; j++)
            {
                float x = Mathf.Sin(angle * j) * xr;
                float z = Mathf.Cos(angle * j) * xr;
                float uvx = (float)j / num;
                vh.AddVert(new Vector3(x, y, z), Color.white, new Vector2(uvx, uvy));
                if (j == num - 1)
                {
                    float x0 = Mathf.Sin(0) * xr;
                    float z0 = Mathf.Cos(0) * xr;
                    vh.AddVert(new Vector3(x0, y, z0), Color.white, new Vector2(1, uvy));
                }
                if (i < num / 2)
                {
                    vh.AddTriangle(i * (num + 1) + j, (i + 1) * (num + 1) + j, i * (num + 1) + j + 1);
                    vh.AddTriangle(i * (num + 1) + j + 1, (i + 1) * (num + 1) + j, (i + 1) * (num + 1) + j + 1);
                }
            }
        }
        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
        GetComponent<MeshRenderer>().material.color = Color.blue;

    }
}

棱台/棱柱:

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

public class LengTai : MonoBehaviour
{
    int num = 8;
    int r0 = 3;
    int r1 = 1;
    //r0与r1相等为棱台

    // Start is called before the first frame update
    void Start()
    {
        gameObject.AddComponent<MeshFilter>();
        gameObject.AddComponent<MeshCollider>();
        gameObject.AddComponent<MeshRenderer>();

        VertexHelper vh = new VertexHelper();
        float angle = 2 * Mathf.PI / num;

        for (int i = 0; i < num; i++)
        {
            float x0 = Mathf.Sin(angle * i) * r0;
            float z0 = Mathf.Cos(angle * i) * r0;
            vh.AddVert(new Vector3(x0, 0, z0), Color.white, Vector2.zero);

            float x1 = Mathf.Sin(angle * i) * r1;
            float z1 = Mathf.Cos(angle * i) * r1;
            vh.AddVert(new Vector3(x1, 3, z1), Color.white, Vector2.zero);
        
        }


        for (int i = 0; i < num; i++)
        {
            if (i == num - 1)
            {
                vh.AddTriangle(0, 2 * i + 1, 2 * i);
                vh.AddTriangle(0, 1, 2 * i + 1);
            }
            else
            {
                vh.AddTriangle(2 * i, 2 * (i + 1) + 1, 2 * i + 1);
                vh.AddTriangle(2 * i, 2 * (i + 1), 2 * (i + 1) + 1);
            }

            if (num == 3)
            {

                vh.AddTriangle(4, 2, 0);
                vh.AddTriangle(1, 3, 5);
            }
            else
            {
                if (i > 1 && i < num - 1)
                {
                    vh.AddTriangle(0, (i + 1) * 2, i * 2);
                    vh.AddTriangle(i * 2 + 1, (i + 1) * 2 + 1, 1);
                    if (i == 2)
                    {
                        vh.AddTriangle(2 * i, (i - 1) * 2, 0);
                        vh.AddTriangle(1, (i - 1) * 2 + 1, 2 * i + 1);
                    }
                }
            }
        }

        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
        GetComponent<MeshRenderer>().material.color = Color.green;
    }

}

可拖拽生成区域:

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

public class MyMesh : MonoBehaviour
{
    public Transform[] trans;
    Mesh mesh;
    VertexHelper vh;
    // Start is called before the first frame update
    void Start()
    {
        mesh = new Mesh();
        vh = new VertexHelper();
        mesh.vertices = new Vector3[trans.Length];
        for (int i = 0; i < trans.Length; i++)
        {
            vh.AddVert(trans[i].position, Color.white, Vector2.zero);
        }
        vh.AddTriangle(0, 1, 2);
        vh.AddTriangle(0, 2, 1);
        vh.FillMesh(mesh);
        gameObject.AddComponent<MeshFilter>().mesh = mesh;
        gameObject.AddComponent<MeshCollider>().sharedMesh = mesh;
        gameObject.AddComponent<MeshRenderer>().material.color = Color.red;
    }

    // Update is called once per frame
    void Update()
    {
        vh.Clear();
        mesh.Clear();
        for (int i = 0; i < trans.Length; i++)
        {
            vh.AddVert(trans[i].localPosition, Color.white, Vector2.zero);
        }
        vh.AddTriangle(0, 1, 2);
        vh.AddTriangle(0, 2, 1);
        vh.FillMesh(mesh);
        gameObject.GetComponent<MeshFilter>().mesh = mesh;
        gameObject.GetComponent<MeshCollider>().sharedMesh = mesh;
        gameObject.GetComponent<MeshRenderer>().material.color = Color.red;
    }
}

拖拽物品挂载脚本:

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

public class OnDragCube : MonoBehaviour
{
    public void OnMouseDrag()
    {
        Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
        Vector3 des = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,pos.z));
        transform.position = des;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值