LoginSignup
0
2

More than 5 years have passed since last update.

オブジェクトのある場所のUVを取得する

Posted at

こんな感じ

textureCoord.gif
※画面をクリックして表示位置を変更してます。

手順

  • Camera.main.ScreenPointToRay
    • スクリーンでタッチされた場所からRayを飛ばす
  • Physics.Raycast
    • 飛ばされたRayとColliderとの当たり判定
  • RaycastHit
    • ヒットしたオブジェクトの情報
  • textureCoord
    • RaycastHitに設定されているテクスチャ座標(UV)

※↓にコード置いておいたので合わせて確認してみてください。

その他メモ(調べておきたいことなど)

  • MeshColliderでないと当たらない?(らしい
  • Rayを飛ばさないとわからない?

参考

Splatoonの塗りみたいのを再現したい その5
http://esprog.hatenablog.com/entry/2016/05/08/212355

ソースコード

MousePainter.cs

public class MousePainter : MonoBehaviour
{
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            // クリックされたスクリーンの位置からRayを飛ばす
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;

            // Rayとの当たり判定
            if (Physics.Raycast(ray, out hitInfo))
            {
                #region Use_textureCoord
                // シェーダにUVを渡す

                var material = hitInfo.transform.gameObject.GetComponent<Renderer>().material;

                Vector4[] _Pos = new Vector4[2];

                _Pos[0] = new Vector4(hitInfo.textureCoord.x, hitInfo.textureCoord.y, 0, 0);

                material.SetVectorArray("_Pos", _Pos);

                #endregion Use_textureCoord
            }
        }
    }
}
0
2
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
0
2