zoukankan      html  css  js  c++  java
  • Mesh编程——三角形,多边形,正方体,园形,圆环

    基类:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public abstract class MeshDrawBase : MonoBehaviour
    {
        protected MeshFilter targetFilter;
        protected Mesh mh;
        protected int[] tris;
        protected Vector2[] uvs;
        protected Vector3[] nornals;
    
        private void Awake()
        {
            targetFilter = GetComponent<MeshFilter>();
        }
        protected virtual void Update()
        {
    
        }
        protected abstract void DrawMesh();
    }

     画出顶点:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    
    public class MeshInfoImationPrint : MeshDrawBase
    {
        protected override void DrawMesh()
        {
            
        }
        private void OnDrawGizmos()//编辑器模式调用
        {
            targetFilter = GetComponent<MeshFilter>();
            mh = targetFilter.sharedMesh;
    
            Gizmos.color = Color.red;
            for (int i = 0; i < mh.vertices.Length; i++)
            {
                Vector3 worldPoint = transform.TransformPoint(mh.vertices[i]);//局部坐标转成世界坐标
                Gizmos.DrawSphere(worldPoint, .1f);
    
            }
        }
    }

     画三角形:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// 画三角形
    /// </summary>
    [RequireComponent(typeof(MeshFilter))]
    [RequireComponent(typeof(MeshRenderer))]
    public class TriangleDraw : MeshDrawBase
    {
        public List<Vector3> vts = new List<Vector3>();
    
        private void Start()
        {
            mh = new Mesh();
    
            //获取顶点
            //世界坐标转换成局部坐标
            for (int i = 0; i < vts.Count; i++)
            {
                vts[i] = transform.InverseTransformPoint(vts[i]);
            }
            mh.vertices = vts.ToArray();
    
            //三角形
            tris = new int[3];
            tris[0] = 0;
            tris[1] = 1;
            tris[2] = 2;
    
            mh.triangles = tris;
    
            //uv
            uvs = new Vector2[vts.Count];
            uvs[0] = new Vector2(0, 0);
            uvs[1] = new Vector2(0, 1);
            uvs[2] = new Vector2(1, 0);
    
            mh.uv = uvs;
    
            mh.RecalculateBounds();
            mh.RecalculateNormals();//法线
            mh.RecalculateTangents();//切线
    
            targetFilter.mesh = mh;
        }
        protected override void DrawMesh()
        {
    
    
        }
    }


    画多边形:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// 画多边形
    /// </summary>
    [RequireComponent(typeof(MeshFilter))]//挂脚本时,自动添加组件
    [RequireComponent(typeof(MeshRenderer))]
    public class CreatManyTriabgle : MeshDrawBase
    {
        private List<Vector3> vts = new List<Vector3>();
    
        protected override void DrawMesh()
        {
           
           
        }
        private void DrawPolygon()
        {
            mh = new Mesh();
    
            //顶点
            mh.vertices = vts.ToArray();
            //三角形
            int trislength = (vts.Count - 2) * 3;
            tris = new int[trislength];
    
            for (int i = 0,n=1; i < trislength; i+=3,n++)
            {
                tris[i] = 0;
                tris[i + 1] = n;
                tris[i + 2] = n + 1;
            }
            mh.triangles = tris;
    
            //法线
            nornals = new Vector3[vts.Count];
            for (int i = 0; i < vts.Count; i++)
            {
                nornals[i] = new Vector3(0, 0, 1);
            }
            mh.normals = nornals;
    
            mh.RecalculateBounds();
            mh.RecalculateTangents();
    
            targetFilter.mesh = mh;
        }
        protected override void Update()
        {
            base.Update();
    
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if(Physics.Raycast(ray,out hit, Mathf.Infinity))
                {
                    Vector3 worldHitPoint = hit.point;
                    //世界转成局部坐标
                    Vector3 localHitPoint = transform.InverseTransformPoint(worldHitPoint);
                    vts.Add(localHitPoint);
                }
            }
    
            if (Input.GetKeyDown(KeyCode.R))
            {
                Reset();
            }
    
            if (Input.GetKeyDown(KeyCode.S))
            {
               // Debug.LogError(111);
                DrawPolygon();
            }
        }
        private void Reset()//重置
        {
            vts.Clear();
    
            targetFilter.mesh = null;
            Destroy(mh);
        }
    
        private void OnGUI()//显示数字序号
        {
            if (vts.Count == 0) return;
    
            GUI.color = Color.red;
    
            for (int i = 0; i < vts.Count; i++)
            {
                Vector3 worldPoint = transform.TransformPoint(vts[i]);
                Vector3 screenPoint = Camera.main.WorldToScreenPoint(worldPoint);
                Vector3 uiPoint = new Vector3(screenPoint.x, Camera.main.pixelHeight - screenPoint.y, screenPoint.z);
                GUI.Label(new Rect(uiPoint, new Vector3(100, 80)), i.ToString());
            }
        }
        private void OnDrawGizmos()//画线
        {
            if (vts.Count == 0) return;
    
            Gizmos.color = Color.cyan;//金色
    
            for (int i = 0; i < vts.Count; i++)
            {
                //转成世界坐标
                Vector3 worldHitPoint = transform.TransformPoint(vts[i]);
                Gizmos.DrawSphere(worldHitPoint, .2f);
            }
        }
    }

    画正方体:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// 画正方体
    /// </summary>
    [RequireComponent(typeof(MeshFilter))]
    [RequireComponent(typeof(MeshRenderer))]
    public class DrawMeshCube : MeshDrawBase
    {
        public List<Vector3> vts = new List<Vector3>();
    
        private void Start()
        {
            mh = new Mesh();
            //顶点
            mh.vertices = vts.ToArray();
            //三角形
            tris = new int[]
            {
                0,3,1,
                3,2,1,//
    
                0,4,7,
                0,7,3,//
    
                3,7,2,//
                2,7,6,
    
                2,6,1,//
                6,5,1,
    
                1,5,4,
                1,4,0,//
    
                7,4,5,
                7,5,6//
            };
            mh.triangles = tris;
    
            mh.RecalculateBounds();
            mh.RecalculateNormals();
            mh.RecalculateTangents();
    
            targetFilter.mesh = mh;
        }
        protected override void DrawMesh()
        {
           
        }
    
        private void OnDrawGizmos()
        {
            if (vts.Count == 0) return;
            Gizmos.color = Color.red;//点的颜色
            for (int i = 0; i < vts.Count; i++)
            {
                Vector3 worldPoint = transform.TransformPoint(vts[i]);//转成世界坐标
                Gizmos.DrawSphere(worldPoint, .1f);//画点
            }
        }
    }

     画圆:

    画圆环:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class huayuan : MonoBehaviour
    {
        public Material mat;//材质
        Vector3 vec = new Vector3(0, 0, 0);//原点坐标
        int num = 100;//园的分割数
        // Use this for initialization
        void Start()
        {
            // DrawCircle(1,num , vec);
            DrawRing(1, .8f, num, vec);
        }
    
        /// <summary>
        /// 画圆
        /// </summary>
        /// <param name="radius">圆的半径</param>
        /// <param name="segments">圆的分割数</param>
        /// <param name="centerCircle">圆心得位置</param>
        void DrawCircle(float radius, int segments, Vector3 centerCircle)
        {
            gameObject.AddComponent<MeshFilter>();
            gameObject.AddComponent<MeshRenderer>();
            gameObject.GetComponent<MeshRenderer>().material = mat;
    
            //顶点
            Vector3[] vertices = new Vector3[segments + 1];
            vertices[0] = centerCircle;
            float deltaAngle = Mathf.Deg2Rad * 360f / segments;
            float currentAngle = 0;
            for (int i = 1; i < vertices.Length; i++)
            {
                float cosA = Mathf.Cos(currentAngle);
                float sinA = Mathf.Sin(currentAngle);
                vertices[i] = new Vector3(cosA * radius + centerCircle.x, sinA * radius + centerCircle.y, 0);
                currentAngle += deltaAngle;
    
    
            }
            int[] triangles = new int[segments * 3];
            for (int i = 0, j = 1; i < segments * 3 - 3; i += 3, j++)
            {
                triangles[i] = 0;
                triangles[i + 1] = j + 1;
                triangles[i + 2] = j;
            }
            triangles[segments * 3 - 3] = 0;
            triangles[segments * 3 - 2] = 1;
            triangles[segments * 3 - 1] = segments;
    
    
            Mesh mesh = GetComponent<MeshFilter>().mesh;
            mesh.Clear();
    
            mesh.vertices = vertices;
            mesh.triangles = triangles;
        }
    
    // 画圆环
        /// <summary>
        /// 画圆环
        /// </summary>
        /// <param name="radius">圆半径</param>
        /// <param name="innerRadius">内圆半径</param>
        /// <param name="segments">圆的分个数</param>
        /// <param name="centerCircle">圆心坐标</param>
        void DrawRing(float radius, float innerRadius, int segments, Vector3 centerCircle)
        {
            gameObject.AddComponent<MeshFilter>();
            gameObject.AddComponent<MeshRenderer>();
            gameObject.GetComponent<MeshRenderer>().material = mat;
    
            //顶点
            Vector3[] vertices = new Vector3[segments * 2];
            float deltaAngle = Mathf.Deg2Rad * 360f / segments;
            float currentAngle = 0;
            for (int i = 0; i < vertices.Length; i += 2)
            {
                float cosA = Mathf.Cos(currentAngle);
                float sinA = Mathf.Sin(currentAngle);
                vertices[i] = new Vector3(cosA * innerRadius + centerCircle.x, sinA * innerRadius + centerCircle.y, 0);
                vertices[i + 1] = new Vector3(cosA * radius + centerCircle.x, sinA * radius + centerCircle.y, 0);
                currentAngle += deltaAngle;
            }
    
            //三角形
            int[] triangles = new int[segments * 6];
            for (int i = 0, j = 0; i < segments * 6; i += 6, j += 2)
            {
                triangles[i] = j;
                triangles[i + 1] = (j + 1) % vertices.Length;
                triangles[i + 2] = (j + 3) % vertices.Length;
    
                triangles[i + 3] = j;
                triangles[i + 4] = (j + 3) % vertices.Length;
                triangles[i + 5] = (j + 2) % vertices.Length;
            }
    
            Mesh mesh = GetComponent<MeshFilter>().mesh;
            mesh.Clear();
    
            mesh.vertices = vertices;
            mesh.triangles = triangles;
        }
    }
     


    莫说我穷的叮当响,大袖揽清风。 莫讥我困时无处眠,天地做床被。 莫笑我渴时无美酒,江湖来做壶。
  • 相关阅读:
    手机屏幕边缘发黄(抢救处理)
    Error parsing HTTP request header 错误解决方法
    SpringBoot项目部署到外部Tomcat重复启动的解决方法
    手机文件管理中网络邻居的使用方法
    解决Tomcat启动过程中报错org.springframework.jmx.export.UnableToRegisterMBeanException
    SpringBoot打成war包后Tomcat无法访问静态资源问题
    Springboot如何使用外部tomcat容器
    Maven打包并入本地jar包
    Excel小技巧之VLOOKUP()使用简单说明
    java.io.IOException: Server returned HTTP response code: 403 for URL
  • 原文地址:https://www.cnblogs.com/huang--wei/p/11578265.html
Copyright © 2011-2022 走看看