LoginSignup
8
9

More than 5 years have passed since last update.

UnityでUVを移動して左右対称にする

Posted at

3Dの頂点情報は、テクスチャやシェーダーなどの位置を判別するのにUVというものを使ってまるみなどによる面のゆがみなどを調整することができます。
このUVを使ってテクスチャーを左右対称にしようって考え方です。

UVは、コンポーネントのMesh Filterを使ってmeshFilter.mesh.uvで
uvポイントの位置を調整する事ができます。

それでは、こちらのテクスチャを左右対称にしてみましょう。
スクリーンショット 2014-09-21 12.15.35.png

真ん中を中心に左側のUVを右側へ移動することで左右対称にする事ができます。
1.png

それでは、オブジェクトを用意しましょう。
Unityのデフォルトだとめっちゃたくさんメッシュがあって
UVを管理するのがメンドクサイので3Dソフトでシンプルなメッシュ数にします。

こんな感じ。
スクリーンショット 2014-09-21 12.40.51.png

Unity以外に使えないよ!って初心者の方用にこのデータを
githubに上げておくので使って下さい。
https://github.com/amano-kiyoyuki/sample/model/plane.fbx

では、このモデルに以下のスクリプトを入れて下さい。
※UVを左右対称にする一個手前の事については、以下のサイトさんをご参考にして下さい。とても詳しく説明されています。
http://narudesign.com/devlog/unity-mesh-vertix-uv-1/

using UnityEngine;
using System.Collections;

public class SymmetrieUV : MonoBehaviour {
    void Start()
    {
     //メッシュデータを調べる
        Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh;
        for(int i = 0; i < mesh.vertices.Length; i++)
        {
            print("vertices[" + i + "] : " + mesh.vertices[i]);
        }        
        for(int i = 0; i < mesh.uv.Length; i++)
        {
            print("uv[" + i + "] : " + mesh.uv[i]);
        }
        for(int i = 0; i < mesh.triangles.Length; i++)
        {
            print("triangles[" + i + "] : " + mesh.triangles[i]);
        }

       //メッシュフィルターに変更したUVを入れる
        MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
        Vector2[] newUV = new Vector2[9];

        /*そのまま貼付けた場合のUV
        newUV [0] = new Vector2(0.0f, 0.0f);
        newUV [1] = new Vector2(0.0f, 0.5f);
        newUV [2] = new Vector2(0.5f, 0.0f);
        newUV [3] = new Vector2(0.5f, 0.5f);
        newUV [4] = new Vector2(1.0f, 0.0f);
        newUV [5] = new Vector2(1.0f, 0.5f);
        newUV [6] = new Vector2(0.0f, 1.0f);
        newUV [7] = new Vector2(0.5f, 1.0f);
        newUV [8] = new Vector2(1.0f, 1.0f);
        */

     //左右対称にする。
        newUV [0] = new Vector2(1.0f, 0.0f);//左下を右下へ移動
        newUV [1] = new Vector2(1.0f, 0.5f);//左から右へ移動
        newUV [2] = new Vector2(0.5f, 0.0f);
        newUV [3] = new Vector2(0.5f, 0.5f);        
        newUV [4] = new Vector2(1.0f, 0.0f);
        newUV [5] = new Vector2(1.0f, 0.5f);
        newUV [6] = new Vector2(1.0f, 1.0f);//左上から右上へ移動
        newUV [7] = new Vector2(0.5f, 1.0f);
        newUV [8] = new Vector2(1.0f, 1.0f);
        meshFilter.mesh.uv = newUV;

    }
}

これを貼付ければ

スクリーンショット 2014-09-21 13.02.18.png

っとこのようになります。

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9