美文网首页Unity技术分享Unity教程合集
MESH水面波动效果和通过UV制作遮罩

MESH水面波动效果和通过UV制作遮罩

作者: Babybus_Unity | 来源:发表于2015-12-17 12:47 被阅读215次
    using UnityEngine;
    
    using System.Collections;
    
    public class WaterSin : MonoBehaviour {
    
        GameObject gameobject1;
    
        MeshFilter meshFlider;
    
        MeshRenderer meshRender;
    
        public Material material;
    
        private float totalHeight;
    
    // Use this for initialization
    
        void Start()
    
        {
    
            //Create Mesh
    
            gameobject1 = new GameObject("Mesh");
    
            meshFlider = gameobject1.AddComponent<MeshFilter>();
    
            meshRender = gameobject1.AddComponent<MeshRenderer>();
    
            meshRender.material = material;
    
            meshFlider.mesh = new Mesh();
    
            Vector3[] veritices = new Vector3[20];
    
            Vector3[] normal = new Vector3[20];
    
            Vector2[] uv = new Vector2[20];
    
            int[] triangles = new int[18 * 3];
    
            for (int i = 0; i < 10; i++)
    
            {
    
                veritices[i] = new Vector3(i - 5, 0, 0);
    
                veritices[i + 10] = new Vector3(i - 5, 1, 0);
    
                uv[i] = new Vector2(0.1f * i, 0);
    
                uv[i+10] = new Vector2(0.1f * i, 1);
    
                normal[i] = new Vector3(0, 1, 0);
    
                normal[i + 10] = new Vector3(0, 1, 0);
    
            }
    
            for (int i = 0; i < 9; i++)
    
            {
    
                triangles[i * 3] = i;
    
                triangles[i * 3 + 1] = i + 10;
    
                triangles[i * 3 + 2] = i + 1;
    
                triangles[(i + 9) * 3] = i + 10;
    
                triangles[(i + 9) * 3 + 1] = i + 11;
    
                triangles[(i + 9) * 3 + 2] = i + 1;
    
            }
    
            meshFlider.mesh.vertices = veritices;
    
            meshFlider.mesh.triangles = triangles;
    
            meshFlider.mesh.uv = uv;
    
            meshFlider.mesh.normals = normal;
    
            meshFlider.mesh.RecalculateBounds();
    
        }
    
             
    
    // Update is called once per frame
    
    void Update () {
    
            MeshWave();
    
    }
    
        void MeshWave()
    
        {
    
            Vector3[] vertices = meshFlider.mesh.vertices;
    
            Vector2[] uv = meshFlider.mesh.uv;
    
            for (int i = 10; i < 20; i++)
    
            {
    
                float meshHeight = totalHeight+0.3f*Mathf.Sin(0.5f*vertices[i].x + Time.time * 4.0f);
    
                vertices[i] = new Vector3(vertices[i].x, meshHeight, 0);
    
                uv[i] = new Vector2(uv[i].x, meshHeight / 11);
    
            }
    
            meshFlider.mesh.vertices = vertices;
    
            meshFlider.mesh.uv = uv;
    
        }
    
        void OnGUI()
    
        {
    
            totalHeight = GUI.HorizontalScrollbar(new Rect(25, 25, 100, 30), totalHeight, 1.0F, 0.0F, 10.0F);
    
        }
    
    }
    

    相关文章

      网友评论

        本文标题:MESH水面波动效果和通过UV制作遮罩

        本文链接:https://www.haomeiwen.com/subject/adlphttx.html