Created
August 3, 2017 13:54
-
-
Save divide-by-zero/282226b44c50dcc8647908af07ca0836 to your computer and use it in GitHub Desktop.
UnityでMeshの頂点をムニャムニャする ref: http://qiita.com/divideby_zero/items/eef7604e306300fd7833
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int cnt = 0; | |
public void Update() | |
{ | |
for (var i = 0; i < vertexList.Count; i += 2) | |
{ | |
var v = vertexList[i]; | |
v.y = Mathf.Sin((i + cnt) / 20.0f); | |
vertexList[i] = v; | |
} | |
cnt++; | |
mesh.SetVertices(vertexList); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
public class MyMesh : MonoBehaviour | |
{ | |
[SerializeField] | |
private MeshFilter meshFilter; | |
private Mesh mesh; | |
private List<Vector3> vertextList = new List<Vector3>(); | |
private List<Vector2> uvList = new List<Vector2>(); | |
private List<int> indexList = new List<int>(); | |
void Start () | |
{ | |
mesh = CreatePlaneMesh(); | |
meshFilter.mesh = mesh; | |
} | |
private Mesh CreatePlaneMesh() | |
{ | |
var mesh = new Mesh(); | |
vertextList.Add(new Vector3(-1, -1, 0));//0番頂点 | |
vertextList.Add(new Vector3(1, -1, 0)); //1番頂点 | |
vertextList.Add(new Vector3(-1, 1, 0)); //2番頂点 | |
vertextList.Add(new Vector3(1, 1, 0)); //3番頂点 | |
uvList.Add(new Vector2(0, 0)); | |
uvList.Add(new Vector2(1, 0)); | |
uvList.Add(new Vector2(0, 1)); | |
uvList.Add(new Vector2(1, 1)); | |
indexList.AddRange(new []{0,2,1,1,2,3});//0-2-1の頂点で1三角形。 1-2-3の頂点で1三角形。 | |
mesh.SetVertices(vertextList);//meshに頂点群をセット | |
mesh.SetUVs(0,uvList);//meshにテクスチャのuv座標をセット(今回は割愛) | |
mesh.SetIndices(indexList.ToArray(),MeshTopology.Triangles, 0);//メッシュにどの頂点の順番で面を作るかセット | |
return mesh; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Update() | |
{ | |
for (var i = 0; i < vertextList.Count; i++) | |
{ | |
vertextList[i] += new Vector3(Random.Range(-0.1f,0.1f), Random.Range(-0.1f, 0.1f),0);//全頂点のxとyをランダムでちょっと動かす | |
} | |
mesh.SetVertices(vertextList); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment