mesh.verticesの編集(もにょもにょした表現)


ポリゴンの頂点をコード側から動かしてもにょもにょさせるにはどうしたらいいかやってみた。

>>サンプル
すごく簡単で、下記のコードで頂点が取り出せる。

1
Vector3[] vertices = this.GetComponent<MeshFilter>().mesh.vertices;


MeshFilterコンポーネントの中にメッシュの情報があり、それをMeshRendererが描画し表示される。MeshFilter.meshのなかに頂点情報(vertices)が格納されているのでそれをいじると、頂点が操作できる。
内部的(実行時?)にはすべて三角ポリゴンに変換されているらしいけど、Unity上では三角ポリゴン、四角ポリゴンがサポートされている。例えばCubeであれば、各面に四角ポリゴンがあるので頂点が4つ。その面が6つあるので頂点数は24個。この頂点をランダムに動かしてしまうと面と面がはなれてしまいCubeではなくなってしまうので、同じ座標にある頂点は同じように動かしてあげないと形状が崩れちゃう。見た目のCubeの角頂点座標と、mesh.verticesの頂点の座標の対応表をつくって、それを動かせば面と面が離れることを回避できる。そんな感じでつくったコードはこちら。
>>プロジェクトファイルダウンロード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
/**
 * 頂点加工のテスト。
 */
public class VerticesTest : MonoBehaviour {
    /**
     * 揺らぐ距離間。
     */
    public float swingDistance = 0.1f;
 
    /**
     * MeshFilter.
     */
    private MeshFilter meshFilter;
 
    /**
     * メッシュデータの頂点。
     */
    private Vector3[] vertices;
 
    /**
     * 重複のないオリジナル頂点データ。
     */
    private List<Vector3> originalVertices = new List<Vector3>();
 
    /**
     * 現在の重複のない頂点データ。
     */
    private List<Vector3> currentVertices = new List<Vector3>();
 
    /**
     * 動かす際のターゲットとなる重複のない頂点データ。
     */
    private List<Vector3> targetlVertices = new List<Vector3>();
 
    /**
     * 頂点ごとの時間の差。
     */
    private List<float> timeGap = new List<float>();
 
    /**
     * 頂点との対応表。
     */
    private Dictionary<int, int> correspondenceTable = new Dictionary<int, int>();
 
    /**
     * Awake.
     */
    public void Awake() {
        this.meshFilter = this.GetComponent<MeshFilter>();
        this.vertices = this.meshFilter.mesh.vertices;
 
        for (int i = 0; i < vertices.Length; ++i) {
            Vector3 vertex = vertices[i];
 
            if (!this.originalVertices.Contains(vertex)) {
                this.correspondenceTable[i] = this.originalVertices.Count;
                this.originalVertices.Add(vertex);
                this.currentVertices.Add(vertex);
                 
                // ランダムな位置を作る
                this.targetlVertices.Add(new Vector3(
                    vertex.x + Random.Range(-this.swingDistance, this.swingDistance),
                    vertex.y + Random.Range(-this.swingDistance, this.swingDistance),
                    vertex.z + Random.Range(-this.swingDistance, this.swingDistance)
                ));
            }
            else {
                this.correspondenceTable[i] = this.originalVertices.FindIndex(vec => vec == vertex);
            }
        }
 
        for (int i = 0; i < this.originalVertices.Count; ++i) {
            this.timeGap.Add(Random.Range(0.0f, 1.0f));
        }
    }
 
    /**
     * Update.
     */
    public void Update() {
        // 現在位置の更新
        for (int i = 0; i < this.currentVertices.Count; ++i) {
            Vector3 originalPos = this.originalVertices[i];
            Vector3 targetPos = this.targetlVertices[i];
            Vector3 currentPos = this.currentVertices[i];
 
            this.timeGap[i] += Time.deltaTime * 1.6f;
            currentPos = Vector3.Slerp(originalPos, targetPos, Mathf.PingPong(this.timeGap[i], 1.0f));
 
            this.currentVertices[i] = currentPos;
        }
 
        // verticesに渡す頂点を作成
        for (int i = 0; i < this.vertices.Length; ++i) {
            int vid = this.correspondenceTable[i];
            this.vertices[i] = this.currentVertices[vid];
        }
 
        // 回転演出
        this.transform.Rotate(Vector3.up * Time.deltaTime * 12.0f, Space.World);
 
        // 頂点を渡す
        this.meshFilter.mesh.vertices = this.vertices;
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です