Unity3D之Mesh(二)为三角形添加纹理

发表于2018-11-14
评论0 5.5k浏览
上篇文章给大家介绍了Mesh绘制三角形,这篇文章就来看看在前面的基础上为三角形添加纹理,具体的实现步骤如下:

一、直接开始上步骤

首先绘制一个三角形

创建一个材质,以便后续使用:

1)材质使用的贴图

2)上代码
using UnityEngine;
using System.Collections;
/* ============================================================================== 
 * 功能描述:创建三角形Mesh 
 * ==============================================================================*/
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class NewBehaviourScript1 : MonoBehaviour
{
    public float sideLength = 2;
    public float angleDegree = 100;
    private static readonly int ANGLE_DEGREE_PRECISION = 1000;
    private static readonly int SIDE_LENGTH_PRECISION = 1000;
    private MeshFilter meshFilter;
    private TriangleMeshCreator creator = new TriangleMeshCreator();
    [ExecuteInEditMode]
    private void Awake()
    {
        meshFilter = GetComponent<MeshFilter>();
    }
    private void Update()
    {
        meshFilter.mesh = creator.CreateMesh(sideLength, angleDegree);
    }
    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]]));
    }
    private Vector3 transformToWorld(Vector3 src)
    {
        return transform.TransformPoint(src);
    }
    private class TriangleMeshCreator
    {
        private float _sideLength;
        private float _angleDegree;
        private Mesh _cacheMesh;
        public Mesh CreateMesh(float sideLength, float angleDegree)
        {
            if (checkDiff(sideLength, angleDegree))
            {
                Mesh newMesh = Create(sideLength, angleDegree);
                if (newMesh != null)
                {
                    _cacheMesh = newMesh;
                    this._sideLength = sideLength;
                    this._angleDegree = angleDegree;
                }
            }
            return _cacheMesh;
        }
        private Mesh Create(float sideLength, float angleDegree)
        {
            Mesh mesh = new Mesh();
            Vector3[] vertices = new Vector3[3];
            float angle = Mathf.Deg2Rad * angleDegree;
            float halfAngle = angle / 2;
            vertices[0] = Vector3.zero;
            float cosA = Mathf.Cos(halfAngle);
            float sinA = Mathf.Sin(halfAngle);
            vertices[1] = new Vector3(cosA * sideLength, 0, sinA * sideLength);
            vertices[2] = new Vector3(cosA * sideLength, 0, -sinA * sideLength);
            int[] triangles = new int[3];
            triangles[0] = 0;
            triangles[1] = 1;
            triangles[2] = 2;
            mesh.vertices = vertices;
            mesh.triangles = triangles;
            //Vector2[] uvs = new Vector2[vertices.Length];
            //for (int i = 0; i < uvs.Length; i++)
            //{
            //    uvs[i] = Vector2.zero;
            //}
            Vector2[] uvs = new Vector2[vertices.Length];
            uvs[0] = new Vector2(0, 0.5f);
            uvs[1] = Vector2.one;
            uvs[2] = Vector2.right;
            mesh.uv = uvs;
            mesh.uv = uvs;
            return mesh;
        }
        private bool checkDiff(float sideLength, float angleDegree)
        {
            return (int)((sideLength - this._sideLength) * SIDE_LENGTH_PRECISION) != 0 ||
                (int)((angleDegree - this._angleDegree) * ANGLE_DEGREE_PRECISION) != 0;
        }
    }
}

注意: 只能在运行的时候才看得到这个三角形,编辑器里看不到怎么办?
简单来讲就是在编辑器模式下,绘制辅助线框。以下三个方法是为了在编辑模式下看到三角形;

关于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,从左到右,自下而上增加的坐标系。即:
赋值顺序变化,会显示出不同的效果。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引