unitymesh系列之画圆柱体

Shader "Custom/Cylinder"
{
    Properties
    {
        _MainColor ("Main Color", Color) = (1, 1, 1, 0.5)
    }
    
    SubShader
    {
        Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
        LOD 100

        Pass
        {
            ZWrite Off
            Cull Off
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            fixed4 _MainColor;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                return _MainColor;
            }
            ENDCG
        }
    }
}
/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描   述:                                                    
*│ 作   者:wangying                                                                                           
*│ 创建时间:2020/10/24 16:17:52   
*│ 作者blog: http://www.laowangomg.com                       
*└──────────────────────────────────────────────────────────────┘
*/

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

namespace Mesh
{
    public class CylinderMy : MonoBehaviour
    {
  

        private GameObject m_CylinderObj;                       // 圆柱体
        private GameObject m_CylinderGridObj;                   // 圆柱体网格
        private MeshRenderer m_MeshRenderer;
        private Material m_CylinderMat;
        private Material m_GridMat;
        private Material[] m_Materials;
        private Material[] m_CylinderMaterials;
        private UnityEngine.Mesh m_CylinderMesh;

        public float Height = 10;
        public float Radius = 10;

        private bool m_ShowMeshGrid;


        public void Start()
        {
            Init();
        }

        public void Init()
        {
            //创建物体
            m_CylinderObj = new GameObject("CylinderObj");
            MeshFilter mf = m_CylinderObj.AddComponent<MeshFilter>();
            m_MeshRenderer = m_CylinderObj.AddComponent<MeshRenderer>();

            m_CylinderMat = new Material(Shader.Find("Custom/Cylinder"));
            m_GridMat = new Material(Shader.Find("Unlit/Color"));

            m_Materials = new Material []{ m_CylinderMat , m_GridMat };
            m_CylinderMaterials = new Material[] { m_CylinderMat };
            m_MeshRenderer.materials = m_Materials;

            m_CylinderMesh = new UnityEngine.Mesh { subMeshCount = 2};




            mf.mesh = m_CylinderMesh;


            RefreshCylinder();


        }

        private void RefreshCylinder()
        {
            //设置顶点数据  和  索引数据
            float radius = Radius;
            float height = Height;

            const int cnt = 20;
            List<Vector3> vertices = new List<Vector3>();
            List<int> indexList = new List<int>();
            List<int> gridIndexList = new List<int>();
            float deltaRad = Mathf.PI * 2 / cnt;

            for (int i = 0; i < cnt; i++)
            {
                float rad = deltaRad * i;
                float x = radius * Mathf.Sin(rad);
                float z = radius * Mathf.Cos(rad);
                vertices.Add(new Vector3(x, 0, z));
                vertices.Add(new Vector3(x, height, z));

                if ((cnt - 1) == i)
                {
                    // 2n 2n+1 0     0 2n+1 1 
                    indexList.Add(2 * i);   
                    indexList.Add(2 * i + 1);
                    indexList.Add(0);

                    indexList.Add(0);
                    indexList.Add(2 * i + 1);
                    indexList.Add(1);
                }
                else
                {
                    //0,1,2   2,1,3
                    //01  12  02  
                    indexList.Add(2 * i);
                    indexList.Add(2 * i + 1);
                    indexList.Add(2 * i + 2);

                    indexList.Add(2 * i + 2);
                    indexList.Add(2 * i + 1);
                    indexList.Add(2 * i + 3);
                  
                }

            }
            m_CylinderMesh.SetVertices(vertices);
            m_CylinderMesh.SetIndices(indexList.ToArray(), MeshTopology.Triangles, 0);


            if (m_ShowMeshGrid)
            {
                // 网格线
                for (int i = 0; i < cnt; i++)
                {
                    if (i == cnt - 1)
                    {
                        gridIndexList.Add(1);
                        gridIndexList.Add(0);

                        gridIndexList.Add(2 * i + 1);
                        gridIndexList.Add(1);

                        gridIndexList.Add(2 * i);
                        gridIndexList.Add(0);

                        gridIndexList.Add(2 * i + 1);
                        gridIndexList.Add(0);
                    }
                    else
                    {
                        //02  13  01 12

                        gridIndexList.Add(2 * i);
                        gridIndexList.Add(2 * i + 1);

                        gridIndexList.Add(2 * i + 1);
                        gridIndexList.Add(2 * (i + 1));

                        gridIndexList.Add(2 * i + 1);
                        gridIndexList.Add(2 * i + 3);

                        gridIndexList.Add(2 * i + 3);
                        gridIndexList.Add(2 * (i + 1));
                    }
                }

                m_CylinderMesh.subMeshCount = 2;
                m_CylinderMesh.SetIndices(gridIndexList.ToArray(), MeshTopology.Lines, 1);
                m_MeshRenderer.materials = m_Materials; ;
            }
            else
            {
                m_CylinderMesh.subMeshCount = 1;
                m_MeshRenderer.materials = m_CylinderMaterials;
            }
        }


        [ContextMenu("更新")]
        public  void OnRadiusChanged()
        {
            m_ShowMeshGrid = !m_ShowMeshGrid;
            RefreshCylinder();
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值