18
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Shaderでオブジェクトやテクスチャのスケールと縦横比を取得する

Last updated at Posted at 2018-11-25

はじめに

よく忘れるので備忘録です

環境

Unity 5.6.3p1
Unity 2017.4.15f1

オブジェクトのスケール比

スケールx
float sX = 1/sqrt(pow(unity_WorldToObject[0].x, 2) + pow(unity_WorldToObject[0].y, 2) + pow(unity_WorldToObject[0].z, 2))

スケールy
float sY = 1/sqrt(pow(unity_WorldToObject[1].x, 2) + pow(unity_WorldToObject[1].y, 2) + pow(unity_WorldToObject[1].z, 2));

スケールz
float sZ = 1/sqrt(pow(unity_WorldToObject[2].x, 2) + pow(unity_WorldToObject[2].y, 2) + pow(unity_WorldToObject[2].z, 2));

xとyの比
X:Y = sX/sY : 1

使用用途によってはWorldToObjectの部分をObjectToWorldにしてもいいかもしれないです

MainTexの縦横比

縦の解像度
float h = _MainTex_TexelSize.w;

横の解像度
float w = _MainTex_TexelSize.z;

縦横比
縦:横 = h/w : 1

_MainTexと宣言されたプロパティが存在し,
コード内でfloat4 _MainTex_TexelSize;と書いておく必要があります
プロパティ名が_MainTex以外のテクスチャ(_Hoge)に対しても_Hoge_TexelSizeと書くことで使えます

参考文献

スケール成分
_MainTex_TexelSizeについて

18
14
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
18
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?