みーのぺーじ

みーが趣味でやっているPCやソフトウェアについて.Python, Javascript, Processing, Unityなど.

Meshの動的な変化

Meshは以下の情報を保持するクラスです。

  • 座標
  • 法線ベクトル
  • テクスチャ座標(U,V)
  • tangents (みーはよくわからない)

三角形の配列

これを利用すると、物体の頂点を直接いじることができ、例えば下のテストような変形ができます。

Meshクラスの変数(一部)

  • vertices : 頂点の配列
  • normals : 法線ベクトルの配列
  • tangents : ?の配列
  • uv : UV座標の配列
  • colors : 色の配列
  • triangles : 三角形の配列
  • vertexCount : 点の数(read-only)

Meshクラスの関数(一部)

Clear() : すべての頂点と三角形配列を削除

参考:http://unity3d.com/support/documentation/ScriptReference/Mesh.html

サンプル

その1

円柱・シリンダー・モンキー(from Blender)に対して以下のBoo Scriptを実行します。

import UnityEngine

class example(MonoBehaviour):

    def Update():
        mesh as Mesh = GetComponent[of MeshFilter]().mesh
        vertices as (Vector3) = mesh.vertices
        normals as (Vector3) = mesh.normals
        i as int = 0
        while i < vertices.Length:
            vertices[i] += 0.005*(normals[i] * Mathf.Sin(Time.time))
            i++
        mesh.vertices = vertices

その2

UnityのMeshクラスのテストです。BooでMeshを新規生成し、頂点を動かします。

以下のスクリプトを実行します。

import UnityEngine

class JellyAnimation (MonoBehaviour): 
    private mesh as Mesh
    def Start ():
        mesh = GetComponent[of MeshFilter]().mesh
        mesh.Clear()
        mesh.vertices = (Vector3(-1,0,0),Vector3(0,-1,0),Vector3(1,0,0),Vector3(0,1,0))
        mesh.normals = (Vector3(0,0,-1),Vector3(0,0,-1),Vector3(0,0,-1),Vector3(0,0,-1))
        mesh.uv = (Vector2(0,0),Vector2(0,0),Vector2(0,0),Vector2(0,0))
        mesh.triangles = (0,2,1,0,3,2)
        mesh.colors = (Color.red,Color.green,Color.blue,Color.white)
    
    def Update ():
        mesh.vertices = (Vector3(Mathf.Cos(Time.time)+1.1,0,0),
            Vector3(0,Mathf.Sin(Time.time)+1.1,0),
            Vector3(-Mathf.Cos(Time.time)-1.1,0,0),
            Vector3(0,-Mathf.Sin(Time.time)-1.1,0))

(2012.6.15)