unitymesh系列之画圆柱体

Posted 那个妹子留步

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
        
    

以上是关于unitymesh系列之画圆柱体的主要内容,如果未能解决你的问题,请参考以下文章

无GC提取List<T>对应Array方法分析

无GC提取List<T>对应Array方法分析

无GC提取List<T>对应Array方法分析

Unity Mesh Mesh给球贴图

Unity Mesh Mesh 平面图形的贴图

unity3d中网格mesh可以制作的吗?怎么做的?