首页 > 其他分享 >用Mesh绘制空心柱体,水管效果

用Mesh绘制空心柱体,水管效果

时间:2024-07-11 19:28:10浏览次数:7  
标签:柱体 float Mathf height Add Mesh indices new 水管

目录

1.主要思路

2.代码展示

3.效果展示


1.主要思路

上下两面圆环,内外是展开的两个矩形面,通过Mesh依次绘制图形。

2.代码展示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 水管
/// </summary>
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class DrawConduit : MonoBehaviour
{
   
    [SerializeField]
    Texture2D texture;
    void Start()
    {
        DrawCustomConduit(2,1,8,30);
        GetComponent<MeshRenderer>().material.mainTexture = texture;
    }

    /// <summary>
    /// 绘制自定义空心柱体(水管)
    /// </summary>
    /// <param name="r1">外半径</param>
    /// <param name="r2">内半径</param>
    /// <param name="height">高度</param>
    /// <param name="num">分割数 棱数</param>
    private void DrawCustomConduit(float r1,float r2,float height,int n)
    {
        Mesh mesh = GetComponent<MeshFilter>().mesh;

        List<Vector3> verties = new List<Vector3>();
        List<Vector2> uvlists = new List<Vector2>();
        //角度
        float angle = (2 * Mathf.PI) / n;
        //外部顶点
        List<Vector3> outcircleVs = new List<Vector3>();
        //内部顶点
        List<Vector3> incircleVs = new List<Vector3>();
        //上方圆环
        for (int i = 0; i < n + 1; i++)
        {
            //外圆
            float x = Mathf.Sin(angle * i) * r1;
            float y = Mathf.Cos(angle * i) * r1;
            float uvx = i / (float)n;
            verties.Add(new Vector3(x, (height / 2), y));
            //存储外圆上点
            outcircleVs.Add(new Vector3(x, (height / 2), y));

            uvlists.Add(new Vector2(uvx, 1));
            //内圆
            float intx = Mathf.Sin(angle * i) * r2;
            float inty = Mathf.Cos(angle * i) * r2;
            verties.Add(new Vector3(intx, (height / 2), inty));
            //存储内圆上点
            incircleVs.Add(new Vector3(intx, (height / 2), inty));

            uvlists.Add(new Vector2(uvx, 0));
        }

        //下方圆环
        for (int i = 0; i < n + 1; i++)
        {
            float x = Mathf.Sin(angle * i) * r1;
            float y = Mathf.Cos(angle * i) * r1;
            verties.Add(new Vector3(x, -(height / 2), y));
            outcircleVs.Add(new Vector3(x, -(height / 2), y));

            float uvx = i / (float)n;
            uvlists.Add(new Vector2(uvx, 1));
            //内圆
            float intx = Mathf.Sin(angle * i) * r2;
            float inty = Mathf.Cos(angle * i) * r2;

            verties.Add(new Vector3(intx, -(height / 2), inty));
            incircleVs.Add(new Vector3(intx, -(height / 2), inty));

            uvlists.Add(new Vector2(uvx, 0));
        }

        //上下两个外圈的点 为后面画外部矩形做准备
        for (int i = 0; i < outcircleVs.Count; i++)
        {
            verties.Add(outcircleVs[i]);
            float uvx = i / (float)(outcircleVs.Count / 2);
            if (i <= (outcircleVs.Count / 2))
            {
                uvlists.Add(new Vector2(uvx, 1));
            }
            else
            {
                uvlists.Add(new Vector2(uvx - 1, 0));
            }

        }

        //上下两个内圈的点  为后面画内部矩形做准备
        for (int i = 0; i < incircleVs.Count; i++)
        {
            verties.Add(incircleVs[i]);
            float uvx = i / (float)(incircleVs.Count / 2);
            if (i <= (incircleVs.Count / 2))
            {
                uvlists.Add(new Vector2(uvx, 1));
            }
            else
            {
                uvlists.Add(new Vector2(uvx - 1, 0));
            }
        }

        mesh.vertices = verties.ToArray();
        mesh.uv = uvlists.ToArray();
        //print("verties" + verties.Count);
        //print("uvlists" + uvlists.Count);
        List<int> indices = new List<int>();

        //通过连接上部圆环
        for (int i = 0; i < n + 1; i++)
        {
            if (i < n)
            {
                indices.Add(2 * i);
                indices.Add(2 * (i + 1));
                indices.Add(2 * (i + 1) + 1);

                indices.Add(2 * i);
                indices.Add(2 * (i + 1) + 1);
                indices.Add(2 * i + 1);
            }
        }
        //连接下部圆环
        for (int i = 0; i < n + 1; i++)
        {
            if (i < n)
            {
                indices.Add(2 * i + 2 * (n + 1));
                indices.Add(2 * (i + 1) + 1 + 2 * (n + 1));
                indices.Add(2 * (i + 1) + 2 * (n + 1));

                indices.Add(2 * i + 2 * (n + 1));
                indices.Add(2 * i + 1 + 2 * (n + 1));
                indices.Add(2 * (i + 1) + 1 + 2 * (n + 1));
            }
        }
        //连接外部矩形
        for (int i = 0; i < n + 1; i++)
        {
            indices.Add(i + 4 * (n + 1));
            indices.Add(i + 5 * (n + 1));
            indices.Add(i + 4 * (n + 1) + 1);

            indices.Add(i + 4 * (n + 1) + 1);
            indices.Add(i + 5 * (n + 1));
            indices.Add(i + 5 * (n + 1) + 1);

            //0 3 1  1 3 4                    
        }
        //连接内部矩形
        for (int i = 0; i < n; i++)
        {
            indices.Add(i + 6 * (n + 1));
            indices.Add(i + 6 * (n + 1) + 1);
            indices.Add(i + 7 * (n + 1));

            indices.Add(i + 6 * (n + 1) + 1);
            indices.Add(i + 7 * (n + 1) + 1);
            indices.Add(i + 7 * (n + 1));
        }
        //下标路径点赋给mesh
        mesh.triangles = indices.ToArray();
        Material mat = new Material(Shader.Find("Standard"));

        GetComponent<MeshRenderer>().material = mat;
        mesh.RecalculateNormals();

    }
}

可通过uv坐标贴图,给Textrue附上不同的纹理材质。修改DrawCustomConduit()方法里面的参数可以设置不同的需求效果。

3.效果展示

标签:柱体,float,Mathf,height,Add,Mesh,indices,new,水管
From: https://blog.csdn.net/2302_81889789/article/details/140348411

相关文章

  • Mesh绘制五角星和简易特效
    目录1.绘制UI上的五角星1.1思路分析1.2 绘制UI五角星的脚本1.3UI上的五角星的效果2.绘制3D的五角星2.1 主要思路2.2 绘制3D五角星的脚本2.3 3D五角星效果展示3.五角星粒子特效3.1 新建一个粒子特效(ParticleSystem)3.2 通过代码导出五角星Mesh3.2.1 ......
  • 全网最全EdgeMesh Q&A手册
    https://zhuanlan.zhihu.com/p/585749690   全网最全EdgeMeshQ&A手册转载请注明出处本人信息如下,有任何问题请联系我:github链接:Poorunga-Overview邮箱:[email protected]前言重要的事情1说三遍:定位问题前先看edgemesh-agent日志!定位问题前先看edgemesh-agent日志!定位......
  • Fluent Meshing|搅拌槽
    本案例演示利用FluentMeshing划分搅拌槽计算网格的基本流程。1几何模型几何模型如下图所示。几何模型中包含两个用于辅助网格划分的BOI几何,如下图所示。注:BOI几何主要用于网格局部加密。本案例中的两个boi几何是拷贝了两个MRF区域(模型树中的fluid-uppper-mrf及fluid......
  • 3年阿里hk主机干房BGP仅需150元 5年270元,单月4.2元(大水管)
    首先需要说明的是这个神仙操作不是我原创的,我只是进行二次归纳进行再次恰饭。感谢各位大佬提供的购买思路,真是把规则研究透了。 结论完成本文全部操作后你将以150-270元获取3-5年的下述服务器(或者白嫖到底0元购获取1年),这个价格我感觉很划算了毕竟是大厂的BGP线路,而且三网拉直,2......
  • 部署KubeEdge、Edgemesh、Sedna
    https://neuromansser.tech/posts/部署kubeedgeedgemeshsedna/ 部署KubeEdge、Edgemesh、SednaPostedonJun10,2024下载keadm下载keadm用于安装KubeEdge,官方文档:https://kubeedge.io/docs/setup/install-with-keadm/(英文版里有下载的部分中文版文档却没有,就有点迷惑…......
  • NRF52840DK PCA10056 BLE Mesh Light例程记录
    1.创建项目在打开的VSCode窗口,打开nRFConnect选项卡,"Createanewapplication" 选择"Copyasample" 输入"light", 选择"BluetoothMeshlight". 选择copy后,保存的路径。 键盘"Enter"一下。 点击"AddBuildConfiguration&qu......
  • 可计算离散整体几何结构的 MeshDGP使用——基于C#的geometry processing framework几
    目录引出MeshDGP项目下载和打开遇到的报错解决如何运行使用打开使用函数工具菜单等总结其他CAD/CAE/CAM几何引擎-软件概述郝建兵CAD/CAE/CAMCADCAECAM几何模型内核ACIS两个老大之一OpenCascadeParasolid两个老大之一Autodesk的内核各种CAD自定义信号和槽1.自定......
  • 3.js - MeshPhysicalMaterial - 虹彩效果
    效果图//@ts-nocheck//引入three.jsimport*asTHREEfrom'three'//导入轨道控制器import{OrbitControls}from'three/examples/jsm/controls/OrbitControls'//导入lil.guiimport{GUI}from'three/examples/jsm/libs/lil-gui.module.min.js......
  • Service Mesh技术详解
    深入探讨ServiceMesh的基本概念和核心技术,涵盖了服务发现、负载均衡、断路器与熔断机制,以及数据平面与控制平面的详细工作原理和实现方法。关注作者,复旦博士,分享云服务领域全维度开发技术。拥有10+年互联网服务架构、AI产品研发经验、团队管理经验,复旦机器人智能实验室成员,国......
  • C++类虚函数实现多态求长方体和圆柱体的体积
    #include<iostream>usingnamespacestd;#definePI3.14classContainer{ public: Container(doubleh){ height=h;//简单的方法初始化h } virtualdoublegetvolumn()=0;//纯虚函数 protected: doubleheight;};classCube:publicC......