zoukankan      html  css  js  c++  java
  • unity Mesh绘制网格线

    绘制网格线

    using System.Collections.Generic;
    using UnityEngine;
     
    public class GridMesh : MonoBehaviour {
     
        // Use this for initialization
        void Start () {
            GameObject obj = new GameObject("cube");
            MeshFilter mf = obj.AddComponent<MeshFilter>();
            MeshRenderer mr = obj.AddComponent<MeshRenderer>();
            mr.sharedMaterial = Resources.Load<Material>("Mat1");
     
            Vector3[] ptsArr1 = new Vector3[5];
            ptsArr1[0].Set(0.0f, 0.0f, 0.0f);
            ptsArr1[1].Set(0.0f, 1.0f, 0.0f);
            ptsArr1[2].Set(1.0f, 1.0f, 0.0f);
            ptsArr1[3].Set(1.0f, 0.0f, 0.0f);
            ptsArr1[4].Set(0.0f, 0.0f, 0.0f);
     
            Vector3[] ptsArr2 = new Vector3[6];
            ptsArr2[0].Set(2.0f, 0.0f, 0.0f);
            ptsArr2[1].Set(2.0f, 1.0f, 0.0f);
            ptsArr2[2].Set(3.0f, 1.0f, 0.0f);
            ptsArr2[3].Set(3.0f, 0.0f, 0.0f);
            ptsArr2[4].Set(2.0f, 0.0f, 0.0f);
    
    
    
            List<int> indices1 = new List<int>();
            CalIndices(ptsArr1, 0, indices1);
    
            List<int> indices2 = new List<int>();
            CalIndices(ptsArr2, ptsArr1.Length, indices2);
    
            List<int> indicesTotal = new List<int>();
            indicesTotal.AddRange(indices1);
            indicesTotal.AddRange(indices2);
     
            List<Vector3> ptsTotal = new List<Vector3>();
            ptsTotal.AddRange(ptsArr1);
            ptsTotal.AddRange(ptsArr2);
     
            mf.mesh.vertices = ptsTotal.ToArray();
            mf.mesh.SetIndices(indicesTotal.ToArray(), MeshTopology.Lines, 0);
        }
     
        void CalIndices(Vector3[] ptsArr, int startIndex, List<int> indiceArr)
        {
            //int[] indiceArr1 = new int[2 * ptsArr.Length];
            int k = 0;
            for (int i = startIndex; i < startIndex + ptsArr.Length - 1; i++)
            {
                indiceArr.Add(i);
                indiceArr.Add(i+1);
            }
     
            indiceArr.Add(startIndex + ptsArr.Length - 1);
            indiceArr.Add(startIndex);
        }
    }

    参考:https://blog.csdn.net/zouxin_88/article/details/82962521

  • 相关阅读:
    Sysinternals Suite
    扩展Visual Studio Test Project:自定义TestClassAttribute
    扩展Visual Studio Test Project:自定义TestClassAttribute
    SQL Server Single-user Mode
    MAXDOP(max degree of parallelism)
    关于log4net
    Go 切片的一种有趣内存泄漏方式
    Go 中的内联优化
    优化 Golang 服务来减少 40% 以上的 CPU
    Go 编译器内部知识:向 Go 添加新语句-第 2 部分
  • 原文地址:https://www.cnblogs.com/sanyejun/p/14662306.html
Copyright © 2011-2022 走看看