csharp 从点创建四元组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 从点创建四元组相关的知识,希望对你有一定的参考价值。

 using UnityEngine;
 using System.Collections;
 
 // Coming from https://answers.unity.com/questions/546473/create-a-plane-from-points.html
 
 
 public class PlaneFromPoly : MonoBehaviour {
     
     public Material mat;
     public Vector3[] poly;  // Initialized in the inspector
 
     void Start () {
         if (poly == null || poly.Length < 3) {
             Debug.Log ("Define 2D polygon in 'poly' in the the Inspector");
             return;
         }
         
         MeshFilter mf = gameObject.AddComponent<MeshFilter>();
         
         Mesh mesh = new Mesh();
         mf.mesh = mesh;
         
         Renderer rend = gameObject.AddComponent<MeshRenderer>();
         rend.material = mat;
 
         Vector3 center = FindCenter ();
                 
         Vector3[] vertices = new Vector3[poly.Length+1];
         vertices[0] = Vector3.zero;
         
         for (int i = 0; i < poly.Length; i++) {
             poly[i].z = 0.0f;
             vertices[i+1] = poly[i] - center;
         }
         
         mesh.vertices = vertices;
         
         int[] triangles = new int[poly.Length*3];
         
         for (int i = 0; i < poly.Length-1; i++) {
             triangles[i*3] = i+2;
             triangles[i*3+1] = 0;
             triangles[i*3+2] = i + 1;
         }
         
         triangles[(poly.Length-1)*3] = 1;
         triangles[(poly.Length-1)*3+1] = 0;
         triangles[(poly.Length-1)*3+2] = poly.Length;
         
         mesh.triangles = triangles = triangles;
         mesh.uv = BuildUVs(vertices);
 
         mesh.RecalculateBounds();
         mesh.RecalculateNormals();
 
     }
     
     Vector3 FindCenter() {
         Vector3 center = Vector3.zero;
         foreach (Vector3 v3 in poly) {
             center += v3;    
         }
         return center / poly.Length;
     }
     
     Vector2[] BuildUVs(Vector3[] vertices) {
         
         float xMin = Mathf.Infinity;
         float yMin = Mathf.Infinity;
         float xMax = -Mathf.Infinity;
         float yMax = -Mathf.Infinity;
         
         foreach (Vector3 v3 in vertices) {
             if (v3.x < xMin)
                 xMin = v3.x;
             if (v3.y < yMin)
                 yMin = v3.y;
             if (v3.x > xMax)
                 xMax = v3.x;
             if (v3.y > yMax)
                 yMax = v3.y;
         }
         
         float xRange = xMax - xMin;
         float yRange = yMax - yMin;
             
         Vector2[] uvs = new Vector2[vertices.Length];
         for (int i = 0; i < vertices.Length; i++) {
             uvs[i].x = (vertices[i].x - xMin) / xRange;
             uvs[i].y = (vertices[i].y - yMin) / yRange;
             
         }
         return uvs;
     }
 }

以上是关于csharp 从点创建四元组的主要内容,如果未能解决你的问题,请参考以下文章

csharp 匿名类型比元组更好

csharp 元组

csharp 使元组更容易使用

如何在 C++ 类中创建四个线程

是否有通过循环创建四次 1-12 块的最佳方法

在覆盖 UIView 的右下角创建四分之一透明孔