IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity 程序化网格几何体之矩形 -> 正文阅读

[游戏开发]Unity 程序化网格几何体之矩形

最近在做编辑器的功能,碰到一个需求是创建任意长度的墙体。思路是点击鼠标,然后拖动鼠标,墙体长度随着鼠标的拖动而增加。
通过实现此功能,感觉其中有几个关键点:
1.绘制矩形顶点
2.绘制矩形三角面
3.绘制矩形纹理
4.绘制矩形法线
5.矩形始终指向鼠标位置
6.在结束绘制矩形时,调用Mesh.Clear(),然后重新给矩形顶点赋值,否则会出现拉近视角,模型消失的情况

目前发现的问题:
在绘制斜着的矩形时,发现贴图会有重影。

实现代码如下:

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

public class CreateMesh : MonoBehaviour
{

    [SerializeField]
    private Material wallMat = null;

    [SerializeField]
    private float width = 1;

    [SerializeField]
    private float height = 3.4f;

    private float length = 0;

    private float halfWidth = 0;

    private Mesh mesh = null;

    private MeshRenderer meshRender = null;

    private Vector3[] vertics = new Vector3[24];

    private int[] triangles = new int[36];

    private Vector2[] UVs = new Vector2[24];

    private int clickCount = 0;

    private RaycastHit hit;

    private bool isStartCreate = false;

    private Vector3 originPos = Vector3.zero;

    private GameObject curObj = null;

    private void Start()
    {
        halfWidth = width / 2f;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (clickCount == 0)
            {
                StartCreateCustomObject();
                clickCount = 1;
            }
            else if (clickCount == 1)
            {
                EndCreateCustomObject();
                clickCount = 0;
            }
        }

        CreatingCustomObject();
    }

    private void StartCreateCustomObject()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            curObj = new GameObject();
            meshRender = curObj.AddComponent<MeshRenderer>();
            meshRender.sharedMaterial = wallMat;
            var vmf = curObj.AddComponent<MeshFilter>();
            mesh = vmf.mesh;

            curObj.transform.position = hit.point;
            originPos = hit.point;
            length = 0;

            vertics = new Vector3[] {
                //left
                new Vector3(0,0,halfWidth),
                new Vector3(0,height,halfWidth),
                new Vector3(0,height,-halfWidth),
                new Vector3(0,0,-halfWidth),

                //front
                new Vector3(0,0,-halfWidth),
                new Vector3(0,height,-halfWidth),
                new Vector3(0,height,-halfWidth),
                new Vector3(0,0,-halfWidth),

                //right
                new Vector3(0,0,-halfWidth),
                new Vector3(0,height,-halfWidth),
                new Vector3(0,height,halfWidth),
                new Vector3(0,0,halfWidth),

                //back
                new Vector3(0,0,halfWidth),
                new Vector3(0,height,halfWidth),
                new Vector3(0,height,halfWidth),
                new Vector3(0,0,halfWidth),

                //up
                new Vector3(0,height,-halfWidth),
                new Vector3(0,height,halfWidth),
                new Vector3(0,height,halfWidth),
                new Vector3(0,height,-halfWidth),

                //down
                new Vector3(0,0,halfWidth),
                new Vector3(0,0,-halfWidth),
                new Vector3(0,0,-halfWidth),
                new Vector3(0,0,halfWidth)

         };
            //绘制顶点
            mesh.vertices = vertics;

            triangles = new int[] {
            //left
            0,1,2,
            2,3,0,

            //front
            4,5,6,
            6,7,4,

            //right
            8,9,10,
            10,11,8,

            //back
            12,13,14,
            14,15,12,

            //up
            16,17,18,
            18,19,16,

            //down
            20,21,22,
            22,23,20
            };
            //绘制三角面
            mesh.triangles = triangles;

            UVs = new Vector2[] {
            //left
            new Vector2(0,0),
            new Vector2(0,1),
            new Vector2(1,1),
            new Vector2(1,0),

            //front
            new Vector2(0,0),
            new Vector2(0,1),
            new Vector2(1,1),
            new Vector2(1,0),

            //right
            new Vector2(0,0),
            new Vector2(0,1),
            new Vector2(1,1),
            new Vector2(1,0),

            //back
            new Vector2(0,0),
            new Vector2(0,1),
            new Vector2(1,1),
            new Vector2(1,0),

            //up
            new Vector2(0,0),
            new Vector2(0,1),
            new Vector2(1,1),
            new Vector2(1,0),

            //down
            new Vector2(0,0),
            new Vector2(0,1),
            new Vector2(1,1),
            new Vector2(1,0),
            };
            //绘制纹理
            mesh.uv = UVs;

            //计算法线
            mesh.RecalculateNormals();

            isStartCreate = true;

        }
    }

    private void CreatingCustomObject()
    {
        if (isStartCreate)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                vertics = mesh.vertices;

                var vEndCenterX = hit.point.x - originPos.x;
                var vEndCenterZ = hit.point.z - originPos.z;
                length = Mathf.Sqrt(vEndCenterX * vEndCenterX + vEndCenterZ * vEndCenterZ);

                var vros = hit.point - originPos;
                curObj.transform.rotation = Quaternion.FromToRotation(Vector3.right, vros);

                vertics[6] = new Vector3(length, height, -halfWidth);
                vertics[7] = new Vector3(length, 0, -halfWidth);
                vertics[8] = new Vector3(length, 0, -halfWidth);
                vertics[9] = new Vector3(length, height, -halfWidth);
                vertics[10] = new Vector3(length, height, halfWidth);
                vertics[11] = new Vector3(length, 0, halfWidth);
                vertics[12] = new Vector3(length, 0, halfWidth);
                vertics[13] = new Vector3(length, height, halfWidth);
                vertics[18] = new Vector3(length, height, halfWidth);
                vertics[19] = new Vector3(length, height, -halfWidth);
                vertics[22] = new Vector3(length, 0, -halfWidth);
                vertics[23] = new Vector3(length, 0, halfWidth);

                mesh.vertices = vertics;

                //如果不实时计算法线的话,在创建过程中模型无法正常接收光照和着色
                mesh.RecalculateNormals();

            }

        }
    }

    private void EndCreateCustomObject()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            //最后结束创建的时候重新赋值一下顶点,否则会出现Bound跟Mesh尺寸不一致的情况
            //如果Bound和Mesh尺寸不一致,拉近视角模型则会消失
            mesh.Clear();

            var vEndCenterX = hit.point.x - originPos.x;
            var vEndCenterZ = hit.point.z - originPos.z;
            length = Mathf.Sqrt(vEndCenterX * vEndCenterX + vEndCenterZ * vEndCenterZ);

            var vros = hit.point - originPos;
            curObj.transform.rotation = Quaternion.FromToRotation(Vector3.right, vros);

            vertics[6] = new Vector3(length, height, -halfWidth);
            vertics[7] = new Vector3(length, 0, -halfWidth);
            vertics[8] = new Vector3(length, 0, -halfWidth);
            vertics[9] = new Vector3(length, height, -halfWidth);
            vertics[10] = new Vector3(length, height, halfWidth);
            vertics[11] = new Vector3(length, 0, halfWidth);
            vertics[12] = new Vector3(length, 0, halfWidth);
            vertics[13] = new Vector3(length, height, halfWidth);
            vertics[18] = new Vector3(length, height, halfWidth);
            vertics[19] = new Vector3(length, height, -halfWidth);
            vertics[22] = new Vector3(length, 0, -halfWidth);
            vertics[23] = new Vector3(length, 0, halfWidth);

            mesh.vertices = vertics;

            mesh.triangles = triangles;

            mesh.uv = UVs;

            mesh.RecalculateNormals();

            curObj.AddComponent<BoxCollider>();

            curObj = null;

            isStartCreate = false;
        }
    }
}

墙体Shader:https://download.csdn.net/download/qq_39353597/52010659
参考资料:https://docs.unity.cn/cn/2019.4/Manual/UsingtheMeshClass.html

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-12-01 18:01:41  更:2021-12-01 18:03:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 23:41:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码