zoukankan      html  css  js  c++  java
  • Unity3D之Mesh(二)为三角形添加纹理

    一、直接開始上步驟

    首先繪製一個三角形

    創建一個材質,以便後續使用:


    1)材質使用的貼圖


    2)上代碼

     

      1 using UnityEngine;
      2 using System.Collections;
      3 
      4 /* ============================================================================== 
      5  * 功能描述:创建三角形Mesh 
      6  * ==============================================================================*/
      7 [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
      8 public class NewBehaviourScript1 : MonoBehaviour
      9 {
     10     public float sideLength = 2;
     11     public float angleDegree = 100;
     12     private static readonly int ANGLE_DEGREE_PRECISION = 1000;
     13     private static readonly int SIDE_LENGTH_PRECISION = 1000;
     14 
     15     private MeshFilter meshFilter;
     16 
     17     private TriangleMeshCreator creator = new TriangleMeshCreator();
     18 
     19     [ExecuteInEditMode]
     20     private void Awake()
     21     {
     22 
     23         meshFilter = GetComponent<MeshFilter>();
     24     }
     25 
     26     private void Update()
     27     {
     28         meshFilter.mesh = creator.CreateMesh(sideLength, angleDegree);
     29     }
     30     void OnDrawGizmos()
     31     {
     32         Gizmos.color = Color.gray;
     33         DrawMesh();
     34     }
     35 
     36     void OnDrawGizmosSelected()
     37     {
     38         Gizmos.color = Color.green;
     39         DrawMesh();
     40     }
     41 
     42     private void DrawMesh()
     43     {
     44         Mesh mesh = creator.CreateMesh(sideLength, angleDegree);
     45         int[] tris = mesh.triangles;
     46         Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[0]]), transformToWorld(mesh.vertices[tris[1]]));
     47         Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[0]]), transformToWorld(mesh.vertices[tris[2]]));
     48         Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[1]]), transformToWorld(mesh.vertices[tris[2]]));
     49     }
     50 
     51     private Vector3 transformToWorld(Vector3 src)
     52     {
     53         return transform.TransformPoint(src);
     54     }
     55 
     56     private class TriangleMeshCreator
     57     {
     58         private float _sideLength;
     59         private float _angleDegree;
     60 
     61         private Mesh _cacheMesh;
     62         public Mesh CreateMesh(float sideLength, float angleDegree)
     63         {
     64             if (checkDiff(sideLength, angleDegree))
     65             {
     66                 Mesh newMesh = Create(sideLength, angleDegree);
     67                 if (newMesh != null)
     68                 {
     69                     _cacheMesh = newMesh;
     70                     this._sideLength = sideLength;
     71                     this._angleDegree = angleDegree;
     72                 }
     73             }
     74             return _cacheMesh;
     75         }
     76 
     77         private Mesh Create(float sideLength, float angleDegree)
     78         {
     79             Mesh mesh = new Mesh();
     80             Vector3[] vertices = new Vector3[3];
     81 
     82             float angle = Mathf.Deg2Rad * angleDegree;
     83             float halfAngle = angle / 2;
     84             vertices[0] = Vector3.zero;
     85             float cosA = Mathf.Cos(halfAngle);
     86             float sinA = Mathf.Sin(halfAngle);
     87             vertices[1] = new Vector3(cosA * sideLength, 0, sinA * sideLength);
     88             vertices[2] = new Vector3(cosA * sideLength, 0, -sinA * sideLength);
     89 
     90             int[] triangles = new int[3];
     91             triangles[0] = 0;
     92             triangles[1] = 1;
     93             triangles[2] = 2;
     94 
     95             mesh.vertices = vertices;
     96             mesh.triangles = triangles;
     97 
     98             //Vector2[] uvs = new Vector2[vertices.Length];
     99             //for (int i = 0; i < uvs.Length; i++)
    100             //{
    101             //    uvs[i] = Vector2.zero;
    102             //}
    103             Vector2[] uvs = new Vector2[vertices.Length];
    104             uvs[0] = new Vector2(0, 0.5f);
    105             uvs[1] = Vector2.one;
    106             uvs[2] = Vector2.right;
    107             mesh.uv = uvs;
    108             mesh.uv = uvs;
    109 
    110             return mesh;
    111         }
    112 
    113         private bool checkDiff(float sideLength, float angleDegree)
    114         {
    115             return (int)((sideLength - this._sideLength) * SIDE_LENGTH_PRECISION) != 0 ||
    116                 (int)((angleDegree - this._angleDegree) * ANGLE_DEGREE_PRECISION) != 0;
    117         }
    118     }
    119 
    120 
    121 }

    注意: 只能在运行的时候才看得到这个三角形,编辑器里看不到怎么办?

    简单来讲就是在编辑器模式下,绘制辅助线框。以下三个方法是为了在编辑模式下看到三角形;

    关于OnDrawGizmos和OnDrawGizmosSelected可以参考这个链接http://www.ceeger.com/Script/Gizmos/Gizmos.html

     void OnDrawGizmos()
        {
            Gizmos.color = Color.gray;
            DrawMesh();
        }
    
        void OnDrawGizmosSelected()
        {
            Gizmos.color = Color.green;
            DrawMesh();
        }
    
        private void DrawMesh()
        {
            Mesh mesh = creator.CreateMesh(sideLength, angleDegree);
            int[] tris = mesh.triangles;
            Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[0]]), transformToWorld(mesh.vertices[tris[1]]));
            Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[0]]), transformToWorld(mesh.vertices[tris[2]]));
            Gizmos.DrawLine(transformToWorld(mesh.vertices[tris[1]]), transformToWorld(mesh.vertices[tris[2]]));
        }

    3)把Mesh Renderer这个组件上的Material设为我们新建的材质。


     效果:

    分情況實驗:

    1、

     uvs[1] = new Vector2(0, 0.5f);
                uvs[0] = Vector2.one;
                uvs[2] = Vector2.right;

    2、

    uvs[0] = new Vector2(0, 0.5f);
     uvs[1] = Vector2.one;
    uvs[2] = Vector2.right;

    3、

     uvs[1] = new Vector2(0, 0.5f);
     uvs[2] = Vector2.one;
     uvs[0] = Vector2.right;

    通过上面的例子,我们知道:

    第二種情況下:

    三处的三角形顶点对应的uv坐标是(0,0.5f),(1,1)和(1,0)。

    这样我们就可以知道,uv坐标系是从0到1,从左到右,自下而上增加的坐标系。即:

     賦值順序變化,会显示出不同的效果。


    【欢迎转载】

     转载请表明出处: 乐学习

  • 相关阅读:
    NSDate 与NSString的互相转换
    NSArray 基础
    IOS 使用自定义字体
    页面跳转添加动画
    Builder 模式
    树的子结构
    Singleton 模式
    合并两个排序的链表(递归算法)
    合并两个排序的链表(非递归)
    反转链表
  • 原文地址:https://www.cnblogs.com/JLZT1223/p/6085339.html
Copyright © 2011-2022 走看看